schemaupdater.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Database schema utilities
  6. *
  7. * PHP version 5
  8. *
  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.
  13. *
  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.
  18. *
  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/>.
  21. *
  22. * @category Database
  23. * @package StatusNet
  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/
  28. */
  29. if (!defined('STATUSNET')) {
  30. exit(1);
  31. }
  32. class SchemaUpdater
  33. {
  34. public function __construct($schema)
  35. {
  36. $this->schema = $schema;
  37. $this->checksums = $this->getChecksums();
  38. }
  39. /**
  40. * @param string $tableName
  41. * @param array $tableDef
  42. */
  43. public function register($tableName, array $tableDef)
  44. {
  45. $this->tables[$tableName] = $tableDef;
  46. }
  47. /**
  48. * Go ping em!
  49. *
  50. * @fixme handle tables that belong on different database servers...?
  51. */
  52. public function checkSchema()
  53. {
  54. $checksums = $this->checksums;
  55. foreach ($this->tables as $table => $def) {
  56. $checksum = $this->checksum($def);
  57. if (empty($checksums[$table])) {
  58. common_log(LOG_DEBUG, "No previous schema_version for $table: updating to $checksum");
  59. } else if ($checksums[$table] == $checksum) {
  60. common_log(LOG_DEBUG, "Last schema_version for $table up to date: $checksum");
  61. continue;
  62. } else {
  63. common_log(LOG_DEBUG, "Last schema_version for $table is {$checksums[$table]}: updating to $checksum");
  64. }
  65. //$this->conn->query('BEGIN');
  66. $this->schema->ensureTable($table, $def);
  67. $this->saveChecksum($table, $checksum);
  68. //$this->conn->commit();
  69. }
  70. }
  71. /**
  72. * Calculate a checksum for this table definition array.
  73. *
  74. * @param array $def
  75. * @return string
  76. */
  77. public function checksum(array $def)
  78. {
  79. $flat = serialize($def);
  80. return sha1($flat);
  81. }
  82. /**
  83. * Pull all known table checksums into an array for easy lookup.
  84. *
  85. * @return array: associative array of table names to checksum strings
  86. */
  87. protected function getChecksums()
  88. {
  89. $checksums = array();
  90. PEAR::pushErrorHandling(PEAR_ERROR_EXCEPTION);
  91. try {
  92. $sv = new Schema_version();
  93. $sv->find();
  94. while ($sv->fetch()) {
  95. $checksums[$sv->table_name] = $sv->checksum;
  96. }
  97. return $checksums;
  98. } catch (Exception $e) {
  99. // no dice!
  100. common_log(LOG_DEBUG, "Possibly schema_version table doesn't exist yet.");
  101. }
  102. PEAR::popErrorHandling();
  103. return $checksums;
  104. }
  105. /**
  106. * Save or update current available checksums.
  107. *
  108. * @param string $table
  109. * @param string $checksum
  110. */
  111. protected function saveChecksum($table, $checksum)
  112. {
  113. PEAR::pushErrorHandling(PEAR_ERROR_EXCEPTION);
  114. try {
  115. $sv = new Schema_version();
  116. $sv->table_name = $table;
  117. $sv->checksum = $checksum;
  118. $sv->modified = common_sql_now();
  119. if (isset($this->checksums[$table])) {
  120. $sv->update();
  121. } else {
  122. $sv->insert();
  123. }
  124. } catch (Exception $e) {
  125. // no dice!
  126. common_log(LOG_DEBUG, "Possibly schema_version table doesn't exist yet.");
  127. }
  128. PEAR::popErrorHandling();
  129. $this->checksums[$table] = $checksum;
  130. }
  131. }