We are no longer offering accounts on this server. Consider https://gitlab.freedesktop.org/ as a place to host projects.

schemaupdater.php 4.17 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
<?php

/**
 * StatusNet, the distributed open-source microblogging tool
 *
 * Database schema utilities
 *
 * PHP version 5
 *
 * LICENCE: This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @category  Database
 * @package   StatusNet
 * @author    Evan Prodromou <evan@status.net>
 * @copyright 2009 StatusNet, Inc.
 * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
 * @link      http://status.net/
 */

if (!defined('STATUSNET')) {
    exit(1);
}

class SchemaUpdater
{
    public function __construct($schema)
    {
        $this->schema = $schema;
        $this->checksums = $this->getChecksums();
    }

    /**
Brion Vibber's avatar
tweak  
Brion Vibber committed
44 45 46 47 48 49 50 51 52
     * @param string $tableName
     * @param array $tableDef
     */
    public function register($tableName, array $tableDef)
    {
        $this->tables[$tableName] = $tableDef;
    }

    /**
Brion Vibber's avatar
Brion Vibber committed
53 54
     * Go ping em!
     *
55 56
     * @fixme handle tables that belong on different database servers...?
     */
Brion Vibber's avatar
Brion Vibber committed
57
    public function checkSchema()
58 59
    {
        $checksums = $this->checksums;
Brion Vibber's avatar
tweak  
Brion Vibber committed
60
        foreach ($this->tables as $table => $def) {
Brion Vibber's avatar
Brion Vibber committed
61
            $checksum = $this->checksum($def);
62 63 64 65 66 67 68 69
            if (empty($checksums[$table])) {
                common_log(LOG_DEBUG, "No previous schema_version for $table: updating to $checksum");
            } else if ($checksums[$table] == $checksum) {
                common_log(LOG_DEBUG, "Last schema_version for $table up to date: $checksum");
                continue;
            } else {
                common_log(LOG_DEBUG, "Last schema_version for $table is {$checksums[$table]}: updating to $checksum");
            }
Brion Vibber's avatar
Brion Vibber committed
70
            //$this->conn->query('BEGIN');
71 72
            $this->schema->ensureTable($table, $def);
            $this->saveChecksum($table, $checksum);
Brion Vibber's avatar
Brion Vibber committed
73
            //$this->conn->commit();
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
        }
    }

    /**
     * Calculate a checksum for this table definition array.
     *
     * @param array $def
     * @return string
     */
    public function checksum(array $def)
    {
        $flat = serialize($def);
        return sha1($flat);
    }

    /**
     * Pull all known table checksums into an array for easy lookup.
     *
     * @return array: associative array of table names to checksum strings
     */
    protected function getChecksums()
    {
        $checksums = array();

98 99 100 101 102 103 104 105 106 107 108 109
        PEAR::pushErrorHandling(PEAR_ERROR_EXCEPTION);
        try {
            $sv = new Schema_version();
            $sv->find();
            while ($sv->fetch()) {
                $checksums[$sv->table_name] = $sv->checksum;
            }

            return $checksums;
        } catch (Exception $e) {
            // no dice!
            common_log(LOG_DEBUG, "Possibly schema_version table doesn't exist yet.");
110
        }
111
        PEAR::popErrorHandling();
112 113 114 115 116 117 118 119 120 121 122 123

        return $checksums;
    }

    /**
     * Save or update current available checksums.
     *
     * @param string $table
     * @param string $checksum
     */
    protected function saveChecksum($table, $checksum)
    {
124 125 126 127 128 129 130 131 132 133 134 135 136 137
        PEAR::pushErrorHandling(PEAR_ERROR_EXCEPTION);
        try {
            $sv = new Schema_version();
            $sv->table_name = $table;
            $sv->checksum = $checksum;
            $sv->modified = common_sql_now();
            if (isset($this->checksums[$table])) {
                $sv->update();
            } else {
                $sv->insert();
            }
        } catch (Exception $e) {
            // no dice!
            common_log(LOG_DEBUG, "Possibly schema_version table doesn't exist yet.");
138
        }
139
        PEAR::popErrorHandling();
140 141 142
        $this->checksums[$table] = $checksum;
    }
}