schemaupdater.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Database schema utilities
  18. *
  19. * @category Database
  20. * @package GNUsocial
  21. * @author Evan Prodromou <evan@status.net>
  22. * @copyright 2009 StatusNet, Inc.
  23. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  24. */
  25. defined('GNUSOCIAL') || die();
  26. class SchemaUpdater
  27. {
  28. public $widgetOpts;
  29. public $scoped;
  30. public function __construct($schema)
  31. {
  32. $this->schema = $schema;
  33. $this->checksums = $this->getChecksums();
  34. }
  35. /**
  36. * @param string $tableName
  37. * @param array $tableDef
  38. */
  39. public function register($tableName, array $tableDef)
  40. {
  41. // Check if the table we're registering is related to a Managed_DataObject
  42. if (is_a(ucfirst($tableName), 'Managed_DataObject', true)) {
  43. call_user_func("{$tableName}::beforeSchemaUpdate");
  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. } elseif ($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('START TRANSACTION');
  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 hash('sha3-256', $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. try {
  91. $sv = new Schema_version();
  92. $sv->find();
  93. while ($sv->fetch()) {
  94. $checksums[$sv->table_name] = $sv->checksum;
  95. }
  96. return $checksums;
  97. } catch (Exception $e) {
  98. // no dice!
  99. common_log(LOG_DEBUG, "Possibly schema_version table doesn't exist yet.");
  100. }
  101. return $checksums;
  102. }
  103. /**
  104. * Save or update current available checksums.
  105. *
  106. * @param string $table
  107. * @param string $checksum
  108. */
  109. protected function saveChecksum($table, $checksum)
  110. {
  111. try {
  112. $sv = new Schema_version();
  113. $sv->table_name = $table;
  114. $sv->checksum = $checksum;
  115. $sv->modified = common_sql_now();
  116. if (isset($this->checksums[$table])) {
  117. $sv->update();
  118. } else {
  119. $sv->insert();
  120. }
  121. } catch (Exception $e) {
  122. // no dice!
  123. common_log(LOG_DEBUG, "Possibly schema_version table doesn't exist yet.");
  124. }
  125. $this->checksums[$table] = $checksum;
  126. }
  127. }