]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - scripts/dumpschema.php
Added type-hint for EndNoticeInScope hook.
[quix0rs-gnu-social.git] / scripts / dumpschema.php
old mode 100644 (file)
new mode 100755 (executable)
index 3c23636..05638cd
@@ -24,15 +24,17 @@ $helptext = <<<END_OF_CHECKSCHEMA_HELP
 Attempt to pull a schema definition for a given table.
 
   --all     run over all defined core tables
-  --diff    do a raw text diff between the expected and live table defs
-  --update  dump SQL that would be run to update or create this table
+  --diff    show differences between the expected and live table defs
+  --raw     skip compatibility filtering for diffs
+  --create  dump SQL that would be run to update or create this table
   --build   dump SQL that would be run to create this table fresh
+  --checksum just output checksums from the source schema defs
 
 
 END_OF_CHECKSCHEMA_HELP;
 
-$longoptions = array('diff', 'all', 'build', 'update');
-require_once INSTALLDIR.'/scripts/commandline.inc';
+$longoptions = array('diff', 'all', 'create', 'update', 'raw', 'checksum');
+require_once INSTALLDIR.'/scripts/commandline.inc.php';
 
 function indentOptions($indent)
 {
@@ -110,6 +112,7 @@ function dumpTable($tableName, $live)
         $def = getCoreSchema($tableName);
     }
     prettyDumpArray($def, $tableName);
+    print "\n";
 }
 
 function dumpBuildTable($tableName)
@@ -129,34 +132,102 @@ function dumpBuildTable($tableName)
 
 function dumpEnsureTable($tableName)
 {
-    echo "-- \n";
-    echo "-- $tableName\n";
-    echo "-- \n";
-
     $schema = Schema::get();
     $def = getCoreSchema($tableName);
     $sql = $schema->buildEnsureTable($tableName, $def);
-    $sql[] = '';
 
-    echo implode(";\n", $sql);
-    echo "\n";
+    if ($sql) {
+        echo "-- \n";
+        echo "-- $tableName\n";
+        echo "-- \n";
+
+        $sql[] = '';
+        echo implode(";\n", $sql);
+        echo "\n";
+    }
+}
+
+function dumpDiff($tableName, $filter)
+{
+    $schema = Schema::get();
+    $def = getCoreSchema($tableName);
+    try {
+        $old = $schema->getTableDef($tableName);
+    } catch (Exception $e) {
+        // @fixme this is a terrible check :D
+        if (preg_match('/no such table/i', $e->getMessage())) {
+            return dumpTable($tableName, false);
+        } else {
+            throw $e;
+        }
+    }
+
+    if ($filter) {
+        //$old = $schema->filterDef($old);
+        $def = $schema->filterDef($def);
+    }
+
+    // @hack
+    $old = tweakPrimaryKey($old);
+    $def = tweakPrimaryKey($def);
+
+    $sections = array_unique(array_merge(array_keys($old), array_keys($def)));
+    $final = array();
+    foreach ($sections as $section) {
+        if ($section == 'fields') {
+            // this shouldn't be needed maybe... wait what?
+        }
+        $diff = $schema->diffArrays($old, $def, $section);
+        $chunks = array('del', 'mod', 'add');
+        foreach ($chunks as $chunk) {
+            if ($diff[$chunk]) {
+                foreach ($diff[$chunk] as $key) {
+                    if ($chunk == 'del') {
+                        $final[$section]["DEL $key"] = $old[$section][$key];
+                    } else if ($chunk == 'add') {
+                        $final[$section]["ADD $key"] = $def[$section][$key];
+                    } else if ($chunk == 'mod') {
+                        $final[$section]["OLD $key"] = $old[$section][$key];
+                        $final[$section]["NEW $key"] = $def[$section][$key];
+                    }
+                }
+            }
+        }
+    }
+
+    prettyDumpArray($final, $tableName);
+    print "\n";
 }
 
-function showDiff($a, $b)
+function tweakPrimaryKey($def)
 {
-    $fnameA = tempnam(sys_get_temp_dir(), 'defined-diff-a');
-    file_put_contents($fnameA, $a);
+    if (isset($def['primary key'])) {
+        $def['primary keys'] = array('primary key' => $def['primary key']);
+        unset($def['primary key']);
+    }
+    if (isset($def['description'])) {
+        $def['descriptions'] = array('description' => $def['description']);
+        unset($def['description']);
+    }
+    return $def;
+}
 
-    $fnameB = tempnam(sys_get_temp_dir(), 'detected-diff-b');
-    file_put_contents($fnameB, $b);
+function dumpChecksum($tableName)
+{
+    $schema = Schema::get();
+    $def = getCoreSchema($tableName);
 
-    $cmd = sprintf('diff -U 100 %s %s',
-            escapeshellarg($fnameA),
-            escapeshellarg($fnameB));
-    passthru($cmd);
+    $updater = new SchemaUpdater($schema);
+    $checksum = $updater->checksum($def);
+    $old = @$updater->checksums[$tableName];
 
-    unlink($fnameA);
-    unlink($fnameB);
+    if ($old == $checksum) {
+        echo "OK  $checksum $tableName\n";
+    } else if (!$old) {
+        echo "NEW $checksum $tableName\n";
+    } else {
+        echo "MOD $checksum $tableName (was $old)\n";
+    }
 }
 
 if (have_option('all')) {
@@ -166,23 +237,17 @@ if (have_option('all')) {
 if (count($args)) {
     foreach ($args as $tableName) {
         if (have_option('diff')) {
-            ob_start();
-            dumpTable($tableName, false);
-            $defined = ob_get_clean();
-
-            ob_start();
-            dumpTable($tableName, true);
-            $detected = ob_get_clean();
-
-            showDiff($defined, $detected);
-        } else if (have_option('build')) {
+            dumpDiff($tableName, !have_option('raw'));
+        } else if (have_option('create')) {
             dumpBuildTable($tableName);
         } else if (have_option('update')) {
             dumpEnsureTable($tableName);
+        } else if (have_option('checksum')) {
+            dumpChecksum($tableName);
         } else {
             dumpTable($tableName, true);
         }
     }
 } else {
     show_help($helptext);
-}
\ No newline at end of file
+}