4 * StatusNet, the distributed open-source microblogging tool
6 * Database schema utilities
10 * LICENCE: This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU Affero General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Affero General Public License for more details.
20 * You should have received a copy of the GNU Affero General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 * @author Evan Prodromou <evan@status.net>
26 * @copyright 2009 StatusNet, Inc.
27 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28 * @link http://status.net/
31 if (!defined('STATUSNET')) {
37 public function __construct($schema)
39 $this->schema = $schema;
41 $this->checksums = $this->getChecksums();
45 * @param string $tableName
46 * @param array $tableDef
48 public function register($tableName, array $tableDef)
50 $this->tables[$tableName] = $tableDef;
54 * @fixme handle tables that belong on different database servers...?
56 public function checkTables()
58 $checksums = $this->checksums;
59 foreach ($this->tables as $table => $def) {
60 $checksum = $this->tableChecksum($def);
61 if (empty($checksums[$table])) {
62 common_log(LOG_DEBUG, "No previous schema_version for $table: updating to $checksum");
63 } else if ($checksums[$table] == $checksum) {
64 common_log(LOG_DEBUG, "Last schema_version for $table up to date: $checksum");
67 common_log(LOG_DEBUG, "Last schema_version for $table is {$checksums[$table]}: updating to $checksum");
69 $this->conn->query('BEGIN');
70 $this->schema->ensureTable($table, $def);
71 $this->saveChecksum($table, $checksum);
72 $this->conn->commit();
77 * Calculate a checksum for this table definition array.
82 public function checksum(array $def)
84 $flat = serialize($def);
89 * Pull all known table checksums into an array for easy lookup.
91 * @return array: associative array of table names to checksum strings
93 protected function getChecksums()
97 $sv = new Schema_version();
99 while ($sv->fetch()) {
100 $checksums[$sv->table_name] = $sv->checksum;
107 * Save or update current available checksums.
109 * @param string $table
110 * @param string $checksum
112 protected function saveChecksum($table, $checksum)
114 $sv = new Schema_version();
115 $sv->table_name = $table;
116 $sv->checksum = $checksum;
117 $sv->modified = common_sql_now();
118 if (isset($this->checksums[$table])) {
123 $this->checksums[$table] = $checksum;