Skip to content

Commit

Permalink
feat(kobo): Add Kobo migration (and support for renaming)
Browse files Browse the repository at this point in the history
  • Loading branch information
ragusa87 committed May 31, 2024
1 parent 213e68f commit 062d60c
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 1 deletion.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@
},
"autoload": {
"psr-4": {
"App\\": "src/"
"App\\": "src/",
"DoctrineMigrations\\": "migrations/"
}
},
"autoload-dev": {
Expand Down
20 changes: 20 additions & 0 deletions migrations/AbstractMigration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Exception\TableDoesNotExist;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\SchemaException;

abstract class AbstractMigration extends \Doctrine\Migrations\AbstractMigration
{
protected function tableExists(Schema $schema, string $table): bool
{
try{
$schema->getTable($table);
return true;
}catch (TableDoesNotExist|SchemaException){
return false;
}
}
}
55 changes: 55 additions & 0 deletions migrations/Version20240101000000.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Exception\TableDoesNotExist;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\SchemaException;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20240101000000 extends AbstractMigration
{
public function getDescription(): string
{
return 'First migration';
}

public function up(Schema $schema): void
{
// We skip this migration if the table "book" is already created
if($this->tableExists($schema, 'book')){
return;
}
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('CREATE TABLE book (id INT AUTO_INCREMENT NOT NULL, title VARCHAR(255) NOT NULL, checksum VARCHAR(255) NOT NULL, summary LONGTEXT DEFAULT NULL, slug VARCHAR(128) NOT NULL, created DATE NOT NULL, updated DATE DEFAULT NULL, image_path VARCHAR(255) DEFAULT NULL, image_filename VARCHAR(255) DEFAULT NULL, book_path VARCHAR(255) NOT NULL, book_filename VARCHAR(255) NOT NULL, serie VARCHAR(255) DEFAULT NULL, serie_index DOUBLE PRECISION DEFAULT NULL, language VARCHAR(2) DEFAULT NULL, publisher VARCHAR(255) DEFAULT NULL, publish_date DATE DEFAULT NULL, authors LONGTEXT NOT NULL COMMENT \'(DC2Type:json)\', extension VARCHAR(5) NOT NULL, image_extension VARCHAR(5) DEFAULT NULL, tags LONGTEXT DEFAULT NULL COMMENT \'(DC2Type:json)\', verified TINYINT(1) NOT NULL, UNIQUE INDEX UNIQ_CBE5A331DE6FDF9A (checksum), UNIQUE INDEX UNIQ_CBE5A331989D9B62 (slug), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE `utf8_unicode_ci` ENGINE = InnoDB');
$this->addSql('CREATE TABLE book_interaction (id INT AUTO_INCREMENT NOT NULL, user_id INT NOT NULL, book_id INT NOT NULL, finished TINYINT(1) NOT NULL, favorite TINYINT(1) NOT NULL, INDEX IDX_657AB2DBA76ED395 (user_id), INDEX IDX_657AB2DB16A2B381 (book_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE `utf8_unicode_ci` ENGINE = InnoDB');
$this->addSql('CREATE TABLE shelf (id INT AUTO_INCREMENT NOT NULL, user_id INT NOT NULL, name VARCHAR(255) NOT NULL, slug VARCHAR(128) NOT NULL, query_string LONGTEXT DEFAULT NULL COMMENT \'(DC2Type:json)\', UNIQUE INDEX UNIQ_A5475BE3989D9B62 (slug), INDEX IDX_A5475BE3A76ED395 (user_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE `utf8_unicode_ci` ENGINE = InnoDB');
$this->addSql('CREATE TABLE shelf_book (shelf_id INT NOT NULL, book_id INT NOT NULL, INDEX IDX_431D356F7C12FBC0 (shelf_id), INDEX IDX_431D356F16A2B381 (book_id), PRIMARY KEY(shelf_id, book_id)) DEFAULT CHARACTER SET utf8 COLLATE `utf8_unicode_ci` ENGINE = InnoDB');
$this->addSql('CREATE TABLE `user` (id INT AUTO_INCREMENT NOT NULL, username VARCHAR(180) NOT NULL, roles LONGTEXT NOT NULL COMMENT \'(DC2Type:json)\', password VARCHAR(255) NOT NULL, UNIQUE INDEX UNIQ_8D93D649F85E0677 (username), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE `utf8_unicode_ci` ENGINE = InnoDB');
$this->addSql('CREATE TABLE messenger_messages (id BIGINT AUTO_INCREMENT NOT NULL, body LONGTEXT NOT NULL, headers LONGTEXT NOT NULL, queue_name VARCHAR(190) NOT NULL, created_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', available_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', delivered_at DATETIME DEFAULT NULL COMMENT \'(DC2Type:datetime_immutable)\', INDEX IDX_75EA56E0FB7336F0 (queue_name), INDEX IDX_75EA56E0E3BD61CE (available_at), INDEX IDX_75EA56E016BA31DB (delivered_at), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE `utf8_unicode_ci` ENGINE = InnoDB');
$this->addSql('ALTER TABLE book_interaction ADD CONSTRAINT FK_657AB2DBA76ED395 FOREIGN KEY (user_id) REFERENCES `user` (id)');
$this->addSql('ALTER TABLE book_interaction ADD CONSTRAINT FK_657AB2DB16A2B381 FOREIGN KEY (book_id) REFERENCES book (id)');
$this->addSql('ALTER TABLE shelf ADD CONSTRAINT FK_A5475BE3A76ED395 FOREIGN KEY (user_id) REFERENCES `user` (id)');
$this->addSql('ALTER TABLE shelf_book ADD CONSTRAINT FK_431D356F7C12FBC0 FOREIGN KEY (shelf_id) REFERENCES shelf (id) ON DELETE CASCADE');
$this->addSql('ALTER TABLE shelf_book ADD CONSTRAINT FK_431D356F16A2B381 FOREIGN KEY (book_id) REFERENCES book (id) ON DELETE CASCADE');
}

public function down(Schema $schema): void
{

}

private function tableBookExists(Schema $schema): bool
{
try{
$schema->getTable('book');
return true;
}catch (TableDoesNotExist|SchemaException){
return false;
}
}
}
63 changes: 63 additions & 0 deletions migrations/Version20240531124659.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20240531124659 extends AbstractMigration
{
public function getDescription(): string
{
return 'Rename kobo to koboDevice (if any)';
}

public function up(Schema $schema): void
{
if(false === $this->tableExists($schema, 'kobo')){
return;
}
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE book_interaction CHANGE read_pages read_pages INT DEFAULT NULL');
$this->addSql('ALTER TABLE shelf_kobo DROP FOREIGN KEY FK_8CD6C0B1F4C8A5B4');
$this->addSql('DROP INDEX IDX_8CD6C0B1F4C8A5B4 ON shelf_kobo');
$this->addSql('DROP INDEX `primary` ON shelf_kobo');
$this->addSql('ALTER TABLE shelf_kobo CHANGE kobo_id kobo_device_id INT NOT NULL');
$this->addSql('ALTER TABLE shelf_kobo ADD CONSTRAINT FK_8CD6C0B162A5CBE FOREIGN KEY (kobo_device_id) REFERENCES kobo (id) ON DELETE CASCADE');
$this->addSql('CREATE INDEX IDX_8CD6C0B162A5CBE ON shelf_kobo (kobo_device_id)');
$this->addSql('ALTER TABLE shelf_kobo ADD PRIMARY KEY (kobo_device_id, shelf_id)');
$this->addSql('ALTER TABLE kobo_synced_book DROP FOREIGN KEY FK_2E188774F4C8A5B4');
$this->addSql('DROP INDEX IDX_2E188774F4C8A5B4 ON kobo_synced_book');
$this->addSql('ALTER TABLE kobo_synced_book CHANGE kobo_id kobo_device_id INT NOT NULL');
$this->addSql('ALTER TABLE kobo_synced_book ADD CONSTRAINT FK_2E18877462A5CBE FOREIGN KEY (kobo_device_id) REFERENCES kobo (id)');
$this->addSql('CREATE INDEX IDX_2E18877462A5CBE ON kobo_synced_book (kobo_device_id)');
$this->addSql('RENAME table kobo to kobo_device');

}

public function down(Schema $schema): void
{
if(false === $this->tableExists($schema, 'kobo_device')){
return;
}
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE shelf_kobo DROP FOREIGN KEY FK_8CD6C0B162A5CBE');
$this->addSql('DROP INDEX IDX_8CD6C0B162A5CBE ON shelf_kobo');
$this->addSql('DROP INDEX `PRIMARY` ON shelf_kobo');
$this->addSql('ALTER TABLE shelf_kobo CHANGE kobo_device_id kobo_id INT NOT NULL');
$this->addSql('ALTER TABLE shelf_kobo ADD CONSTRAINT FK_8CD6C0B1F4C8A5B4 FOREIGN KEY (kobo_id) REFERENCES kobo (id) ON DELETE CASCADE');
$this->addSql('CREATE INDEX IDX_8CD6C0B1F4C8A5B4 ON shelf_kobo (kobo_id)');
$this->addSql('ALTER TABLE shelf_kobo ADD PRIMARY KEY (kobo_id, shelf_id)');
$this->addSql('ALTER TABLE kobo_synced_book DROP FOREIGN KEY FK_2E18877462A5CBE');
$this->addSql('DROP INDEX IDX_2E18877462A5CBE ON kobo_synced_book');
$this->addSql('ALTER TABLE kobo_synced_book CHANGE kobo_device_id kobo_id INT NOT NULL');
$this->addSql('ALTER TABLE kobo_synced_book ADD CONSTRAINT FK_2E188774F4C8A5B4 FOREIGN KEY (kobo_id) REFERENCES kobo (id)');
$this->addSql('CREATE INDEX IDX_2E188774F4C8A5B4 ON kobo_synced_book (kobo_id)');
$this->addSql('ALTER TABLE book_interaction CHANGE read_pages read_pages INT NOT NULL');
$this->addSql('RENAME table kobo_device to kobo');
}
}
48 changes: 48 additions & 0 deletions migrations/Version20240531125758.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20240531125758 extends AbstractMigration
{
public function getDescription(): string
{
return 'Create Kobo Schema if not exist';
}

public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('CREATE TABLE kobo_device (id INT AUTO_INCREMENT NOT NULL, user_id INT NOT NULL, name VARCHAR(255) DEFAULT NULL, access_key VARCHAR(255) DEFAULT NULL, force_sync TINYINT(1) DEFAULT 0 NOT NULL, INDEX IDX_2EB06A2BA76ED395 (user_id), UNIQUE INDEX kobo_access_key (access_key), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE `utf8_unicode_ci` ENGINE = InnoDB');
$this->addSql('CREATE TABLE shelf_kobo (kobo_device_id INT NOT NULL, shelf_id INT NOT NULL, INDEX IDX_8CD6C0B162A5CBE (kobo_device_id), INDEX IDX_8CD6C0B17C12FBC0 (shelf_id), PRIMARY KEY(kobo_device_id, shelf_id)) DEFAULT CHARACTER SET utf8 COLLATE `utf8_unicode_ci` ENGINE = InnoDB');
$this->addSql('CREATE TABLE kobo_synced_book (id INT AUTO_INCREMENT NOT NULL, book_id INT NOT NULL, kobo_device_id INT NOT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, INDEX IDX_2E18877416A2B381 (book_id), INDEX IDX_2E18877462A5CBE (kobo_device_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE `utf8_unicode_ci` ENGINE = InnoDB');
$this->addSql('ALTER TABLE kobo_device ADD CONSTRAINT FK_2EB06A2BA76ED395 FOREIGN KEY (user_id) REFERENCES `user` (id)');
$this->addSql('ALTER TABLE shelf_kobo ADD CONSTRAINT FK_8CD6C0B162A5CBE FOREIGN KEY (kobo_device_id) REFERENCES kobo_device (id) ON DELETE CASCADE');
$this->addSql('ALTER TABLE shelf_kobo ADD CONSTRAINT FK_8CD6C0B17C12FBC0 FOREIGN KEY (shelf_id) REFERENCES shelf (id) ON DELETE CASCADE');
$this->addSql('ALTER TABLE kobo_synced_book ADD CONSTRAINT FK_2E18877416A2B381 FOREIGN KEY (book_id) REFERENCES book (id)');
$this->addSql('ALTER TABLE kobo_synced_book ADD CONSTRAINT FK_2E18877462A5CBE FOREIGN KEY (kobo_device_id) REFERENCES kobo_device (id)');
$this->addSql('ALTER TABLE shelf ADD created DATETIME NOT NULL, ADD updated DATETIME DEFAULT NULL, ADD uuid VARCHAR(36) DEFAULT NULL');
$this->addSql('CREATE UNIQUE INDEX UNIQ_A5475BE3D17F50A6 ON shelf (uuid)');
}

public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE kobo_device DROP FOREIGN KEY FK_2EB06A2BA76ED395');
$this->addSql('ALTER TABLE shelf_kobo DROP FOREIGN KEY FK_8CD6C0B162A5CBE');
$this->addSql('ALTER TABLE shelf_kobo DROP FOREIGN KEY FK_8CD6C0B17C12FBC0');
$this->addSql('ALTER TABLE kobo_synced_book DROP FOREIGN KEY FK_2E18877416A2B381');
$this->addSql('ALTER TABLE kobo_synced_book DROP FOREIGN KEY FK_2E18877462A5CBE');
$this->addSql('DROP TABLE kobo_device');
$this->addSql('DROP TABLE shelf_kobo');
$this->addSql('DROP TABLE kobo_synced_book');
$this->addSql('DROP INDEX UNIQ_A5475BE3D17F50A6 ON shelf');
$this->addSql('ALTER TABLE shelf DROP created, DROP updated, DROP uuid');
}
}

0 comments on commit 062d60c

Please sign in to comment.