Search in sources :

Example 91 with Liquibase

use of liquibase.Liquibase in project liquibase by liquibase.

the class AbstractMssqlIntegrationTest method smartDataLoad.

@Test
public void smartDataLoad() throws Exception {
    assumeNotNull(this.getDatabase());
    Liquibase liquibase = createLiquibase("changelogs/common/smartDataLoad.changelog.xml");
    clearDatabase();
    try {
        liquibase.update(this.contexts);
    } catch (ValidationFailedException e) {
        e.printDescriptiveError(System.out);
        throw e;
    }
    try {
        liquibase.rollback(new Date(0), this.contexts);
    } catch (ValidationFailedException e) {
        e.printDescriptiveError(System.out);
        throw e;
    }
}
Also used : Liquibase(liquibase.Liquibase) ValidationFailedException(liquibase.exception.ValidationFailedException) Date(java.util.Date) AbstractIntegrationTest(liquibase.dbtest.AbstractIntegrationTest) Test(org.junit.Test)

Example 92 with Liquibase

use of liquibase.Liquibase in project liquibase by liquibase.

the class MssqlIntegrationTest method dataTypeParamsTest.

@Test
public /**
 * When snapshotting an MSSQL database, size information is included for
 * XML, SMALLMONEY, HIERARCHYID, DATETIME2, IMAGE, and DATETIMEOFFSET even when the default precisions (if
 * applicable at all) are used. Default sizes/precisions should not be transferred into resulting ChangeLogs/
 * snapshots.
 *
 * Reference: https://liquibase.jira.com/browse/CORE-1515
 */
void dataTypeParamsTest() throws Exception {
    assumeNotNull(this.getDatabase());
    clearDatabase();
    Liquibase liquibase = createLiquibase("changelogs/mssql/issues/data.type.params.xml");
    liquibase.update((String) null);
    DatabaseSnapshot snapshot = SnapshotGeneratorFactory.getInstance().createSnapshot(CatalogAndSchema.DEFAULT, this.getDatabase(), new SnapshotControl(getDatabase()));
    for (Table table : snapshot.get(Table.class)) {
        if (getDatabase().isLiquibaseObject(table)) {
            continue;
        }
        for (Column column : table.getColumns()) {
            String expectedType = column.getName().split("_")[0];
            String foundTypeDefinition = DataTypeFactory.getInstance().from(column.getType(), new MSSQLDatabase()).toDatabaseDataType(getDatabase()).toString();
            assertFalse("Parameter found in " + table.getName() + "." + column.getName(), foundTypeDefinition.contains("("));
        }
    }
}
Also used : Liquibase(liquibase.Liquibase) Table(liquibase.structure.core.Table) Column(liquibase.structure.core.Column) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) DatabaseSnapshot(liquibase.snapshot.DatabaseSnapshot) SnapshotControl(liquibase.snapshot.SnapshotControl) Test(org.junit.Test)

Example 93 with Liquibase

use of liquibase.Liquibase in project liquibase by liquibase.

the class MssqlIntegrationTest method defaultValuesTests.

@Test
public void defaultValuesTests() throws Exception {
    clearDatabase();
    assumeNotNull(this.getDatabase());
    Liquibase liquibase = createLiquibase("changelogs/mssql/issues/default.values.xml");
    liquibase.update((String) null);
    DatabaseSnapshot snapshot = SnapshotGeneratorFactory.getInstance().createSnapshot(CatalogAndSchema.DEFAULT, this.getDatabase(), new SnapshotControl(getDatabase()));
    for (Table table : snapshot.get(Table.class)) {
        for (Column column : table.getColumns()) {
            if (column.getName().toLowerCase().endsWith("_default")) {
                Object defaultValue = column.getDefaultValue();
                assertNotNull("Null default value for " + table.getName() + "." + column.getName(), defaultValue);
                if (column.getName().toLowerCase().contains("date") || column.getName().toLowerCase().contains("time")) {
                    if (defaultValue instanceof String) {
                        assertTrue(defaultValue.equals("2017-12-09 23:52:39.1234567 +01:00"));
                    } else if (defaultValue instanceof DatabaseFunction) {
                        ((DatabaseFunction) defaultValue).getValue().contains("type datetimeoffset");
                    } else if (defaultValue instanceof Time) {
                        Calendar calendar = Calendar.getInstance();
                        calendar.setTime(((Date) defaultValue));
                        assertEquals(23, calendar.get(Calendar.HOUR_OF_DAY));
                        assertEquals(52, calendar.get(Calendar.MINUTE));
                        assertEquals(39, calendar.get(Calendar.SECOND));
                    } else {
                        assertTrue("Unexpected default type " + defaultValue.getClass().getName() + " for " + table.getName() + "." + column.getName(), defaultValue instanceof Date);
                        Calendar calendar = Calendar.getInstance();
                        calendar.setTime(((Date) defaultValue));
                        assertEquals(9, calendar.get(Calendar.DAY_OF_MONTH));
                        assertEquals(11, calendar.get(Calendar.MONTH));
                        assertEquals(2017, calendar.get(Calendar.YEAR));
                    }
                } else if (column.getName().toLowerCase().contains("char_")) {
                    assertTrue("Unexpected default type " + defaultValue.getClass().getName() + " for " + table.getName() + "." + column.getName(), defaultValue instanceof String);
                } else if (column.getName().toLowerCase().contains("binary_")) {
                    assertTrue("Unexpected default type " + defaultValue.getClass().getName() + " for " + table.getName() + "." + column.getName(), defaultValue instanceof DatabaseFunction);
                } else if (column.getName().toLowerCase().contains("bit_")) {
                // todo: test better. Bits are handled odd
                } else {
                    assertTrue("Unexpected default type " + defaultValue.getClass().getName() + " for " + table.getName() + "." + column.getName(), defaultValue instanceof Number);
                    assertEquals(1, ((Number) defaultValue).intValue());
                }
            }
        }
    }
}
Also used : Liquibase(liquibase.Liquibase) Table(liquibase.structure.core.Table) DatabaseFunction(liquibase.statement.DatabaseFunction) Column(liquibase.structure.core.Column) Calendar(java.util.Calendar) Time(java.sql.Time) DatabaseSnapshot(liquibase.snapshot.DatabaseSnapshot) SnapshotControl(liquibase.snapshot.SnapshotControl) Date(java.util.Date) Test(org.junit.Test)

Example 94 with Liquibase

use of liquibase.Liquibase in project liquibase by liquibase.

the class LiquibaseUpdateSQL method createLiquibase.

@Override
@java.lang.SuppressWarnings("squid:S2095")
protected Liquibase createLiquibase(Database db) throws MojoExecutionException {
    Liquibase liquibase = super.createLiquibase(db);
    // Setup the output file writer
    try {
        if (!migrationSqlOutputFile.exists()) {
            // Ensure the parent directories exist
            migrationSqlOutputFile.getParentFile().mkdirs();
            // Create the actual file
            if (!migrationSqlOutputFile.createNewFile()) {
                throw new MojoExecutionException("Cannot create the migration SQL file; " + migrationSqlOutputFile.getAbsolutePath());
            }
        }
        outputWriter = getOutputWriter(migrationSqlOutputFile);
    } catch (IOException e) {
        getLog().error(e);
        throw new MojoExecutionException("Failed to create SQL output writer", e);
    }
    getLog().info("Output SQL Migration File: " + migrationSqlOutputFile.getAbsolutePath());
    return liquibase;
}
Also used : Liquibase(liquibase.Liquibase) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) IOException(java.io.IOException)

Example 95 with Liquibase

use of liquibase.Liquibase in project liquibase by liquibase.

the class LiquibaseRollbackSQL method createLiquibase.

@Override
protected Liquibase createLiquibase(Database db) throws MojoExecutionException {
    Liquibase liquibase = super.createLiquibase(db);
    // Setup the output file writer
    try {
        if (!migrationSqlOutputFile.exists()) {
            // Ensure the parent directories exist
            migrationSqlOutputFile.getParentFile().mkdirs();
            // Create the actual file
            if (!migrationSqlOutputFile.createNewFile()) {
                throw new MojoExecutionException("Cannot create the migration SQL file; " + migrationSqlOutputFile.getAbsolutePath());
            }
        }
        outputWriter = getOutputWriter(migrationSqlOutputFile);
    } catch (IOException e) {
        getLog().error(e);
        throw new MojoExecutionException("Failed to create SQL output writer", e);
    }
    getLog().info("Output SQL Migration File: " + migrationSqlOutputFile.getAbsolutePath());
    return liquibase;
}
Also used : Liquibase(liquibase.Liquibase) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) IOException(java.io.IOException)

Aggregations

Liquibase (liquibase.Liquibase)124 LiquibaseException (liquibase.exception.LiquibaseException)37 JdbcConnection (liquibase.database.jvm.JdbcConnection)35 ClassLoaderResourceAccessor (liquibase.resource.ClassLoaderResourceAccessor)33 Database (liquibase.database.Database)32 Test (org.junit.Test)30 IOException (java.io.IOException)27 Contexts (liquibase.Contexts)24 SQLException (java.sql.SQLException)23 ResourceAccessor (liquibase.resource.ResourceAccessor)14 ChangeSet (liquibase.changelog.ChangeSet)13 DatabaseException (liquibase.exception.DatabaseException)12 ValidationFailedException (liquibase.exception.ValidationFailedException)12 LabelExpression (liquibase.LabelExpression)11 Connection (java.sql.Connection)10 AbstractIntegrationTest (liquibase.dbtest.AbstractIntegrationTest)10 FileSystemResourceAccessor (liquibase.resource.FileSystemResourceAccessor)10 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)10 BuildException (org.apache.tools.ant.BuildException)10 LockException (liquibase.exception.LockException)9