]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/schemaupdater.php
Starting to encapsulate some of the schema_version checksum / updater logic
[quix0rs-gnu-social.git] / lib / schemaupdater.php
1 <?php
2
3 /**
4  * StatusNet, the distributed open-source microblogging tool
5  *
6  * Database schema utilities
7  *
8  * PHP version 5
9  *
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.
14  *
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.
19  *
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/>.
22  *
23  * @category  Database
24  * @package   StatusNet
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/
29  */
30
31 if (!defined('STATUSNET')) {
32     exit(1);
33 }
34
35 class SchemaUpdater
36 {
37     public function __construct($schema)
38     {
39         $this->schema = $schema;
40         $this->conn = $conn;
41         $this->checksums = $this->getChecksums();
42     }
43
44     /**
45      * @param array $tableDefs
46      * @fixme handle tables that belong on different database servers...?
47      */
48     public function checkTables(array $tableDefs)
49     {
50         $checksums = $this->checksums;
51         foreach ($tableDefs as $table => $def) {
52             $checksum = $this->tableChecksum($def);
53             if (empty($checksums[$table])) {
54                 common_log(LOG_DEBUG, "No previous schema_version for $table: updating to $checksum");
55             } else if ($checksums[$table] == $checksum) {
56                 common_log(LOG_DEBUG, "Last schema_version for $table up to date: $checksum");
57                 continue;
58             } else {
59                 common_log(LOG_DEBUG, "Last schema_version for $table is {$checksums[$table]}: updating to $checksum");
60             }
61             $this->conn->query('BEGIN');
62             $this->schema->ensureTable($table, $def);
63             $this->saveChecksum($table, $checksum);
64             $this->conn->commit();
65         }
66     }
67
68     /**
69      * Calculate a checksum for this table definition array.
70      *
71      * @param array $def
72      * @return string
73      */
74     public function checksum(array $def)
75     {
76         $flat = serialize($def);
77         return sha1($flat);
78     }
79
80     /**
81      * Pull all known table checksums into an array for easy lookup.
82      *
83      * @return array: associative array of table names to checksum strings
84      */
85     protected function getChecksums()
86     {
87         $checksums = array();
88
89         $sv = new Schema_version();
90         $sv->find();
91         while ($sv->fetch()) {
92             $checksums[$sv->table_name] = $sv->checksum;
93         }
94
95         return $checksums;
96     }
97
98     /**
99      * Save or update current available checksums.
100      *
101      * @param string $table
102      * @param string $checksum
103      */
104     protected function saveChecksum($table, $checksum)
105     {
106         $sv = new Schema_version();
107         $sv->table_name = $table;
108         $sv->checksum = $checksum;
109         $sv->modified = common_sql_now();
110         if (isset($this->checksums[$table])) {
111             $sv->update();
112         } else {
113             $sv->insert();
114         }
115         $this->checksums[$table] = $checksum;
116     }
117 }