public virtual void CreateDatabaseObjects(IDatabaseStructure db, CreateDatabaseObjectsProps props) { if (Dialect.DialectCaps.Domains && props.CreateDomains) { foreach (var domain in db.Domains) { try { CreateDomain(domain); } catch (Exception err) { ProgressInfo.RaiseErrorEx(err, "DAE-00244 " + Texts.Get("s_error_creating$domain", "domain", domain.FullName), "DOMAIN"); } } } if (Dialect.DialectCaps.MultipleSchema && props.CreateSchemata) { foreach (var schema in db.Schemata) { try { CreateSchema(schema); } catch (Exception err) { ProgressInfo.RaiseErrorEx(err, "DAE-00245 " + Texts.Get("s_error_creating$schema", "schema", schema.SchemaName), "SCHEMA"); } } } var refsToCreate = new List <IForeignKey>(); if (props.CreateTables) { foreach (var tbl in db.Tables) { ITableStructure tbl2 = tbl; if (!Dialect.DialectCaps.UncheckedReferences) { var newtbl = new TableStructure(tbl); foreach (ForeignKey fk in new List <ForeignKey>(newtbl.GetConstraints <ForeignKey>())) { newtbl._Constraints.Remove(fk); fk.SetDummyTable(tbl.FullName); refsToCreate.Add(fk); } tbl2 = newtbl; } Logging.Debug("Creating table {0}", tbl2.FullName); SetCurWork(String.Format("{0} {1}", Texts.Get("s_creating_table"), tbl2.FullName)); if (m_props.DumpWriterConfig != null && m_props.DumpWriterConfig.IncludeDropStatement) { DropTable(tbl2, DropFlags.TestIfExist); } try { CreateTable(tbl2); } catch (Exception err) { ProgressInfo.RaiseErrorEx(err, "DAE-00246 " + Texts.Get("s_error_creating$table", "table", tbl2.FullName), "TABLE"); } } } if (props.CreateFixedData) { foreach (var tbl in db.Tables) { this.UpdateData(tbl, DbDiffTool.AlterFixedData(null, tbl.FixedData, null, new DbDiffOptions()), null); } } foreach (var fk in refsToCreate) { CreateConstraint(fk); } if (props.CreateSpecificObjects) { foreach (var obj in db.GetSpecObjectsOrderByDependency()) { SetCurWork(String.Format("{0} {1}", Texts.Get("s_creating_object"), obj.ObjectName)); if (m_props.DumpWriterConfig != null && m_props.DumpWriterConfig.IncludeDropStatement) { DropSpecificObject(obj, DropFlags.TestIfExist); } try { CreateSpecificObject(obj); } catch (Exception err) { if (ProgressInfo != null) { ProgressInfo.LogMessage("s_create_object", LogLevel.Error, Texts.Get("s_error_creating_object$name", "name", obj.ObjectName) + ": " + err.Message); } Logging.Error("Error creating object:" + err.ToString()); } } } }
public static void AlterDatabase(AlterPlan plan, DbObjectPairing pairing, DbDiffOptions opts) { var src = pairing.Source; var dst = pairing.Target; //var caps = proc.AlterCaps; // domains foreach (IDomainStructure dsrc in src.Domains) { IDomainStructure ddst = pairing.FindPair(dsrc); if (ddst == null) { plan.DropDomain(dsrc); } else if (!DbDiffTool.EqualDomains(dsrc, ddst, opts, true)) { if (DbDiffTool.EqualDomains(dsrc, ddst, opts, false)) { plan.RenameDomain(dsrc, ddst.FullName); } else { plan.ChangeDomain(dsrc, ddst); } } } foreach (IDomainStructure ddst in dst.Domains) { if (!pairing.IsPaired(ddst)) { plan.CreateDomain(ddst); } } // drop tables foreach (ITableStructure tsrc in new List <ITableStructure>(src.Tables)) { ITableStructure tdst = pairing.FindPair(tsrc); if (tdst == null) { plan.DropTable(tsrc); } } // change tables foreach (ITableStructure tsrc in new List <ITableStructure>(src.Tables)) { ITableStructure tdst = pairing.FindPair(tsrc); if (tdst == null) { continue; } if (!DbDiffTool.EqualTables(tsrc, tdst, opts, pairing)) { DbDiffTool.AlterTable(plan, tsrc, tdst, opts, pairing); } else { DbDiffTool.AlterFixedData(plan, tsrc, tdst, opts); } } // create tables foreach (ITableStructure tdst in dst.Tables) { if (!pairing.IsPaired(tdst)) { var script = DbDiffTool.AlterFixedData(null, tdst.FixedData, null, opts); plan.CreateTable(tdst, script); //if (script != null) plan.UpdateData(tdst.FullName, script); } } // specific objects foreach (ISpecificObjectStructure osrc in src.GetAllSpecificObjects()) { var repr = SpecificRepresentationAddonType.Instance.FindRepresentation(osrc.ObjectType); if (!repr.UseInSynchronization) { continue; } ISpecificObjectStructure odst = pairing.FindPair(osrc); if (odst == null) { plan.DropSpecificObject(osrc); //proc.DropSpecificObject(osrc); } else if (!DbDiffTool.EqualsSpecificObjects(osrc, odst, opts)) { DbDiffTool.AlterSpecificObject(osrc, odst, plan, opts, pairing); } } foreach (ISpecificObjectStructure odst in dst.GetAllSpecificObjects()) { var repr = SpecificRepresentationAddonType.Instance.FindRepresentation(odst.ObjectType); if (!repr.UseInSynchronization) { continue; } if (!pairing.IsPaired(odst)) { plan.CreateSpecificObject(odst); //proc.CreateSpecificObject(odst); } } foreach (ISchemaStructure ssrc in src.Schemata) { ISchemaStructure sdst = pairing.FindPair(ssrc); if (sdst == null) { plan.DropSchema(ssrc); } else if (ssrc.SchemaName != sdst.SchemaName) { plan.RenameSchema(ssrc, sdst.SchemaName); //if (caps.RenameSchema) proc.RenameSchema(ssrc, sdst.SchemaName); //else //{ // proc.DropSchema(ssrc); // proc.CreateSchema(sdst); //} } } foreach (ISchemaStructure sdst in dst.Schemata) { if (!pairing.IsPaired(sdst)) { plan.CreateSchema(sdst); } } var alteredOptions = GetDatabaseAlteredOptions(src, dst, opts); if (alteredOptions.Count > 0) { plan.ChangeDatabaseOptions(null, alteredOptions); } if (opts.SchemaMode == DbDiffSchemaMode.Ignore) { plan.RunNameTransformation(new SetSchemaNameTransformation(null)); } }