Skip to content

Commit

Permalink
Merge pull request #6799 from yguedidi/identify-platforms-by-their-class
Browse files Browse the repository at this point in the history
Identify platforms by their class
  • Loading branch information
Kdecherf committed Aug 8, 2023
2 parents a4b0a01 + f48f982 commit f6e85e8
Show file tree
Hide file tree
Showing 23 changed files with 185 additions and 100 deletions.
13 changes: 9 additions & 4 deletions app/DoctrineMigrations/Version20160401000000.php
Expand Up @@ -2,6 +2,9 @@

namespace Application\Migrations;

use Doctrine\DBAL\Platforms\MySQLPlatform;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\DBAL\Platforms\SqlitePlatform;
use Doctrine\DBAL\Schema\Schema;
use Wallabag\CoreBundle\Doctrine\WallabagMigration;

Expand All @@ -14,8 +17,10 @@ public function up(Schema $schema): void
{
$this->skipIf($schema->hasTable($this->getTable('entry')), 'Database already initialized');

switch ($this->connection->getDatabasePlatform()->getName()) {
case 'sqlite':
$platform = $this->connection->getDatabasePlatform();

switch (true) {
case $platform instanceof SqlitePlatform:
$sql = <<<SQL
CREATE TABLE {$this->getTable('craue_config_setting')} (name VARCHAR(255) NOT NULL, value VARCHAR(255) DEFAULT NULL, section VARCHAR(255) DEFAULT NULL, PRIMARY KEY(name));
CREATE UNIQUE INDEX UNIQ_5D9649505E237E06 ON {$this->getTable('craue_config_setting')} (name);
Expand Down Expand Up @@ -58,7 +63,7 @@ public function up(Schema $schema): void
}

break;
case 'mysql':
case $platform instanceof MySQLPlatform:
$sql = <<<SQL
CREATE TABLE {$this->getTable('craue_config_setting')} (name VARCHAR(255) NOT NULL, value VARCHAR(255) DEFAULT NULL, section VARCHAR(255) DEFAULT NULL, UNIQUE INDEX UNIQ_5D9649505E237E06 (name), PRIMARY KEY(name)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
CREATE TABLE {$this->getTable('entry')} (id INT AUTO_INCREMENT NOT NULL, user_id INT DEFAULT NULL, title LONGTEXT DEFAULT NULL, url LONGTEXT DEFAULT NULL, is_archived TINYINT(1) NOT NULL, is_starred TINYINT(1) NOT NULL, content LONGTEXT DEFAULT NULL, created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, mimetype LONGTEXT DEFAULT NULL, language LONGTEXT DEFAULT NULL, reading_time INT DEFAULT NULL, domain_name LONGTEXT DEFAULT NULL, preview_picture LONGTEXT DEFAULT NULL, is_public TINYINT(1) DEFAULT '0', INDEX IDX_F4D18282A76ED395 (user_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
Expand Down Expand Up @@ -91,7 +96,7 @@ public function up(Schema $schema): void
$this->addSql($query);
}
break;
case 'postgresql':
case $platform instanceof PostgreSQLPlatform:
$sql = <<<SQL
CREATE TABLE {$this->getTable('craue_config_setting')} (name VARCHAR(255) NOT NULL, value VARCHAR(255) DEFAULT NULL, section VARCHAR(255) DEFAULT NULL, PRIMARY KEY(name));
CREATE UNIQUE INDEX UNIQ_5D9649505E237E06 ON {$this->getTable('craue_config_setting')} (name);
Expand Down
5 changes: 3 additions & 2 deletions app/DoctrineMigrations/Version20160812120952.php
Expand Up @@ -2,6 +2,7 @@

namespace Application\Migrations;

use Doctrine\DBAL\Platforms\SqlitePlatform;
use Doctrine\DBAL\Schema\Schema;
use Wallabag\CoreBundle\Doctrine\WallabagMigration;

Expand All @@ -15,7 +16,7 @@ public function up(Schema $schema): void
$clientsTable = $schema->getTable($this->getTable('oauth2_clients'));
$this->skipIf($clientsTable->hasColumn('name'), 'It seems that you already played this migration.');

if ('sqlite' === $this->connection->getDatabasePlatform()->getName()) {
if ($this->connection->getDatabasePlatform() instanceof SqlitePlatform) {
// Can't use $clientsTable->addColumn('name', 'blob');
// because of the error:
// SQLSTATE[HY000]: General error: 1 Cannot add a NOT NULL column with default value NULL
Expand All @@ -35,7 +36,7 @@ public function down(Schema $schema): void
{
$clientsTable = $schema->getTable($this->getTable('oauth2_clients'));

if ('sqlite' === $this->connection->getDatabasePlatform()->getName()) {
if ($this->connection->getDatabasePlatform() instanceof SqlitePlatform) {
$databaseTablePrefix = $this->container->getParameter('database_table_prefix');
$this->addSql('DROP INDEX IDX_635D765EA76ED395');
$this->addSql('CREATE TEMPORARY TABLE __temp__' . $databaseTablePrefix . 'oauth2_clients AS SELECT id, random_id, redirect_uris, secret, allowed_grant_types FROM ' . $databaseTablePrefix . 'oauth2_clients');
Expand Down
19 changes: 12 additions & 7 deletions app/DoctrineMigrations/Version20161001072726.php
Expand Up @@ -3,6 +3,9 @@
namespace Application\Migrations;

use Doctrine\DBAL\Migrations\SkipMigrationException;
use Doctrine\DBAL\Platforms\MySQLPlatform;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\DBAL\Platforms\SqlitePlatform;
use Doctrine\DBAL\Schema\Schema;
use Wallabag\CoreBundle\Doctrine\WallabagMigration;

Expand All @@ -13,11 +16,13 @@ class Version20161001072726 extends WallabagMigration
{
public function up(Schema $schema): void
{
$this->skipIf('sqlite' === $this->connection->getDatabasePlatform()->getName(), 'Migration can only be executed safely on \'mysql\' or \'postgresql\'.');
$platform = $this->connection->getDatabasePlatform();

$this->skipIf($platform instanceof SqlitePlatform, 'Migration can only be executed safely on \'mysql\' or \'postgresql\'.');

// remove all FK from entry_tag
switch ($this->connection->getDatabasePlatform()->getName()) {
case 'mysql':
switch (true) {
case $platform instanceof MySQLPlatform:
$query = $this->connection->query("
SELECT CONSTRAINT_NAME
FROM information_schema.key_column_usage
Expand All @@ -29,7 +34,7 @@ public function up(Schema $schema): void
$this->addSql('ALTER TABLE ' . $this->getTable('entry_tag') . ' DROP FOREIGN KEY ' . $fk['CONSTRAINT_NAME']);
}
break;
case 'postgresql':
case $platform instanceof PostgreSQLPlatform:
// http://dba.stackexchange.com/questions/36979/retrieving-all-pk-and-fk
$query = $this->connection->query("
SELECT conrelid::regclass AS table_from
Expand All @@ -53,8 +58,8 @@ public function up(Schema $schema): void

// remove entry FK from annotation

switch ($this->connection->getDatabasePlatform()->getName()) {
case 'mysql':
switch (true) {
case $platform instanceof MySQLPlatform:
$query = $this->connection->query("
SELECT CONSTRAINT_NAME
FROM information_schema.key_column_usage
Expand All @@ -68,7 +73,7 @@ public function up(Schema $schema): void
$this->addSql('ALTER TABLE ' . $this->getTable('annotation') . ' DROP FOREIGN KEY ' . $fk['CONSTRAINT_NAME']);
}
break;
case 'postgresql':
case $platform instanceof PostgreSQLPlatform:
// http://dba.stackexchange.com/questions/36979/retrieving-all-pk-and-fk
$query = $this->connection->query("
SELECT conrelid::regclass AS table_from
Expand Down
5 changes: 3 additions & 2 deletions app/DoctrineMigrations/Version20161022134138.php
Expand Up @@ -2,6 +2,7 @@

namespace Application\Migrations;

use Doctrine\DBAL\Platforms\MySQLPlatform;
use Doctrine\DBAL\Schema\Schema;
use Wallabag\CoreBundle\Doctrine\WallabagMigration;

Expand All @@ -12,7 +13,7 @@ class Version20161022134138 extends WallabagMigration
{
public function up(Schema $schema): void
{
$this->skipIf('mysql' !== $this->connection->getDatabasePlatform()->getName(), 'This migration only apply to MySQL');
$this->skipIf(!$this->connection->getDatabasePlatform() instanceof MySQLPlatform, 'This migration only apply to MySQL');

$this->addSql('ALTER DATABASE `' . $this->connection->getParams()['dbname'] . '` CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;');

Expand Down Expand Up @@ -40,7 +41,7 @@ public function up(Schema $schema): void

public function down(Schema $schema): void
{
$this->skipIf('mysql' !== $this->connection->getDatabasePlatform()->getName(), 'This migration only apply to MySQL');
$this->skipIf(!$this->connection->getDatabasePlatform() instanceof MySQLPlatform, 'This migration only apply to MySQL');

$this->addSql('ALTER DATABASE `' . $this->connection->getParams()['dbname'] . '` CHARACTER SET = utf8 COLLATE = utf8_unicode_ci;');

Expand Down
3 changes: 2 additions & 1 deletion app/DoctrineMigrations/Version20161024212538.php
Expand Up @@ -2,6 +2,7 @@

namespace Application\Migrations;

use Doctrine\DBAL\Platforms\SqlitePlatform;
use Doctrine\DBAL\Schema\Schema;
use Wallabag\CoreBundle\Doctrine\WallabagMigration;

Expand Down Expand Up @@ -37,7 +38,7 @@ public function down(Schema $schema): void

$clientsTable->dropColumn('user_id', 'integer');

if ('sqlite' !== $this->connection->getDatabasePlatform()->getName()) {
if (!$this->connection->getDatabasePlatform() instanceof SqlitePlatform) {
$clientsTable->removeForeignKey($this->constraintName);
}
}
Expand Down
23 changes: 15 additions & 8 deletions app/DoctrineMigrations/Version20161214094402.php
Expand Up @@ -2,6 +2,9 @@

namespace Application\Migrations;

use Doctrine\DBAL\Platforms\MySQLPlatform;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\DBAL\Platforms\SqlitePlatform;
use Doctrine\DBAL\Schema\Schema;
use Wallabag\CoreBundle\Doctrine\WallabagMigration;

Expand All @@ -16,18 +19,20 @@ public function up(Schema $schema): void

$this->skipIf($entryTable->hasColumn('uid'), 'It seems that you already played this migration.');

switch ($this->connection->getDatabasePlatform()->getName()) {
case 'sqlite':
$platform = $this->connection->getDatabasePlatform();

switch (true) {
case $platform instanceof SqlitePlatform:
$this->addSql('CREATE TEMPORARY TABLE __temp__wallabag_entry AS SELECT id, user_id, uuid, title, url, is_archived, is_starred, content, created_at, updated_at, mimetype, language, reading_time, domain_name, preview_picture, is_public FROM ' . $this->getTable('entry'));
$this->addSql('DROP TABLE ' . $this->getTable('entry'));
$this->addSql('CREATE TABLE ' . $this->getTable('entry') . ' (id INTEGER NOT NULL, user_id INTEGER DEFAULT NULL, uid CLOB DEFAULT NULL COLLATE BINARY, title CLOB DEFAULT NULL COLLATE BINARY, url CLOB DEFAULT NULL COLLATE BINARY, is_archived BOOLEAN NOT NULL, is_starred BOOLEAN NOT NULL, content CLOB DEFAULT NULL COLLATE BINARY, created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, mimetype CLOB DEFAULT NULL COLLATE BINARY, language CLOB DEFAULT NULL COLLATE BINARY, reading_time INTEGER DEFAULT NULL, domain_name CLOB DEFAULT NULL COLLATE BINARY, preview_picture CLOB DEFAULT NULL COLLATE BINARY, is_public BOOLEAN DEFAULT "0", PRIMARY KEY(id));');
$this->addSql('INSERT INTO ' . $this->getTable('entry') . ' (id, user_id, uid, title, url, is_archived, is_starred, content, created_at, updated_at, mimetype, language, reading_time, domain_name, preview_picture, is_public) SELECT id, user_id, uuid, title, url, is_archived, is_starred, content, created_at, updated_at, mimetype, language, reading_time, domain_name, preview_picture, is_public FROM __temp__wallabag_entry;');
$this->addSql('DROP TABLE __temp__wallabag_entry');
break;
case 'mysql':
case $platform instanceof MySQLPlatform:
$this->addSql('ALTER TABLE ' . $this->getTable('entry') . ' CHANGE uuid uid VARCHAR(23)');
break;
case 'postgresql':
case $platform instanceof PostgreSQLPlatform:
$this->addSql('ALTER TABLE ' . $this->getTable('entry') . ' RENAME uuid TO uid');
}
}
Expand All @@ -38,14 +43,16 @@ public function down(Schema $schema): void

$this->skipIf($entryTable->hasColumn('uuid'), 'It seems that you already played this migration.');

switch ($this->connection->getDatabasePlatform()->getName()) {
case 'sqlite':
$platform = $this->connection->getDatabasePlatform();

switch (true) {
case $platform instanceof SqlitePlatform:
throw new SkipMigrationException('Too complex ...');
break;
case 'mysql':
case $platform instanceof MySQLPlatform:
$this->addSql('ALTER TABLE ' . $this->getTable('entry') . ' CHANGE uid uuid VARCHAR(23)');
break;
case 'postgresql':
case $platform instanceof PostgreSQLPlatform:
$this->addSql('ALTER TABLE ' . $this->getTable('entry') . ' RENAME uid TO uuid');
}
}
Expand Down
3 changes: 2 additions & 1 deletion app/DoctrineMigrations/Version20170501115751.php
Expand Up @@ -2,6 +2,7 @@

namespace Application\Migrations;

use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\DBAL\Schema\Schema;
use Wallabag\CoreBundle\Doctrine\WallabagMigration;

Expand All @@ -25,7 +26,7 @@ public function up(Schema $schema): void
$table->setPrimaryKey(['id']);
$table->addForeignKeyConstraint($this->getTable('user'), ['user_id'], ['id'], [], 'fk_user');

if ('postgresql' === $this->connection->getDatabasePlatform()->getName()) {
if ($this->connection->getDatabasePlatform() instanceof PostgreSQLPlatform) {
$schema->dropSequence('site_credential_id_seq');
$schema->createSequence('site_credential_id_seq');
}
Expand Down
5 changes: 3 additions & 2 deletions app/DoctrineMigrations/Version20170510082609.php
Expand Up @@ -2,6 +2,7 @@

namespace Application\Migrations;

use Doctrine\DBAL\Platforms\MySQLPlatform;
use Doctrine\DBAL\Schema\Schema;
use Wallabag\CoreBundle\Doctrine\WallabagMigration;

Expand All @@ -19,7 +20,7 @@ class Version20170510082609 extends WallabagMigration

public function up(Schema $schema): void
{
$this->skipIf('mysql' !== $this->connection->getDatabasePlatform()->getName(), 'This migration only apply to MySQL');
$this->skipIf(!$this->connection->getDatabasePlatform() instanceof MySQLPlatform, 'This migration only apply to MySQL');

foreach ($this->fields as $field) {
$this->addSql('ALTER TABLE ' . $this->getTable('user') . ' CHANGE ' . $field . ' ' . $field . ' VARCHAR(180) NOT NULL;');
Expand All @@ -28,7 +29,7 @@ public function up(Schema $schema): void

public function down(Schema $schema): void
{
$this->skipIf('mysql' !== $this->connection->getDatabasePlatform()->getName(), 'This migration only apply to MySQL');
$this->skipIf(!$this->connection->getDatabasePlatform() instanceof MySQLPlatform, 'This migration only apply to MySQL');

foreach ($this->fields as $field) {
$this->addSql('ALTER TABLE ' . $this->getTable('user') . ' CHANGE ' . $field . ' ' . $field . ' VARCHAR(255) NOT NULL;');
Expand Down
23 changes: 15 additions & 8 deletions app/DoctrineMigrations/Version20170511211659.php
Expand Up @@ -3,6 +3,9 @@
namespace Application\Migrations;

use Doctrine\DBAL\Migrations\SkipMigrationException;
use Doctrine\DBAL\Platforms\MySQLPlatform;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\DBAL\Platforms\SqlitePlatform;
use Doctrine\DBAL\Schema\Schema;
use Wallabag\CoreBundle\Doctrine\WallabagMigration;

Expand All @@ -13,8 +16,10 @@ class Version20170511211659 extends WallabagMigration
{
public function up(Schema $schema): void
{
switch ($this->connection->getDatabasePlatform()->getName()) {
case 'sqlite':
$platform = $this->connection->getDatabasePlatform();

switch (true) {
case $platform instanceof SqlitePlatform:
$annotationTableName = $this->getTable('annotation', true);
$userTableName = $this->getTable('user', true);
$entryTableName = $this->getTable('entry', true);
Expand Down Expand Up @@ -53,10 +58,10 @@ public function up(Schema $schema): void
);
$this->addSql('DROP TABLE __temp__wallabag_annotation');
break;
case 'mysql':
case $platform instanceof MySQLPlatform:
$this->addSql('ALTER TABLE ' . $this->getTable('annotation') . ' MODIFY quote TEXT NOT NULL');
break;
case 'postgresql':
case $platform instanceof PostgreSQLPlatform:
$this->addSql('ALTER TABLE ' . $this->getTable('annotation') . ' ALTER COLUMN quote TYPE TEXT');
break;
}
Expand All @@ -66,14 +71,16 @@ public function down(Schema $schema): void
{
$tableName = $this->getTable('annotation');

switch ($this->connection->getDatabasePlatform()->getName()) {
case 'sqlite':
$platform = $this->connection->getDatabasePlatform();

switch (true) {
case $platform instanceof SqlitePlatform:
throw new SkipMigrationException('Too complex ...');
break;
case 'mysql':
case $platform instanceof MySQLPlatform:
$this->addSql('ALTER TABLE ' . $tableName . ' MODIFY quote VARCHAR(255) NOT NULL');
break;
case 'postgresql':
case $platform instanceof PostgreSQLPlatform:
$this->addSql('ALTER TABLE ' . $tableName . ' ALTER COLUMN quote TYPE VARCHAR(255)');
break;
}
Expand Down
3 changes: 2 additions & 1 deletion app/DoctrineMigrations/Version20170719231144.php
Expand Up @@ -2,6 +2,7 @@

namespace Application\Migrations;

use Doctrine\DBAL\Platforms\SqlitePlatform;
use Doctrine\DBAL\Schema\Schema;
use Wallabag\CoreBundle\Doctrine\WallabagMigration;

Expand All @@ -12,7 +13,7 @@ class Version20170719231144 extends WallabagMigration
{
public function up(Schema $schema): void
{
$this->skipIf('sqlite' === $this->connection->getDatabasePlatform()->getName(), 'Migration can only be executed safely on \'mysql\' or \'postgresql\'.');
$this->skipIf($this->connection->getDatabasePlatform() instanceof SqlitePlatform, 'Migration can only be executed safely on \'mysql\' or \'postgresql\'.');

// Find tags which need to be merged
$dupTags = $this->connection->query('
Expand Down
23 changes: 15 additions & 8 deletions app/DoctrineMigrations/Version20171008195606.php
Expand Up @@ -2,6 +2,9 @@

namespace Application\Migrations;

use Doctrine\DBAL\Platforms\MySQLPlatform;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\DBAL\Platforms\SqlitePlatform;
use Doctrine\DBAL\Schema\Schema;
use Wallabag\CoreBundle\Doctrine\WallabagMigration;

Expand All @@ -12,14 +15,16 @@ class Version20171008195606 extends WallabagMigration
{
public function up(Schema $schema): void
{
$this->skipIf('sqlite' === $this->connection->getDatabasePlatform()->getName(), 'Migration can only be executed safely on \'mysql\' or \'postgresql\'.');
$platform = $this->connection->getDatabasePlatform();

switch ($this->connection->getDatabasePlatform()->getName()) {
case 'mysql':
$this->skipIf($platform instanceof SqlitePlatform, 'Migration can only be executed safely on \'mysql\' or \'postgresql\'.');

switch (true) {
case $platform instanceof MySQLPlatform:
$this->addSql('UPDATE ' . $this->getTable('entry') . ' SET reading_time = 0 WHERE reading_time IS NULL;');
$this->addSql('ALTER TABLE ' . $this->getTable('entry') . ' CHANGE reading_time reading_time INT(11) NOT NULL;');
break;
case 'postgresql':
case $platform instanceof PostgreSQLPlatform:
$this->addSql('UPDATE ' . $this->getTable('entry') . ' SET reading_time = 0 WHERE reading_time IS NULL;');
$this->addSql('ALTER TABLE ' . $this->getTable('entry') . ' ALTER COLUMN reading_time SET NOT NULL;');
break;
Expand All @@ -28,13 +33,15 @@ public function up(Schema $schema): void

public function down(Schema $schema): void
{
$this->skipIf('sqlite' === $this->connection->getDatabasePlatform()->getName(), 'Migration can only be executed safely on \'mysql\' or \'postgresql\'.');
$platform = $this->connection->getDatabasePlatform();

$this->skipIf($platform instanceof SqlitePlatform, 'Migration can only be executed safely on \'mysql\' or \'postgresql\'.');

switch ($this->connection->getDatabasePlatform()->getName()) {
case 'mysql':
switch (true) {
case $platform instanceof MySQLPlatform:
$this->addSql('ALTER TABLE ' . $this->getTable('entry') . ' CHANGE reading_time reading_time INT(11);');
break;
case 'postgresql':
case $platform instanceof PostgreSQLPlatform:
$this->addSql('ALTER TABLE ' . $this->getTable('entry') . ' ALTER COLUMN reading_time DROP NOT NULL;');
break;
}
Expand Down

0 comments on commit f6e85e8

Please sign in to comment.