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 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
45 * @link http://status.net/
48 class MysqlSchema extends Schema
50 static $_single = null;
51 protected $conn = null;
54 * Constructor. Only run once for singleton object.
57 protected function __construct()
59 // XXX: there should be an easier way to do this.
62 $this->conn = $user->getDatabaseConnection();
70 * Main public entry point. Use this to get
71 * the singleton object.
73 * @return Schema the (single) Schema object
78 if (empty(self::$_single)) {
79 self::$_single = new Schema();
81 return self::$_single;
85 * Returns a TableDef object for the table
86 * in the schema with the given name.
88 * Throws an exception if the table is not found.
90 * @param string $name Name of the table to get
92 * @return TableDef tabledef for that table.
93 * @throws SchemaTableMissingException
96 public function getTableDef($name)
98 $query = "SELECT * FROM INFORMATION_SCHEMA.COLUMNS " .
99 "WHERE TABLE_SCHEMA='%s' AND TABLE_NAME='%s'";
100 $schema = $this->conn->dsn['database'];
101 $sql = sprintf($query, $schema, $name);
102 $res = $this->conn->query($sql);
104 if (PEAR::isError($res)) {
105 throw new Exception($res->getMessage());
107 if ($res->numRows() == 0) {
109 throw new SchemaTableMissingException("No such table: $name");
112 $td = new TableDef();
115 $td->columns = array();
119 while ($res->fetchInto($row, DB_FETCHMODE_ASSOC)) {
121 $cd = new ColumnDef();
123 $cd->name = $row['COLUMN_NAME'];
125 $packed = $row['COLUMN_TYPE'];
127 if (preg_match('/^(\w+)\((\d+)\)$/', $packed, $match)) {
128 $cd->type = $match[1];
129 $cd->size = $match[2];
134 $cd->nullable = ($row['IS_NULLABLE'] == 'YES') ? true : false;
135 $cd->key = $row['COLUMN_KEY'];
136 $cd->default = $row['COLUMN_DEFAULT'];
137 $cd->extra = $row['EXTRA'];
139 // Autoincrement is stuck into the extra column.
140 // Pull it out so we don't accidentally mod it every time...
141 $extra = preg_replace('/(^|\s)auto_increment(\s|$)/i', '$1$2', $cd->extra);
142 if ($extra != $cd->extra) {
143 $cd->extra = trim($extra);
144 $cd->auto_increment = true;
147 // mysql extensions -- not (yet) used by base class
148 $cd->charset = $row['CHARACTER_SET_NAME'];
149 $cd->collate = $row['COLLATION_NAME'];
151 $td->columns[] = $cd;
159 * Pull the given table properties from INFORMATION_SCHEMA.
160 * Most of the good stuff is MySQL extensions.
163 * @throws Exception if table info can't be looked up
166 function getTableProperties($table, $props)
168 $query = "SELECT %s FROM INFORMATION_SCHEMA.TABLES " .
169 "WHERE TABLE_SCHEMA='%s' AND TABLE_NAME='%s'";
170 $schema = $this->conn->dsn['database'];
171 $sql = sprintf($query, implode(',', $props), $schema, $table);
172 $res = $this->conn->query($sql);
175 $ok = $res->fetchInto($row, DB_FETCHMODE_ASSOC);
181 throw new SchemaTableMissingException("No such table: $table");
186 * Gets a ColumnDef object for a single column.
188 * Throws an exception if the table is not found.
190 * @param string $table name of the table
191 * @param string $column name of the column
193 * @return ColumnDef definition of the column or null
197 public function getColumnDef($table, $column)
199 $td = $this->getTableDef($table);
201 foreach ($td->columns as $cd) {
202 if ($cd->name == $column) {
211 * Creates a table with the given names and columns.
213 * @param string $name Name of the table
214 * @param array $columns Array of ColumnDef objects
217 * @return boolean success flag
220 public function createTable($name, $columns)
226 $sql = "CREATE TABLE $name (\n";
228 for ($i = 0; $i < count($columns); $i++) {
236 $sql .= $this->_columnSql($cd);
239 $idx = $this->_indexList($columns);
241 if ($idx['primary']) {
242 $sql .= ",\nconstraint primary key (" . implode(',', $idx['primary']) . ")";
245 foreach ($idx['uniques'] as $u) {
246 $key = $this->_uniqueKey($name, $u);
247 $sql .= ",\nunique index $key ($u)";
250 foreach ($idx['indices'] as $i) {
251 $key = $this->_key($name, $i);
252 $sql .= ",\nindex $key ($i)";
255 $sql .= ") ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_bin; ";
257 $res = $this->conn->query($sql);
259 if (PEAR::isError($res)) {
260 throw new Exception($res->getMessage());
267 * Look over a list of column definitions and list up which
268 * indices will be present
270 private function _indexList(array $columns)
272 $list = array('uniques' => array(),
273 'primary' => array(),
274 'indices' => array());
275 foreach ($columns as $cd) {
278 $list['uniques'][] = $cd->name;
281 $list['primary'][] = $cd->name;
284 $list['indices'][] = $cd->name;
292 * Get the unique index key name for a given column on this table
294 function _uniqueKey($tableName, $columnName)
296 return $this->_key($tableName, $columnName);
300 * Get the index key name for a given column on this table
302 function _key($tableName, $columnName)
304 return "{$tableName}_{$columnName}_idx";
308 * Drops a table from the schema
310 * Throws an exception if the table is not found.
312 * @param string $name Name of the table to drop
314 * @return boolean success flag
317 public function dropTable($name)
319 $res = $this->conn->query("DROP TABLE $name");
321 if (PEAR::isError($res)) {
322 throw new Exception($res->getMessage());
329 * Adds an index to a table.
331 * If no name is provided, a name will be made up based
332 * on the table name and column names.
334 * Throws an exception on database error, esp. if the table
337 * @param string $table Name of the table
338 * @param array $columnNames Name of columns to index
339 * @param string $name (Optional) name of the index
341 * @return boolean success flag
344 public function createIndex($table, $columnNames, $name=null)
346 if (!is_array($columnNames)) {
347 $columnNames = array($columnNames);
351 $name = "$table_".implode("_", $columnNames)."_idx";
354 $res = $this->conn->query("ALTER TABLE $table ".
356 implode(",", $columnNames).")");
358 if (PEAR::isError($res)) {
359 throw new Exception($res->getMessage());
366 * Drops a named index from a table.
368 * @param string $table name of the table the index is on.
369 * @param string $name name of the index
371 * @return boolean success flag
374 public function dropIndex($table, $name)
376 $res = $this->conn->query("ALTER TABLE $table DROP INDEX $name");
378 if (PEAR::isError($res)) {
379 throw new Exception($res->getMessage());
386 * Adds a column to a table
388 * @param string $table name of the table
389 * @param ColumnDef $columndef Definition of the new
392 * @return boolean success flag
395 public function addColumn($table, $columndef)
397 $sql = "ALTER TABLE $table ADD COLUMN " . $this->_columnSql($columndef);
399 $res = $this->conn->query($sql);
401 if (PEAR::isError($res)) {
402 throw new Exception($res->getMessage());
409 * Modifies a column in the schema.
411 * The name must match an existing column and table.
413 * @param string $table name of the table
414 * @param ColumnDef $columndef new definition of the column.
416 * @return boolean success flag
419 public function modifyColumn($table, $columndef)
421 $sql = "ALTER TABLE $table MODIFY COLUMN " .
422 $this->_columnSql($columndef);
424 $res = $this->conn->query($sql);
426 if (PEAR::isError($res)) {
427 throw new Exception($res->getMessage());
434 * Drops a column from a table
436 * The name must match an existing column.
438 * @param string $table name of the table
439 * @param string $columnName name of the column to drop
441 * @return boolean success flag
444 public function dropColumn($table, $columnName)
446 $sql = "ALTER TABLE $table DROP COLUMN $columnName";
448 $res = $this->conn->query($sql);
450 if (PEAR::isError($res)) {
451 throw new Exception($res->getMessage());
458 * Ensures that a table exists with the given
459 * name and the given column definitions.
461 * If the table does not yet exist, it will
462 * create the table. If it does exist, it will
463 * alter the table to match the column definitions.
465 * @param string $tableName name of the table
466 * @param array $columns array of ColumnDef
467 * objects for the table
469 * @return boolean success flag
472 public function ensureTable($tableName, $columns)
474 // XXX: DB engine portability -> toilet
477 $td = $this->getTableDef($tableName);
478 } catch (SchemaTableMissingException $e) {
479 return $this->createTable($tableName, $columns);
482 $cur = $this->_names($td->columns);
483 $new = $this->_names($columns);
485 $dropIndex = array();
486 $toadd = array_diff($new, $cur);
487 $todrop = array_diff($cur, $new);
488 $same = array_intersect($new, $cur);
491 $tableProps = array();
493 foreach ($same as $m) {
494 $curCol = $this->_byName($td->columns, $m);
495 $newCol = $this->_byName($columns, $m);
497 if (!$newCol->equals($curCol)) {
498 $tomod[] = $newCol->name;
502 // Earlier versions may have accidentally left tables at default
503 // charsets which might be latin1 or other freakish things.
504 if ($this->_isString($curCol)) {
505 if ($curCol->charset != 'utf8') {
506 $tomod[] = $newCol->name;
512 // Find any indices we have to change...
513 $curIdx = $this->_indexList($td->columns);
514 $newIdx = $this->_indexList($columns);
516 if ($curIdx['primary'] != $newIdx['primary']) {
517 if ($curIdx['primary']) {
518 $dropIndex[] = 'drop primary key';
520 if ($newIdx['primary']) {
521 $keys = implode(',', $newIdx['primary']);
522 $addIndex[] = "add constraint primary key ($keys)";
526 $dropUnique = array_diff($curIdx['uniques'], $newIdx['uniques']);
527 $addUnique = array_diff($newIdx['uniques'], $curIdx['uniques']);
528 foreach ($dropUnique as $columnName) {
529 $dropIndex[] = 'drop key ' . $this->_uniqueKey($tableName, $columnName);
531 foreach ($addUnique as $columnName) {
532 $addIndex[] = 'add constraint unique key ' . $this->_uniqueKey($tableName, $columnName) . " ($columnName)";;
535 $dropMultiple = array_diff($curIdx['indices'], $newIdx['indices']);
536 $addMultiple = array_diff($newIdx['indices'], $curIdx['indices']);
537 foreach ($dropMultiple as $columnName) {
538 $dropIndex[] = 'drop key ' . $this->_key($tableName, $columnName);
540 foreach ($addMultiple as $columnName) {
541 $addIndex[] = 'add key ' . $this->_key($tableName, $columnName) . " ($columnName)";
544 // Check for table properties: make sure we're using a sane
545 // engine type and charset/collation.
546 // @fixme make the default engine configurable?
547 $oldProps = $this->getTableProperties($tableName, array('ENGINE', 'TABLE_COLLATION'));
548 if (strtolower($oldProps['ENGINE']) != 'innodb') {
549 $tableProps['ENGINE'] = 'InnoDB';
551 if (strtolower($oldProps['TABLE_COLLATION']) != 'utf8_bin') {
552 $tableProps['DEFAULT CHARSET'] = 'utf8';
553 $tableProps['COLLATE'] = 'utf8_bin';
556 if (count($dropIndex) + count($toadd) + count($todrop) + count($tomod) + count($addIndex) + count($tableProps) == 0) {
561 // For efficiency, we want this all in one
562 // query, instead of using our methods.
566 foreach ($dropIndex as $indexSql) {
567 $phrase[] = $indexSql;
570 foreach ($toadd as $columnName) {
571 $cd = $this->_byName($columns, $columnName);
573 $phrase[] = 'ADD COLUMN ' . $this->_columnSql($cd);
576 foreach ($todrop as $columnName) {
577 $phrase[] = 'DROP COLUMN ' . $columnName;
580 foreach ($tomod as $columnName) {
581 $cd = $this->_byName($columns, $columnName);
583 $phrase[] = 'MODIFY COLUMN ' . $this->_columnSql($cd);
586 foreach ($addIndex as $indexSql) {
587 $phrase[] = $indexSql;
590 foreach ($tableProps as $key => $val) {
591 $phrase[] = "$key=$val";
594 $sql = 'ALTER TABLE ' . $tableName . ' ' . implode(', ', $phrase);
596 common_log(LOG_DEBUG, __METHOD__ . ': ' . $sql);
597 $res = $this->conn->query($sql);
599 if (PEAR::isError($res)) {
600 throw new Exception($res->getMessage());
607 * Returns the array of names from an array of
610 * @param array $cds array of ColumnDef objects
612 * @return array strings for name values
615 private function _names($cds)
619 foreach ($cds as $cd) {
620 $names[] = $cd->name;
627 * Get a ColumnDef from an array matching
630 * @param array $cds Array of ColumnDef objects
631 * @param string $name Name of the column
633 * @return ColumnDef matching item or null if no match.
636 private function _byName($cds, $name)
638 foreach ($cds as $cd) {
639 if ($cd->name == $name) {
648 * Return the proper SQL for creating or
651 * Appropriate for use in CREATE TABLE or
652 * ALTER TABLE statements.
654 * @param ColumnDef $cd column to create
656 * @return string correct SQL for that column
659 private function _columnSql($cd)
661 $sql = "{$cd->name} ";
663 if (!empty($cd->size)) {
664 $sql .= "{$cd->type}({$cd->size}) ";
666 $sql .= "{$cd->type} ";
669 if ($this->_isString($cd)) {
670 $sql .= " CHARACTER SET utf8 ";
673 if (!empty($cd->default)) {
674 $sql .= "default {$cd->default} ";
676 $sql .= ($cd->nullable) ? "null " : "not null ";
679 if (!empty($cd->auto_increment)) {
680 $sql .= " auto_increment ";
683 if (!empty($cd->extra)) {
684 $sql .= "{$cd->extra} ";
691 * Is this column a string type?
693 private function _isString(ColumnDef $cd)
695 $strings = array('char', 'varchar', 'text');
696 return in_array(strtolower($cd->type), $strings);