use of liquibase.statement.core.AddUniqueConstraintStatement in project liquibase by liquibase.
the class AddUniqueConstraintChange method generateStatements.
@Override
public SqlStatement[] generateStatements(Database database) {
//todo if (database instanceof SQLiteDatabase) {
// // return special statements for SQLite databases
// return generateStatementsForSQLiteDatabase(database);
// }
boolean deferrable = false;
if (getDeferrable() != null) {
deferrable = getDeferrable();
}
boolean initiallyDeferred = false;
if (getInitiallyDeferred() != null) {
initiallyDeferred = getInitiallyDeferred();
}
boolean disabled = false;
if (getDisabled() != null) {
disabled = getDisabled();
}
boolean clustered = false;
if (getClustered() != null) {
clustered = getClustered();
}
AddUniqueConstraintStatement statement = new AddUniqueConstraintStatement(getCatalogName(), getSchemaName(), getTableName(), ColumnConfig.arrayFromNames(getColumnNames()), getConstraintName());
statement.setTablespace(getTablespace()).setDeferrable(deferrable).setInitiallyDeferred(initiallyDeferred).setDisabled(disabled).setClustered(clustered);
statement.setForIndexName(getForIndexName());
statement.setForIndexSchemaName(getForIndexSchemaName());
statement.setForIndexCatalogName(getForIndexCatalogName());
return new SqlStatement[] { statement };
}
use of liquibase.statement.core.AddUniqueConstraintStatement in project liquibase by liquibase.
the class AddUniqueConstraintExecutorTest method execute_withSchema.
@SuppressWarnings("unchecked")
@Test
public void execute_withSchema() throws Exception {
statementUnderTest = new AddUniqueConstraintStatement(DatabaseTestContext.ALT_CATALOG, DatabaseTestContext.ALT_SCHEMA, TABLE_NAME, new ColumnConfig[] { new ColumnConfig().setName(COLUMN_NAME) }, CONSTRAINT_NAME);
// FIXME Syntax for mysql is correct, but exception "Table 'liquibaseb.adduqtest' doesn't exist" is thrown
// assertCorrect("alter table `liquibaseb`.`adduqtest` add constraint `uq_test` unique (`coltomakeuq`)", MySQLDatabase.class);
assertCorrect("alter table liquibaseb.adduqtest add constraint unique (coltomakeuq) constraint uq_test", InformixDatabase.class);
assertCorrect("alter table liquibasec.adduqtest add constraint uq_test unique (coltomakeuq)", OracleDatabase.class);
assertCorrect("alter table liquibaseb.\"adduqtest\" add constraint uq_test unique (\"coltomakeuq\")", PostgresDatabase.class);
assertCorrect("alter table liquibasec.adduqtest add constraint uq_test unique (coltomakeuq)", DerbyDatabase.class);
assertCorrect("alter table [liquibaseb].[adduqtest] add constraint [uq_test] unique ([coltomakeuq])", SybaseASADatabase.class, SybaseDatabase.class, MSSQLDatabase.class);
assertCorrect("alter table [adduqtest] add constraint [uq_test] unique ([coltomakeuq])", FirebirdDatabase.class);
assertCorrect("alter table [liquibaseb].[adduqtest] add constraint [uq_test] unique ([coltomakeuq])", HsqlDatabase.class);
assertCorrectOnRest("alter table [liquibasec].[adduqtest] add constraint [uq_test] unique ([coltomakeuq])");
}
use of liquibase.statement.core.AddUniqueConstraintStatement in project liquibase by liquibase.
the class AddUniqueConstraintExecutorTest method execute_noConstraintName.
@SuppressWarnings("unchecked")
@Test
public void execute_noConstraintName() throws Exception {
this.statementUnderTest = new AddUniqueConstraintStatement(null, null, TABLE_NAME, new ColumnConfig[] { new ColumnConfig().setName(COLUMN_NAME) }, null);
assertCorrect("alter table adduqtest add unique (coltomakeuq)", MySQLDatabase.class);
assertCorrect("alter table adduqtest add constraint unique (coltomakeuq)", InformixDatabase.class);
assertCorrect("alter table adduqtest add unique (coltomakeuq)", OracleDatabase.class);
assertCorrect("alter table \"adduqtest\" add unique (\"coltomakeuq\")", PostgresDatabase.class);
assertCorrect("alter table adduqtest add unique (coltomakeuq)", DerbyDatabase.class);
assertCorrect("alter table [adduqtest] add unique ([coltomakeuq])", SybaseASADatabase.class, SybaseDatabase.class);
assertCorrect("alter table [adduqtest] add unique ([coltomakeuq])", MSSQLDatabase.class);
assertCorrect("alter table [adduqtest] add unique ([coltomakeuq])");
}
use of liquibase.statement.core.AddUniqueConstraintStatement in project liquibase by liquibase.
the class AddUniqueConstraintExecutorTest method execute_noSchema.
// @Test
// public void execute_noSchema() throws Exception {
// new DatabaseTestTemplate().testOnAvailableDatabases(
// new SqlStatementDatabaseTest(null, new AddUniqueConstraintStatement(null, TABLE_NAME, COLUMN_NAME, "uq_adduqtest")) {
//
// protected void preExecuteAssert(DatabaseSnapshotGenerator snapshot) {
// assertFalse(snapshot.getTable(TABLE_NAME).getColumn(COLUMN_NAME).isUnique());
// }
//
// protected void postExecuteAssert(DatabaseSnapshotGenerator snapshot) {
// //todo: enable snapshot and assertion when snapshot can check for unique constraints
// //snapshot = new DatabaseSnapshotGenerator(snapshot);
// assertTrue(snapshot.getTable(TABLE_NAME).getColumn(COLUMN_NAME).isUnique());
// }
// });
// }
//
// @Test
// public void execute_withSchema() throws Exception {
// new DatabaseTestTemplate().testOnAvailableDatabases(
// new SqlStatementDatabaseTest(TestContext.ALT_SCHEMA, new AddUniqueConstraintStatement(TestContext.ALT_SCHEMA, TABLE_NAME, COLUMN_NAME, "uq_adduqtest")) {
// protected void preExecuteAssert(DatabaseSnapshotGenerator snapshot) {
// assertFalse(snapshot.getTable(TABLE_NAME).getColumn(COLUMN_NAME).isUnique());
// }
//
// protected void postExecuteAssert(DatabaseSnapshotGenerator snapshot) {
// //todo: enable snapshot and assertion when snapshot can check for unique constraints
// snapshot = new DatabaseSnapshotGenerator(database, TestContext.ALT_SCHEMA);
// assertTrue(snapshot.getTable(TABLE_NAME).getColumn(COLUMN_NAME).isUnique());
// }
//
// });
// }
//
// @Test
// public void execute_withTablespace() throws Exception {
// new DatabaseTestTemplate().testOnAvailableDatabases(
// new SqlStatementDatabaseTest(null, new AddUniqueConstraintStatement(null, TABLE_NAME, COLUMN_NAME, "uq_adduqtest").setTablespace(TestContext.ALT_TABLESPACE)) {
// protected void preExecuteAssert(DatabaseSnapshotGenerator snapshot) {
// assertFalse(snapshot.getTable(TABLE_NAME).getColumn(COLUMN_NAME).isUnique());
// }
//
// protected void postExecuteAssert(DatabaseSnapshotGenerator snapshot) {
// //todo: enable snapshot and assertion when snapshot can check for unique constraints
// // snapshot = new DatabaseSnapshotGenerator(database);
//// assertTrue(snapshot.getTable(TABLE_NAME).getColumn(COLUMN_NAME).isUnique());
// }
// });
// }
@SuppressWarnings("unchecked")
@Test
public void execute_noSchema() throws Exception {
this.statementUnderTest = new AddUniqueConstraintStatement(null, null, TABLE_NAME, new ColumnConfig[] { new ColumnConfig().setName(COLUMN_NAME) }, CONSTRAINT_NAME);
assertCorrect("alter table [adduqtest] add constraint [uq_test] unique ([coltomakeuq])", SybaseDatabase.class);
assertCorrect("alter table [adduqtest] add constraint [uq_test] unique ([coltomakeuq])", MSSQLDatabase.class);
assertCorrect("alter table [adduqtest] add constraint [uq_test] unique ([coltomakeuq])", SybaseASADatabase.class);
assertCorrect("alter table adduqtest add constraint uq_test unique (coltomakeuq)", MySQLDatabase.class);
assertCorrect("alter table adduqtest add constraint unique (coltomakeuq) constraint uq_test", InformixDatabase.class);
assertCorrect("alter table adduqtest add constraint uq_test unique (coltomakeuq)", OracleDatabase.class);
assertCorrect("alter table \"adduqtest\" add constraint uq_test unique (\"coltomakeuq\")", PostgresDatabase.class);
assertCorrect("alter table adduqtest add constraint uq_test unique (coltomakeuq)", DerbyDatabase.class);
assertCorrect("alter table [adduqtest] add constraint [uq_test] unique ([coltomakeuq])");
}
use of liquibase.statement.core.AddUniqueConstraintStatement in project liquibase by liquibase.
the class AddColumnGenerator method addUniqueConstrantStatements.
protected void addUniqueConstrantStatements(AddColumnStatement statement, Database database, List<Sql> returnSql) {
if (statement.isUnique()) {
AddUniqueConstraintStatement addConstraintStmt = new AddUniqueConstraintStatement(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName(), ColumnConfig.arrayFromNames(statement.getColumnName()), statement.getUniqueStatementName());
returnSql.addAll(Arrays.asList(SqlGeneratorFactory.getInstance().generateSql(addConstraintStmt, database)));
}
}
Aggregations