2021-12-14 18:32:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2007-07-06 04:02:24 +00:00
|
|
|
|
2023-01-10 22:44:09 +00:00
|
|
|
namespace Shimmie2;
|
|
|
|
|
2024-01-11 00:55:05 +00:00
|
|
|
use Symfony\Component\Console\Command\Command;
|
|
|
|
use Symfony\Component\Console\Input\{InputInterface,InputArgument};
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
class Upgrade extends Extension
|
|
|
|
{
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onCliGen(CliGenEvent $event): void
|
2019-11-03 19:04:57 +00:00
|
|
|
{
|
2024-01-11 00:55:05 +00:00
|
|
|
$event->app->register('db-upgrade')
|
|
|
|
->setDescription('Run DB schema updates, if automatic updates are disabled')
|
|
|
|
->setCode(function (InputInterface $input, OutputInterface $output): int {
|
|
|
|
$output->writeln("Running DB Upgrade");
|
|
|
|
global $database;
|
|
|
|
$database->set_timeout(null); // These updates can take a little bit
|
|
|
|
send_event(new DatabaseUpgradeEvent());
|
|
|
|
return Command::SUCCESS;
|
|
|
|
});
|
2019-11-03 19:04:57 +00:00
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onDatabaseUpgrade(DatabaseUpgradeEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $config, $database;
|
|
|
|
|
2020-03-23 19:56:05 +00:00
|
|
|
if (!file_exists("data/index.php")) {
|
|
|
|
file_put_contents("data/index.php", "<?php\n// Silence is golden...\n");
|
|
|
|
}
|
|
|
|
|
2020-10-29 00:57:58 +00:00
|
|
|
if ($this->get_version("db_version") < 1) {
|
2019-11-03 19:04:57 +00:00
|
|
|
$this->set_version("db_version", 2);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// v7 is convert to innodb with adodb
|
|
|
|
// now done again as v9 with PDO
|
|
|
|
|
2019-11-03 19:04:57 +00:00
|
|
|
if ($this->get_version("db_version") < 8) {
|
2020-10-27 01:05:12 +00:00
|
|
|
$database->execute(
|
|
|
|
"ALTER TABLE images ADD COLUMN locked BOOLEAN NOT NULL DEFAULT FALSE"
|
|
|
|
);
|
2019-05-28 16:59:38 +00:00
|
|
|
|
2019-11-03 19:04:57 +00:00
|
|
|
$this->set_version("db_version", 8);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2019-11-03 19:04:57 +00:00
|
|
|
if ($this->get_version("db_version") < 9) {
|
2022-10-28 00:45:35 +00:00
|
|
|
if ($database->get_driver_id() == DatabaseDriverID::MYSQL) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$tables = $database->get_col("SHOW TABLES");
|
|
|
|
foreach ($tables as $table) {
|
|
|
|
log_info("upgrade", "converting $table to innodb");
|
|
|
|
$database->execute("ALTER TABLE $table ENGINE=INNODB");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-03 19:04:57 +00:00
|
|
|
$this->set_version("db_version", 9);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2019-11-03 19:04:57 +00:00
|
|
|
if ($this->get_version("db_version") < 10) {
|
2019-05-28 16:59:38 +00:00
|
|
|
log_info("upgrade", "Adding foreign keys to images");
|
2020-10-25 21:34:52 +00:00
|
|
|
$database->execute("ALTER TABLE images ADD FOREIGN KEY (owner_id) REFERENCES users(id) ON DELETE RESTRICT");
|
2019-08-07 19:53:59 +00:00
|
|
|
|
2019-11-03 19:04:57 +00:00
|
|
|
$this->set_version("db_version", 10);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2019-11-03 19:04:57 +00:00
|
|
|
if ($this->get_version("db_version") < 11) {
|
2019-05-28 16:59:38 +00:00
|
|
|
log_info("upgrade", "Converting user flags to classes");
|
|
|
|
$database->execute("ALTER TABLE users ADD COLUMN class VARCHAR(32) NOT NULL default :user", ["user" => "user"]);
|
2023-11-11 21:49:12 +00:00
|
|
|
$database->execute("UPDATE users SET class = :name WHERE id=:id", ["name" => "anonymous", "id" => $config->get_int('anon_id')]);
|
|
|
|
$database->execute("UPDATE users SET class = :name WHERE admin=:admin", ["name" => "admin", "admin" => 'Y']);
|
2019-05-28 16:59:38 +00:00
|
|
|
|
2019-11-03 19:04:57 +00:00
|
|
|
$this->set_version("db_version", 11);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2019-11-03 19:04:57 +00:00
|
|
|
if ($this->get_version("db_version") < 12) {
|
2022-10-28 00:45:35 +00:00
|
|
|
if ($database->get_driver_id() == DatabaseDriverID::PGSQL) {
|
2019-05-28 16:59:38 +00:00
|
|
|
log_info("upgrade", "Changing ext column to VARCHAR");
|
|
|
|
$database->execute("ALTER TABLE images ALTER COLUMN ext SET DATA TYPE VARCHAR(4)");
|
|
|
|
}
|
|
|
|
|
|
|
|
log_info("upgrade", "Lowering case of all exts");
|
|
|
|
$database->execute("UPDATE images SET ext = LOWER(ext)");
|
|
|
|
|
2019-11-03 19:04:57 +00:00
|
|
|
$this->set_version("db_version", 12);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2019-11-03 19:04:57 +00:00
|
|
|
if ($this->get_version("db_version") < 13) {
|
2019-05-28 16:59:38 +00:00
|
|
|
log_info("upgrade", "Changing password column to VARCHAR(250)");
|
2022-10-28 00:45:35 +00:00
|
|
|
if ($database->get_driver_id() == DatabaseDriverID::PGSQL) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$database->execute("ALTER TABLE users ALTER COLUMN pass SET DATA TYPE VARCHAR(250)");
|
2022-10-28 00:45:35 +00:00
|
|
|
} elseif ($database->get_driver_id() == DatabaseDriverID::MYSQL) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$database->execute("ALTER TABLE users CHANGE pass pass VARCHAR(250)");
|
|
|
|
}
|
|
|
|
|
2019-11-03 19:04:57 +00:00
|
|
|
$this->set_version("db_version", 13);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2019-11-03 19:04:57 +00:00
|
|
|
if ($this->get_version("db_version") < 14) {
|
2019-05-28 16:59:38 +00:00
|
|
|
log_info("upgrade", "Changing tag column to VARCHAR(255)");
|
2022-10-28 00:45:35 +00:00
|
|
|
if ($database->get_driver_id() == DatabaseDriverID::PGSQL) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$database->execute('ALTER TABLE tags ALTER COLUMN tag SET DATA TYPE VARCHAR(255)');
|
|
|
|
$database->execute('ALTER TABLE aliases ALTER COLUMN oldtag SET DATA TYPE VARCHAR(255)');
|
|
|
|
$database->execute('ALTER TABLE aliases ALTER COLUMN newtag SET DATA TYPE VARCHAR(255)');
|
2022-10-28 00:45:35 +00:00
|
|
|
} elseif ($database->get_driver_id() == DatabaseDriverID::MYSQL) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$database->execute('ALTER TABLE tags MODIFY COLUMN tag VARCHAR(255) NOT NULL');
|
|
|
|
$database->execute('ALTER TABLE aliases MODIFY COLUMN oldtag VARCHAR(255) NOT NULL');
|
|
|
|
$database->execute('ALTER TABLE aliases MODIFY COLUMN newtag VARCHAR(255) NOT NULL');
|
|
|
|
}
|
|
|
|
|
2019-11-03 19:04:57 +00:00
|
|
|
$this->set_version("db_version", 14);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2019-06-20 15:05:53 +00:00
|
|
|
|
2019-11-03 19:04:57 +00:00
|
|
|
if ($this->get_version("db_version") < 15) {
|
2019-06-20 15:05:53 +00:00
|
|
|
log_info("upgrade", "Adding lower indexes for postgresql use");
|
2022-10-28 00:45:35 +00:00
|
|
|
if ($database->get_driver_id() == DatabaseDriverID::PGSQL) {
|
2019-06-20 15:05:53 +00:00
|
|
|
$database->execute('CREATE INDEX tags_lower_tag_idx ON tags ((lower(tag)))');
|
|
|
|
$database->execute('CREATE INDEX users_lower_name_idx ON users ((lower(name)))');
|
|
|
|
}
|
|
|
|
|
2019-11-03 19:04:57 +00:00
|
|
|
$this->set_version("db_version", 15);
|
2019-06-20 15:05:53 +00:00
|
|
|
}
|
2019-06-24 22:14:53 +00:00
|
|
|
|
2019-11-03 19:04:57 +00:00
|
|
|
if ($this->get_version("db_version") < 16) {
|
2019-06-24 22:14:53 +00:00
|
|
|
log_info("upgrade", "Adding tag_id, image_id index to image_tags");
|
|
|
|
$database->execute('CREATE UNIQUE INDEX image_tags_tag_id_image_id_idx ON image_tags(tag_id,image_id) ');
|
|
|
|
|
|
|
|
log_info("upgrade", "Changing filename column to VARCHAR(255)");
|
2022-10-28 00:45:35 +00:00
|
|
|
if ($database->get_driver_id() == DatabaseDriverID::PGSQL) {
|
2019-06-24 22:14:53 +00:00
|
|
|
$database->execute('ALTER TABLE images ALTER COLUMN filename SET DATA TYPE VARCHAR(255)');
|
2019-06-25 15:05:36 +00:00
|
|
|
// Postgresql creates a unique index for unique columns, not just a constraint,
|
|
|
|
// so we don't need two indexes on the same column
|
|
|
|
$database->execute('DROP INDEX IF EXISTS images_hash_idx');
|
|
|
|
$database->execute('DROP INDEX IF EXISTS users_name_idx');
|
2022-10-28 00:45:35 +00:00
|
|
|
} elseif ($database->get_driver_id() == DatabaseDriverID::MYSQL) {
|
2019-06-24 22:14:53 +00:00
|
|
|
$database->execute('ALTER TABLE images MODIFY COLUMN filename VARCHAR(255) NOT NULL');
|
|
|
|
}
|
|
|
|
// SQLite doesn't support altering existing columns? This seems like a problem?
|
|
|
|
|
2019-11-03 19:04:57 +00:00
|
|
|
$this->set_version("db_version", 16);
|
2019-06-24 22:14:53 +00:00
|
|
|
}
|
2019-06-25 20:17:13 +00:00
|
|
|
|
2019-11-03 19:04:57 +00:00
|
|
|
if ($this->get_version("db_version") < 17) {
|
2019-06-25 20:17:13 +00:00
|
|
|
log_info("upgrade", "Adding media information columns to images table");
|
2020-10-27 00:49:50 +00:00
|
|
|
$database->execute("ALTER TABLE images ADD COLUMN lossless BOOLEAN NULL");
|
|
|
|
$database->execute("ALTER TABLE images ADD COLUMN video BOOLEAN NULL");
|
|
|
|
$database->execute("ALTER TABLE images ADD COLUMN audio BOOLEAN NULL");
|
2019-06-25 20:17:13 +00:00
|
|
|
$database->execute("ALTER TABLE images ADD COLUMN length INTEGER NULL ");
|
|
|
|
|
|
|
|
log_info("upgrade", "Setting indexes for media columns");
|
2022-10-28 00:45:35 +00:00
|
|
|
switch ($database->get_driver_id()) {
|
|
|
|
case DatabaseDriverID::PGSQL:
|
|
|
|
case DatabaseDriverID::SQLITE:
|
2019-06-25 20:17:13 +00:00
|
|
|
$database->execute('CREATE INDEX images_video_idx ON images(video) WHERE video IS NOT NULL');
|
|
|
|
$database->execute('CREATE INDEX images_audio_idx ON images(audio) WHERE audio IS NOT NULL');
|
|
|
|
$database->execute('CREATE INDEX images_length_idx ON images(length) WHERE length IS NOT NULL');
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$database->execute('CREATE INDEX images_video_idx ON images(video)');
|
|
|
|
$database->execute('CREATE INDEX images_audio_idx ON images(audio)');
|
|
|
|
$database->execute('CREATE INDEX images_length_idx ON images(length)');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-09-22 14:42:41 +00:00
|
|
|
$database->set_timeout(null); // These updates can take a little bit
|
2019-06-25 20:17:13 +00:00
|
|
|
|
|
|
|
log_info("upgrade", "Setting index for ext column");
|
|
|
|
$database->execute('CREATE INDEX images_ext_idx ON images(ext)');
|
|
|
|
|
2020-01-27 19:27:20 +00:00
|
|
|
$this->set_version("db_version", 17);
|
|
|
|
}
|
2019-06-25 20:17:13 +00:00
|
|
|
|
2020-10-27 00:49:50 +00:00
|
|
|
// 18 was populating data using an out of date format
|
2020-06-14 16:05:55 +00:00
|
|
|
|
|
|
|
if ($this->get_version("db_version") < 19) {
|
|
|
|
log_info("upgrade", "Adding MIME type column");
|
|
|
|
|
2020-10-26 23:18:14 +00:00
|
|
|
$database->execute("ALTER TABLE images ADD COLUMN mime varchar(512) NULL");
|
2020-06-14 16:05:55 +00:00
|
|
|
// Column is primed in mime extension
|
|
|
|
log_info("upgrade", "Setting index for mime column");
|
|
|
|
$database->execute('CREATE INDEX images_mime_idx ON images(mime)');
|
|
|
|
|
|
|
|
$this->set_version("db_version", 19);
|
|
|
|
}
|
2020-10-27 00:49:50 +00:00
|
|
|
|
|
|
|
if ($this->get_version("db_version") < 20) {
|
|
|
|
$database->standardise_boolean("images", "lossless");
|
|
|
|
$database->standardise_boolean("images", "video");
|
|
|
|
$database->standardise_boolean("images", "audio");
|
|
|
|
$this->set_version("db_version", 20);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->get_version("db_version") < 21) {
|
|
|
|
log_info("upgrade", "Setting predictable media values for known file types");
|
2024-01-15 22:56:06 +00:00
|
|
|
if ($database->is_transaction_open()) {
|
|
|
|
// Each of these commands could hit a lot of data, combining
|
|
|
|
// them into one big transaction would not be a good idea.
|
|
|
|
$database->commit();
|
|
|
|
}
|
2023-11-11 21:49:12 +00:00
|
|
|
$database->execute("UPDATE images SET lossless = :t, video = :t WHERE ext IN ('swf')", ["t" => true]);
|
|
|
|
$database->execute("UPDATE images SET lossless = :f, video = :f, audio = :t WHERE ext IN ('mp3')", ["t" => true, "f" => false]);
|
|
|
|
$database->execute("UPDATE images SET lossless = :f, video = :f, audio = :f WHERE ext IN ('jpg','jpeg')", ["f" => false]);
|
|
|
|
$database->execute("UPDATE images SET lossless = :t, video = :f, audio = :f WHERE ext IN ('ico','ani','cur','png','svg')", ["t" => true, "f" => false]);
|
|
|
|
$database->execute("UPDATE images SET lossless = :t, audio = :f WHERE ext IN ('gif')", ["t" => true, "f" => false]);
|
|
|
|
$database->execute("UPDATE images SET audio = :f WHERE ext IN ('webp')", ["f" => false]);
|
|
|
|
$database->execute("UPDATE images SET lossless = :f, video = :t WHERE ext IN ('flv','mp4','m4v','ogv','webm')", ["t" => true, "f" => false]);
|
2020-10-27 00:49:50 +00:00
|
|
|
$this->set_version("db_version", 21);
|
2024-01-15 22:50:43 +00:00
|
|
|
$database->begin_transaction();
|
2020-10-27 00:49:50 +00:00
|
|
|
}
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function get_priority(): int
|
|
|
|
{
|
|
|
|
return 5;
|
|
|
|
}
|
2007-07-06 04:02:24 +00:00
|
|
|
}
|