Search in sources :

Example 26 with Table

use of liquibase.structure.core.Table in project liquibase by liquibase.

the class MssqlIntegrationTest method defaultValuesTests.

@Test
public void defaultValuesTests() throws Exception {
    if (this.getDatabase() == null) {
        return;
    }
    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 DatabaseFunction) {
                        ((DatabaseFunction) defaultValue).getValue().contains("type datetimeoffset");
                    } 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(1, calendar.get(Calendar.DAY_OF_MONTH));
                        assertEquals(1, calendar.get(Calendar.MONTH));
                        assertEquals(2000, 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 {
                    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) DatabaseSnapshot(liquibase.snapshot.DatabaseSnapshot) EmptyDatabaseSnapshot(liquibase.snapshot.EmptyDatabaseSnapshot) SnapshotControl(liquibase.snapshot.SnapshotControl) Date(java.util.Date) Test(org.junit.Test)

Example 27 with Table

use of liquibase.structure.core.Table in project liquibase by liquibase.

the class MssqlIntegrationTest method dataTypeParamsTest.

@Test
public void dataTypeParamsTest() throws Exception {
    if (this.getDatabase() == null) {
        return;
    }
    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) EmptyDatabaseSnapshot(liquibase.snapshot.EmptyDatabaseSnapshot) SnapshotControl(liquibase.snapshot.SnapshotControl) Test(org.junit.Test)

Example 28 with Table

use of liquibase.structure.core.Table in project liquibase by liquibase.

the class MySQLIntegrationTest method dateDefaultValue.

@Test
public void dateDefaultValue() throws Exception {
    if (getDatabase() == null) {
        return;
    }
    ExecutorService.getInstance().getExecutor(getDatabase()).execute(new RawSqlStatement("CREATE TABLE ad (\n" + "ad_id int(10) unsigned NOT NULL AUTO_INCREMENT,\n" + "advertiser_id int(10) unsigned NOT NULL,\n" + "ad_type_id int(10) unsigned NOT NULL,\n" + "name varchar(155) NOT NULL DEFAULT '',\n" + "label varchar(155)NOT NULL DEFAULT '',\n" + "description text NOT NULL,\n" + "active tinyint(1) NOT NULL DEFAULT '0',\n" + "created datetime NOT NULL DEFAULT '0000-00-00 00:00:00',\n" + "updated datetime DEFAULT '0000-00-00 00:00:00',\n" + "PRIMARY KEY (ad_id),\n" + "KEY active (active)\n" + ")"));
    DatabaseSnapshot snapshot = SnapshotGeneratorFactory.getInstance().createSnapshot(CatalogAndSchema.DEFAULT, getDatabase(), new SnapshotControl(getDatabase()));
    Column createdColumn = snapshot.get(new Column().setRelation(new Table().setName("ad").setSchema(new Schema())).setName("created"));
    Object defaultValue = createdColumn.getDefaultValue();
    assertNotNull(defaultValue);
    assertTrue(defaultValue instanceof DatabaseFunction);
    assertEquals("0000-00-00 00:00:00", ((DatabaseFunction) defaultValue).getValue());
}
Also used : RawSqlStatement(liquibase.statement.core.RawSqlStatement) Table(liquibase.structure.core.Table) DatabaseFunction(liquibase.statement.DatabaseFunction) Column(liquibase.structure.core.Column) Schema(liquibase.structure.core.Schema) CatalogAndSchema(liquibase.CatalogAndSchema) Test(org.junit.Test) AbstractIntegrationTest(liquibase.dbtest.AbstractIntegrationTest)

Aggregations

Table (liquibase.structure.core.Table)28 Column (liquibase.structure.core.Column)13 Schema (liquibase.structure.core.Schema)7 DatabaseException (liquibase.exception.DatabaseException)5 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)5 SnapshotControl (liquibase.snapshot.SnapshotControl)5 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)4 DatabaseSnapshot (liquibase.snapshot.DatabaseSnapshot)4 ArrayList (java.util.ArrayList)3 Liquibase (liquibase.Liquibase)3 Change (liquibase.change.Change)3 PostgresDatabase (liquibase.database.core.PostgresDatabase)3 EmptyDatabaseSnapshot (liquibase.snapshot.EmptyDatabaseSnapshot)3 ForeignKey (liquibase.structure.core.ForeignKey)3 PrimaryKey (liquibase.structure.core.PrimaryKey)3 Test (org.junit.Test)3 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 Statement (java.sql.Statement)2 Date (java.util.Date)2