diff options
author | Nicolas LÅ“uillet <nicolas@loeuillet.org> | 2016-11-25 13:47:09 +0100 |
---|---|---|
committer | Nicolas LÅ“uillet <nicolas@loeuillet.org> | 2016-11-28 14:28:35 +0100 |
commit | 73f7eabb6e10cff09a79105b6525e3c269e3cf08 (patch) | |
tree | 9319bc5e96c641ffadb70e9754c3ac247b108e0a /app/DoctrineMigrations | |
parent | 34ea7be6228c633ef8da703994eed034026e9c18 (diff) | |
download | wallabag-73f7eabb6e10cff09a79105b6525e3c269e3cf08.tar.gz wallabag-73f7eabb6e10cff09a79105b6525e3c269e3cf08.tar.zst wallabag-73f7eabb6e10cff09a79105b6525e3c269e3cf08.zip |
Added hasColumn() in migration to check column existence
Diffstat (limited to 'app/DoctrineMigrations')
-rw-r--r-- | app/DoctrineMigrations/Version20160410190541.php | 44 |
1 files changed, 40 insertions, 4 deletions
diff --git a/app/DoctrineMigrations/Version20160410190541.php b/app/DoctrineMigrations/Version20160410190541.php index f034b0e4..5de53d4b 100644 --- a/app/DoctrineMigrations/Version20160410190541.php +++ b/app/DoctrineMigrations/Version20160410190541.php | |||
@@ -21,7 +21,41 @@ class Version20160410190541 extends AbstractMigration implements ContainerAwareI | |||
21 | 21 | ||
22 | private function getTable($tableName) | 22 | private function getTable($tableName) |
23 | { | 23 | { |
24 | return $this->container->getParameter('database_table_prefix') . $tableName; | 24 | return $this->container->getParameter('database_table_prefix').$tableName; |
25 | } | ||
26 | |||
27 | private function hasColumn($tableName, $columnName) | ||
28 | { | ||
29 | switch ($this->connection->getDatabasePlatform()->getName()) { | ||
30 | case 'sqlite': | ||
31 | $rows = $this->connection->executeQuery('pragma table_info('.$tableName.')')->fetchAll(); | ||
32 | foreach ($rows as $column) { | ||
33 | if (strcasecmp($column['name'], $columnName) === 0) { | ||
34 | return true; | ||
35 | } | ||
36 | } | ||
37 | |||
38 | return false; | ||
39 | case 'mysql': | ||
40 | $rows = $this->connection->executeQuery('SHOW COLUMNS FROM '.$tableName)->fetchAll(); | ||
41 | foreach ($rows as $column) { | ||
42 | if (strcasecmp($column['Field'], $columnName) === 0) { | ||
43 | return true; | ||
44 | } | ||
45 | } | ||
46 | |||
47 | return false; | ||
48 | case 'postgresql': | ||
49 | $sql = sprintf("SELECT count(*) | ||
50 | FROM information_schema.columns | ||
51 | WHERE table_schema = 'public' AND table_name = '%s' AND column_name = '%s'", | ||
52 | $tableName, | ||
53 | $columnName | ||
54 | ); | ||
55 | $result = $this->connection->executeQuery($sql)->fetch(); | ||
56 | |||
57 | return $result['count'] > 0; | ||
58 | } | ||
25 | } | 59 | } |
26 | 60 | ||
27 | /** | 61 | /** |
@@ -29,13 +63,15 @@ class Version20160410190541 extends AbstractMigration implements ContainerAwareI | |||
29 | */ | 63 | */ |
30 | public function up(Schema $schema) | 64 | public function up(Schema $schema) |
31 | { | 65 | { |
66 | $this->skipIf($this->hasColumn($this->getTable('entry'), 'uuid'), 'It seems that you already played this migration.'); | ||
67 | |||
32 | if ($this->connection->getDatabasePlatform()->getName() == 'postgresql') { | 68 | if ($this->connection->getDatabasePlatform()->getName() == 'postgresql') { |
33 | $this->addSql('ALTER TABLE "'.$this->getTable('entry').'" ADD uuid UUID DEFAULT NULL'); | 69 | $this->addSql('ALTER TABLE "'.$this->getTable('entry').'" ADD uuid UUID DEFAULT NULL'); |
34 | } else { | 70 | } else { |
35 | $this->addSql('ALTER TABLE "'.$this->getTable('entry').'" ADD uuid LONGTEXT DEFAULT NULL'); | 71 | $this->addSql('ALTER TABLE "'.$this->getTable('entry').'" ADD uuid LONGTEXT DEFAULT NULL'); |
36 | } | 72 | } |
37 | 73 | ||
38 | $this->addSql("INSERT INTO \"".$this->getTable('craue_config_setting')."\" (name, value, section) VALUES ('share_public', '1', 'entry')"); | 74 | $this->addSql('INSERT INTO "'.$this->getTable('craue_config_setting')."\" (name, value, section) VALUES ('share_public', '1', 'entry')"); |
39 | } | 75 | } |
40 | 76 | ||
41 | /** | 77 | /** |
@@ -43,9 +79,9 @@ class Version20160410190541 extends AbstractMigration implements ContainerAwareI | |||
43 | */ | 79 | */ |
44 | public function down(Schema $schema) | 80 | public function down(Schema $schema) |
45 | { | 81 | { |
46 | $this->abortIf($this->connection->getDatabasePlatform()->getName() != 'sqlite', 'This down migration can\'t be executed on SQLite databases, because SQLite don\'t support DROP COLUMN.'); | 82 | $this->skipIf($this->connection->getDatabasePlatform()->getName() != 'sqlite', 'This down migration can\'t be executed on SQLite databases, because SQLite don\'t support DROP COLUMN.'); |
47 | 83 | ||
48 | $this->addSql('ALTER TABLE "'.$this->getTable('entry').'" DROP uuid'); | 84 | $this->addSql('ALTER TABLE "'.$this->getTable('entry').'" DROP uuid'); |
49 | $this->addSql("DELETE FROM \"".$this->getTable('craue_config_setting')."\" WHERE name = 'share_public'"); | 85 | $this->addSql('DELETE FROM "'.$this->getTable('craue_config_setting')."\" WHERE name = 'share_public'"); |
50 | } | 86 | } |
51 | } | 87 | } |