X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=classes%2FFile_redirection.php;h=8c64c58a80c79c4ae18b4df999c050b6bbf5cf5b;hb=0a2c51510ca785b5e3564fc0830518527929dc38;hp=d341839cf23385dd8125a76c73dfc1c031667225;hpb=27480d8e8e82f1f87362acd53f60bf24c2227b01;p=quix0rs-gnu-social.git diff --git a/classes/File_redirection.php b/classes/File_redirection.php index d341839cf2..8c64c58a80 100644 --- a/classes/File_redirection.php +++ b/classes/File_redirection.php @@ -43,7 +43,7 @@ class File_redirection extends Managed_DataObject { return array( 'fields' => array( - 'urlhash' => array('type' => 'varchar', 'length' => 64, 'description' => 'sha256 hash of the URL'), + 'urlhash' => array('type' => 'varchar', 'length' => 64, 'not null' => true, 'description' => 'sha256 hash of the URL'), 'url' => array('type' => 'text', 'description' => 'short URL (or any other kind of redirect) for file (id)'), 'file_id' => array('type' => 'int', 'description' => 'short URL for what URL/file'), 'redirections' => array('type' => 'int', 'description' => 'redirect count'), @@ -355,4 +355,46 @@ class File_redirection extends Managed_DataObject $file_redir->httpcode = intval($data['code']); $file_redir->insert(); } + + static public function beforeSchemaUpdate() + { + $table = strtolower(get_called_class()); + $schema = Schema::get(); + $schemadef = $schema->getTableDef($table); + + // 2015-02-19 We have to upgrade our table definitions to have the urlhash field populated + if (isset($schemadef['fields']['urlhash']) && in_array('urlhash', $schemadef['primary key'])) { + // We already have the urlhash field, so no need to migrate it. + return; + } + echo "\nFound old $table table, upgrading it to contain 'urlhash' field..."; + // We have to create a urlhash that is _not_ the primary key, + // transfer data and THEN run checkSchema + $schemadef['fields']['urlhash'] = array ( + 'type' => 'varchar', + 'length' => 64, + 'not null' => true, + 'description' => 'sha256 hash of the URL', + ); + $schemadef['fields']['url'] = array ( + 'type' => 'text', + 'description' => 'short URL (or any other kind of redirect) for file (id)', + ); + unset($schemadef['primary key']); + $schema->ensureTable($table, $schemadef); + echo "DONE.\n"; + + $classname = ucfirst($table); + $tablefix = new $classname; + // urlhash is hash('sha256', $url) in the File table + echo "Updating urlhash fields in $table table..."; + // Maybe very MySQL specific :( + $tablefix->query(sprintf('UPDATE %1$s SET %2$s=%3$s;', + $schema->quoteIdentifier($table), + 'urlhash', + // The line below is "result of sha256 on column `url`" + 'SHA2(url, 256)')); + echo "DONE.\n"; + echo "Resuming core schema upgrade..."; + } }