]> git.immae.eu Git - github/wallabag/wallabag.git/blame - app/DoctrineMigrations/Version20161024212538.php
Added hardcoded SQL for migration to 2.2
[github/wallabag/wallabag.git] / app / DoctrineMigrations / Version20161024212538.php
CommitLineData
23406ca3
NL
1<?php
2
3namespace Application\Migrations;
4
5use Doctrine\DBAL\Migrations\AbstractMigration;
6use Doctrine\DBAL\Schema\Schema;
7use Symfony\Component\DependencyInjection\ContainerAwareInterface;
8use Symfony\Component\DependencyInjection\ContainerInterface;
9
b87f1712
NL
10/**
11 * Added user_id column on oauth2_clients to prevent users to delete API clients from other users
12 */
23406ca3
NL
13class Version20161024212538 extends AbstractMigration implements ContainerAwareInterface
14{
15 /**
16 * @var ContainerInterface
17 */
18 private $container;
19
4acbeb93
NL
20 private $constraintName = 'IDX_user_oauth_client';
21
23406ca3
NL
22 public function setContainer(ContainerInterface $container = null)
23 {
24 $this->container = $container;
25 }
26
27 private function getTable($tableName)
28 {
18d7bc3a 29 return $this->container->getParameter('database_table_prefix').$tableName;
23406ca3
NL
30 }
31
32 /**
33 * @param Schema $schema
34 */
35 public function up(Schema $schema)
36 {
84c6a48d 37 $clientsTable = $schema->getTable($this->getTable('oauth2_clients'));
23406ca3 38
84c6a48d 39 $this->skipIf($clientsTable->hasColumn('user_id'), 'It seems that you already played this migration.');
18d7bc3a 40
4acbeb93 41 $clientsTable->addColumn('user_id', 'integer', ['notnull' => false]);
84c6a48d
NL
42
43 $clientsTable->addForeignKeyConstraint(
44 $this->getTable('user'),
65a8c6e1
NL
45 ['user_id'],
46 ['id'],
4acbeb93
NL
47 ['onDelete' => 'CASCADE'],
48 $this->constraintName
84c6a48d 49 );
23406ca3
NL
50 }
51
52 /**
53 * @param Schema $schema
54 */
55 public function down(Schema $schema)
56 {
4acbeb93
NL
57 $clientsTable = $schema->getTable($this->getTable('oauth2_clients'));
58
59 $this->skipIf(!$clientsTable->hasColumn('user_id'), 'It seems that you already played this migration.');
60
61 $clientsTable->dropColumn('user_id', 'integer');
62
63 if ($this->connection->getDatabasePlatform()->getName() != 'sqlite') {
64 $clientsTable->removeForeignKey($this->constraintName);
65 }
23406ca3
NL
66 }
67}