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 foreach ($td->columns as $cd) {
111 if ($cd->name == $column) {
120 * Creates a table with the given names and columns.
122 * @param string $name Name of the table
123 * @param array $columns Array of ColumnDef objects
126 * @return boolean success flag
129 public function createTable($name, $columns)
131 $statements = $this->buildCreateTable($tableName, $def);
132 return $this->runSqlSet($statements);
136 * Build a set of SQL statements to create a table with the given
139 * @param string $name Name of the table
140 * @param array $def Table definition array
142 * @return boolean success flag
144 public function buildCreateTable($name, $def)
146 $def = $this->filterDef($def);
149 foreach ($def['fields'] as $col => $colDef) {
150 $this->appendColumnDef($sql, $col, $colDef);
153 // Primary and unique keys are constraints, so go within
154 // the CREATE TABLE statement normally.
155 if (!empty($def['primary key'])) {
156 $this->appendPrimaryKeyDef($sql, $def['primary key']);
159 if (!empty($def['unique keys'])) {
160 foreach ($def['unique keys'] as $col => $colDef) {
161 $this->appendUniqueKeyDef($sql, $col, $colDef);
165 // Multi-value indexes are advisory and for best portability
166 // should be created as separate statements.
167 $statements = array();
168 $statements[] = $this->startCreateTable($name, $def) . "\n" .
169 implode($sql, ",\n") . "\n" .
170 $this->endCreateTable($name, $def);
171 if (!empty($def['indexes'])) {
172 foreach ($def['indexes'] as $col => $colDef) {
173 $this->appendCreateIndex($statements, $name, $col, $colDef);
181 * Set up a 'create table' SQL statement.
183 * @param string $name table name
184 * @param array $def table definition
187 function startCreateTable($name, array $def)
189 return 'CREATE TABLE ' . $this->quoteIdentifier($name) . ' (';
193 * Close out a 'create table' SQL statement.
195 * @param string $name table name
196 * @param array $def table definition
199 function endCreateTable($name, array $def)
205 * Append an SQL fragment with a column definition in a CREATE TABLE statement.
208 * @param string $name
211 function appendColumnDef(array &$sql, $name, array $def)
213 $sql[] = "$name " . $this->columnSql($def);
217 * Append an SQL fragment with a constraint definition for a primary
218 * key in a CREATE TABLE statement.
223 function appendPrimaryKeyDef(array &$sql, array $def)
225 $sql[] = "PRIMARY KEY " . $this->buildIndexList($def);
229 * Append an SQL fragment with a constraint definition for a primary
230 * key in a CREATE TABLE statement.
233 * @param string $name
236 function appendUniqueKeyDef(array &$sql, $name, array $def)
238 $sql[] = "UNIQUE $name " . $this->buildIndexList($def);
242 * Append an SQL statement with an index definition for an advisory
243 * index over one or more columns on a table.
245 * @param array $statements
246 * @param string $table
247 * @param string $name
250 function appendCreateIndex(array &$statements, $table, $name, array $def)
252 $statements[] = "CREATE INDEX $name ON $table " . $this->buildIndexList($def);
255 function buildIndexList(array $def)
258 return '(' . implode(',', array_map(array($this, 'buildIndexItem'), $def)) . ')';
261 function buildIndexItem($def)
263 if (is_array($def)) {
264 list($name, $size) = $def;
265 return $this->quoteIdentifier($name) . '(' . intval($size) . ')';
267 return $this->quoteIdentifier($def);
271 * Drops a table from the schema
273 * Throws an exception if the table is not found.
275 * @param string $name Name of the table to drop
277 * @return boolean success flag
280 public function dropTable($name)
282 $res = $this->conn->query("DROP TABLE $name");
284 if (PEAR::isError($res)) {
285 throw new Exception($res->getMessage());
292 * Adds an index to a table.
294 * If no name is provided, a name will be made up based
295 * on the table name and column names.
297 * Throws an exception on database error, esp. if the table
300 * @param string $table Name of the table
301 * @param array $columnNames Name of columns to index
302 * @param string $name (Optional) name of the index
304 * @return boolean success flag
307 public function createIndex($table, $columnNames, $name=null)
309 if (!is_array($columnNames)) {
310 $columnNames = array($columnNames);
314 $name = "{$table}_".implode("_", $columnNames)."_idx";
317 $res = $this->conn->query("ALTER TABLE $table ".
319 implode(",", $columnNames).")");
321 if (PEAR::isError($res)) {
322 throw new Exception($res->getMessage());
329 * Drops a named index from a table.
331 * @param string $table name of the table the index is on.
332 * @param string $name name of the index
334 * @return boolean success flag
337 public function dropIndex($table, $name)
339 $res = $this->conn->query("ALTER TABLE $table DROP INDEX $name");
341 if (PEAR::isError($res)) {
342 throw new Exception($res->getMessage());
349 * Adds a column to a table
351 * @param string $table name of the table
352 * @param ColumnDef $columndef Definition of the new
355 * @return boolean success flag
358 public function addColumn($table, $columndef)
360 $sql = "ALTER TABLE $table ADD COLUMN " . $this->_columnSql($columndef);
362 $res = $this->conn->query($sql);
364 if (PEAR::isError($res)) {
365 throw new Exception($res->getMessage());
372 * Modifies a column in the schema.
374 * The name must match an existing column and table.
376 * @param string $table name of the table
377 * @param ColumnDef $columndef new definition of the column.
379 * @return boolean success flag
382 public function modifyColumn($table, $columndef)
384 $sql = "ALTER TABLE $table MODIFY COLUMN " .
385 $this->_columnSql($columndef);
387 $res = $this->conn->query($sql);
389 if (PEAR::isError($res)) {
390 throw new Exception($res->getMessage());
397 * Drops a column from a table
399 * The name must match an existing column.
401 * @param string $table name of the table
402 * @param string $columnName name of the column to drop
404 * @return boolean success flag
407 public function dropColumn($table, $columnName)
409 $sql = "ALTER TABLE $table DROP COLUMN $columnName";
411 $res = $this->conn->query($sql);
413 if (PEAR::isError($res)) {
414 throw new Exception($res->getMessage());
421 * Ensures that a table exists with the given
422 * name and the given column definitions.
424 * If the table does not yet exist, it will
425 * create the table. If it does exist, it will
426 * alter the table to match the column definitions.
428 * @param string $tableName name of the table
429 * @param array $def Table definition array
431 * @return boolean success flag
434 public function ensureTable($tableName, $def)
436 $statements = $this->buildEnsureTable($tableName, $def);
437 return $this->runSqlSet($statements);
441 * Run a given set of SQL commands on the connection in sequence.
444 * @fixme if multiple statements, wrap in a transaction?
445 * @param array $statements
446 * @return boolean success flag
448 function runSqlSet(array $statements)
451 foreach ($statements as $sql) {
452 $res = $this->conn->query($sql);
454 if (PEAR::isError($res)) {
455 throw new Exception($res->getMessage());
462 * Check a table's status, and if needed build a set
463 * of SQL statements which change it to be consistent
464 * with the given table definition.
466 * If the table does not yet exist, statements will
467 * be returned to create the table. If it does exist,
468 * statements will be returned to alter the table to
469 * match the column definitions.
471 * @param string $tableName name of the table
472 * @param array $columns array of ColumnDef
473 * objects for the table
475 * @return array of SQL statements
478 function buildEnsureTable($tableName, $def)
481 $old = $this->getTableDef($tableName);
482 } catch (Exception $e) {
483 // @fixme this is a terrible check :D
484 if (preg_match('/no such table/', $e->getMessage())) {
485 return $this->buildCreateTable($tableName, $def);
491 $old = $this->filterDef($old);
492 $def = $this->filterDef($def);
494 // @fixme check if not present
495 $fields = $this->diffArrays($old['fields'], $def['fields'], array($this, 'columnsEqual'));
496 $uniques = $this->diffArrays($old['unique keys'], $def['unique keys']);
497 $indexes = $this->diffArrays($old['indexes'], $def['indexes']);
499 $total = $fields['count'] + $uniques['count'] + $indexes['count'];
505 // For efficiency, we want this all in one
506 // query, instead of using our methods.
510 foreach ($uniques['del'] + $uniques['mod'] as $keyName) {
511 $this->appendAlterDropUnique($phrase, $keyName);
514 foreach ($fields['add'] as $columnName) {
515 $this->appendAlterAddColumn($phrase, $columnName,
516 $def['fields'][$columnName]);
519 foreach ($fields['mod'] as $columnName) {
520 $this->appendAlterModifyColumn($phrase, $columnName,
521 $old['fields'][$columnName],
522 $def['fields'][$columnName]);
525 foreach ($fields['del'] as $columnName) {
526 $this->appendAlterDropColumn($phrase, $columnName);
529 foreach ($uniques['mod'] + $uniques['add'] as $keyName) {
530 $this->appendAlterAddUnique($phrase, $keyName, $def['unique keys'][$keyName]);
533 $sql = 'ALTER TABLE ' . $tableName . ' ' . implode(",\n", $phrase);
538 function diffArrays($old, $new, $compareCallback=null)
541 $oldKeys = array_keys($old ? $old : array());
542 $newKeys = array_keys($new ? $new : array());
544 $toadd = array_diff($newKeys, $oldKeys);
545 $todrop = array_diff($oldKeys, $newKeys);
546 $same = array_intersect($newKeys, $oldKeys);
550 // Find which fields have actually changed definition
551 // in a way that we need to tweak them for this DB type.
552 foreach ($same as $name) {
553 if ($compareCallback) {
554 $same = call_user_func($compareCallback, $old[$name], $new[$name]);
556 $same = ($old[$name] != $new[$name]);
564 return array('add' => $toadd,
568 'count' => count($toadd) + count($todrop) + count($tomod));
572 * Append phrase(s) to an array of partial ALTER TABLE chunks in order
573 * to add the given column definition to the table.
575 * @param array $phrase
576 * @param string $columnName
579 function appendAlterAddColumn(array &$phrase, $columnName, array $cd)
581 $phrase[] = 'ADD COLUMN ' .
582 $this->quoteIdentifier($columnName) .
584 $this->columnSql($cd);
588 * Append phrase(s) to an array of partial ALTER TABLE chunks in order
589 * to alter the given column from its old state to a new one.
591 * @param array $phrase
592 * @param string $columnName
593 * @param array $old previous column definition as found in DB
594 * @param array $cd current column definition
596 function appendAlterModifyColumn(array &$phrase, $columnName, array $old, array $cd)
598 $phrase[] = 'MODIFY COLUMN ' .
599 $this->quoteIdentifier($columnName) .
601 $this->columnSql($cd);
605 * Append phrase(s) to an array of partial ALTER TABLE chunks in order
606 * to drop the given column definition from the table.
608 * @param array $phrase
609 * @param string $columnName
611 function appendAlterDropColumn(array &$phrase, $columnName)
613 $phrase[] = 'DROP COLUMN ' . $this->quoteIdentifier($columnName);
616 function appendAlterAddUnique(array &$phrase, $keyName, array $def)
620 $this->appendUniqueKeyDef($sql, $keyName, $def);
621 $phrase[] = implode(' ', $sql);'ADD CONSTRAINT ' . $keyName;
624 function appendAlterDropUnique(array &$phrase, $keyName)
626 $phrase[] = 'DROP CONSTRAINT ' . $keyName;
630 * Quote a db/table/column identifier if necessary.
632 * @param string $name
635 function quoteIdentifier($name)
640 function quoteDefaultValue($cd)
642 if ($cd['type'] == 'datetime' && $cd['default'] == 'CURRENT_TIMESTAMP') {
643 return $cd['default'];
645 return $this->quoteValue($cd['default']);
649 function quoteValue($val)
651 if (is_int($val) || is_float($val) || is_double($val)) {
654 return '"' . $this->conn->escapeSimple($val) . '"';
659 * Check if two column definitions are equivalent.
660 * The default implementation checks _everything_ but in many cases
661 * you may be able to discard a bunch of equivalencies.
667 function columnsEqual(array $a, array $b)
669 return !array_diff_assoc($a, $b) && !array_diff_assoc($b, $a);
673 * Returns the array of names from an array of
676 * @param array $cds array of ColumnDef objects
678 * @return array strings for name values
681 protected function _names($cds)
685 foreach ($cds as $cd) {
686 $names[] = $cd->name;
693 * Get a ColumnDef from an array matching
696 * @param array $cds Array of ColumnDef objects
697 * @param string $name Name of the column
699 * @return ColumnDef matching item or null if no match.
702 protected function _byName($cds, $name)
704 foreach ($cds as $cd) {
705 if ($cd->name == $name) {
714 * Return the proper SQL for creating or
717 * Appropriate for use in CREATE TABLE or
718 * ALTER TABLE statements.
720 * @param ColumnDef $cd column to create
722 * @return string correct SQL for that column
725 function columnSql(array $cd)
728 $line[] = $this->typeAndSize($cd);
730 if (isset($cd['default'])) {
732 $line[] = $this->quoteDefaultValue($cd);
733 } else if (!empty($cd['not null'])) {
734 // Can't have both not null AND default!
735 $line[] = 'not null';
738 return implode(' ', $line);
743 * @param string $column canonical type name in defs
744 * @return string native DB type name
746 function mapType($column)
751 function typeAndSize($column)
753 $type = $this->mapType($column);
756 if ($column['type'] == 'numeric') {
757 if (isset($column['precision'])) {
758 $lengths[] = $column['precision'];
759 if (isset($column['scale'])) {
760 $lengths[] = $column['scale'];
763 } else if (isset($column['length'])) {
764 $lengths[] = $column['length'];
768 return $type . '(' . implode(',', $lengths) . ')';
775 * Map a native type back to an independent type + size
777 * @param string $type
778 * @return array ($type, $size) -- $size may be null
780 protected function reverseMapType($type)
782 return array($type, null);
786 * Convert an old-style set of ColumnDef objects into the current
787 * Drupal-style schema definition array, for backwards compatibility
788 * with plugins written for 0.9.x.
790 * @param string $tableName
794 function oldToNew($tableName, $defs)
803 foreach ($defs as $cd) {
804 $cd->addToTableDef($table);
806 $column['type'] = $cd->type;
807 foreach ($prefixes as $prefix) {
808 if (substr($cd->type, 0, strlen($prefix)) == $prefix) {
809 $column['type'] = substr($cd->type, strlen($prefix));
810 $column['size'] = $prefix;
816 if ($cd->type == 'varchar' || $cd->type == 'char') {
817 $column['length'] = $cd->size;
820 if (!$cd->nullable) {
821 $column['not null'] = true;
823 if ($cd->autoincrement) {
824 $column['type'] = 'serial';
827 $column['default'] = $cd->default;
829 $table['fields'][$cd->name] = $column;
831 if ($cd->key == 'PRI') {
832 // If multiple columns are defined as primary key,
833 // we'll pile them on in sequence.
834 if (!isset($table['primary key'])) {
835 $table['primary key'] = array();
837 $table['primary key'][] = $cd->name;
838 } else if ($cd->key == 'MUL') {
839 // Individual multiple-value indexes are only per-column
840 // using the old ColumnDef syntax.
841 $idx = "{$tableName}_{$cd->name}_idx";
842 $table['indexes'][$idx] = array($cd->name);
843 } else if ($cd->key == 'UNI') {
844 // Individual unique-value indexes are only per-column
845 // using the old ColumnDef syntax.
846 $idx = "{$tableName}_{$cd->name}_idx";
847 $table['unique keys'][$idx] = array($cd->name);
855 * Filter the given table definition array to match features available
858 * This lets us strip out unsupported things like comments, foreign keys,
859 * or type variants that we wouldn't get back from getTableDef().
861 * @param array $tableDef
863 function filterDef(array $tableDef)
868 function isNumericType($type)
870 $type = strtolower($type);
871 $known = array('int', 'serial', 'numeric');
872 return in_array($type, $known);
876 * Pull info from the query into a fun-fun array of dooooom
879 * @return array of arrays
881 protected function fetchQueryData($sql)
883 $res = $this->conn->query($sql);
884 if (PEAR::isError($res)) {
885 throw new Exception($res->getMessage());
890 while ($res->fetchInto($row, DB_FETCHMODE_ASSOC)) {
900 class SchemaTableMissingException extends Exception