3 * StatusNet, the distributed open-source microblogging tool
5 * Database schema utilities
9 * LICENCE: This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * @author Evan Prodromou <evan@status.net>
25 * @copyright 2009 StatusNet, Inc.
26 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27 * @link http://status.net/
30 if (!defined('STATUSNET')) {
35 * Class representing the database schema
37 * A class representing the database schema. Can be used to
38 * manipulate the schema -- especially for plugins and upgrade
43 * @author Evan Prodromou <evan@status.net>
44 * @author Brion Vibber <brion@status.net>
45 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
46 * @link http://status.net/
51 static $_static = null;
52 protected $conn = null;
55 * Constructor. Only run once for singleton object.
58 protected function __construct($conn = null)
61 // XXX: there should be an easier way to do this.
63 $conn = $user->getDatabaseConnection();
72 * Main public entry point. Use this to get
75 * @return Schema the Schema object for the connection
78 static function get($conn = null)
83 $key = md5(serialize($conn->dsn));
86 $type = common_config('db', 'type');
87 if (empty(self::$_static[$key])) {
88 $schemaClass = ucfirst($type).'Schema';
89 self::$_static[$key] = new $schemaClass($conn);
91 return self::$_static[$key];
95 * Gets a ColumnDef object for a single column.
97 * Throws an exception if the table is not found.
99 * @param string $table name of the table
100 * @param string $column name of the column
102 * @return ColumnDef definition of the column or null
106 public function getColumnDef($table, $column)
108 $td = $this->getTableDef($table);
110 if (!empty($td) && !empty($td->columns)) {
111 foreach ($td->columns as $cd) {
112 if ($cd->name == $column) {
122 * Creates a table with the given names and columns.
124 * @param string $tableName Name of the table
125 * @param array $def Table definition array listing fields and indexes.
127 * @return boolean success flag
130 public function createTable($tableName, $def)
132 $statements = $this->buildCreateTable($tableName, $def);
133 return $this->runSqlSet($statements);
137 * Build a set of SQL statements to create a table with the given
140 * @param string $name Name of the table
141 * @param array $def Table definition array
143 * @return boolean success flag
145 public function buildCreateTable($name, $def)
147 $def = $this->validateDef($name, $def);
148 $def = $this->filterDef($def);
151 foreach ($def['fields'] as $col => $colDef) {
152 $this->appendColumnDef($sql, $col, $colDef);
155 // Primary, unique, and foreign keys are constraints, so go within
156 // the CREATE TABLE statement normally.
157 if (!empty($def['primary key'])) {
158 $this->appendPrimaryKeyDef($sql, $def['primary key']);
161 if (!empty($def['unique keys'])) {
162 foreach ($def['unique keys'] as $col => $colDef) {
163 $this->appendUniqueKeyDef($sql, $col, $colDef);
167 if (!empty($def['foreign keys'])) {
168 foreach ($def['foreign keys'] as $keyName => $keyDef) {
169 $this->appendForeignKeyDef($sql, $keyName, $keyDef);
173 // Wrap the CREATE TABLE around the main body chunks...
174 $statements = array();
175 $statements[] = $this->startCreateTable($name, $def) . "\n" .
176 implode($sql, ",\n") . "\n" .
177 $this->endCreateTable($name, $def);
179 // Multi-value indexes are advisory and for best portability
180 // should be created as separate statements.
181 if (!empty($def['indexes'])) {
182 foreach ($def['indexes'] as $col => $colDef) {
183 $this->appendCreateIndex($statements, $name, $col, $colDef);
186 if (!empty($def['fulltext indexes'])) {
187 foreach ($def['fulltext indexes'] as $col => $colDef) {
188 $this->appendCreateFulltextIndex($statements, $name, $col, $colDef);
196 * Set up a 'create table' SQL statement.
198 * @param string $name table name
199 * @param array $def table definition
202 function startCreateTable($name, array $def)
204 return 'CREATE TABLE ' . $this->quoteIdentifier($name) . ' (';
208 * Close out a 'create table' SQL statement.
210 * @param string $name table name
211 * @param array $def table definition
214 function endCreateTable($name, array $def)
220 * Append an SQL fragment with a column definition in a CREATE TABLE statement.
223 * @param string $name
226 function appendColumnDef(array &$sql, $name, array $def)
228 $sql[] = "$name " . $this->columnSql($def);
232 * Append an SQL fragment with a constraint definition for a primary
233 * key in a CREATE TABLE statement.
238 function appendPrimaryKeyDef(array &$sql, array $def)
240 $sql[] = "PRIMARY KEY " . $this->buildIndexList($def);
244 * Append an SQL fragment with a constraint definition for a unique
245 * key in a CREATE TABLE statement.
248 * @param string $name
251 function appendUniqueKeyDef(array &$sql, $name, array $def)
253 $sql[] = "CONSTRAINT $name UNIQUE " . $this->buildIndexList($def);
257 * Append an SQL fragment with a constraint definition for a foreign
258 * key in a CREATE TABLE statement.
261 * @param string $name
264 function appendForeignKeyDef(array &$sql, $name, array $def)
266 if (count($def) != 2) {
267 throw new Exception("Invalid foreign key def for $name: " . var_export($def, true));
269 list($refTable, $map) = $def;
270 $srcCols = array_keys($map);
271 $refCols = array_values($map);
272 $sql[] = "CONSTRAINT $name FOREIGN KEY " .
273 $this->buildIndexList($srcCols) .
275 $this->quoteIdentifier($refTable) .
277 $this->buildIndexList($refCols);
281 * Append an SQL statement with an index definition for an advisory
282 * index over one or more columns on a table.
284 * @param array $statements
285 * @param string $table
286 * @param string $name
289 function appendCreateIndex(array &$statements, $table, $name, array $def)
291 $statements[] = "CREATE INDEX $name ON $table " . $this->buildIndexList($def);
295 * Append an SQL statement with an index definition for a full-text search
296 * index over one or more columns on a table.
298 * @param array $statements
299 * @param string $table
300 * @param string $name
303 function appendCreateFulltextIndex(array &$statements, $table, $name, array $def)
305 throw new Exception("Fulltext index not supported in this database");
309 * Append an SQL statement to drop an index from a table.
311 * @param array $statements
312 * @param string $table
313 * @param string $name
316 function appendDropIndex(array &$statements, $table, $name)
318 $statements[] = "DROP INDEX $name ON " . $this->quoteIdentifier($table);
321 function buildIndexList(array $def)
324 return '(' . implode(',', array_map(array($this, 'buildIndexItem'), $def)) . ')';
327 function buildIndexItem($def)
329 if (is_array($def)) {
330 list($name, $size) = $def;
331 return $this->quoteIdentifier($name) . '(' . intval($size) . ')';
333 return $this->quoteIdentifier($def);
337 * Drops a table from the schema
339 * Throws an exception if the table is not found.
341 * @param string $name Name of the table to drop
343 * @return boolean success flag
346 public function dropTable($name)
350 $res = $this->conn->query("DROP TABLE $name");
352 if ($_PEAR->isError($res)) {
353 PEAR_ErrorToPEAR_Exception($res);
360 * Adds an index to a table.
362 * If no name is provided, a name will be made up based
363 * on the table name and column names.
365 * Throws an exception on database error, esp. if the table
368 * @param string $table Name of the table
369 * @param array $columnNames Name of columns to index
370 * @param string $name (Optional) name of the index
372 * @return boolean success flag
375 public function createIndex($table, $columnNames, $name=null)
379 if (!is_array($columnNames)) {
380 $columnNames = array($columnNames);
384 $name = "{$table}_".implode("_", $columnNames)."_idx";
387 $res = $this->conn->query("ALTER TABLE $table ".
389 implode(",", $columnNames).")");
391 if ($_PEAR->isError($res)) {
392 PEAR_ErrorToPEAR_Exception($res);
399 * Drops a named index from a table.
401 * @param string $table name of the table the index is on.
402 * @param string $name name of the index
404 * @return boolean success flag
407 public function dropIndex($table, $name)
411 $res = $this->conn->query("ALTER TABLE $table DROP INDEX $name");
413 if ($_PEAR->isError($res)) {
414 PEAR_ErrorToPEAR_Exception($res);
421 * Adds a column to a table
423 * @param string $table name of the table
424 * @param ColumnDef $columndef Definition of the new
427 * @return boolean success flag
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 boolean success flag
456 public function modifyColumn($table, $columndef)
460 $sql = "ALTER TABLE $table MODIFY COLUMN " .
461 $this->_columnSql($columndef);
463 $res = $this->conn->query($sql);
465 if ($_PEAR->isError($res)) {
466 PEAR_ErrorToPEAR_Exception($res);
473 * Drops a column from a table
475 * The name must match an existing column.
477 * @param string $table name of the table
478 * @param string $columnName name of the column to drop
480 * @return boolean success flag
483 public function dropColumn($table, $columnName)
487 $sql = "ALTER TABLE $table DROP COLUMN $columnName";
489 $res = $this->conn->query($sql);
491 if ($_PEAR->isError($res)) {
492 PEAR_ErrorToPEAR_Exception($res);
499 * Ensures that a table exists with the given
500 * name and the given column definitions.
502 * If the table does not yet exist, it will
503 * create the table. If it does exist, it will
504 * alter the table to match the column definitions.
506 * @param string $tableName name of the table
507 * @param array $def Table definition array
509 * @return boolean success flag
512 public function ensureTable($tableName, $def)
514 $statements = $this->buildEnsureTable($tableName, $def);
515 return $this->runSqlSet($statements);
519 * Run a given set of SQL commands on the connection in sequence.
522 * @fixme if multiple statements, wrap in a transaction?
523 * @param array $statements
524 * @return boolean success flag
526 function runSqlSet(array $statements)
531 foreach ($statements as $sql) {
532 if (defined('DEBUG_INSTALLER')) {
533 echo "<tt>" . htmlspecialchars($sql) . "</tt><br/>\n";
535 $res = $this->conn->query($sql);
537 if ($_PEAR->isError($res)) {
538 common_debug('PEAR exception on query: '.$sql);
539 PEAR_ErrorToPEAR_Exception($res);
546 * Check a table's status, and if needed build a set
547 * of SQL statements which change it to be consistent
548 * with the given table definition.
550 * If the table does not yet exist, statements will
551 * be returned to create the table. If it does exist,
552 * statements will be returned to alter the table to
553 * match the column definitions.
555 * @param string $tableName name of the table
556 * @param array $columns array of ColumnDef
557 * objects for the table
559 * @return array of SQL statements
562 function buildEnsureTable($tableName, array $def)
565 $old = $this->getTableDef($tableName);
566 } catch (SchemaTableMissingException $e) {
567 return $this->buildCreateTable($tableName, $def);
570 // Filter the DB-independent table definition to match the current
571 // database engine's features and limitations.
572 $def = $this->validateDef($tableName, $def);
573 $def = $this->filterDef($def);
575 $statements = array();
576 $fields = $this->diffArrays($old, $def, 'fields', array($this, 'columnsEqual'));
577 $uniques = $this->diffArrays($old, $def, 'unique keys');
578 $indexes = $this->diffArrays($old, $def, 'indexes');
579 $foreign = $this->diffArrays($old, $def, 'foreign keys');
580 $fulltext = $this->diffArrays($old, $def, 'fulltext indexes');
582 // Drop any obsolete or modified indexes ahead...
583 foreach ($indexes['del'] + $indexes['mod'] as $indexName) {
584 $this->appendDropIndex($statements, $tableName, $indexName);
587 // Drop any obsolete or modified fulltext indexes ahead...
588 foreach ($fulltext['del'] + $fulltext['mod'] as $indexName) {
589 $this->appendDropIndex($statements, $tableName, $indexName);
592 // For efficiency, we want this all in one
593 // query, instead of using our methods.
597 foreach ($foreign['del'] + $foreign['mod'] as $keyName) {
598 $this->appendAlterDropForeign($phrase, $keyName);
601 foreach ($uniques['del'] + $uniques['mod'] as $keyName) {
602 $this->appendAlterDropUnique($phrase, $keyName);
605 if (isset($old['primary key']) && (!isset($def['primary key']) || $def['primary key'] != $old['primary key'])) {
606 $this->appendAlterDropPrimary($phrase);
609 foreach ($fields['add'] as $columnName) {
610 $this->appendAlterAddColumn($phrase, $columnName,
611 $def['fields'][$columnName]);
614 foreach ($fields['mod'] as $columnName) {
615 $this->appendAlterModifyColumn($phrase, $columnName,
616 $old['fields'][$columnName],
617 $def['fields'][$columnName]);
620 foreach ($fields['del'] as $columnName) {
621 $this->appendAlterDropColumn($phrase, $columnName);
624 if (isset($def['primary key']) && (!isset($old['primary key']) || $old['primary key'] != $def['primary key'])) {
625 $this->appendAlterAddPrimary($phrase, $def['primary key']);
628 foreach ($uniques['mod'] + $uniques['add'] as $keyName) {
629 $this->appendAlterAddUnique($phrase, $keyName, $def['unique keys'][$keyName]);
632 foreach ($foreign['mod'] + $foreign['add'] as $keyName) {
633 $this->appendAlterAddForeign($phrase, $keyName, $def['foreign keys'][$keyName]);
636 $this->appendAlterExtras($phrase, $tableName, $def);
638 if (count($phrase) > 0) {
639 $sql = 'ALTER TABLE ' . $tableName . ' ' . implode(",\n", $phrase);
640 $statements[] = $sql;
643 // Now create any indexes...
644 foreach ($indexes['mod'] + $indexes['add'] as $indexName) {
645 $this->appendCreateIndex($statements, $tableName, $indexName, $def['indexes'][$indexName]);
648 foreach ($fulltext['mod'] + $fulltext['add'] as $indexName) {
649 $colDef = $def['fulltext indexes'][$indexName];
650 $this->appendCreateFulltextIndex($statements, $tableName, $indexName, $colDef);
656 function diffArrays($oldDef, $newDef, $section, $compareCallback=null)
658 $old = isset($oldDef[$section]) ? $oldDef[$section] : array();
659 $new = isset($newDef[$section]) ? $newDef[$section] : array();
661 $oldKeys = array_keys($old);
662 $newKeys = array_keys($new);
664 $toadd = array_diff($newKeys, $oldKeys);
665 $todrop = array_diff($oldKeys, $newKeys);
666 $same = array_intersect($newKeys, $oldKeys);
670 // Find which fields have actually changed definition
671 // in a way that we need to tweak them for this DB type.
672 foreach ($same as $name) {
673 if ($compareCallback) {
674 $same = call_user_func($compareCallback, $old[$name], $new[$name]);
676 $same = ($old[$name] == $new[$name]);
684 return array('add' => $toadd,
688 'count' => count($toadd) + count($todrop) + count($tomod));
692 * Append phrase(s) to an array of partial ALTER TABLE chunks in order
693 * to add the given column definition to the table.
695 * @param array $phrase
696 * @param string $columnName
699 function appendAlterAddColumn(array &$phrase, $columnName, array $cd)
701 $phrase[] = 'ADD COLUMN ' .
702 $this->quoteIdentifier($columnName) .
704 $this->columnSql($cd);
708 * Append phrase(s) to an array of partial ALTER TABLE chunks in order
709 * to alter the given column from its old state to a new one.
711 * @param array $phrase
712 * @param string $columnName
713 * @param array $old previous column definition as found in DB
714 * @param array $cd current column definition
716 function appendAlterModifyColumn(array &$phrase, $columnName, array $old, array $cd)
718 $phrase[] = 'MODIFY COLUMN ' .
719 $this->quoteIdentifier($columnName) .
721 $this->columnSql($cd);
725 * Append phrase(s) to an array of partial ALTER TABLE chunks in order
726 * to drop the given column definition from the table.
728 * @param array $phrase
729 * @param string $columnName
731 function appendAlterDropColumn(array &$phrase, $columnName)
733 $phrase[] = 'DROP COLUMN ' . $this->quoteIdentifier($columnName);
736 function appendAlterAddUnique(array &$phrase, $keyName, array $def)
740 $this->appendUniqueKeyDef($sql, $keyName, $def);
741 $phrase[] = implode(' ', $sql);
744 function appendAlterAddForeign(array &$phrase, $keyName, array $def)
748 $this->appendForeignKeyDef($sql, $keyName, $def);
749 $phrase[] = implode(' ', $sql);
752 function appendAlterAddPrimary(array &$phrase, array $def)
756 $this->appendPrimaryKeyDef($sql, $def);
757 $phrase[] = implode(' ', $sql);
760 function appendAlterDropPrimary(array &$phrase)
762 $phrase[] = 'DROP CONSTRAINT PRIMARY KEY';
765 function appendAlterDropUnique(array &$phrase, $keyName)
767 $phrase[] = 'DROP CONSTRAINT ' . $keyName;
770 function appendAlterDropForeign(array &$phrase, $keyName)
772 $phrase[] = 'DROP FOREIGN KEY ' . $keyName;
775 function appendAlterExtras(array &$phrase, $tableName, array $def)
781 * Quote a db/table/column identifier if necessary.
783 * @param string $name
786 function quoteIdentifier($name)
791 function quoteDefaultValue($cd)
793 if ($cd['type'] == 'datetime' && $cd['default'] == 'CURRENT_TIMESTAMP') {
794 return $cd['default'];
796 return $this->quoteValue($cd['default']);
800 function quoteValue($val)
802 return $this->conn->quoteSmart($val);
806 * Check if two column definitions are equivalent.
807 * The default implementation checks _everything_ but in many cases
808 * you may be able to discard a bunch of equivalencies.
814 function columnsEqual(array $a, array $b)
816 return !array_diff_assoc($a, $b) && !array_diff_assoc($b, $a);
820 * Returns the array of names from an array of
823 * @param array $cds array of ColumnDef objects
825 * @return array strings for name values
828 protected function _names($cds)
832 foreach ($cds as $cd) {
833 $names[] = $cd->name;
840 * Get a ColumnDef from an array matching
843 * @param array $cds Array of ColumnDef objects
844 * @param string $name Name of the column
846 * @return ColumnDef matching item or null if no match.
849 protected function _byName($cds, $name)
851 foreach ($cds as $cd) {
852 if ($cd->name == $name) {
861 * Return the proper SQL for creating or
864 * Appropriate for use in CREATE TABLE or
865 * ALTER TABLE statements.
867 * @param ColumnDef $cd column to create
869 * @return string correct SQL for that column
872 function columnSql(array $cd)
875 $line[] = $this->typeAndSize($cd);
877 if (isset($cd['default'])) {
879 $line[] = $this->quoteDefaultValue($cd);
880 } else if (!empty($cd['not null'])) {
881 // Can't have both not null AND default!
882 $line[] = 'not null';
885 return implode(' ', $line);
890 * @param string $column canonical type name in defs
891 * @return string native DB type name
893 function mapType($column)
898 function typeAndSize($column)
900 //$type = $this->mapType($column);
901 $type = $column['type'];
902 if (isset($column['size'])) {
903 $type = $column['size'] . $type;
907 if (isset($column['precision'])) {
908 $lengths[] = $column['precision'];
909 if (isset($column['scale'])) {
910 $lengths[] = $column['scale'];
912 } else if (isset($column['length'])) {
913 $lengths[] = $column['length'];
917 return $type . '(' . implode(',', $lengths) . ')';
924 * Convert an old-style set of ColumnDef objects into the current
925 * Drupal-style schema definition array, for backwards compatibility
926 * with plugins written for 0.9.x.
928 * @param string $tableName
929 * @param array $defs: array of ColumnDef objects
932 protected function oldToNew($tableName, array $defs)
941 foreach ($defs as $cd) {
943 $column['type'] = $cd->type;
944 foreach ($prefixes as $prefix) {
945 if (substr($cd->type, 0, strlen($prefix)) == $prefix) {
946 $column['type'] = substr($cd->type, strlen($prefix));
947 $column['size'] = $prefix;
953 if ($cd->type == 'varchar' || $cd->type == 'char') {
954 $column['length'] = $cd->size;
957 if (!$cd->nullable) {
958 $column['not null'] = true;
960 if ($cd->auto_increment) {
961 $column['type'] = 'serial';
964 $column['default'] = $cd->default;
966 $table['fields'][$cd->name] = $column;
968 if ($cd->key == 'PRI') {
969 // If multiple columns are defined as primary key,
970 // we'll pile them on in sequence.
971 if (!isset($table['primary key'])) {
972 $table['primary key'] = array();
974 $table['primary key'][] = $cd->name;
975 } else if ($cd->key == 'MUL') {
976 // Individual multiple-value indexes are only per-column
977 // using the old ColumnDef syntax.
978 $idx = "{$tableName}_{$cd->name}_idx";
979 $table['indexes'][$idx] = array($cd->name);
980 } else if ($cd->key == 'UNI') {
981 // Individual unique-value indexes are only per-column
982 // using the old ColumnDef syntax.
983 $idx = "{$tableName}_{$cd->name}_idx";
984 $table['unique keys'][$idx] = array($cd->name);
992 * Filter the given table definition array to match features available
995 * This lets us strip out unsupported things like comments, foreign keys,
996 * or type variants that we wouldn't get back from getTableDef().
998 * @param array $tableDef
1000 function filterDef(array $tableDef)
1006 * Validate a table definition array, checking for basic structure.
1008 * If necessary, converts from an old-style array of ColumnDef objects.
1010 * @param string $tableName
1011 * @param array $def: table definition array
1012 * @return array validated table definition array
1014 * @throws Exception on wildly invalid input
1016 function validateDef($tableName, array $def)
1018 if (isset($def[0]) && $def[0] instanceof ColumnDef) {
1019 $def = $this->oldToNew($tableName, $def);
1022 // A few quick checks :D
1023 if (!isset($def['fields'])) {
1024 throw new Exception("Invalid table definition for $tableName: no fields.");
1030 function isNumericType($type)
1032 $type = strtolower($type);
1033 $known = array('int', 'serial', 'numeric');
1034 return in_array($type, $known);
1038 * Pull info from the query into a fun-fun array of dooooom
1040 * @param string $sql
1041 * @return array of arrays
1043 protected function fetchQueryData($sql)
1047 $res = $this->conn->query($sql);
1048 if ($_PEAR->isError($res)) {
1049 PEAR_ErrorToPEAR_Exception($res);
1054 while ($res->fetchInto($row, DB_FETCHMODE_ASSOC)) {
1064 class SchemaTableMissingException extends Exception