1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace ShlinkMigrations; |
5
|
|
|
|
6
|
|
|
use Doctrine\DBAL\Schema\Schema; |
7
|
|
|
use Doctrine\DBAL\Schema\SchemaException; |
8
|
|
|
use Doctrine\DBAL\Types\Type; |
9
|
|
|
use Doctrine\Migrations\AbstractMigration; |
10
|
|
|
|
11
|
|
|
final class Version20190930165521 extends AbstractMigration |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @throws SchemaException |
15
|
|
|
*/ |
16
|
|
|
public function up(Schema $schema): void |
17
|
|
|
{ |
18
|
|
|
$shortUrls = $schema->getTable('short_urls'); |
19
|
|
|
if ($shortUrls->hasColumn('domain_id')) { |
20
|
|
|
return; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
$domains = $schema->createTable('domains'); |
24
|
|
|
$domains->addColumn('id', Type::BIGINT, [ |
25
|
|
|
'unsigned' => true, |
26
|
|
|
'autoincrement' => true, |
27
|
|
|
'notnull' => true, |
28
|
|
|
]); |
29
|
|
|
$domains->addColumn('authority', Type::STRING, [ |
30
|
|
|
'length' => 512, |
31
|
|
|
'notnull' => true, |
32
|
|
|
]); |
33
|
|
|
$domains->addUniqueIndex(['authority']); |
34
|
|
|
$domains->setPrimaryKey(['id']); |
35
|
|
|
|
36
|
|
|
$shortUrls->addColumn('domain_id', Type::BIGINT, [ |
37
|
|
|
'unsigned' => true, |
38
|
|
|
'notnull' => false, |
39
|
|
|
]); |
40
|
|
|
$shortUrls->addForeignKeyConstraint('domains', ['domain_id'], ['id'], [ |
41
|
|
|
'onDelete' => 'RESTRICT', |
42
|
|
|
'onUpdate' => 'RESTRICT', |
43
|
|
|
]); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @throws SchemaException |
48
|
|
|
*/ |
49
|
|
|
public function down(Schema $schema): void |
50
|
|
|
{ |
51
|
|
|
$schema->getTable('short_urls')->dropColumn('domain_id'); |
52
|
|
|
$schema->dropTable('domains'); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|