use of liquibase.sql.Sql in project liquibase by liquibase.
the class InsertOrUpdateGeneratorPostgresTest method testInsertSequenceValWithSchema.
@Test
public void testInsertSequenceValWithSchema() {
PostgresDatabase postgresDatabase = new PostgresDatabase();
InsertGenerator generator = new InsertGenerator();
InsertStatement statement = new InsertStatement(CATALOG_NAME, SCHEMA_NAME, TABLE_NAME);
ColumnConfig columnConfig = new ColumnConfig();
columnConfig.setValueSequenceNext(new SequenceNextValueFunction(SCHEMA_NAME + '.' + SEQUENCE_NAME));
columnConfig.setName("col3");
statement.addColumn(columnConfig);
Sql[] sql = generator.generateSql(statement, postgresDatabase, null);
String theSql = sql[0].toSql();
assertEquals(String.format("INSERT INTO %s.%s (col3) VALUES (nextval('%s.%s'))", SCHEMA_NAME, TABLE_NAME, SCHEMA_NAME, SEQUENCE_NAME), theSql);
}
use of liquibase.sql.Sql in project liquibase by liquibase.
the class VerifyChangeClassesTest method minimumRequiredIsValidSql.
@Ignore
@Test
public void minimumRequiredIsValidSql() throws Exception {
ChangeFactory changeFactory = ChangeFactory.getInstance();
for (String changeName : changeFactory.getDefinedChanges()) {
if (changeName.equals("addDefaultValue")) {
//need to better handle strange "one of defaultValue* is required" logic
continue;
}
if (changeName.equals("changeWithNestedTags") || changeName.equals("sampleChange")) {
//not a real change
continue;
}
for (Database database : DatabaseFactory.getInstance().getImplementedDatabases()) {
if (database.getShortName() == null) {
continue;
}
TestState state = new TestState(name.getMethodName(), changeName, database.getShortName(), TestState.Type.SQL);
state.addComment("Database: " + database.getShortName());
Change change = changeFactory.create(changeName);
if (!change.supports(database)) {
continue;
}
if (change.generateStatementsVolatile(database)) {
continue;
}
ChangeMetaData changeMetaData = ChangeFactory.getInstance().getChangeMetaData(change);
change.setResourceAccessor(new JUnitResourceAccessor());
for (String paramName : new TreeSet<String>(changeMetaData.getRequiredParameters(database).keySet())) {
ChangeParameterMetaData param = changeMetaData.getParameters().get(paramName);
Object paramValue = param.getExampleValue(database);
String serializedValue;
serializedValue = formatParameter(paramValue);
state.addComment("Change Parameter: " + param.getParameterName() + "=" + serializedValue);
param.setValue(change, paramValue);
}
ValidationErrors errors = change.validate(database);
assertFalse("Validation errors for " + changeMetaData.getName() + " on " + database.getShortName() + ": " + errors.toString(), errors.hasErrors());
SqlStatement[] sqlStatements = change.generateStatements(database);
for (SqlStatement statement : sqlStatements) {
Sql[] sql = SqlGeneratorFactory.getInstance().generateSql(statement, database);
if (sql == null) {
System.out.println("Null sql for " + statement + " on " + database.getShortName());
} else {
for (Sql line : sql) {
String sqlLine = line.toSql();
assertFalse("Change " + changeMetaData.getName() + " contains 'null' for " + database.getShortName() + ": " + sqlLine, sqlLine.contains(" null "));
state.addValue(sqlLine + ";");
}
}
}
state.test();
}
}
}
use of liquibase.sql.Sql in project liquibase by liquibase.
the class MarkChangeSetRanGeneratorTest method generateSql_markRan.
@Test
public void generateSql_markRan() {
Sql[] sqls = new MarkChangeSetRanGenerator().generateSql(new MarkChangeSetRanStatement(new ChangeSet("1", "a", false, false, "c", null, null, null), ChangeSet.ExecType.MARK_RAN), new MockDatabase(), new MockSqlGeneratorChain());
assertEquals(1, sqls.length);
assertTrue(sqls[0].toSql(), sqls[0].toSql().contains("MARK_RAN"));
}
use of liquibase.sql.Sql in project liquibase by liquibase.
the class SetNullableGeneratorTest method testGenerateOracleNotNullSqlWithConstraintName.
@Test
public void testGenerateOracleNotNullSqlWithConstraintName() throws Exception {
final Sql[] sqls = generator.generateSql(new SetNullableStatement(null, "schema_name", "table_name", "column_name", null, false, "constraint_name"), oracle, null);
assertEquals(1, sqls.length);
final Sql sql = sqls[0];
assertEquals("ALTER TABLE schema_name.table_name MODIFY column_name CONSTRAINT constraint_name NOT NULL", sql.toSql());
}
use of liquibase.sql.Sql in project liquibase by liquibase.
the class TagDatabaseGeneratorTest method testMSSQL.
//// @Test
//// public void supports() throws Exception {
//// new DatabaseTestTemplate().testOnAllDatabases(new DatabaseTest() {
//// public void performTest(Database database) throws Exception {
//// assertTrue(createGeneratorUnderTest().supportsDatabase(database));
//// }
//// });
//// }
//
// @Test
// public void execute() throws Exception {
// new DatabaseTestTemplate().testOnAvailableDatabases(
// new SqlStatementDatabaseTest(null, new TagDatabaseStatement("TAG_NAME")) {
// protected void setup(Database database) throws Exception {
// new Liquibase("changelogs/common/common.tests.changelog.xml", new JUnitResourceAccessor(), database).update(null);
// }
//
// protected void preExecuteAssert(DatabaseSnapshotGenerator snapshot) throws DatabaseException {
// assertFalse(snapshot.getDatabase().doesTagExist("TAG_NAME"));
// }
//
// protected void postExecuteAssert(DatabaseSnapshotGenerator snapshot) throws DatabaseException {
// assertTrue(snapshot.getDatabase().doesTagExist("TAG_NAME"));
// }
//
// });
// }
@Test
public void testMSSQL() throws Exception {
TagDatabaseStatement statement = new TagDatabaseStatement("v1.0");
Sql[] sql = SqlGeneratorFactory.getInstance().generateSql(statement, new MSSQLDatabase());
assertEquals(1, sql.length);
assertEquals("UPDATE [changelog] " + "SET [TAG] = 'v1.0' " + "FROM [DATABASECHANGELOG] AS [changelog] " + "INNER JOIN (" + "SELECT TOP (1) [ID], [AUTHOR], [FILENAME] " + "FROM [DATABASECHANGELOG] " + "ORDER BY [DATEEXECUTED] DESC, [ORDEREXECUTED] DESC" + ") AS [latest] " + "ON [latest].[ID] = [changelog].[ID] " + "AND [latest].[AUTHOR] = [changelog].[AUTHOR] " + "AND [latest].[FILENAME] = [changelog].[FILENAME]", sql[0].toSql());
}
Aggregations