protected String normalizeTableName(liquibase.structure.core.Table table, DatabaseSnapshot databaseSnapshot) { //Work around MySQL & case-insensitive dbs liquibase.structure.core.Table fkTable = databaseSnapshot.get(table); String fkTableName; if(fkTable != null) { fkTableName = fkTable.getName(); } else { logger.warn("Could not normalize table name: " + table.getName() + "; most probably this is a sign of a foreign key to another schema, which is not supported at the moment."); fkTableName = null; } return fkTableName; }
@Override protected DatabaseObject snapshotObject(DatabaseObject example, DatabaseSnapshot snapshot) throws DatabaseException, InvalidExampleException { return new Schema(snapshot.getDatabase().getDefaultCatalogName(), snapshot.getDatabase().getDefaultSchemaName()).setDefault(true); }
@Override protected void addTo(DatabaseObject foundObject, DatabaseSnapshot snapshot) throws DatabaseException, InvalidExampleException { if (!snapshot.getSnapshotControl().shouldInclude(Table.class)) { return; } if (foundObject instanceof Schema) { Schema schema = (Schema) foundObject; HibernateDatabase database = (HibernateDatabase) snapshot.getDatabase(); Configuration cfg = database.getConfiguration(); Iterator<org.hibernate.mapping.Table> tableMappings = cfg.getTableMappings(); while (tableMappings.hasNext()) { org.hibernate.mapping.Table hibernateTable = tableMappings.next(); if (hibernateTable.isPhysicalTable()) { Table table = new Table().setName(hibernateTable.getName()); table.setSchema(schema); LOG.info("Found table " + table.getName()); schema.addDatabaseObject(table); } } } } }
@Override protected void addTo(DatabaseObject foundObject, DatabaseSnapshot snapshot) throws DatabaseException, InvalidExampleException { if (!snapshot.getSnapshotControl().shouldInclude(Index.class)) { return; } if (foundObject instanceof Table) { Table table = (Table) foundObject; org.hibernate.mapping.Table hibernateTable = findHibernateTable(table, snapshot); if (hibernateTable == null) { return; } Iterator indexIterator = hibernateTable.getIndexIterator(); while (indexIterator.hasNext()) { org.hibernate.mapping.Index hibernateIndex = (org.hibernate.mapping.Index) indexIterator.next(); Index index = new Index(); index.setTable(table); index.setName(hibernateIndex.getName()); Iterator columnIterator = hibernateIndex.getColumnIterator(); while (columnIterator.hasNext()) { org.hibernate.mapping.Column hibernateColumn = (org.hibernate.mapping.Column) columnIterator.next(); index.getColumns().add(new Column(hibernateColumn.getName()).setRelation(table)); } LOG.info("Found index " + index.getName()); table.getIndexes().add(index); } } }
@Override protected void addTo(DatabaseObject foundObject, DatabaseSnapshot snapshot) throws DatabaseException, InvalidExampleException { if (!snapshot.getSnapshotControl().shouldInclude(Sequence.class)) { return; HibernateDatabase database = (HibernateDatabase) snapshot.getDatabase(); MetadataImplementor metadata = (MetadataImplementor) database.getMetadata(); Iterator<PersistentClass> classMappings = metadata.getEntityBindings().iterator();
@Override protected void addTo(DatabaseObject foundObject, DatabaseSnapshot snapshot) throws DatabaseException, InvalidExampleException { if (!snapshot.getSnapshotControl().shouldInclude(UniqueConstraint.class)) { return;
@Override protected void addTo(DatabaseObject foundObject, DatabaseSnapshot snapshot) throws DatabaseException, InvalidExampleException { if (!snapshot.getSnapshotControl().shouldInclude(Table.class)) { return; HibernateDatabase database = (HibernateDatabase) snapshot.getDatabase(); MetadataImplementor metadata = (MetadataImplementor) database.getMetadata();
protected String normalizeTableName(liquibase.structure.core.Table table, DatabaseSnapshot databaseSnapshot) { //Work around MySQL & case-insensitive dbs liquibase.structure.core.Table fkTable = databaseSnapshot.get(table); String fkTableName; if(fkTable != null) { fkTableName = fkTable.getName(); } else { logger.warn("Could not normalize table name: " + table.getName() + "; most probably this is a sign of a foreign key to another schema, which is not supported at the moment."); fkTableName = null; } return fkTableName; }
protected org.hibernate.mapping.Table findHibernateTable(DatabaseObject example, DatabaseSnapshot snapshot) throws DatabaseException { HibernateDatabase database = (HibernateDatabase) snapshot.getDatabase(); MetadataImplementor metadata = (MetadataImplementor) database.getMetadata(); Collection<Table> tmapp = metadata.collectTableMappings(); Iterator<Table> tableMappings = tmapp.iterator(); while (tableMappings.hasNext()) { org.hibernate.mapping.Table hibernateTable = tableMappings.next(); if (hibernateTable.getName().equalsIgnoreCase(example.getName())) { return hibernateTable; } } return null; } }
@Override protected void addTo(DatabaseObject foundObject, DatabaseSnapshot snapshot) throws DatabaseException, InvalidExampleException { if (!snapshot.getSnapshotControl().shouldInclude(PrimaryKey.class)) { return;
@Override protected void addTo(DatabaseObject foundObject, DatabaseSnapshot snapshot) throws DatabaseException, InvalidExampleException { if (!snapshot.getSnapshotControl().shouldInclude(ForeignKey.class)) { return; HibernateDatabase database = (HibernateDatabase) snapshot.getDatabase(); MetadataImplementor metadata = (MetadataImplementor) database.getMetadata();
protected void syncTables(DatabaseSnapshot databaseSnapshot, Schema sourceSchema, Schema targetSchema) { logger.info("Synchronizing tables"); for (liquibase.structure.core.Table liquibaseTable : databaseSnapshot.get(liquibase.structure.core.Table.class)) { String tableName = liquibaseTable.getName(); logger.info("Processing table: {}", tableName); Table sourceTable = DatabaseLogic.findTableByNameIgnoreCase(sourceSchema, tableName); if(sourceTable == null) { logger.debug("Added new table: {}", tableName); sourceTable = new Table(); } Table targetTable = new Table(targetSchema); targetSchema.getTables().add(targetTable); targetTable.setTableName(tableName); logger.debug("Merging table attributes and annotations"); targetTable.setEntityName(sourceTable.getEntityName()); targetTable.setJavaClass(sourceTable.getJavaClass()); targetTable.setShortName(sourceTable.getShortName()); copyAnnotations(sourceTable, targetTable); syncColumns(liquibaseTable, sourceTable, targetTable); copySelectionProviders(sourceTable, targetTable); } }
@Override protected DatabaseObject snapshotObject(DatabaseObject example, DatabaseSnapshot snapshot) throws DatabaseException, InvalidExampleException { return new Catalog(snapshot.getDatabase().getDefaultCatalogName()).setDefault(true); }
protected void syncTables(DatabaseSnapshot databaseSnapshot, Schema sourceSchema, Schema targetSchema) { logger.info("Synchronizing tables"); for (liquibase.structure.core.Table liquibaseTable : databaseSnapshot.get(liquibase.structure.core.Table.class)) { String tableName = liquibaseTable.getName(); logger.info("Processing table: {}", tableName); Table sourceTable = DatabaseLogic.findTableByNameIgnoreCase(sourceSchema, tableName); if(sourceTable == null) { logger.debug("Added new table: {}", tableName); sourceTable = new Table(); } Table targetTable = new Table(targetSchema); targetSchema.getTables().add(targetTable); targetTable.setTableName(tableName); logger.debug("Merging table attributes and annotations"); targetTable.setEntityName(sourceTable.getEntityName()); targetTable.setJavaClass(sourceTable.getJavaClass()); targetTable.setShortName(sourceTable.getShortName()); copyAnnotations(sourceTable, targetTable); syncColumns(liquibaseTable, sourceTable, targetTable); copySelectionProviders(sourceTable, targetTable); } }
protected void snapshotColumn(Column column, DatabaseSnapshot snapshot) throws DatabaseException { HibernateDatabase database = (HibernateDatabase) snapshot.getDatabase(); snapshot.getDatabase(), defaultValue, parseType));
@Override protected DatabaseObject snapshotObject(DatabaseObject example, DatabaseSnapshot snapshot) throws DatabaseException, InvalidExampleException { Column column = (Column) example; if (column.getType() == null) { //not the actual full version found with the table if (column.getRelation() == null) { throw new InvalidExampleException("No relation set on " + column); } Relation relation = snapshot.get(column.getRelation()); if (relation != null) { for (Column columnSnapshot : relation.getColumns()) { if (columnSnapshot.getName().equalsIgnoreCase(column.getName())) { return columnSnapshot; } } } snapshotColumn((Column) example, snapshot); return example; //did not find it } else { return example; } }
@Override protected DatabaseObject snapshotObject(DatabaseObject example, DatabaseSnapshot snapshot) throws DatabaseException, InvalidExampleException { HibernateDatabase database = (HibernateDatabase) snapshot.getDatabase(); Configuration cfg = database.getConfiguration();
protected void syncPrimaryKeys(DatabaseSnapshot databaseSnapshot, Schema sourceSchema, Schema targetSchema) { logger.info("Synchronizing primary keys"); for(liquibase.structure.core.PrimaryKey liquibasePK : databaseSnapshot.get(liquibase.structure.core.PrimaryKey.class)) { String pkTableName = liquibasePK.getTable().getName();
protected void syncPrimaryKeys(DatabaseSnapshot databaseSnapshot, Schema sourceSchema, Schema targetSchema) { logger.info("Synchronizing primary keys"); for(liquibase.structure.core.PrimaryKey liquibasePK : databaseSnapshot.get(liquibase.structure.core.PrimaryKey.class)) { String pkTableName = liquibasePK.getTable().getName();
protected void syncForeignKeys(DatabaseSnapshot databaseSnapshot, Schema sourceSchema, Schema targetSchema) { logger.info("Synchronizing foreign keys"); for(liquibase.structure.core.ForeignKey liquibaseFK : databaseSnapshot.get(liquibase.structure.core.ForeignKey.class)) { String fkName = liquibaseFK.getName(); logger.info("Synchronizing foreign key {}", fkName);