Browse Source

Reorder comparison script to take into account some dependencies. Fixes issue #27

Reinier Olislagers 11 years ago
parent
commit
67bc5b0c69
1 changed files with 34 additions and 10 deletions
  1. 34 10
      comparison.pas

+ 34 - 10
comparison.pas

@@ -330,6 +330,7 @@ procedure TfmComparison.laScriptClick(Sender: TObject);
 var
 var
   ObjectType: TObjectType;
   ObjectType: TObjectType;
   i: Integer;
   i: Integer;
+  ObjectCounter: integer;
   ScriptList: TStringList;
   ScriptList: TStringList;
   ViewBody: string;
   ViewBody: string;
   Columns: string;
   Columns: string;
@@ -355,6 +356,7 @@ begin
   ScriptList:= TStringList.Create;
   ScriptList:= TStringList.Create;
   FieldsList:= TStringList.Create;
   FieldsList:= TStringList.Create;
   try
   try
+    // Do this in rough order of dependency so a table can depend on a domain
     if cxRemovedObjects.Checked then
     if cxRemovedObjects.Checked then
     begin
     begin
       ScriptRemovedDBObjects;
       ScriptRemovedDBObjects;
@@ -362,6 +364,15 @@ begin
         ScriptRemovedFields;
         ScriptRemovedFields;
     end;
     end;
 
 
+    // Tables etc can depend on domains so have domains in front
+    if cxDomains.Checked then
+      ScriptModifiedDomains;
+
+    // Presumably table check constraints can depend on UDFs so put
+    // UDFs fairly in front
+    if cxUDFs.Checked then
+      ScriptModifiedFunctions;
+
     if cxTables.Checked then
     if cxTables.Checked then
     begin
     begin
       ScriptMissingFields;
       ScriptMissingFields;
@@ -379,18 +390,34 @@ begin
     if cxStoredProcs.Checked then
     if cxStoredProcs.Checked then
       ScriptModifiedProcedures;
       ScriptModifiedProcedures;
 
 
-    if cxUDFs.Checked then
-      ScriptModifiedFunctions;
-
-    if cxDomains.Checked then
-      ScriptModifiedDomains;
-
     dmSysTables.Init(FDBIndex);
     dmSysTables.Init(FDBIndex);
 
 
     FQueryWindow.meQuery.Lines.Add('');
     FQueryWindow.meQuery.Lines.Add('');
 
 
-    for ObjectType:= low(TObjectType) to high(TobjectType) do
+    // Easiest would have been to have
+    // for ObjectType:= low(TObjectType) to high(TObjectType)
+    // but that does not take dependencies into account
+    // e.g. domains need to be created before tables that depend on them
+    // So we use a manual mapping table
+    for ObjectCounter:= ord(low(TObjectType)) to ord(high(TObjectType)) do
     begin
     begin
+      case ObjectCounter of
+        ord(otTables): ObjectType:= otRoles; //these depend on nothing
+        ord(otGenerators): ObjectType:= otExceptions; //these depend on nothing
+        ord(otTriggers): ObjectType:= otDomains; //tables,views,procs can depend
+        ord(otViews): ObjectType:= otUDF; //table check constraints can depend on these
+        ord(otStoredProcedures): ObjectType:= otSystemTables; //for completeness
+        ord(otUDF): ObjectType:= otTables; //from here on, more or less follow regular order
+        ord(otSystemTables): ObjectType:= otGenerators;
+        ord(otDomains): ObjectType:= otTriggers;
+        ord(otRoles): ObjectType:= otViews;
+        ord(otExceptions): ObjectType:= otStoredProcedures;
+        ord(otUsers): ObjectType:= otIndexes;
+        ord(otIndexes): ObjectType:= otConstraints;
+        ord(otConstraints): ObjectType:= otUsers; //for completeness
+        else raise Exception.Create('Technical error: laScriptClick: update case clause with all TObjectTypes');
+      end;
+
       if (ObjectType = otTables) and cxTables.Checked then // Tables
       if (ObjectType = otTables) and cxTables.Checked then // Tables
       for i:= 0 to FDBObjectsList[ord(ObjectType)].Count - 1 do
       for i:= 0 to FDBObjectsList[ord(ObjectType)].Count - 1 do
       begin
       begin
@@ -1338,7 +1365,6 @@ begin
       System.Delete(Line, 1, Pos(',', Line));
       System.Delete(Line, 1, Pos(',', Line));
       AFieldName:= Line;
       AFieldName:= Line;
 
 
-      //todo: (high priority) test comparison with and without collations, character set, numeric, decimal datatype with scale change
       dmSysTables.GetFieldInfo(FDBIndex, ATableName,
       dmSysTables.GetFieldInfo(FDBIndex, ATableName,
         AFieldName, FieldType, FieldSize, FieldScale, NotNull,
         AFieldName, FieldType, FieldSize, FieldScale, NotNull,
         DefaultValue, Characterset, Collation, Description);
         DefaultValue, Characterset, Collation, Description);
@@ -1647,11 +1673,9 @@ begin
     if Trim(CheckConstraint) <> '' then
     if Trim(CheckConstraint) <> '' then
       FQueryWindow.meQuery.Lines.Add(CheckConstraint);
       FQueryWindow.meQuery.Lines.Add(CheckConstraint);
 
 
-    // todo: verify if this character set clause works correctly
     if Trim(CharacterSet) <> '' then
     if Trim(CharacterSet) <> '' then
       FQueryWindow.meQuery.Lines.Add('characterset ' + CharacterSet);
       FQueryWindow.meQuery.Lines.Add('characterset ' + CharacterSet);
 
 
-    // todo: verify if this collation clause works correctly
     if Trim(Collation) <> '' then
     if Trim(Collation) <> '' then
       FQueryWindow.meQuery.Lines.Add('collation ' + Collation);
       FQueryWindow.meQuery.Lines.Add('collation ' + Collation);