Search in sources :

Example 26 with Column

use of liquibase.structure.core.Column in project liquibase by liquibase.

the class YamlSnapshotSerializer method toMap.

// @Override
// public String serialize(LiquibaseSerializable object, boolean pretty) {
// if (object instanceof DatabaseObject) {
// if (alreadySerializingObject) {
// return ((DatabaseObject) object).getObjectTypeName()+"#"+((DatabaseObject) object).getSnapshotId();
// } else {
// alreadySerializingObject = true;
// String string = super.serialize(object, pretty);
// alreadySerializingObject = false;
// return string;
// }
// }
// return super.serialize(object, pretty);
// }
@Override
protected Object toMap(final LiquibaseSerializable object) {
    if (object instanceof DatabaseObject) {
        if (object instanceof Column && (BooleanUtil.isTrue(((Column) object).getDescending()) || BooleanUtil.isTrue(((Column) object).getComputed()))) {
            // not really a "real" column that has a snapshot to reference, just serialize it
            return super.toMap(object);
        } else if (alreadySerializingObject) {
            String snapshotId = ((DatabaseObject) object).getSnapshotId();
            if (snapshotId == null) {
                String name = ((DatabaseObject) object).getName();
                Object table = ((DatabaseObject) object).getAttribute("table", Object.class);
                if (table == null) {
                    table = ((DatabaseObject) object).getAttribute("relation", Object.class);
                }
                if (table != null) {
                    name = table.toString() + "." + name;
                }
                if (((DatabaseObject) object).getSchema() != null) {
                    name = ((DatabaseObject) object).getSchema().toString() + "." + name;
                }
                throw new UnexpectedLiquibaseException("Found a null snapshotId for " + StringUtil.lowerCaseFirst(object.getClass().getSimpleName()) + " " + name);
            }
            return ((DatabaseObject) object).getClass().getName() + "#" + snapshotId;
        } else {
            alreadySerializingObject = true;
            Object map = super.toMap(object);
            alreadySerializingObject = false;
            return map;
        }
    }
    if (object instanceof DatabaseObjectCollection) {
        SortedMap<String, Object> returnMap = new TreeMap<>();
        for (Map.Entry<Class<? extends DatabaseObject>, Set<? extends DatabaseObject>> entry : ((DatabaseObjectCollection) object).toMap().entrySet()) {
            ArrayList value = new ArrayList(entry.getValue());
            Collections.sort(value, new DatabaseObjectComparator());
            returnMap.put(entry.getKey().getName(), value);
        }
        return returnMap;
    }
    return super.toMap(object);
}
Also used : DatabaseObjectCollection(liquibase.structure.DatabaseObjectCollection) Column(liquibase.structure.core.Column) DatabaseObject(liquibase.structure.DatabaseObject) DatabaseObjectComparator(liquibase.structure.DatabaseObjectComparator) DatabaseObject(liquibase.structure.DatabaseObject) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException)

Example 27 with Column

use of liquibase.structure.core.Column in project liquibase by liquibase.

the class IndexExistsPrecondition method check.

@Override
public void check(Database database, DatabaseChangeLog changeLog, ChangeSet changeSet, ChangeExecListener changeExecListener) throws PreconditionFailedException, PreconditionErrorException {
    try {
        Schema schema = new Schema(getCatalogName(), getSchemaName());
        Index example = new Index();
        String tableName = StringUtil.trimToNull(getTableName());
        if (tableName != null) {
            example.setRelation((Table) new Table().setName(database.correctObjectName(getTableName(), Table.class)).setSchema(schema));
        }
        example.setName(database.correctObjectName(getIndexName(), Index.class));
        if (StringUtil.trimToNull(getColumnNames()) != null) {
            for (String column : getColumnNames().split("\\s*,\\s*")) {
                example.addColumn(new Column(database.correctObjectName(column, Column.class)));
            }
        }
        if (!SnapshotGeneratorFactory.getInstance().has(example, database)) {
            String name = "";
            if (getIndexName() != null) {
                name += database.escapeObjectName(getIndexName(), Index.class);
            }
            if (tableName != null) {
                name += " on " + database.escapeObjectName(getTableName(), Table.class);
                if (StringUtil.trimToNull(getColumnNames()) != null) {
                    name += " columns " + getColumnNames();
                }
            }
            throw new PreconditionFailedException("Index " + name + " does not exist", changeLog, this);
        }
    } catch (Exception e) {
        if (e instanceof PreconditionFailedException) {
            throw (((PreconditionFailedException) e));
        }
        throw new PreconditionErrorException(e, changeLog, this);
    }
}
Also used : Table(liquibase.structure.core.Table) Column(liquibase.structure.core.Column) Schema(liquibase.structure.core.Schema) Index(liquibase.structure.core.Index) PreconditionFailedException(liquibase.exception.PreconditionFailedException) PreconditionFailedException(liquibase.exception.PreconditionFailedException) PreconditionErrorException(liquibase.exception.PreconditionErrorException) PreconditionErrorException(liquibase.exception.PreconditionErrorException)

Example 28 with Column

use of liquibase.structure.core.Column in project liquibase by liquibase.

the class DBDocVisitor method writeHTML.

public void writeHTML(File rootOutputDir, ResourceAccessor resourceAccessor) throws IOException, LiquibaseException {
    ChangeLogWriter changeLogWriter = new ChangeLogWriter(resourceAccessor, rootOutputDir);
    HTMLWriter authorWriter = new AuthorWriter(rootOutputDir, database);
    HTMLWriter tableWriter = new TableWriter(rootOutputDir, database);
    HTMLWriter columnWriter = new ColumnWriter(rootOutputDir, database);
    HTMLWriter pendingChangesWriter = new PendingChangesWriter(rootOutputDir, database);
    HTMLWriter recentChangesWriter = new RecentChangesWriter(rootOutputDir, database);
    HTMLWriter pendingSQLWriter = new PendingSQLWriter(rootOutputDir, database, rootChangeLog);
    copyFile("liquibase/dbdoc/stylesheet.css", rootOutputDir);
    copyFile("liquibase/dbdoc/index.html", rootOutputDir);
    copyFile("liquibase/dbdoc/globalnav.html", rootOutputDir);
    copyFile("liquibase/dbdoc/overview-summary.html", rootOutputDir);
    DatabaseSnapshot snapshot = SnapshotGeneratorFactory.getInstance().createSnapshot(database.getDefaultSchema(), database, new SnapshotControl(database));
    new ChangeLogListWriter(rootOutputDir).writeHTML(changeLogs);
    SortedSet<Table> tables = new TreeSet<>(snapshot.get(Table.class));
    Iterator<Table> tableIterator = tables.iterator();
    while (tableIterator.hasNext()) {
        if (database.isLiquibaseObject(tableIterator.next())) {
            tableIterator.remove();
        }
    }
    new TableListWriter(rootOutputDir).writeHTML(tables);
    new AuthorListWriter(rootOutputDir).writeHTML(new TreeSet<Object>(changesByAuthor.keySet()));
    for (String author : changesByAuthor.keySet()) {
        authorWriter.writeHTML(author, changesByAuthor.get(author), changesToRunByAuthor.get(author), rootChangeLogName);
    }
    for (Table table : tables) {
        if (database.isLiquibaseObject(table)) {
            continue;
        }
        tableWriter.writeHTML(table, changesByObject.get(table), changesToRunByObject.get(table), rootChangeLogName);
    }
    for (Column column : snapshot.get(Column.class)) {
        if (database.isLiquibaseObject(column.getRelation())) {
            continue;
        }
        columnWriter.writeHTML(column, changesByObject.get(column), changesToRunByObject.get(column), rootChangeLogName);
    }
    for (ChangeLogInfo changeLog : changeLogs) {
        changeLogWriter.writeChangeLog(changeLog.logicalPath, changeLog.physicalPath);
    }
    pendingChangesWriter.writeHTML("index", null, changesToRun, rootChangeLogName);
    pendingSQLWriter.writeHTML("sql", null, changesToRun, rootChangeLogName);
    if (recentChanges.size() > MAX_RECENT_CHANGE) {
        recentChanges = recentChanges.subList(0, MAX_RECENT_CHANGE);
    }
    recentChangesWriter.writeHTML("index", recentChanges, null, rootChangeLogName);
}
Also used : Table(liquibase.structure.core.Table) Column(liquibase.structure.core.Column) DatabaseObject(liquibase.structure.DatabaseObject) DatabaseSnapshot(liquibase.snapshot.DatabaseSnapshot) SnapshotControl(liquibase.snapshot.SnapshotControl)

Example 29 with Column

use of liquibase.structure.core.Column in project liquibase by liquibase.

the class MySQLIntegrationTest method dateDefaultValue.

@Test
public void dateDefaultValue() throws Exception {
    if (getDatabase() == null) {
        return;
    }
    Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor("jdbc", getDatabase()).execute(new RawSqlStatement("DROP TABLE IF " + "EXISTS ad"));
    try {
        Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor("jdbc", getDatabase()).execute(new RawSqlStatement("CREATE TABLE ad (\n" + "ad_id int(10) unsigned NOT NULL AUTO_INCREMENT,\n" + "advertiser_id int(10) unsigned NOT NULL,\n" + "ad_type_id int(10) unsigned NOT NULL,\n" + "name varchar(155) NOT NULL DEFAULT '',\n" + "label varchar(155)NOT NULL DEFAULT '',\n" + "description text NOT NULL,\n" + "active tinyint(1) NOT NULL DEFAULT '0',\n" + "created datetime NOT NULL DEFAULT '0000-00-00 00:00:00',\n" + "updated datetime DEFAULT '0000-00-00 00:00:00',\n" + "PRIMARY KEY (ad_id),\n" + "KEY active (active)\n" + ")"));
    } catch (DatabaseException e) {
        if (e.getCause() instanceof SQLSyntaxErrorException) {
            Scope.getCurrentScope().getLog(getClass()).warning("MySQL returned DatabaseException", e);
            assumeTrue("MySQL seems to run in strict mode (no datetime literals with 0000-00-00 allowed). " + "Cannot run this test", false);
        } else {
            throw e;
        }
    }
    DatabaseSnapshot snapshot = SnapshotGeneratorFactory.getInstance().createSnapshot(CatalogAndSchema.DEFAULT, getDatabase(), new SnapshotControl(getDatabase()));
    Column createdColumn = snapshot.get(new Column().setRelation(new Table().setName("ad").setSchema(new Schema())).setName("created"));
    Object defaultValue = createdColumn.getDefaultValue();
    assertNotNull(defaultValue);
    assertEquals("0000-00-00 00:00:00", defaultValue);
}
Also used : RawSqlStatement(liquibase.statement.core.RawSqlStatement) Table(liquibase.structure.core.Table) Column(liquibase.structure.core.Column) Schema(liquibase.structure.core.Schema) CatalogAndSchema(liquibase.CatalogAndSchema) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) DatabaseException(liquibase.exception.DatabaseException) DatabaseSnapshot(liquibase.snapshot.DatabaseSnapshot) SnapshotControl(liquibase.snapshot.SnapshotControl) Test(org.junit.Test) AbstractIntegrationTest(liquibase.dbtest.AbstractIntegrationTest)

Example 30 with Column

use of liquibase.structure.core.Column in project liquibase by liquibase.

the class MariaDBIntegrationTest method dateDefaultValue.

@Test
public void dateDefaultValue() throws Exception {
    if (getDatabase() == null) {
        return;
    }
    Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor("jdbc", getDatabase()).execute(new RawSqlStatement("DROP TABLE IF " + "EXISTS ad"));
    try {
        Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor("jdbc", getDatabase()).execute(new RawSqlStatement("CREATE TABLE ad (\n" + "ad_id int(10) unsigned NOT NULL AUTO_INCREMENT,\n" + "advertiser_id int(10) unsigned NOT NULL,\n" + "ad_type_id int(10) unsigned NOT NULL,\n" + "name varchar(155) NOT NULL DEFAULT '',\n" + "label varchar(155)NOT NULL DEFAULT '',\n" + "description text NOT NULL,\n" + "active tinyint(1) NOT NULL DEFAULT '0',\n" + "created datetime NOT NULL DEFAULT '0000-00-00 00:00:00',\n" + "updated datetime DEFAULT '0000-00-00 00:00:00',\n" + "PRIMARY KEY (ad_id),\n" + "KEY active (active)\n" + ")"));
    } catch (DatabaseException e) {
        if (e.getCause() instanceof SQLSyntaxErrorException) {
            Scope.getCurrentScope().getLog(getClass()).warning("MariaDB returned DatabaseException", e);
            assumeTrue("MariaDB seems to run in strict mode (no datetime literals with 0000-00-00 allowed). " + "Cannot run this test", false);
        } else {
            throw e;
        }
    }
    DatabaseSnapshot snapshot = SnapshotGeneratorFactory.getInstance().createSnapshot(CatalogAndSchema.DEFAULT, getDatabase(), new SnapshotControl(getDatabase()));
    Column createdColumn = snapshot.get(new Column().setRelation(new Table().setName("ad").setSchema(new Schema())).setName("created"));
    Object defaultValue = createdColumn.getDefaultValue();
    assertNotNull(defaultValue);
    assertEquals("0000-00-00 00:00:00", defaultValue);
}
Also used : RawSqlStatement(liquibase.statement.core.RawSqlStatement) Table(liquibase.structure.core.Table) Column(liquibase.structure.core.Column) Schema(liquibase.structure.core.Schema) CatalogAndSchema(liquibase.CatalogAndSchema) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) DatabaseException(liquibase.exception.DatabaseException) DatabaseSnapshot(liquibase.snapshot.DatabaseSnapshot) SnapshotControl(liquibase.snapshot.SnapshotControl) Test(org.junit.Test) AbstractIntegrationTest(liquibase.dbtest.AbstractIntegrationTest)

Aggregations

Column (liquibase.structure.core.Column)43 Table (liquibase.structure.core.Table)22 Change (liquibase.change.Change)8 SnapshotControl (liquibase.snapshot.SnapshotControl)8 Index (liquibase.structure.core.Index)8 DatabaseSnapshot (liquibase.snapshot.DatabaseSnapshot)7 DatabaseObject (liquibase.structure.DatabaseObject)6 Test (org.junit.Test)6 DatabaseException (liquibase.exception.DatabaseException)5 SqlStatement (liquibase.statement.SqlStatement)5 Schema (liquibase.structure.core.Schema)5 UniqueConstraint (liquibase.structure.core.UniqueConstraint)5 ArrayList (java.util.ArrayList)4 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)4 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)4 CatalogAndSchema (liquibase.CatalogAndSchema)3 Liquibase (liquibase.Liquibase)3 AbstractIntegrationTest (liquibase.dbtest.AbstractIntegrationTest)3 RawSqlStatement (liquibase.statement.core.RawSqlStatement)3 PrimaryKey (liquibase.structure.core.PrimaryKey)3