Search in sources :

Example 11 with AddColumnStatement

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

the class AddColumnGeneratorTest method isValid.

@Override
public void isValid() throws Exception {
    super.isValid();
    AddColumnStatement addPKColumn = new AddColumnStatement(null, null, TABLE_NAME, COLUMN_NAME, COLUMN_TYPE, null, new PrimaryKeyConstraint("pk_name"));
    assertFalse(generatorUnderTest.validate(addPKColumn, new OracleDatabase(), new MockSqlGeneratorChain()).hasErrors());
    assertTrue(generatorUnderTest.validate(addPKColumn, new H2Database(), new MockSqlGeneratorChain()).getErrorMessages().contains("Cannot add a primary key column"));
    assertTrue(generatorUnderTest.validate(addPKColumn, new DB2Database(), new MockSqlGeneratorChain()).getErrorMessages().contains("Cannot add a primary key column"));
    assertTrue(generatorUnderTest.validate(addPKColumn, new DerbyDatabase(), new MockSqlGeneratorChain()).getErrorMessages().contains("Cannot add a primary key column"));
    assertTrue(generatorUnderTest.validate(addPKColumn, new SQLiteDatabase(), new MockSqlGeneratorChain()).getErrorMessages().contains("Cannot add a primary key column"));
    assertTrue(generatorUnderTest.validate(new AddColumnStatement(null, null, null, null, null, null, new AutoIncrementConstraint()), new MySQLDatabase(), new MockSqlGeneratorChain()).getErrorMessages().contains("Cannot add a non-primary key identity column"));
    assertTrue(generatorUnderTest.validate(new AddColumnStatement(null, null, null, null, null, null, new AutoIncrementConstraint()), new MySQLDatabase(), new MockSqlGeneratorChain()).getErrorMessages().contains("Cannot add a non-primary key identity column"));
    assertTrue(generatorUnderTest.validate(new AddColumnStatement(new AddColumnStatement(null, null, TABLE_NAME, COLUMN_NAME, COLUMN_TYPE, null), new AddColumnStatement(null, null, "other_table", "other_column", COLUMN_TYPE, null)), new MySQLDatabase(), new MockSqlGeneratorChain()).getErrorMessages().contains("All columns must be targeted at the same table"));
}
Also used : OracleDatabase(liquibase.database.core.OracleDatabase) DB2Database(liquibase.database.core.DB2Database) AutoIncrementConstraint(liquibase.statement.AutoIncrementConstraint) SQLiteDatabase(liquibase.database.core.SQLiteDatabase) DerbyDatabase(liquibase.database.core.DerbyDatabase) MySQLDatabase(liquibase.database.core.MySQLDatabase) H2Database(liquibase.database.core.H2Database) AddColumnStatement(liquibase.statement.core.AddColumnStatement) PrimaryKeyConstraint(liquibase.statement.PrimaryKeyConstraint) MockSqlGeneratorChain(liquibase.sqlgenerator.MockSqlGeneratorChain)

Example 12 with AddColumnStatement

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

the class AddColumnExecutorTest method generateSql_autoIncrement.

@SuppressWarnings("unchecked")
@Test
public void generateSql_autoIncrement() throws Exception {
    this.statementUnderTest = new AddColumnStatement(null, "table_name", "column_name", "int", null, new AutoIncrementConstraint("column_name"));
    assertCorrect("alter table table_name add column_name serial", InformixDatabase.class);
    assertCorrect("alter table [table_name] add [column_name] int default autoincrement null", SybaseASADatabase.class);
    assertCorrect("alter table [table_name] add [column_name] serial", PostgresDatabase.class);
    assertCorrect("alter table [dbo].[table_name] add [column_name] int identity", MSSQLDatabase.class);
    assertCorrect("alter table [table_name] add [column_name] int identity null", SybaseDatabase.class);
    assertCorrect("not supported. fixme!!", SQLiteDatabase.class);
    assertCorrect("alter table table_name add column_name int auto_increment_clause");
}
Also used : AddColumnStatement(liquibase.statement.core.AddColumnStatement) Test(org.junit.Test)

Example 13 with AddColumnStatement

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

the class AddColumnExecutorTest method fullNoConstraints.

@SuppressWarnings("unchecked")
@Test
public void fullNoConstraints() throws Exception {
    this.statementUnderTest = new AddColumnStatement(null, null, "table_name", "column_name", "int", 42);
    assertCorrect("alter table [table_name] add [column_name] int default 42 null", SybaseDatabase.class);
    assertCorrect("alter table [table_name] add [column_name] int constraint df_table_name_column_name default 42", MSSQLDatabase.class);
    //        assertCorrect("alter table [table_name] add [column_name] integer default 42", SQLiteDatabase.class);
    assertCorrect("not supported. fixme!!", SQLiteDatabase.class);
    assertCorrect("alter table table_name add column_name int default 42", PostgresDatabase.class, InformixDatabase.class, OracleDatabase.class, DerbyDatabase.class, HsqlDatabase.class, DB2Database.class, H2Database.class, FirebirdDatabase.class);
    assertCorrect("alter table [table_name] add [column_name] int default 42 null", SybaseASADatabase.class);
    assertCorrect("alter table table_name add column_name int null default 42", MySQLDatabase.class);
    assertCorrectOnRest("ALTER TABLE [table_name] ADD [column_name] int DEFAULT 42");
}
Also used : AddColumnStatement(liquibase.statement.core.AddColumnStatement) Test(org.junit.Test)

Example 14 with AddColumnStatement

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

the class AddColumnExecutorTest method generateSql_foreignKey.

@SuppressWarnings("unchecked")
@Test
public void generateSql_foreignKey() throws Exception {
    this.statementUnderTest = new AddColumnStatement(null, "table_name", "column_name", "int", null, new PrimaryKeyConstraint(), new ForeignKeyConstraint("fk_test_fk", "table_name(column_name)"));
    assertCorrect(new String[] { "alter table [table_name] add [column_name] int not null primary key", "alter table [table_name] add constraint [fk_test_fk] foreign key ([column_name]) references [table_name]([column_name])" }, HsqlDatabase.class);
    assertCorrect(new String[] { "alter table [table_name] add [column_name] int primary key not null", "alter table [table_name] add constraint [fk_test_fk] foreign key ([column_name]) references [table_name]([column_name])" }, SybaseASADatabase.class, SybaseDatabase.class);
    assertCorrect(new String[] { "alter table [dbo].[table_name] add [column_name] int not null primary key", "alter table [dbo].[table_name] add constraint [fk_test_fk] foreign key ([column_name]) references [dbo].[table_name]([column_name])" }, MSSQLDatabase.class);
    assertCorrect(new String[] { "alter table table_name add column_name int not null primary key", "alter table [table_name] add constraint [fk_test_fk] foreign key ([column_name]) references [table_name]([column_name])" }, PostgresDatabase.class);
    assertCorrect(new String[] { "alter table `table_name` add `column_name` int not null primary key", "alter table [table_name] add constraint [fk_test_fk] foreign key ([column_name]) references [table_name]([column_name])" }, MySQLDatabase.class);
    assertCorrect(new String[] { "ALTER TABLE [table_name] ADD [column_name] int PRIMARY KEY NOT NULL", "alter table [table_name] add constraint  foreign key ([column_name]) references [table_name]([column_name]) constraint [fk_test_fk]" }, InformixDatabase.class);
    assertCorrect(new String[] { "ALTER TABLE [table_name] ADD [column_name] int PRIMARY KEY NOT NULL", "alter table [table_name] add constraint [fk_test_fk] foreign key ([column_name]) references [table_name]([column_name])" });
}
Also used : AddColumnStatement(liquibase.statement.core.AddColumnStatement) Test(org.junit.Test)

Example 15 with AddColumnStatement

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

the class AddColumnChangeTest method generateStatements_DB2_multipleColumns_single_reorg.

@Test
public void generateStatements_DB2_multipleColumns_single_reorg() {
    AddColumnChange change = new AddColumnChange();
    AddColumnConfig column1 = new AddColumnConfig();
    column1.setName("column1");
    column1.setType("INT");
    change.addColumn(column1);
    AddColumnConfig column2 = new AddColumnConfig();
    column2.setName("column2");
    column2.setType("INT");
    change.addColumn(column2);
    SqlStatement[] statements = change.generateStatements(new DB2Database());
    Assert.assertEquals(2, statements.length);
    Assert.assertTrue(statements[0] instanceof AddColumnStatement);
    AddColumnStatement stmt = (AddColumnStatement) statements[0];
    Assert.assertTrue(stmt.isMultiple());
    Assert.assertEquals(2, stmt.getColumns().size());
    Assert.assertTrue(statements[1] instanceof ReorganizeTableStatement);
}
Also used : DB2Database(liquibase.database.core.DB2Database) SqlStatement(liquibase.statement.SqlStatement) ReorganizeTableStatement(liquibase.statement.core.ReorganizeTableStatement) AddColumnChange(liquibase.change.core.AddColumnChange) AddColumnStatement(liquibase.statement.core.AddColumnStatement) Test(org.junit.Test)

Aggregations

AddColumnStatement (liquibase.statement.core.AddColumnStatement)17 Test (org.junit.Test)13 MockSqlGeneratorChain (liquibase.sqlgenerator.MockSqlGeneratorChain)4 AddColumnChange (liquibase.change.core.AddColumnChange)3 MySQLDatabase (liquibase.database.core.MySQLDatabase)3 AutoIncrementConstraint (liquibase.statement.AutoIncrementConstraint)3 SqlStatement (liquibase.statement.SqlStatement)3 ArrayList (java.util.ArrayList)2 DB2Database (liquibase.database.core.DB2Database)2 ValidationErrors (liquibase.exception.ValidationErrors)2 Sql (liquibase.sql.Sql)2 AbstractSqlGeneratorTest (liquibase.sqlgenerator.AbstractSqlGeneratorTest)2 ReorganizeTableStatement (liquibase.statement.core.ReorganizeTableStatement)2 HashSet (java.util.HashSet)1 AddColumnConfig (liquibase.change.AddColumnConfig)1 DerbyDatabase (liquibase.database.core.DerbyDatabase)1 H2Database (liquibase.database.core.H2Database)1 OracleDatabase (liquibase.database.core.OracleDatabase)1 SQLiteDatabase (liquibase.database.core.SQLiteDatabase)1 DatabaseException (liquibase.exception.DatabaseException)1