Search in sources :

Example 31 with CreateTableStatement

use of liquibase.statement.core.CreateTableStatement in project liquibase by liquibase.

the class CreateTableGeneratorTest method testAutoIncrementStartWithIncrementByH2Database.

@Test
public void testAutoIncrementStartWithIncrementByH2Database() throws Exception {
    for (Database database : TestContext.getInstance().getAllDatabases()) {
        if (database instanceof H2Database) {
            CreateTableStatement statement = new CreateTableStatement(CATALOG_NAME, SCHEMA_NAME, TABLE_NAME);
            statement.addColumn(COLUMN_NAME1, DataTypeFactory.getInstance().fromDescription("BIGINT{autoIncrement:true}", database), new AutoIncrementConstraint(COLUMN_NAME1, BigInteger.ZERO, BigInteger.TEN));
            Sql[] generatedSql = this.generatorUnderTest.generateSql(statement, database, null);
            assertEquals("CREATE TABLE SCHEMA_NAME.TABLE_NAME (COLUMN1_NAME BIGINT GENERATED BY DEFAULT AS IDENTITY (0, 10))", generatedSql[0].toSql());
        }
    }
}
Also used : CreateTableStatement(liquibase.statement.core.CreateTableStatement) SQLiteDatabase(liquibase.database.core.SQLiteDatabase) DerbyDatabase(liquibase.database.core.DerbyDatabase) H2Database(liquibase.database.core.H2Database) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) MySQLDatabase(liquibase.database.core.MySQLDatabase) PostgresDatabase(liquibase.database.core.PostgresDatabase) OracleDatabase(liquibase.database.core.OracleDatabase) SybaseDatabase(liquibase.database.core.SybaseDatabase) SybaseASADatabase(liquibase.database.core.SybaseASADatabase) Database(liquibase.database.Database) AbstractDb2Database(liquibase.database.core.AbstractDb2Database) HsqlDatabase(liquibase.database.core.HsqlDatabase) H2Database(liquibase.database.core.H2Database) Sql(liquibase.sql.Sql) AbstractSqlGeneratorTest(liquibase.sqlgenerator.AbstractSqlGeneratorTest) Test(org.junit.Test)

Example 32 with CreateTableStatement

use of liquibase.statement.core.CreateTableStatement in project liquibase by liquibase.

the class AddLookupTableChange method generateStatements.

@Override
public SqlStatement[] generateStatements(Database database) {
    List<SqlStatement> statements = new ArrayList<>();
    String newTableCatalogName = getNewTableCatalogName();
    String newTableSchemaName = getNewTableSchemaName();
    String existingTableCatalogName = getExistingTableCatalogName();
    String existingTableSchemaName = getExistingTableSchemaName();
    SqlStatement[] createTablesSQL = { new RawSqlStatement("CREATE TABLE " + database.escapeTableName(newTableCatalogName, newTableSchemaName, getNewTableName()) + " AS SELECT DISTINCT " + database.escapeObjectName(getExistingColumnName(), Column.class) + " AS " + database.escapeObjectName(getNewColumnName(), Column.class) + " FROM " + database.escapeTableName(existingTableCatalogName, existingTableSchemaName, getExistingTableName()) + " WHERE " + database.escapeObjectName(getExistingColumnName(), Column.class) + " IS NOT NULL") };
    if (database instanceof MSSQLDatabase) {
        createTablesSQL = new SqlStatement[] { new RawSqlStatement("SELECT DISTINCT " + database.escapeObjectName(getExistingColumnName(), Column.class) + " AS " + database.escapeObjectName(getNewColumnName(), Column.class) + " INTO " + database.escapeTableName(newTableCatalogName, newTableSchemaName, getNewTableName()) + " FROM " + database.escapeTableName(existingTableCatalogName, existingTableSchemaName, getExistingTableName()) + " WHERE " + database.escapeObjectName(getExistingColumnName(), Column.class) + " IS NOT NULL") };
    } else if (database instanceof SybaseASADatabase) {
        createTablesSQL = new SqlStatement[] { new RawSqlStatement("SELECT DISTINCT " + database.escapeObjectName(getExistingColumnName(), Column.class) + " AS " + database.escapeObjectName(getNewColumnName(), Column.class) + " INTO " + database.escapeTableName(newTableCatalogName, newTableSchemaName, getNewTableName()) + " FROM " + database.escapeTableName(existingTableCatalogName, existingTableSchemaName, getExistingTableName()) + " WHERE " + database.escapeObjectName(getExistingColumnName(), Column.class) + " IS NOT NULL") };
    } else if (database instanceof Db2zDatabase) {
        CreateTableStatement tableStatement = new CreateTableStatement(newTableCatalogName, newTableSchemaName, getNewTableName());
        if (getNewColumnName() != null) {
            tableStatement.addColumn(getNewColumnName(), DataTypeFactory.getInstance().fromDescription(getNewColumnDataType(), database));
            tableStatement.addColumnConstraint(new NotNullConstraint(getNewColumnName()));
        }
        createTablesSQL = new SqlStatement[] { tableStatement, new RawSqlStatement("INSERT INTO " + database.escapeTableName(newTableCatalogName, newTableSchemaName, getNewTableName()) + " SELECT DISTINCT " + database.escapeObjectName(getExistingColumnName(), Column.class) + " FROM " + database.escapeTableName(existingTableCatalogName, existingTableSchemaName, getExistingTableName()) + " WHERE " + database.escapeObjectName(getExistingColumnName(), Column.class) + " IS NOT NULL") };
    } else if (database instanceof DB2Database) {
        createTablesSQL = new SqlStatement[] { new RawSqlStatement("CREATE TABLE " + database.escapeTableName(newTableCatalogName, newTableSchemaName, getNewTableName()) + " AS (SELECT " + database.escapeObjectName(getExistingColumnName(), Column.class) + " AS " + database.escapeObjectName(getNewColumnName(), Column.class) + " FROM " + database.escapeTableName(existingTableCatalogName, existingTableSchemaName, getExistingTableName()) + ") WITH NO DATA"), new RawSqlStatement("INSERT INTO " + database.escapeTableName(newTableCatalogName, newTableSchemaName, getNewTableName()) + " SELECT DISTINCT " + database.escapeObjectName(getExistingColumnName(), Column.class) + " FROM " + database.escapeTableName(existingTableCatalogName, existingTableSchemaName, getExistingTableName()) + " WHERE " + database.escapeObjectName(getExistingColumnName(), Column.class) + " IS NOT NULL") };
    } else if (database instanceof InformixDatabase) {
        createTablesSQL = new SqlStatement[] { new RawSqlStatement("CREATE TABLE " + database.escapeTableName(newTableCatalogName, newTableSchemaName, getNewTableName()) + " ( " + database.escapeObjectName(getNewColumnName(), Column.class) + " " + getNewColumnDataType() + " )"), new RawSqlStatement("INSERT INTO " + database.escapeTableName(newTableCatalogName, newTableSchemaName, getNewTableName()) + " ( " + database.escapeObjectName(getNewColumnName(), Column.class) + " ) SELECT DISTINCT " + database.escapeObjectName(getExistingColumnName(), Column.class) + " FROM " + database.escapeTableName(existingTableCatalogName, existingTableSchemaName, getExistingTableName()) + " WHERE " + database.escapeObjectName(getExistingColumnName(), Column.class) + " IS NOT NULL") };
    }
    statements.addAll(Arrays.asList(createTablesSQL));
    if (!(database instanceof OracleDatabase) && !(database instanceof Db2zDatabase)) {
        AddNotNullConstraintChange addNotNullChange = new AddNotNullConstraintChange();
        addNotNullChange.setSchemaName(newTableSchemaName);
        addNotNullChange.setTableName(getNewTableName());
        addNotNullChange.setColumnName(getNewColumnName());
        addNotNullChange.setColumnDataType(getNewColumnDataType());
        statements.addAll(Arrays.asList(addNotNullChange.generateStatements(database)));
    }
    if (database instanceof DB2Database) {
        statements.add(new ReorganizeTableStatement(newTableCatalogName, newTableSchemaName, getNewTableName()));
    }
    AddPrimaryKeyChange addPKChange = new AddPrimaryKeyChange();
    addPKChange.setSchemaName(newTableSchemaName);
    addPKChange.setTableName(getNewTableName());
    addPKChange.setColumnNames(getNewColumnName());
    statements.addAll(Arrays.asList(addPKChange.generateStatements(database)));
    if (database instanceof DB2Database) {
        statements.add(new ReorganizeTableStatement(newTableCatalogName, newTableSchemaName, getNewTableName()));
    }
    AddForeignKeyConstraintChange addFKChange = new AddForeignKeyConstraintChange();
    addFKChange.setBaseTableSchemaName(existingTableSchemaName);
    addFKChange.setBaseTableName(getExistingTableName());
    addFKChange.setBaseColumnNames(getExistingColumnName());
    addFKChange.setReferencedTableSchemaName(newTableSchemaName);
    addFKChange.setReferencedTableName(getNewTableName());
    addFKChange.setReferencedColumnNames(getNewColumnName());
    addFKChange.setConstraintName(getFinalConstraintName());
    statements.addAll(Arrays.asList(addFKChange.generateStatements(database)));
    return statements.toArray(new SqlStatement[statements.size()]);
}
Also used : RawSqlStatement(liquibase.statement.core.RawSqlStatement) DB2Database(liquibase.database.core.DB2Database) ReorganizeTableStatement(liquibase.statement.core.ReorganizeTableStatement) CreateTableStatement(liquibase.statement.core.CreateTableStatement) ArrayList(java.util.ArrayList) SybaseASADatabase(liquibase.database.core.SybaseASADatabase) OracleDatabase(liquibase.database.core.OracleDatabase) RawSqlStatement(liquibase.statement.core.RawSqlStatement) SqlStatement(liquibase.statement.SqlStatement) InformixDatabase(liquibase.database.core.InformixDatabase) Column(liquibase.structure.core.Column) Db2zDatabase(liquibase.database.core.Db2zDatabase) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) NotNullConstraint(liquibase.statement.NotNullConstraint)

Example 33 with CreateTableStatement

use of liquibase.statement.core.CreateTableStatement in project liquibase by liquibase.

the class CreateDatabaseChangeLogLockTableGenerator method generateSql.

@Override
public Sql[] generateSql(CreateDatabaseChangeLogLockTableStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
    String charTypeName = getCharTypeName(database);
    String dateTimeTypeString = getDateTimeTypeString(database);
    CreateTableStatement createTableStatement = new CreateTableStatement(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), database.getDatabaseChangeLogLockTableName()).setTablespace(database.getLiquibaseTablespaceName()).addPrimaryKeyColumn("ID", DataTypeFactory.getInstance().fromDescription("int", database), null, null, null, new NotNullConstraint()).addColumn("LOCKED", DataTypeFactory.getInstance().fromDescription("boolean", database), null, null, new NotNullConstraint()).addColumn("LOCKGRANTED", DataTypeFactory.getInstance().fromDescription(dateTimeTypeString, database)).addColumn("LOCKEDBY", DataTypeFactory.getInstance().fromDescription(charTypeName + "(255)", database));
    // use LEGACY quoting since we're dealing with system objects
    ObjectQuotingStrategy currentStrategy = database.getObjectQuotingStrategy();
    database.setObjectQuotingStrategy(ObjectQuotingStrategy.LEGACY);
    try {
        return SqlGeneratorFactory.getInstance().generateSql(createTableStatement, database);
    } finally {
        database.setObjectQuotingStrategy(currentStrategy);
    }
}
Also used : CreateTableStatement(liquibase.statement.core.CreateTableStatement) ObjectQuotingStrategy(liquibase.database.ObjectQuotingStrategy) NotNullConstraint(liquibase.statement.NotNullConstraint)

Example 34 with CreateTableStatement

use of liquibase.statement.core.CreateTableStatement in project liquibase by liquibase.

the class AddColumnExecutorTest method setupStatements.

@Override
protected List<? extends SqlStatement> setupStatements(Database database) {
    ArrayList<CreateTableStatement> statements = new ArrayList<CreateTableStatement>();
    CreateTableStatement table = new CreateTableStatement(null, null, TABLE_NAME);
    table.addColumn("id", DataTypeFactory.getInstance().fromDescription("int", database), null, new ColumnConstraint[] { new NotNullConstraint() });
    statements.add(table);
    // }
    return statements;
}
Also used : CreateTableStatement(liquibase.statement.core.CreateTableStatement) ArrayList(java.util.ArrayList)

Example 35 with CreateTableStatement

use of liquibase.statement.core.CreateTableStatement in project liquibase by liquibase.

the class AddAutoIncrementExecuteTest method setupStatements.

@Override
protected List<? extends SqlStatement> setupStatements(Database database) {
    ArrayList<CreateTableStatement> statements = new ArrayList<CreateTableStatement>();
    CreateTableStatement table = new CreateTableStatement(null, null, TABLE_NAME);
    if (database instanceof MySQLDatabase) {
        table.addPrimaryKeyColumn("id", DataTypeFactory.getInstance().fromDescription("int", database), null, "pk_", null);
    } else {
        table.addColumn("id", DataTypeFactory.getInstance().fromDescription("int", database), null, new ColumnConstraint[] { new NotNullConstraint() });
    }
    statements.add(table);
    // }
    return statements;
}
Also used : CreateTableStatement(liquibase.statement.core.CreateTableStatement) ArrayList(java.util.ArrayList) NotNullConstraint(liquibase.statement.NotNullConstraint)

Aggregations

CreateTableStatement (liquibase.statement.core.CreateTableStatement)54 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)45 OracleDatabase (liquibase.database.core.OracleDatabase)45 SybaseASADatabase (liquibase.database.core.SybaseASADatabase)45 Test (org.junit.Test)45 Database (liquibase.database.Database)44 AbstractDb2Database (liquibase.database.core.AbstractDb2Database)44 DerbyDatabase (liquibase.database.core.DerbyDatabase)44 H2Database (liquibase.database.core.H2Database)44 HsqlDatabase (liquibase.database.core.HsqlDatabase)44 MySQLDatabase (liquibase.database.core.MySQLDatabase)44 PostgresDatabase (liquibase.database.core.PostgresDatabase)44 SQLiteDatabase (liquibase.database.core.SQLiteDatabase)44 SybaseDatabase (liquibase.database.core.SybaseDatabase)44 AbstractSqlGeneratorTest (liquibase.sqlgenerator.AbstractSqlGeneratorTest)44 Sql (liquibase.sql.Sql)41 MockDatabaseConnection (liquibase.database.MockDatabaseConnection)10 ArrayList (java.util.ArrayList)6 NotNullConstraint (liquibase.statement.NotNullConstraint)6 ColumnConfig (liquibase.change.ColumnConfig)3