]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - lib/schema.php
Convert SamplePlugin to new-style table defs, tweak some stuff to test basic checkschema
[quix0rs-gnu-social.git] / lib / schema.php
index c32e6b5230ccb39eb6210e0a05a871a532151d47..e4b7f416cd7fa7a4e474e9548e8831e876702e46 100644 (file)
@@ -142,6 +142,7 @@ class Schema
      */
     public function buildCreateTable($name, $def)
     {
+        $def = $this->validateDef($name, $def);
         $def = $this->filterDef($def);
         $sql = array();
 
@@ -253,6 +254,9 @@ class Schema
      */
     function appendForeignKeyDef(array &$sql, $name, array $def)
     {
+        if (count($def) != 2) {
+            throw new Exception("Invalid foreign key def for $name: " . var_export($def, true));
+        }
         list($refTable, $map) = $def;
         $srcCols = array_keys($map);
         $refCols = array_values($map);
@@ -517,7 +521,7 @@ class Schema
      * @return array of SQL statements
      */
 
-    function buildEnsureTable($tableName, $def)
+    function buildEnsureTable($tableName, array $def)
     {
         try {
             $old = $this->getTableDef($tableName);
@@ -527,6 +531,7 @@ class Schema
 
         // Filter the DB-independent table definition to match the current
         // database engine's features and limitations.
+        $def = $this->validateDef($tableName, $def);
         $def = $this->filterDef($def);
 
         $statements = array();
@@ -851,10 +856,10 @@ class Schema
      * with plugins written for 0.9.x.
      *
      * @param string $tableName
-     * @param array $defs
+     * @param array $defs: array of ColumnDef objects
      * @return array
      */
-    function oldToNew($tableName, $defs)
+    protected function oldToNew($tableName, array $defs)
     {
         $table = array();
         $prefixes = array(
@@ -864,7 +869,6 @@ class Schema
             'big',
         );
         foreach ($defs as $cd) {
-            $cd->addToTableDef($table);
             $column = array();
             $column['type'] = $cd->type;
             foreach ($prefixes as $prefix) {
@@ -883,7 +887,7 @@ class Schema
             if (!$cd->nullable) {
                 $column['not null'] = true;
             }
-            if ($cd->autoincrement) {
+            if ($cd->auto_increment) {
                 $column['type'] = 'serial';
             }
             if ($cd->default) {
@@ -928,6 +932,31 @@ class Schema
         return $tableDef;
     }
 
+    /**
+     * Validate a table definition array, checking for basic structure.
+     *
+     * If necessary, converts from an old-style array of ColumnDef objects.
+     *
+     * @param string $tableName
+     * @param array $def: table definition array
+     * @return array validated table definition array
+     *
+     * @throws Exception on wildly invalid input
+     */
+    function validateDef($tableName, array $def)
+    {
+        if (count($def) && $def[0] instanceof ColumnDef) {
+            $def = $this->oldToNew($tableName, $def);
+        }
+
+        // A few quick checks :D
+        if (!isset($def['fields'])) {
+            throw new Exception("Invalid table definition for $tableName: no fields.");
+        }
+
+        return $def;
+    }
+
     function isNumericType($type)
     {
         $type = strtolower($type);