private void createSequence(String sequenceName, Schema schema) { Sequence sequence = new Sequence(); sequence.setName(sequenceName); sequence.setSchema(schema); schema.addDatabaseObject(sequence); }
protected Index getBackingIndex(UniqueConstraint uniqueConstraint, org.hibernate.mapping.Table hibernateTable, DatabaseSnapshot snapshot) { Index index = new Index(); index.setTable(uniqueConstraint.getTable()); index.setColumns(uniqueConstraint.getColumns()); index.setUnique(true); return index; }
StoredProcedure sp = new StoredProcedure(datasource, spName) { {// init code if there are any} };
@Override public Table snapshot(IdentifierGenerator ig) { TableGenerator tableGenerator = (TableGenerator) ig; Table table = new Table().setName(tableGenerator.getTableName()); Column pkColumn = new Column(); pkColumn.setName(tableGenerator.getSegmentColumnName()); DataType pkDataType = new DataType(PK_DATA_TYPE); pkDataType.setColumnSize(tableGenerator.getSegmentValueLength()); pkColumn.setType(pkDataType); pkColumn.setCertainDataType(false); pkColumn.setRelation(table); table.getColumns().add(pkColumn); PrimaryKey primaryKey = new PrimaryKey(); primaryKey.setName(tableGenerator.getTableName() + "PK"); primaryKey.addColumn(0, new Column(pkColumn.getName()).setRelation(table)); primaryKey.setTable(table); table.setPrimaryKey(primaryKey); Column valueColumn = new Column(); valueColumn.setName(tableGenerator.getValueColumnName()); valueColumn.setType(new DataType(VALUE_DATA_TYPE)); valueColumn.setNullable(false); valueColumn.setCertainDataType(false); valueColumn.setRelation(table); table.getColumns().add(valueColumn); return table; }
keySize.setAccessible(true); table = new Table().setName((String) tableName.get(tableGenerator)); Column pkColumn = new Column(); pkColumn.setName((String) pkColumnName.get(tableGenerator)); DataType pkDataType = new DataType(PK_DATA_TYPE); pkDataType.setColumnSize(keySize.getInt(tableGenerator)); pkColumn.setType(pkDataType); pkColumn.setCertainDataType(false); pkColumn.setRelation(table); table.getColumns().add(pkColumn); Column valueColumn = new Column(); valueColumn.setName((String) valueColumnName.get(tableGenerator)); valueColumn.setType(new DataType(VALUE_DATA_TYPE)); valueColumn.setCertainDataType(false); valueColumn.setRelation(table); table.getColumns().add(valueColumn);
while (fkIterator.hasNext()) { org.hibernate.mapping.ForeignKey hibernateForeignKey = (org.hibernate.mapping.ForeignKey) fkIterator.next(); Table currentTable = new Table().setName(hibernateTable.getName()); currentTable.setSchema(hibernateTable.getCatalog(), hibernateTable.getSchema()); Table referencedTable = new Table().setName(hibernateReferencedTable.getName()); referencedTable.setSchema(hibernateReferencedTable.getCatalog(), hibernateReferencedTable.getSchema()); ForeignKey fk = new ForeignKey(); fk.setName(hibernateForeignKey.getName()); fk.setPrimaryKeyTable(referencedTable); fk.setForeignKeyTable(currentTable); for (Object column : hibernateForeignKey.getColumns()) { fk.addForeignKeyColumn(new liquibase.structure.core.Column(((org.hibernate.mapping.Column) column).getName())); fk.addPrimaryKeyColumn(new liquibase.structure.core.Column(((org.hibernate.mapping.Column) column).getName())); if (fk.getPrimaryKeyColumns() == null || fk.getPrimaryKeyColumns().isEmpty()) { for (Object column : hibernateReferencedTable.getPrimaryKey().getColumns()) { fk.addPrimaryKeyColumn(new liquibase.structure.core.Column(((org.hibernate.mapping.Column) column).getName())); fk.setDeferrable(false); fk.setInitiallyDeferred(false); table.getOutgoingForeignKeys().add(fk); table.getSchema().addDatabaseObject(fk);
PrimaryKey pk = new PrimaryKey(); String hbnPrimaryKeyName = hibernatePrimaryKey.getName(); pk.setName(hbnPrimaryKeyName); pk.setTable(table); for (Object hibernateColumn : hibernatePrimaryKey.getColumns()) { pk.getColumns().add(new Column(((org.hibernate.mapping.Column) hibernateColumn).getName()).setRelation(table)); LOG.info("Found primary key " + pk.getName()); table.setPrimaryKey(pk); Index index = new Index(); index.setName("IX_" + pk.getName()); index.setTable(table); index.setColumns(pk.getColumns()); index.setUnique(true); pk.setBackingIndex(index); table.getIndexes().add(index);
@Override protected DatabaseObject snapshotObject(DatabaseObject example, DatabaseSnapshot snapshot) throws DatabaseException, InvalidExampleException { if (example.getSnapshotId() != null) { return example; } org.hibernate.mapping.Table hibernateTable = findHibernateTable(example, snapshot); if (hibernateTable == null) { return example; } Table table = new Table().setName(hibernateTable.getName()); LOG.info("Found table " + table.getName()); // table.setSnapshotId(SnapshotIdService.getInstance().generateId()); table.setSchema(example.getSchema()); return table; }
@Override protected DatabaseObject snapshotObject(DatabaseObject example, DatabaseSnapshot snapshot) throws DatabaseException, InvalidExampleException { if (example.getSnapshotId() != null) { return example; } Table table = ((Index) example).getTable(); org.hibernate.mapping.Table hibernateTable = findHibernateTable(table, snapshot); if (hibernateTable == null) { return example; } 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)); } if (index.getColumnNames().equalsIgnoreCase(((Index) example).getColumnNames())) { LOG.info("Found index " + index.getName()); table.getIndexes().add(index); return index; } } return example; }
@Override protected void addTo(DatabaseObject foundObject, DatabaseSnapshot snapshot) throws DatabaseException, InvalidExampleException { if (foundObject instanceof Table) { org.hibernate.mapping.Table hibernateTable = findHibernateTable(foundObject, snapshot); if (hibernateTable == null) { return; } Iterator columnIterator = hibernateTable.getColumnIterator(); while (columnIterator.hasNext()) { org.hibernate.mapping.Column hibernateColumn = (org.hibernate.mapping.Column) columnIterator.next(); Column column = new Column(); column.setName(hibernateColumn.getName()); column.setRelation((Table) foundObject); snapshotColumn(column, snapshot); ((Table) foundObject).getColumns().add(column); } } }
@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; } }
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 public void check(Database database) throws CustomPreconditionFailedException, CustomPreconditionErrorException { Column column = new Column(this.columnNames); UniqueConstraint uniqueConstraint = new UniqueConstraint(this.constraintName, null, null, this.tableName, column); boolean markFailed = false; try { if (!SnapshotGeneratorFactory.getInstance().has(uniqueConstraint, database)) { markFailed = true; } } catch (Exception e) { throw new CustomPreconditionErrorException("custom precondition check errored", e); } if (markFailed) { throw new CustomPreconditionFailedException(this.constraintName + " doesn't exist"); } } }
StoredProcedure storedProcedure = new StoredProcedure( DataSourceFactory.getSomeDatasource(), "_SomeStoredProc" ) {}; storedProcedure.declareParameter( new SqlReturnResultSet( "foo", new RowMapper() { public Object mapRow( final ResultSet rs, final int rowNum ) throws SQLException { return rs.getObject( 1 ); } } ) ); storedProcedure.declareParameter( new SqlParameter( "inParamName", Types.VARCHAR ) ); HashMap< Object, Object > inParams = new HashMap< Object, Object >(); inParams.put( "inParamName", "myparam" ); Map execute = storedProcedure.execute( inParams ); System.out.println( execute.get( "foo" ) );
@Override protected DatabaseObject snapshotObject(DatabaseObject example, DatabaseSnapshot snapshot) throws DatabaseException, InvalidExampleException { return new Catalog(snapshot.getDatabase().getDefaultCatalogName()).setDefault(true); }
@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); } } }
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; }
StoredProcedure sp = new StoredProcedure(ds, SQL) { public Object execute(String tableName) { Map in = new HashMap(); in.put("param_in", tableName); Map out = execute(in); if (!out.isEmpty()) { return out.get("param_out"); } else { return null; } } };