private void compareIndices(Table expected, Table actual) { // TODO -- drop obsolete indices? var schemaName = expected.Identifier.Schema; foreach (var index in expected.Indexes) { if (actual.ActualIndices.ContainsKey(index.IndexName)) { var actualIndex = actual.ActualIndices[index.IndexName]; if (!index.Matches(actualIndex)) { IndexChanges.Add($"drop index {schemaName}.{index.IndexName};{Environment.NewLine}{index.ToDDL()};"); IndexRollbacks.Add($"drop index {schemaName}.{index.IndexName};{Environment.NewLine}{actualIndex.DDL};"); } } else { IndexChanges.Add(index.ToDDL()); IndexRollbacks.Add($"drop index concurrently if exists {schemaName}.{index.IndexName};"); } } var obsoleteIndexes = actual.ActualIndices.Values.Where(x => expected.Indexes.All(_ => _.IndexName != x.Name)); foreach (var index in obsoleteIndexes) { IndexRollbacks.Add(index.DDL); IndexChanges.Add($"drop index concurrently if exists {schemaName}.{index.Name};"); } }
public SchemaDiff(SchemaObjects existing, DocumentMapping mapping, DdlRules rules) { if (existing.HasNone()) { AllMissing = true; } else { var expectedTable = mapping.SchemaObjects.As <DocumentSchemaObjects>().StorageTable(); TableDiff = new TableDiff(expectedTable, existing.Table); // TODO -- drop obsolete indices? mapping.Indexes.Each(index => { if (existing.ActualIndices.ContainsKey(index.IndexName)) { var actualIndex = existing.ActualIndices[index.IndexName]; if (!index.Matches(actualIndex)) { IndexChanges.Add($"drop index {expectedTable.Table.Schema}.{index.IndexName};{Environment.NewLine}{index.ToDDL()};"); IndexRollbacks.Add($"drop index {expectedTable.Table.Schema}.{index.IndexName};{Environment.NewLine}{actualIndex.DDL};"); } } else { IndexChanges.Add(index.ToDDL()); IndexRollbacks.Add($"drop index concurrently if exists {expectedTable.Table.Schema}.{index.IndexName};"); } }); existing.ActualIndices.Values.Where(x => mapping.Indexes.All(_ => _.IndexName != x.Name)).Each( index => { IndexRollbacks.Add(index.DDL); IndexChanges.Add($"drop index concurrently if exists {mapping.Table.Schema}.{index.Name};"); }); var expectedFunction = new UpsertFunction(mapping); FunctionDiff = new FunctionDiff(expectedFunction.ToBody(rules), existing.Function); var missingFKs = mapping.ForeignKeys.Where(x => !existing.ForeignKeys.Contains(x.KeyName)); MissingForeignKeys.AddRange(missingFKs); } _mapping = mapping; }
private void compareIndices(Table expected, Table actual) { // TODO -- drop obsolete indices? var schemaName = expected.Identifier.Schema; var obsoleteIndexes = actual.ActualIndices.Values.Where(x => expected.Indexes.All(_ => _.IndexName != x.Name)); foreach (var index in obsoleteIndexes) { IndexRollbacks.Add(index.DDL); if (!index.Name.EndsWith("pkey")) { IndexChanges.Add($"drop index concurrently if exists {schemaName}.{index.Name};"); } /* else * { * IndexChanges.Add($"alter table {_tableName} drop constraint if exists {schemaName}.{index.Name};"); * }*/ } foreach (var index in expected.Indexes) { if (actual.ActualIndices.TryGetValue(index.IndexName, out var actualIndex)) { if (!index.Matches(actualIndex)) { IndexChanges.Add($"drop index {schemaName}.{index.IndexName};{Environment.NewLine}{index.ToDDL()};"); IndexRollbacks.Add($"drop index {schemaName}.{index.IndexName};{Environment.NewLine}{actualIndex.DDL};"); } } else { IndexChanges.Add(index.ToDDL()); IndexRollbacks.Add($"drop index concurrently if exists {schemaName}.{index.IndexName};"); } } }