]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/schemaupdater.php
Merge branch 'master' into 1.0.x
[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->checksums = $this->getChecksums();
41     }
42
43     /**
44      * @param string $tableName
45      * @param array $tableDef
46      */
47     public function register($tableName, array $tableDef)
48     {
49         $this->tables[$tableName] = $tableDef;
50     }
51
52     /**
53      * Go ping em!
54      *
55      * @fixme handle tables that belong on different database servers...?
56      */
57     public function checkSchema()
58     {
59         $checksums = $this->checksums;
60         foreach ($this->tables as $table => $def) {
61             $checksum = $this->checksum($def);
62             if (empty($checksums[$table])) {
63                 common_log(LOG_DEBUG, "No previous schema_version for $table: updating to $checksum");
64             } else if ($checksums[$table] == $checksum) {
65                 common_log(LOG_DEBUG, "Last schema_version for $table up to date: $checksum");
66                 continue;
67             } else {
68                 common_log(LOG_DEBUG, "Last schema_version for $table is {$checksums[$table]}: updating to $checksum");
69             }
70             //$this->conn->query('BEGIN');
71             $this->schema->ensureTable($table, $def);
72             $this->saveChecksum($table, $checksum);
73             //$this->conn->commit();
74         }
75     }
76
77     /**
78      * Calculate a checksum for this table definition array.
79      *
80      * @param array $def
81      * @return string
82      */
83     public function checksum(array $def)
84     {
85         $flat = serialize($def);
86         return sha1($flat);
87     }
88
89     /**
90      * Pull all known table checksums into an array for easy lookup.
91      *
92      * @return array: associative array of table names to checksum strings
93      */
94     protected function getChecksums()
95     {
96         $checksums = array();
97
98         PEAR::pushErrorHandling(PEAR_ERROR_EXCEPTION);
99         try {
100             $sv = new Schema_version();
101             $sv->find();
102             while ($sv->fetch()) {
103                 $checksums[$sv->table_name] = $sv->checksum;
104             }
105
106             return $checksums;
107         } catch (Exception $e) {
108             // no dice!
109             common_log(LOG_DEBUG, "Possibly schema_version table doesn't exist yet.");
110         }
111         PEAR::popErrorHandling();
112
113         return $checksums;
114     }
115
116     /**
117      * Save or update current available checksums.
118      *
119      * @param string $table
120      * @param string $checksum
121      */
122     protected function saveChecksum($table, $checksum)
123     {
124         PEAR::pushErrorHandling(PEAR_ERROR_EXCEPTION);
125         try {
126             $sv = new Schema_version();
127             $sv->table_name = $table;
128             $sv->checksum = $checksum;
129             $sv->modified = common_sql_now();
130             if (isset($this->checksums[$table])) {
131                 $sv->update();
132             } else {
133                 $sv->insert();
134             }
135         } catch (Exception $e) {
136             // no dice!
137             common_log(LOG_DEBUG, "Possibly schema_version table doesn't exist yet.");
138         }
139         PEAR::popErrorHandling();
140         $this->checksums[$table] = $checksum;
141     }
142 }