2 // This file is part of GNU social - https://www.gnu.org/software/social
4 // GNU social is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // GNU social is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
22 * @author Evan Prodromou <evan@status.net>
23 * @author Brion Vibber <brion@status.net>
24 * @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
25 * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
28 defined('GNUSOCIAL') || die();
31 * Class representing the database schema
33 * A class representing the database schema. Can be used to
34 * manipulate the schema -- especially for plugins and upgrade
37 * @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
38 * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
42 static $_static = null;
43 protected $conn = null;
46 * Constructor. Only run once for singleton object.
50 protected function __construct($conn = null)
53 // XXX: there should be an easier way to do this.
55 $conn = $user->getDatabaseConnection();
64 * Main public entry point. Use this to get
68 * @return Schema the Schema object for the connection
71 static function get($conn = null)
76 $key = md5(serialize($conn->dsn));
79 $type = common_config('db', 'type');
80 if (empty(self::$_static[$key])) {
81 $schemaClass = ucfirst($type) . 'Schema';
82 self::$_static[$key] = new $schemaClass($conn);
84 return self::$_static[$key];
88 * Gets a ColumnDef object for a single column.
90 * Throws an exception if the table is not found.
92 * @param string $table name of the table
93 * @param string $column name of the column
95 * @return ColumnDef definition of the column or null
99 public function getColumnDef($table, $column)
101 $td = $this->getTableDef($table);
103 if (!empty($td) && !empty($td->columns)) {
104 foreach ($td->columns as $cd) {
105 if ($cd->name == $column) {
115 * Creates a table with the given names and columns.
117 * @param string $tableName Name of the table
118 * @param array $def Table definition array listing fields and indexes.
120 * @return bool success flag
121 * @throws PEAR_Exception
124 public function createTable($tableName, $def)
126 $statements = $this->buildCreateTable($tableName, $def);
127 return $this->runSqlSet($statements);
131 * Build a set of SQL statements to create a table with the given
134 * @param string $name Name of the table
135 * @param array $def Table definition array
137 * @return array success flag
140 public function buildCreateTable($name, $def)
142 $def = $this->validateDef($name, $def);
143 $def = $this->filterDef($def);
146 foreach ($def['fields'] as $col => $colDef) {
147 $this->appendColumnDef($sql, $col, $colDef);
150 // Primary, unique, and foreign keys are constraints, so go within
151 // the CREATE TABLE statement normally.
152 if (!empty($def['primary key'])) {
153 $this->appendPrimaryKeyDef($sql, $def['primary key']);
156 if (!empty($def['unique keys'])) {
157 foreach ($def['unique keys'] as $col => $colDef) {
158 $this->appendUniqueKeyDef($sql, $col, $colDef);
162 if (!empty($def['foreign keys'])) {
163 foreach ($def['foreign keys'] as $keyName => $keyDef) {
164 $this->appendForeignKeyDef($sql, $keyName, $keyDef);
168 // Wrap the CREATE TABLE around the main body chunks...
170 $statements[] = $this->startCreateTable($name, $def) . "\n" .
171 implode($sql, ",\n") . "\n" .
172 $this->endCreateTable($name, $def);
174 // Multi-value indexes are advisory and for best portability
175 // should be created as separate statements.
176 if (!empty($def['indexes'])) {
177 foreach ($def['indexes'] as $col => $colDef) {
178 $this->appendCreateIndex($statements, $name, $col, $colDef);
181 if (!empty($def['fulltext indexes'])) {
182 foreach ($def['fulltext indexes'] as $col => $colDef) {
183 $this->appendCreateFulltextIndex($statements, $name, $col, $colDef);
191 * Set up a 'create table' SQL statement.
193 * @param string $name table name
194 * @param array $def table definition
197 function startCreateTable($name, array $def)
199 return 'CREATE TABLE ' . $this->quoteIdentifier($name) . ' (';
203 * Close out a 'create table' SQL statement.
205 * @param string $name table name
206 * @param array $def table definition
209 function endCreateTable($name, array $def)
215 * Append an SQL fragment with a column definition in a CREATE TABLE statement.
218 * @param string $name
221 function appendColumnDef(array &$sql, $name, array $def)
223 $sql[] = "$name " . $this->columnSql($def);
227 * Append an SQL fragment with a constraint definition for a primary
228 * key in a CREATE TABLE statement.
233 function appendPrimaryKeyDef(array &$sql, array $def)
235 $sql[] = "PRIMARY KEY " . $this->buildIndexList($def);
239 * Append an SQL fragment with a constraint definition for a unique
240 * key in a CREATE TABLE statement.
243 * @param string $name
246 function appendUniqueKeyDef(array &$sql, $name, array $def)
248 $sql[] = "CONSTRAINT $name UNIQUE " . $this->buildIndexList($def);
252 * Append an SQL fragment with a constraint definition for a foreign
253 * key in a CREATE TABLE statement.
256 * @param string $name
260 function appendForeignKeyDef(array &$sql, $name, array $def)
262 if (count($def) != 2) {
263 throw new Exception("Invalid foreign key def for $name: " . var_export($def, true));
265 list($refTable, $map) = $def;
266 $srcCols = array_keys($map);
267 $refCols = array_values($map);
268 $sql[] = "CONSTRAINT $name FOREIGN KEY " .
269 $this->buildIndexList($srcCols) .
271 $this->quoteIdentifier($refTable) .
273 $this->buildIndexList($refCols);
277 * Append an SQL statement with an index definition for an advisory
278 * index over one or more columns on a table.
280 * @param array $statements
281 * @param string $table
282 * @param string $name
285 function appendCreateIndex(array &$statements, $table, $name, array $def)
287 $statements[] = "CREATE INDEX $name ON $table " . $this->buildIndexList($def);
291 * Append an SQL statement with an index definition for a full-text search
292 * index over one or more columns on a table.
294 * @param array $statements
295 * @param string $table
296 * @param string $name
300 function appendCreateFulltextIndex(array &$statements, $table, $name, array $def)
302 throw new Exception("Fulltext index not supported in this database");
306 * Append an SQL statement to drop an index from a table.
308 * @param array $statements
309 * @param string $table
310 * @param string $name
312 function appendDropIndex(array &$statements, $table, $name)
314 $statements[] = "DROP INDEX $name ON " . $this->quoteIdentifier($table);
317 function buildIndexList(array $def)
320 return '(' . implode(',', array_map([$this, 'buildIndexItem'], $def)) . ')';
323 function buildIndexItem($def)
325 if (is_array($def)) {
326 list($name, $size) = $def;
327 return $this->quoteIdentifier($name) . '(' . intval($size) . ')';
329 return $this->quoteIdentifier($def);
333 * Drops a table from the schema
335 * Throws an exception if the table is not found.
337 * @param string $name Name of the table to drop
339 * @return bool success flag
340 * @throws PEAR_Exception
343 public function dropTable($name)
347 $res = $this->conn->query("DROP TABLE $name");
349 if ($_PEAR->isError($res)) {
350 PEAR_ErrorToPEAR_Exception($res);
357 * Adds an index to a table.
359 * If no name is provided, a name will be made up based
360 * on the table name and column names.
362 * Throws an exception on database error, esp. if the table
365 * @param string $table Name of the table
366 * @param array $columnNames Name of columns to index
367 * @param string $name (Optional) name of the index
369 * @return bool success flag
370 * @throws PEAR_Exception
373 public function createIndex($table, $columnNames, $name = null)
377 if (!is_array($columnNames)) {
378 $columnNames = [$columnNames];
382 $name = "{$table}_" . implode("_", $columnNames) . "_idx";
385 $res = $this->conn->query("ALTER TABLE $table " .
386 "ADD INDEX $name (" .
387 implode(",", $columnNames) . ")");
389 if ($_PEAR->isError($res)) {
390 PEAR_ErrorToPEAR_Exception($res);
397 * Drops a named index from a table.
399 * @param string $table name of the table the index is on.
400 * @param string $name name of the index
402 * @return bool success flag
403 * @throws PEAR_Exception
406 public function dropIndex($table, $name)
410 $res = $this->conn->query("ALTER TABLE $table DROP INDEX $name");
412 if ($_PEAR->isError($res)) {
413 PEAR_ErrorToPEAR_Exception($res);
420 * Adds a column to a table
422 * @param string $table name of the table
423 * @param ColumnDef $columndef Definition of the new
426 * @return bool success flag
427 * @throws PEAR_Exception
430 public function addColumn($table, $columndef)
434 $sql = "ALTER TABLE $table ADD COLUMN " . $this->_columnSql($columndef);
436 $res = $this->conn->query($sql);
438 if ($_PEAR->isError($res)) {
439 PEAR_ErrorToPEAR_Exception($res);
446 * Modifies a column in the schema.
448 * The name must match an existing column and table.
450 * @param string $table name of the table
451 * @param ColumnDef $columndef new definition of the column.
453 * @return bool success flag
454 * @throws PEAR_Exception
457 public function modifyColumn($table, $columndef)
461 $sql = "ALTER TABLE $table MODIFY COLUMN " .
462 $this->_columnSql($columndef);
464 $res = $this->conn->query($sql);
466 if ($_PEAR->isError($res)) {
467 PEAR_ErrorToPEAR_Exception($res);
474 * Drops a column from a table
476 * The name must match an existing column.
478 * @param string $table name of the table
479 * @param string $columnName name of the column to drop
481 * @return bool success flag
482 * @throws PEAR_Exception
485 public function dropColumn($table, $columnName)
489 $sql = "ALTER TABLE $table DROP COLUMN $columnName";
491 $res = $this->conn->query($sql);
493 if ($_PEAR->isError($res)) {
494 PEAR_ErrorToPEAR_Exception($res);
501 * Ensures that a table exists with the given
502 * name and the given column definitions.
504 * If the table does not yet exist, it will
505 * create the table. If it does exist, it will
506 * alter the table to match the column definitions.
508 * @param string $tableName name of the table
509 * @param array $def Table definition array
511 * @return bool success flag
512 * @throws PEAR_Exception
515 public function ensureTable($tableName, $def)
517 $statements = $this->buildEnsureTable($tableName, $def);
518 return $this->runSqlSet($statements);
522 * Run a given set of SQL commands on the connection in sequence.
525 * @fixme if multiple statements, wrap in a transaction?
526 * @param array $statements
527 * @return bool success flag
528 * @throws PEAR_Exception
530 function runSqlSet(array $statements)
535 foreach ($statements as $sql) {
536 if (defined('DEBUG_INSTALLER')) {
537 echo "<code>" . htmlspecialchars($sql) . "</code><br/>\n";
539 $res = $this->conn->query($sql);
541 if ($_PEAR->isError($res)) {
542 common_debug('PEAR exception on query: ' . $sql);
543 PEAR_ErrorToPEAR_Exception($res);
550 * Check a table's status, and if needed build a set
551 * of SQL statements which change it to be consistent
552 * with the given table definition.
554 * If the table does not yet exist, statements will
555 * be returned to create the table. If it does exist,
556 * statements will be returned to alter the table to
557 * match the column definitions.
559 * @param string $tableName name of the table
561 * @return array of SQL statements
565 function buildEnsureTable($tableName, array $def)
568 $old = $this->getTableDef($tableName);
569 } catch (SchemaTableMissingException $e) {
570 return $this->buildCreateTable($tableName, $def);
573 // Filter the DB-independent table definition to match the current
574 // database engine's features and limitations.
575 $def = $this->validateDef($tableName, $def);
576 $def = $this->filterDef($def);
579 $fields = $this->diffArrays($old, $def, 'fields', [$this, 'columnsEqual']);
580 $uniques = $this->diffArrays($old, $def, 'unique keys');
581 $indexes = $this->diffArrays($old, $def, 'indexes');
582 $foreign = $this->diffArrays($old, $def, 'foreign keys');
583 $fulltext = $this->diffArrays($old, $def, 'fulltext indexes');
585 // Drop any obsolete or modified indexes ahead...
586 foreach ($indexes['del'] + $indexes['mod'] as $indexName) {
587 $this->appendDropIndex($statements, $tableName, $indexName);
590 // Drop any obsolete or modified fulltext indexes ahead...
591 foreach ($fulltext['del'] + $fulltext['mod'] as $indexName) {
592 $this->appendDropIndex($statements, $tableName, $indexName);
595 // For efficiency, we want this all in one
596 // query, instead of using our methods.
600 foreach ($foreign['del'] + $foreign['mod'] as $keyName) {
601 $this->appendAlterDropForeign($phrase, $keyName);
604 foreach ($uniques['del'] + $uniques['mod'] as $keyName) {
605 $this->appendAlterDropUnique($phrase, $keyName);
608 if (isset($old['primary key']) && (!isset($def['primary key']) || $def['primary key'] != $old['primary key'])) {
609 $this->appendAlterDropPrimary($phrase);
612 foreach ($fields['add'] as $columnName) {
613 $this->appendAlterAddColumn($phrase, $columnName,
614 $def['fields'][$columnName]);
617 foreach ($fields['mod'] as $columnName) {
618 $this->appendAlterModifyColumn($phrase, $columnName,
619 $old['fields'][$columnName],
620 $def['fields'][$columnName]);
623 foreach ($fields['del'] as $columnName) {
624 $this->appendAlterDropColumn($phrase, $columnName);
627 if (isset($def['primary key']) && (!isset($old['primary key']) || $old['primary key'] != $def['primary key'])) {
628 $this->appendAlterAddPrimary($phrase, $def['primary key']);
631 foreach ($uniques['mod'] + $uniques['add'] as $keyName) {
632 $this->appendAlterAddUnique($phrase, $keyName, $def['unique keys'][$keyName]);
635 foreach ($foreign['mod'] + $foreign['add'] as $keyName) {
636 $this->appendAlterAddForeign($phrase, $keyName, $def['foreign keys'][$keyName]);
639 $this->appendAlterExtras($phrase, $tableName, $def);
641 if (count($phrase) > 0) {
642 $sql = 'ALTER TABLE ' . $tableName . ' ' . implode(",\n", $phrase);
643 $statements[] = $sql;
646 // Now create any indexes...
647 foreach ($indexes['mod'] + $indexes['add'] as $indexName) {
648 $this->appendCreateIndex($statements, $tableName, $indexName, $def['indexes'][$indexName]);
651 foreach ($fulltext['mod'] + $fulltext['add'] as $indexName) {
652 $colDef = $def['fulltext indexes'][$indexName];
653 $this->appendCreateFulltextIndex($statements, $tableName, $indexName, $colDef);
659 function diffArrays($oldDef, $newDef, $section, $compareCallback = null)
661 $old = isset($oldDef[$section]) ? $oldDef[$section] : [];
662 $new = isset($newDef[$section]) ? $newDef[$section] : [];
664 $oldKeys = array_keys($old);
665 $newKeys = array_keys($new);
667 $toadd = array_diff($newKeys, $oldKeys);
668 $todrop = array_diff($oldKeys, $newKeys);
669 $same = array_intersect($newKeys, $oldKeys);
673 // Find which fields have actually changed definition
674 // in a way that we need to tweak them for this DB type.
675 foreach ($same as $name) {
676 if ($compareCallback) {
677 $same = call_user_func($compareCallback, $old[$name], $new[$name]);
679 $same = ($old[$name] == $new[$name]);
692 'count' => count($toadd) + count($todrop) + count($tomod)
697 * Append phrase(s) to an array of partial ALTER TABLE chunks in order
698 * to add the given column definition to the table.
700 * @param array $phrase
701 * @param string $columnName
704 function appendAlterAddColumn(array &$phrase, $columnName, array $cd)
706 $phrase[] = 'ADD COLUMN ' .
707 $this->quoteIdentifier($columnName) .
709 $this->columnSql($cd);
713 * Append phrase(s) to an array of partial ALTER TABLE chunks in order
714 * to alter the given column from its old state to a new one.
716 * @param array $phrase
717 * @param string $columnName
718 * @param array $old previous column definition as found in DB
719 * @param array $cd current column definition
721 function appendAlterModifyColumn(array &$phrase, $columnName, array $old, array $cd)
723 $phrase[] = 'MODIFY COLUMN ' .
724 $this->quoteIdentifier($columnName) .
726 $this->columnSql($cd);
730 * Append phrase(s) to an array of partial ALTER TABLE chunks in order
731 * to drop the given column definition from the table.
733 * @param array $phrase
734 * @param string $columnName
736 function appendAlterDropColumn(array &$phrase, $columnName)
738 $phrase[] = 'DROP COLUMN ' . $this->quoteIdentifier($columnName);
741 function appendAlterAddUnique(array &$phrase, $keyName, array $def)
745 $this->appendUniqueKeyDef($sql, $keyName, $def);
746 $phrase[] = implode(' ', $sql);
749 function appendAlterAddForeign(array &$phrase, $keyName, array $def)
753 $this->appendForeignKeyDef($sql, $keyName, $def);
754 $phrase[] = implode(' ', $sql);
757 function appendAlterAddPrimary(array &$phrase, array $def)
761 $this->appendPrimaryKeyDef($sql, $def);
762 $phrase[] = implode(' ', $sql);
765 function appendAlterDropPrimary(array &$phrase)
767 $phrase[] = 'DROP CONSTRAINT PRIMARY KEY';
770 function appendAlterDropUnique(array &$phrase, $keyName)
772 $phrase[] = 'DROP CONSTRAINT ' . $keyName;
775 function appendAlterDropForeign(array &$phrase, $keyName)
777 $phrase[] = 'DROP FOREIGN KEY ' . $keyName;
780 function appendAlterExtras(array &$phrase, $tableName, array $def)
786 * Quote a db/table/column identifier if necessary.
788 * @param string $name
791 function quoteIdentifier($name)
796 function quoteDefaultValue($cd)
798 if (($cd['type'] == 'datetime' || $cd['type'] == 'timestamp') && $cd['default'] == 'CURRENT_TIMESTAMP') {
799 return $cd['default'];
801 return $this->quoteValue($cd['default']);
805 function quoteValue($val)
807 return $this->conn->quoteSmart($val);
811 * Check if two column definitions are equivalent.
812 * The default implementation checks _everything_ but in many cases
813 * you may be able to discard a bunch of equivalencies.
819 function columnsEqual(array $a, array $b)
821 return !array_diff_assoc($a, $b) && !array_diff_assoc($b, $a);
825 * Returns the array of names from an array of
828 * @param array $cds array of ColumnDef objects
830 * @return array strings for name values
833 protected function _names($cds)
837 foreach ($cds as $cd) {
838 $names[] = $cd->name;
845 * Get a ColumnDef from an array matching
848 * @param array $cds Array of ColumnDef objects
849 * @param string $name Name of the column
851 * @return ColumnDef matching item or null if no match.
854 protected function _byName($cds, $name)
856 foreach ($cds as $cd) {
857 if ($cd->name == $name) {
866 * Return the proper SQL for creating or
869 * Appropriate for use in CREATE TABLE or
870 * ALTER TABLE statements.
872 * @param array $cd column to create
874 * @return string correct SQL for that column
877 function columnSql(array $cd)
880 $line[] = $this->typeAndSize($cd);
882 if (isset($cd['default'])) {
884 $line[] = $this->quoteDefaultValue($cd);
885 } else if (!empty($cd['not null'])) {
886 // Can't have both not null AND default!
887 $line[] = 'not null';
890 return implode(' ', $line);
895 * @param string $column canonical type name in defs
896 * @return string native DB type name
898 function mapType($column)
903 function typeAndSize($column)
905 //$type = $this->mapType($column);
906 $type = $column['type'];
907 if (isset($column['size'])) {
908 $type = $column['size'] . $type;
912 if (isset($column['precision'])) {
913 $lengths[] = $column['precision'];
914 if (isset($column['scale'])) {
915 $lengths[] = $column['scale'];
917 } else if (isset($column['length'])) {
918 $lengths[] = $column['length'];
922 return $type . '(' . implode(',', $lengths) . ')';
929 * Convert an old-style set of ColumnDef objects into the current
930 * Drupal-style schema definition array, for backwards compatibility
931 * with plugins written for 0.9.x.
933 * @param string $tableName
934 * @param array $defs : array of ColumnDef objects
937 protected function oldToNew($tableName, array $defs)
946 foreach ($defs as $cd) {
948 $column['type'] = $cd->type;
949 foreach ($prefixes as $prefix) {
950 if (substr($cd->type, 0, strlen($prefix)) == $prefix) {
951 $column['type'] = substr($cd->type, strlen($prefix));
952 $column['size'] = $prefix;
958 if ($cd->type == 'varchar' || $cd->type == 'char') {
959 $column['length'] = $cd->size;
962 if (!$cd->nullable) {
963 $column['not null'] = true;
965 if ($cd->auto_increment) {
966 $column['type'] = 'serial';
969 $column['default'] = $cd->default;
971 $table['fields'][$cd->name] = $column;
973 if ($cd->key == 'PRI') {
974 // If multiple columns are defined as primary key,
975 // we'll pile them on in sequence.
976 if (!isset($table['primary key'])) {
977 $table['primary key'] = [];
979 $table['primary key'][] = $cd->name;
980 } else if ($cd->key == 'MUL') {
981 // Individual multiple-value indexes are only per-column
982 // using the old ColumnDef syntax.
983 $idx = "{$tableName}_{$cd->name}_idx";
984 $table['indexes'][$idx] = [$cd->name];
985 } else if ($cd->key == 'UNI') {
986 // Individual unique-value indexes are only per-column
987 // using the old ColumnDef syntax.
988 $idx = "{$tableName}_{$cd->name}_idx";
989 $table['unique keys'][$idx] = [$cd->name];
997 * Filter the given table definition array to match features available
1000 * This lets us strip out unsupported things like comments, foreign keys,
1001 * or type variants that we wouldn't get back from getTableDef().
1003 * @param array $tableDef
1006 function filterDef(array $tableDef)
1012 * Validate a table definition array, checking for basic structure.
1014 * If necessary, converts from an old-style array of ColumnDef objects.
1016 * @param string $tableName
1017 * @param array $def : table definition array
1018 * @return array validated table definition array
1020 * @throws Exception on wildly invalid input
1022 function validateDef($tableName, array $def)
1024 if (isset($def[0]) && $def[0] instanceof ColumnDef) {
1025 $def = $this->oldToNew($tableName, $def);
1028 // A few quick checks :D
1029 if (!isset($def['fields'])) {
1030 throw new Exception("Invalid table definition for $tableName: no fields.");
1036 function isNumericType($type)
1038 $type = strtolower($type);
1039 $known = ['int', 'serial', 'numeric'];
1040 return in_array($type, $known);
1044 * Pull info from the query into a fun-fun array of dooooom
1046 * @param string $sql
1047 * @return array of arrays
1048 * @throws PEAR_Exception
1050 protected function fetchQueryData($sql)
1054 $res = $this->conn->query($sql);
1055 if ($_PEAR->isError($res)) {
1056 PEAR_ErrorToPEAR_Exception($res);
1061 while ($res->fetchInto($row, DB_FETCHMODE_ASSOC)) {
1071 class SchemaTableMissingException extends Exception