Search in sources :

Example 1 with CreateTableStatement

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

the class RenameColumnExecuteTest 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() });
    }
    table.addColumn(COLUMN_NAME, DataTypeFactory.getInstance().fromDescription("int", database));
    statements.add(table);
    // }
    return statements;
}
Also used : CreateTableStatement(liquibase.statement.core.CreateTableStatement) ArrayList(java.util.ArrayList) NotNullConstraint(liquibase.statement.NotNullConstraint)

Example 2 with CreateTableStatement

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

the class AbstractIntegrationTest method testInsertLongClob.

@Test
public void testInsertLongClob() {
    assumeNotNull(this.getDatabase());
    DatabaseChangeLog longClobChangelog = new DatabaseChangeLog();
    ChangeSet longClobInsert = new ChangeSet(longClobChangelog);
    ColumnConfig clobColumn = new ColumnConfig();
    clobColumn.setName("clobColumn");
    clobColumn.setType(LoadDataChange.LOAD_DATA_TYPE.CLOB.name());
    // Oracle database only allows string values of up to 4000 characters
    // so we test that the CLOB insertion is actually done as a CLOB in the JDBC statement
    StringBuilder longClobString = new StringBuilder(4001);
    for (int i = 0; i < 4001; i++) {
        longClobString.append('a');
    }
    clobColumn.setValue(longClobString.toString());
    CreateTableStatement clobTableCreator = new CreateTableStatement(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), "tableWithClob");
    clobTableCreator.addColumn("clobColumn", new ClobType());
    InsertExecutablePreparedStatement insertStatement = new InsertExecutablePreparedStatement(database, database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), "tableWithClob", Arrays.asList(clobColumn), longClobInsert, Scope.getCurrentScope().getResourceAccessor());
    try {
        database.execute(new SqlStatement[] { clobTableCreator, insertStatement }, new ArrayList<>());
    } catch (LiquibaseException ex) {
        ex.printStackTrace();
        fail("Long clob insertion failed!");
    }
}
Also used : ClobType(liquibase.datatype.core.ClobType) ColumnConfig(liquibase.change.ColumnConfig) CreateTableStatement(liquibase.statement.core.CreateTableStatement) LiquibaseException(liquibase.exception.LiquibaseException) ChangeSet(liquibase.changelog.ChangeSet) InsertExecutablePreparedStatement(liquibase.statement.InsertExecutablePreparedStatement) DatabaseChangeLog(liquibase.changelog.DatabaseChangeLog) Test(org.junit.Test)

Example 3 with CreateTableStatement

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

the class CreateDatabaseChangeLogTableGenerator method generateSql.

@Override
public Sql[] generateSql(CreateDatabaseChangeLogTableStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
    String charTypeName = getCharTypeName(database);
    String dateTimeTypeString = getDateTimeTypeString(database);
    CreateTableStatement createTableStatement = new CreateTableStatement(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), database.getDatabaseChangeLogTableName()).setTablespace(database.getLiquibaseTablespaceName()).addColumn("ID", DataTypeFactory.getInstance().fromDescription(charTypeName + "(" + getIdColumnSize() + ")", database), null, null, new NotNullConstraint()).addColumn("AUTHOR", DataTypeFactory.getInstance().fromDescription(charTypeName + "(" + getAuthorColumnSize() + ")", database), null, null, new NotNullConstraint()).addColumn("FILENAME", DataTypeFactory.getInstance().fromDescription(charTypeName + "(" + getFilenameColumnSize() + ")", database), null, null, new NotNullConstraint()).addColumn("DATEEXECUTED", DataTypeFactory.getInstance().fromDescription(dateTimeTypeString, database), null, null, new NotNullConstraint()).addColumn("ORDEREXECUTED", DataTypeFactory.getInstance().fromDescription("int", database), null, null, new NotNullConstraint()).addColumn("EXECTYPE", DataTypeFactory.getInstance().fromDescription(charTypeName + "(10)", database), null, null, new NotNullConstraint()).addColumn("MD5SUM", DataTypeFactory.getInstance().fromDescription(charTypeName + "(35)", database)).addColumn("DESCRIPTION", DataTypeFactory.getInstance().fromDescription(charTypeName + "(255)", database)).addColumn("COMMENTS", DataTypeFactory.getInstance().fromDescription(charTypeName + "(255)", database)).addColumn("TAG", DataTypeFactory.getInstance().fromDescription(charTypeName + "(255)", database)).addColumn("LIQUIBASE", DataTypeFactory.getInstance().fromDescription(charTypeName + "(20)", database)).addColumn("CONTEXTS", DataTypeFactory.getInstance().fromDescription(charTypeName + "(" + getContextsSize() + ")", database)).addColumn("LABELS", DataTypeFactory.getInstance().fromDescription(charTypeName + "(" + getLabelsSize() + ")", database)).addColumn("DEPLOYMENT_ID", DataTypeFactory.getInstance().fromDescription(charTypeName + "(10)", 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 4 with CreateTableStatement

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

the class CreateTableGeneratorTest method testAutoIncrementStartWithIncrementByDerbyDatabase.

@Test
public void testAutoIncrementStartWithIncrementByDerbyDatabase() throws Exception {
    for (Database database : TestContext.getInstance().getAllDatabases()) {
        if (database instanceof DerbyDatabase) {
            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 CATALOG_NAME.TABLE_NAME (COLUMN1_NAME BIGINT GENERATED BY DEFAULT AS IDENTITY (START WITH 0, INCREMENT BY 10))", generatedSql[0].toSql());
        }
    }
}
Also used : DerbyDatabase(liquibase.database.core.DerbyDatabase) 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) Sql(liquibase.sql.Sql) AbstractSqlGeneratorTest(liquibase.sqlgenerator.AbstractSqlGeneratorTest) Test(org.junit.Test)

Example 5 with CreateTableStatement

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

the class CreateTableGeneratorTest method testAutoIncrementMSSQLDatabase.

@Test
public void testAutoIncrementMSSQLDatabase() throws Exception {
    for (Database database : TestContext.getInstance().getAllDatabases()) {
        if (database instanceof MSSQLDatabase) {
            CreateTableStatement statement = new CreateTableStatement(CATALOG_NAME, SCHEMA_NAME, TABLE_NAME);
            statement.addColumn(COLUMN_NAME1, DataTypeFactory.getInstance().fromDescription("BIGINT{autoIncrement:true}", database), new ColumnConstraint[] { new AutoIncrementConstraint(COLUMN_NAME1), new NotNullConstraint(COLUMN_NAME1) });
            Sql[] generatedSql = this.generatorUnderTest.generateSql(statement, database, null);
            assertEquals("Error on " + database, "CREATE TABLE CATALOG_NAME.SCHEMA_NAME.TABLE_NAME (COLUMN1_NAME " + "bigint IDENTITY (1, 1) NOT NULL)", 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) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) Sql(liquibase.sql.Sql) AbstractSqlGeneratorTest(liquibase.sqlgenerator.AbstractSqlGeneratorTest) Test(org.junit.Test)

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