Merge pull request #2650 from wallabag/add-hascolumn

Added hasColumn() in migration to check column existence
This commit is contained in:
Jeremy Benoist 2016-11-30 14:17:40 +01:00 committed by GitHub
commit 9abac9651f
14 changed files with 99 additions and 87 deletions

View file

@ -21,7 +21,7 @@ class Version20160410190541 extends AbstractMigration implements ContainerAwareI
private function getTable($tableName)
{
return $this->container->getParameter('database_table_prefix') . $tableName;
return $this->container->getParameter('database_table_prefix').$tableName;
}
/**
@ -29,13 +29,14 @@ class Version20160410190541 extends AbstractMigration implements ContainerAwareI
*/
public function up(Schema $schema)
{
if ($this->connection->getDatabasePlatform()->getName() == 'postgresql') {
$this->addSql('ALTER TABLE "'.$this->getTable('entry').'" ADD uuid UUID DEFAULT NULL');
} else {
$this->addSql('ALTER TABLE "'.$this->getTable('entry').'" ADD uuid LONGTEXT DEFAULT NULL');
}
$entryTable = $schema->getTable($this->getTable('entry'));
$this->addSql("INSERT INTO \"".$this->getTable('craue_config_setting')."\" (name, value, section) VALUES ('share_public', '1', 'entry')");
$this->skipIf($entryTable->hasColumn('uuid'), 'It seems that you already played this migration.');
$entryTable->addColumn('uuid', 'guid', [
'notnull' => false,
]);
$this->addSql('INSERT INTO '.$this->getTable('craue_config_setting')." (name, value, section) VALUES ('share_public', '1', 'entry')");
}
/**
@ -43,9 +44,9 @@ class Version20160410190541 extends AbstractMigration implements ContainerAwareI
*/
public function down(Schema $schema)
{
$this->abortIf($this->connection->getDatabasePlatform()->getName() != 'sqlite', 'This down migration can\'t be executed on SQLite databases, because SQLite don\'t support DROP COLUMN.');
$entryTable = $schema->getTable($this->getTable('entry'));
$entryTable->dropColumn('uuid');
$this->addSql('ALTER TABLE "'.$this->getTable('entry').'" DROP uuid');
$this->addSql("DELETE FROM \"".$this->getTable('craue_config_setting')."\" WHERE name = 'share_public'");
$this->addSql('DELETE FROM '.$this->getTable('craue_config_setting')." WHERE name = 'share_public'");
}
}

View file

@ -21,7 +21,7 @@ class Version20160812120952 extends AbstractMigration implements ContainerAwareI
private function getTable($tableName)
{
return $this->container->getParameter('database_table_prefix') . $tableName;
return $this->container->getParameter('database_table_prefix').$tableName;
}
/**
@ -29,18 +29,10 @@ class Version20160812120952 extends AbstractMigration implements ContainerAwareI
*/
public function up(Schema $schema)
{
switch ($this->connection->getDatabasePlatform()->getName()) {
case 'sqlite':
$this->addSql('ALTER TABLE '.$this->getTable('oauth2_clients').' ADD name longtext DEFAULT NULL');
break;
$clientsTable = $schema->getTable($this->getTable('oauth2_clients'));
$this->skipIf($clientsTable->hasColumn('name'), 'It seems that you already played this migration.');
case 'mysql':
$this->addSql('ALTER TABLE '.$this->getTable('oauth2_clients').' ADD name longtext COLLATE \'utf8_unicode_ci\' DEFAULT NULL');
break;
case 'postgresql':
$this->addSql('ALTER TABLE '.$this->getTable('oauth2_clients').' ADD name text DEFAULT NULL');
}
$clientsTable->addColumn('name', 'blob');
}
/**
@ -48,8 +40,7 @@ class Version20160812120952 extends AbstractMigration implements ContainerAwareI
*/
public function down(Schema $schema)
{
$this->abortIf($this->connection->getDatabasePlatform()->getName() == 'sqlite', 'Migration can only be executed safely on \'mysql\' or \'postgresql\'.');
$this->addSql('ALTER TABLE '.$this->getTable('oauth2_clients').' DROP COLUMN name');
$clientsTable = $schema->getTable($this->getTable('oauth2_clients'));
$clientsTable->dropColumn('name');
}
}

View file

@ -21,7 +21,7 @@ class Version20160911214952 extends AbstractMigration implements ContainerAwareI
private function getTable($tableName)
{
return $this->container->getParameter('database_table_prefix') . $tableName;
return $this->container->getParameter('database_table_prefix').$tableName;
}
/**
@ -29,8 +29,8 @@ class Version20160911214952 extends AbstractMigration implements ContainerAwareI
*/
public function up(Schema $schema)
{
$this->addSql('INSERT INTO "'.$this->getTable('craue_config_setting').'" (name, value, section) VALUES (\'import_with_redis\', \'0\', \'import\')');
$this->addSql('INSERT INTO "'.$this->getTable('craue_config_setting').'" (name, value, section) VALUES (\'import_with_rabbitmq\', \'0\', \'import\')');
$this->addSql('INSERT INTO '.$this->getTable('craue_config_setting')." (name, value, section) VALUES ('import_with_redis', 0, 'import')");
$this->addSql('INSERT INTO '.$this->getTable('craue_config_setting')." (name, value, section) VALUES ('import_with_rabbitmq', 0, 'import')");
}
/**
@ -38,5 +38,7 @@ class Version20160911214952 extends AbstractMigration implements ContainerAwareI
*/
public function down(Schema $schema)
{
$this->addSql('DELETE FROM '.$this->getTable('craue_config_setting')." WHERE name = 'import_with_redis';");
$this->addSql('DELETE FROM '.$this->getTable('craue_config_setting')." WHERE name = 'import_with_rabbitmq';");
}
}

View file

@ -21,7 +21,7 @@ class Version20160916201049 extends AbstractMigration implements ContainerAwareI
private function getTable($tableName)
{
return $this->container->getParameter('database_table_prefix') . $tableName;
return $this->container->getParameter('database_table_prefix').$tableName;
}
/**
@ -29,8 +29,12 @@ class Version20160916201049 extends AbstractMigration implements ContainerAwareI
*/
public function up(Schema $schema)
{
$this->addSql('ALTER TABLE "'.$this->getTable('config').'" ADD pocket_consumer_key VARCHAR(255) DEFAULT NULL');
$this->addSql("DELETE FROM \"".$this->getTable('craue_config_setting')."\" WHERE name = 'pocket_consumer_key';");
$configTable = $schema->getTable($this->getTable('config'));
$this->skipIf($configTable->hasColumn('pocket_consumer_key'), 'It seems that you already played this migration.');
$configTable->addColumn('pocket_consumer_key', 'string');
$this->addSql('DELETE FROM '.$this->getTable('craue_config_setting')." WHERE name = 'pocket_consumer_key';");
}
/**
@ -38,9 +42,8 @@ class Version20160916201049 extends AbstractMigration implements ContainerAwareI
*/
public function down(Schema $schema)
{
$this->abortIf($this->connection->getDatabasePlatform()->getName() == 'sqlite', 'Migration can only be executed safely on \'mysql\' or \'postgresql\'.');
$this->addSql('ALTER TABLE "'.$this->getTable('config').'" DROP pocket_consumer_key');
$this->addSql("INSERT INTO \"".$this->getTable('craue_config_setting')."\" (name, value, section) VALUES ('pocket_consumer_key', NULL, 'import')");
$configTable = $schema->getTable($this->getTable('config'));
$configTable->dropColumn('pocket_consumer_key');
$this->addSql('INSERT INTO '.$this->getTable('craue_config_setting')." (name, value, section) VALUES ('pocket_consumer_key', NULL, 'import')");
}
}

View file

@ -21,7 +21,7 @@ class Version20161001072726 extends AbstractMigration implements ContainerAwareI
private function getTable($tableName)
{
return $this->container->getParameter('database_table_prefix') . $tableName;
return $this->container->getParameter('database_table_prefix').$tableName;
}
/**

View file

@ -21,7 +21,7 @@ class Version20161022134138 extends AbstractMigration implements ContainerAwareI
private function getTable($tableName)
{
return $this->container->getParameter('database_table_prefix') . $tableName;
return $this->container->getParameter('database_table_prefix').$tableName;
}
/**
@ -72,6 +72,5 @@ class Version20161022134138 extends AbstractMigration implements ContainerAwareI
$this->addSql('ALTER TABLE '.$this->getTable('tag').' CHANGE `label` `label` longtext CHARACTER SET utf8 COLLATE utf8_unicode_ci;');
$this->addSql('ALTER TABLE '.$this->getTable('user').' CHANGE `name` `name` longtext CHARACTER SET utf8 COLLATE utf8_unicode_ci;');
}
}

View file

@ -21,7 +21,7 @@ class Version20161024212538 extends AbstractMigration implements ContainerAwareI
private function getTable($tableName)
{
return $this->container->getParameter('database_table_prefix') . $tableName;
return $this->container->getParameter('database_table_prefix').$tableName;
}
/**
@ -29,10 +29,18 @@ class Version20161024212538 extends AbstractMigration implements ContainerAwareI
*/
public function up(Schema $schema)
{
$this->skipIf($this->connection->getDatabasePlatform()->getName() == 'sqlite', 'Migration can only be executed safely on \'mysql\' or \'postgresql\'.');
$clientsTable = $schema->getTable($this->getTable('oauth2_clients'));
$this->addSql('ALTER TABLE '.$this->getTable('oauth2_clients').' ADD user_id INT(11) DEFAULT NULL');
$this->addSql('ALTER TABLE '.$this->getTable('oauth2_clients').' ADD CONSTRAINT FK_clients_user_clients FOREIGN KEY (user_id) REFERENCES '.$this->getTable('user').' (id) ON DELETE CASCADE');
$this->skipIf($clientsTable->hasColumn('user_id'), 'It seems that you already played this migration.');
$clientsTable->addColumn('user_id', 'integer');
$clientsTable->addForeignKeyConstraint(
$this->getTable('user'),
['user_id'],
['id'],
['onDelete' => 'CASCADE']
);
}
/**
@ -40,6 +48,5 @@ class Version20161024212538 extends AbstractMigration implements ContainerAwareI
*/
public function down(Schema $schema)
{
}
}

View file

@ -21,7 +21,7 @@ class Version20161031132655 extends AbstractMigration implements ContainerAwareI
private function getTable($tableName)
{
return $this->container->getParameter('database_table_prefix') . $tableName;
return $this->container->getParameter('database_table_prefix').$tableName;
}
/**
@ -29,7 +29,7 @@ class Version20161031132655 extends AbstractMigration implements ContainerAwareI
*/
public function up(Schema $schema)
{
$this->addSql("INSERT INTO \"".$this->getTable('craue_config_setting')."\" (name, value, section) VALUES ('download_images_enabled', 0, 'misc')");
$this->addSql('INSERT INTO "'.$this->getTable('craue_config_setting')."\" (name, value, section) VALUES ('download_images_enabled', 0, 'misc')");
}
/**
@ -37,8 +37,6 @@ class Version20161031132655 extends AbstractMigration implements ContainerAwareI
*/
public function down(Schema $schema)
{
$this->abortIf($this->connection->getDatabasePlatform()->getName() == 'sqlite', 'Migration can only be executed safely on \'mysql\' or \'postgresql\'.');
$this->addSql("DELETE FROM \"".$this->getTable('craue_config_setting')."\" WHERE name = 'download_images_enabled';");
$this->addSql('DELETE FROM "'.$this->getTable('craue_config_setting')."\" WHERE name = 'download_images_enabled';");
}
}

View file

@ -14,6 +14,8 @@ class Version20161104073720 extends AbstractMigration implements ContainerAwareI
*/
private $container;
private $indexName = 'IDX_entry_created_at';
public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
@ -21,7 +23,7 @@ class Version20161104073720 extends AbstractMigration implements ContainerAwareI
private function getTable($tableName)
{
return $this->container->getParameter('database_table_prefix') . $tableName;
return $this->container->getParameter('database_table_prefix').$tableName;
}
/**
@ -29,18 +31,10 @@ class Version20161104073720 extends AbstractMigration implements ContainerAwareI
*/
public function up(Schema $schema)
{
switch ($this->connection->getDatabasePlatform()->getName()) {
case 'sqlite':
$this->addSql('CREATE INDEX `created_at` ON `'.$this->getTable('entry').'` (`created_at` DESC)');
break;
$entryTable = $schema->getTable($this->getTable('entry'));
$this->skipIf($entryTable->hasIndex($this->indexName), 'It seems that you already played this migration.');
case 'mysql':
$this->addSql('ALTER TABLE '.$this->getTable('entry').' ADD INDEX created_at (created_at);');
break;
case 'postgresql':
$this->addSql('CREATE INDEX created_at ON '.$this->getTable('entry').' (created_at DESC)');
}
$entryTable->addIndex(['created_at'], $this->indexName);
}
/**
@ -48,6 +42,9 @@ class Version20161104073720 extends AbstractMigration implements ContainerAwareI
*/
public function down(Schema $schema)
{
$entryTable = $schema->getTable($this->getTable('entry'));
$this->skipIf(false === $entryTable->hasIndex($this->indexName), 'It seems that you already played this migration.');
$entryTable->dropIndex($this->indexName);
}
}

View file

@ -21,7 +21,7 @@ class Version20161106113822 extends AbstractMigration implements ContainerAwareI
private function getTable($tableName)
{
return $this->container->getParameter('database_table_prefix') . $tableName;
return $this->container->getParameter('database_table_prefix').$tableName;
}
/**
@ -29,7 +29,13 @@ class Version20161106113822 extends AbstractMigration implements ContainerAwareI
*/
public function up(Schema $schema)
{
$this->addSql('ALTER TABLE '.$this->getTable('config').' ADD action_mark_as_read INT DEFAULT 0');
$configTable = $schema->getTable($this->getTable('config'));
$this->skipIf($configTable->hasColumn('action_mark_as_read'), 'It seems that you already played this migration.');
$configTable->addColumn('action_mark_as_read', 'integer', [
'default' => 0,
]);
}
/**
@ -37,8 +43,7 @@ class Version20161106113822 extends AbstractMigration implements ContainerAwareI
*/
public function down(Schema $schema)
{
$this->abortIf($this->connection->getDatabasePlatform()->getName() != 'sqlite', 'This down migration can\'t be executed on SQLite databases, because SQLite don\'t support DROP COLUMN.');
$this->addSql('ALTER TABLE '.$this->getTable('config').' DROP action_mark_as_read');
$configTable = $schema->getTable($this->getTable('config'));
$userTable->dropColumn('action_mark_as_read');
}
}

View file

@ -21,7 +21,7 @@ class Version20161117071626 extends AbstractMigration implements ContainerAwareI
private function getTable($tableName)
{
return $this->container->getParameter('database_table_prefix') . $tableName;
return $this->container->getParameter('database_table_prefix').$tableName;
}
/**
@ -29,8 +29,8 @@ class Version20161117071626 extends AbstractMigration implements ContainerAwareI
*/
public function up(Schema $schema)
{
$this->addSql("INSERT INTO ".$this->getTable('craue_config_setting')." (name, value, section) VALUES ('share_unmark', 0, 'entry')");
$this->addSql("INSERT INTO ".$this->getTable('craue_config_setting')." (name, value, section) VALUES ('unmark_url', 'https://unmark.it', 'entry')");
$this->addSql('INSERT INTO '.$this->getTable('craue_config_setting')." (name, value, section) VALUES ('share_unmark', 0, 'entry')");
$this->addSql('INSERT INTO '.$this->getTable('craue_config_setting')." (name, value, section) VALUES ('unmark_url', 'https://unmark.it', 'entry')");
}
/**
@ -38,7 +38,7 @@ class Version20161117071626 extends AbstractMigration implements ContainerAwareI
*/
public function down(Schema $schema)
{
$this->addSql("DELETE FROM ".$this->getTable('craue_config_setting')." WHERE name = 'share_unmark';");
$this->addSql("DELETE FROM ".$this->getTable('craue_config_setting')." WHERE name = 'unmark_url';");
$this->addSql('DELETE FROM '.$this->getTable('craue_config_setting')." WHERE name = 'share_unmark';");
$this->addSql('DELETE FROM '.$this->getTable('craue_config_setting')." WHERE name = 'unmark_url';");
}
}

View file

@ -8,7 +8,7 @@ use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Add http_status in `entry_table`
* Add http_status in `entry_table`.
*/
class Version20161118134328 extends AbstractMigration implements ContainerAwareInterface
{
@ -24,7 +24,7 @@ class Version20161118134328 extends AbstractMigration implements ContainerAwareI
private function getTable($tableName)
{
return $this->container->getParameter('database_table_prefix') . $tableName;
return $this->container->getParameter('database_table_prefix').$tableName;
}
/**
@ -32,7 +32,14 @@ class Version20161118134328 extends AbstractMigration implements ContainerAwareI
*/
public function up(Schema $schema)
{
$this->addSql('ALTER TABLE '.$this->getTable('entry').' ADD http_status VARCHAR(3) DEFAULT NULL');
$entryTable = $schema->getTable($this->getTable('entry'));
$this->skipIf($entryTable->hasColumn('http_status'), 'It seems that you already played this migration.');
$entryTable->addColumn('http_status', 'string', [
'length' => 3,
'notnull' => false,
]);
}
/**
@ -40,8 +47,7 @@ class Version20161118134328 extends AbstractMigration implements ContainerAwareI
*/
public function down(Schema $schema)
{
$this->abortIf($this->connection->getDatabasePlatform()->getName() != 'sqlite', 'This down migration can\'t be executed on SQLite databases, because SQLite don\'t support DROP COLUMN.');
$this->addSql('ALTER TABLE '.$this->getTable('entry').' DROP http_status');
$userTable = $schema->getTable($this->getTable('entry'));
$userTable->dropColumn('http_status');
}
}

View file

@ -8,7 +8,7 @@ use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Add the restricted_access internal setting for articles with paywall
* Add the restricted_access internal setting for articles with paywall.
*/
class Version20161122144743 extends AbstractMigration implements ContainerAwareInterface
{
@ -24,7 +24,7 @@ class Version20161122144743 extends AbstractMigration implements ContainerAwareI
private function getTable($tableName)
{
return $this->container->getParameter('database_table_prefix') . $tableName;
return $this->container->getParameter('database_table_prefix').$tableName;
}
/**
@ -32,7 +32,7 @@ class Version20161122144743 extends AbstractMigration implements ContainerAwareI
*/
public function up(Schema $schema)
{
$this->addSql("INSERT INTO ".$this->getTable('craue_config_setting')." (name, value, section) VALUES ('restricted_access', 0, 'entry')");
$this->addSql('INSERT INTO '.$this->getTable('craue_config_setting')." (name, value, section) VALUES ('restricted_access', 0, 'entry')");
}
/**
@ -40,6 +40,6 @@ class Version20161122144743 extends AbstractMigration implements ContainerAwareI
*/
public function down(Schema $schema)
{
$this->addSql("DELETE FROM ".$this->getTable('craue_config_setting')." WHERE name = 'restricted_access';");
$this->addSql('DELETE FROM '.$this->getTable('craue_config_setting')." WHERE name = 'restricted_access';");
}
}

View file

@ -8,7 +8,7 @@ use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Methods and properties removed from `FOS\UserBundle\Model\User`
* Methods and properties removed from `FOS\UserBundle\Model\User`.
*
* - `$expired`
* - `$credentialsExpired`
@ -32,7 +32,7 @@ class Version20161122203647 extends AbstractMigration implements ContainerAwareI
private function getTable($tableName)
{
return $this->container->getParameter('database_table_prefix') . $tableName;
return $this->container->getParameter('database_table_prefix').$tableName;
}
/**
@ -40,10 +40,12 @@ class Version20161122203647 extends AbstractMigration implements ContainerAwareI
*/
public function up(Schema $schema)
{
$this->abortIf($this->connection->getDatabasePlatform()->getName() === 'sqlite', 'This up migration can\'t be executed on SQLite databases, because SQLite don\'t support DROP COLUMN.');
$userTable = $schema->getTable($this->getTable('user'));
$this->addSql('ALTER TABLE '.$this->getTable('user').' DROP expired');
$this->addSql('ALTER TABLE '.$this->getTable('user').' DROP credentials_expired');
$this->skipIf(false === $userTable->hasColumn('expired') || false === $userTable->hasColumn('credentials_expired'), 'It seems that you already played this migration.');
$userTable->dropColumn('expired');
$userTable->dropColumn('credentials_expired');
}
/**
@ -51,7 +53,8 @@ class Version20161122203647 extends AbstractMigration implements ContainerAwareI
*/
public function down(Schema $schema)
{
$this->addSql('ALTER TABLE '.$this->getTable('user').' ADD expired tinyint(1) NULL DEFAULT 0');
$this->addSql('ALTER TABLE '.$this->getTable('user').' ADD credentials_expired tinyint(1) NULL DEFAULT 0');
$userTable = $schema->getTable($this->getTable('user'));
$userTable->addColumn('expired', 'smallint');
$userTable->addColumn('credentials_expired', 'smallint');
}
}