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 c724387 commit df4e941
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 4 deletions.
1 change: 1 addition & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
$finder = PhpCsFixer\Finder::create()
->in(__DIR__.'/src')
->in(__DIR__.'/tests')
->in(__DIR__.'/migrations')
;

$config = new PhpCsFixer\Config('Biblioteca');
Expand Down
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;
}
}
}
2 changes: 0 additions & 2 deletions migrations/Version20240101000000.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

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!
Expand Down
1 change: 0 additions & 1 deletion migrations/Version20240112163633.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
Expand Down
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');
}
}
58 changes: 58 additions & 0 deletions migrations/Version20240531125758.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?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';
}

public function up(Schema $schema): void
{
if(true === $this->tableExists($schema, 'kobo_device')){
return;
}

// 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 NULL, ADD updated DATETIME DEFAULT NULL, ADD uuid VARCHAR(36) DEFAULT NULL');
$this->addSql('UPDATE shelf SET uuid = UUID() where uuid is null');
$this->addSql('UPDATE shelf SET created = now() where created is null');
$this->addSql('UPDATE shelf SET updated = now() where updated is null');
$this->addSql('UPDATE book SET uuid = UUID() where uuid is null');
$this->addSql('ALTER TABLE shelf modify created datetime not 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 IF EXISTS FK_2EB06A2BA76ED395 ');
$this->addSql('ALTER TABLE shelf_kobo DROP FOREIGN KEY IF EXISTS FK_8CD6C0B162A5CBE');
$this->addSql('ALTER TABLE shelf_kobo DROP FOREIGN KEY IF EXISTS FK_8CD6C0B17C12FBC0');
$this->addSql('ALTER TABLE kobo_synced_book DROP FOREIGN KEY IF EXISTS FK_2E18877416A2B381');
$this->addSql('ALTER TABLE kobo_synced_book DROP FOREIGN KEY IF EXISTS 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 df4e941

Please sign in to comment.