|
|
|
@ -6,12 +6,6 @@
|
|
|
|
|
*/ |
|
|
|
|
package com.fr.third.org.hibernate.tool.schema.internal; |
|
|
|
|
|
|
|
|
|
import java.util.HashMap; |
|
|
|
|
import java.util.HashSet; |
|
|
|
|
import java.util.Iterator; |
|
|
|
|
import java.util.Map; |
|
|
|
|
import java.util.Set; |
|
|
|
|
|
|
|
|
|
import com.fr.third.org.hibernate.boot.Metadata; |
|
|
|
|
import com.fr.third.org.hibernate.boot.model.naming.Identifier; |
|
|
|
|
import com.fr.third.org.hibernate.boot.model.relational.AuxiliaryDatabaseObject; |
|
|
|
@ -26,17 +20,21 @@ import com.fr.third.org.hibernate.engine.config.spi.StandardConverters;
|
|
|
|
|
import com.fr.third.org.hibernate.engine.jdbc.internal.FormatStyle; |
|
|
|
|
import com.fr.third.org.hibernate.engine.jdbc.internal.Formatter; |
|
|
|
|
import com.fr.third.org.hibernate.internal.util.StringHelper; |
|
|
|
|
import com.fr.third.org.hibernate.mapping.Column; |
|
|
|
|
import com.fr.third.org.hibernate.mapping.Constraint; |
|
|
|
|
import com.fr.third.org.hibernate.mapping.ForeignKey; |
|
|
|
|
import com.fr.third.org.hibernate.mapping.Index; |
|
|
|
|
import com.fr.third.org.hibernate.mapping.PrimaryKey; |
|
|
|
|
import com.fr.third.org.hibernate.mapping.Table; |
|
|
|
|
import com.fr.third.org.hibernate.mapping.UniqueKey; |
|
|
|
|
import com.fr.third.org.hibernate.resource.transaction.spi.DdlTransactionIsolator; |
|
|
|
|
import com.fr.third.org.hibernate.tool.hbm2ddl.UniqueConstraintSchemaUpdateStrategy; |
|
|
|
|
import com.fr.third.org.hibernate.tool.schema.extract.spi.ColumnInformation; |
|
|
|
|
import com.fr.third.org.hibernate.tool.schema.extract.spi.DatabaseInformation; |
|
|
|
|
import com.fr.third.org.hibernate.tool.schema.extract.spi.ForeignKeyInformation; |
|
|
|
|
import com.fr.third.org.hibernate.tool.schema.extract.spi.IndexInformation; |
|
|
|
|
import com.fr.third.org.hibernate.tool.schema.extract.spi.NameSpaceTablesInformation; |
|
|
|
|
import com.fr.third.org.hibernate.tool.schema.extract.spi.PrimaryKeyInformation; |
|
|
|
|
import com.fr.third.org.hibernate.tool.schema.extract.spi.SequenceInformation; |
|
|
|
|
import com.fr.third.org.hibernate.tool.schema.extract.spi.TableInformation; |
|
|
|
|
import com.fr.third.org.hibernate.tool.schema.internal.exec.GenerationTarget; |
|
|
|
@ -48,9 +46,15 @@ import com.fr.third.org.hibernate.tool.schema.spi.SchemaFilter;
|
|
|
|
|
import com.fr.third.org.hibernate.tool.schema.spi.SchemaManagementException; |
|
|
|
|
import com.fr.third.org.hibernate.tool.schema.spi.SchemaMigrator; |
|
|
|
|
import com.fr.third.org.hibernate.tool.schema.spi.TargetDescriptor; |
|
|
|
|
|
|
|
|
|
import com.fr.third.org.jboss.logging.Logger; |
|
|
|
|
|
|
|
|
|
import java.util.HashMap; |
|
|
|
|
import java.util.HashSet; |
|
|
|
|
import java.util.Iterator; |
|
|
|
|
import java.util.List; |
|
|
|
|
import java.util.Map; |
|
|
|
|
import java.util.Set; |
|
|
|
|
|
|
|
|
|
import static com.fr.third.org.hibernate.cfg.AvailableSettings.UNIQUE_CONSTRAINT_SCHEMA_UPDATE_STRATEGY; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -355,6 +359,29 @@ public abstract class AbstractSchemaMigrator implements SchemaMigrator {
|
|
|
|
|
if ( uniqueConstraintStrategy != UniqueConstraintSchemaUpdateStrategy.SKIP ) { |
|
|
|
|
final Exporter<Constraint> exporter = dialect.getUniqueKeyExporter(); |
|
|
|
|
|
|
|
|
|
Set<Identifier> identifiers = getUniqueKeyIdentifiers(table); |
|
|
|
|
if (tableInfo != null) { |
|
|
|
|
for (IndexInformation indexInfo : tableInfo.getIndexes()) { |
|
|
|
|
if (!indexInfo.isUnique() || indexInfo.getIndexIdentifier().equals(tableInfo.getPrimaryKey().getPrimaryKeyIdentifier())) { |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
//如果旧的唯一键已经不存在, 则删除
|
|
|
|
|
if (!identifiers.contains(indexInfo.getIndexIdentifier())) { |
|
|
|
|
log.warn("delete unique key not existed in entity: " + indexInfo.getIndexIdentifier().getText()); |
|
|
|
|
UniqueKey old = new UniqueKey(); |
|
|
|
|
old.setTable(table); |
|
|
|
|
old.setName(indexInfo.getIndexIdentifier().getText()); |
|
|
|
|
applySqlStrings( |
|
|
|
|
true, |
|
|
|
|
exporter.getSqlDropStrings(old, metadata), |
|
|
|
|
formatter, |
|
|
|
|
options, |
|
|
|
|
targets |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
final Iterator ukItr = table.getUniqueKeyIterator(); |
|
|
|
|
while ( ukItr.hasNext() ) { |
|
|
|
|
final UniqueKey uniqueKey = (UniqueKey) ukItr.next(); |
|
|
|
@ -388,6 +415,18 @@ public abstract class AbstractSchemaMigrator implements SchemaMigrator {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private Set<Identifier> getUniqueKeyIdentifiers(Table table) { |
|
|
|
|
Set<Identifier> identifiers = new HashSet<>(); |
|
|
|
|
Iterator<UniqueKey> uniqueKeys = table.getUniqueKeyIterator(); |
|
|
|
|
while (uniqueKeys.hasNext()) { |
|
|
|
|
UniqueKey uniqueKey = uniqueKeys.next(); |
|
|
|
|
if (StringHelper.isNotEmpty(uniqueKey.getName())) { |
|
|
|
|
identifiers.add(Identifier.toIdentifier(uniqueKey.getName())); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return identifiers; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private UniqueConstraintSchemaUpdateStrategy determineUniqueConstraintSchemaUpdateStrategy(Metadata metadata) { |
|
|
|
|
final ConfigurationService cfgService = ((MetadataImplementor) metadata).getMetadataBuildingOptions() |
|
|
|
|
.getServiceRegistry() |
|
|
|
@ -446,6 +485,57 @@ public abstract class AbstractSchemaMigrator implements SchemaMigrator {
|
|
|
|
|
return tableInformation.getForeignKey( Identifier.toIdentifier( foreignKey.getName() ) ); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
protected void applyPrimaryKey( |
|
|
|
|
Table table, |
|
|
|
|
TableInformation tableInfo, |
|
|
|
|
Dialect dialect, |
|
|
|
|
Metadata metadata, |
|
|
|
|
Formatter formatter, |
|
|
|
|
ExecutionOptions options, |
|
|
|
|
GenerationTarget... targets) { |
|
|
|
|
try { |
|
|
|
|
PrimaryKey primaryKey = table.getPrimaryKey(); |
|
|
|
|
if (primaryKey == null) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
Exporter<PrimaryKey> exporter = new StandardPrimaryKeyExporter(dialect); |
|
|
|
|
PrimaryKeyInformation existedPrimaryKeyInfo = tableInfo.getPrimaryKey(); |
|
|
|
|
if (existedPrimaryKeyInfo == null) { |
|
|
|
|
applySqlStrings(true, exporter.getSqlCreateStrings(primaryKey, metadata), formatter, options, targets); |
|
|
|
|
} else if (!columnEquals(table.getPrimaryKey().getColumns(), existedPrimaryKeyInfo.getColumns())) { |
|
|
|
|
//主键列不匹配, 先删再增
|
|
|
|
|
PrimaryKey deletedKey = copyPrimaryKey(tableInfo.getPrimaryKey().getPrimaryKeyIdentifier().getText(), table); |
|
|
|
|
applySqlStrings(true, exporter.getSqlDropStrings(deletedKey, metadata), formatter, options, targets); |
|
|
|
|
applySqlStrings(true, exporter.getSqlCreateStrings(primaryKey, metadata), formatter, options, targets); |
|
|
|
|
} |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
log.error(e.getMessage(), e); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private PrimaryKey copyPrimaryKey(String newName, Table table) { |
|
|
|
|
PrimaryKey copy = new PrimaryKey(table); |
|
|
|
|
copy.setName(newName); |
|
|
|
|
return copy; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private boolean columnEquals(List<Column> columns, Iterable<ColumnInformation> iterator) { |
|
|
|
|
int count = 0; |
|
|
|
|
Set<String> names = new HashSet<String>(); |
|
|
|
|
for (Column column: columns) { |
|
|
|
|
names.add(column.getName().toLowerCase()); |
|
|
|
|
} |
|
|
|
|
for (ColumnInformation columnInformation : iterator) { |
|
|
|
|
String name = columnInformation.getColumnIdentifier().getText(); |
|
|
|
|
if (!names.contains(name.toLowerCase())) { |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
count++; |
|
|
|
|
} |
|
|
|
|
return count == columns.size(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
protected void checkExportIdentifier(Exportable exportable, Set<String> exportIdentifiers) { |
|
|
|
|
final String exportIdentifier = exportable.getExportIdentifier(); |
|
|
|
|
if ( exportIdentifiers.contains( exportIdentifier ) ) { |
|
|
|
|