2016-08-21 10:41:21 +03:00
|
|
|
<?php
|
2019-10-05 18:26:10 +03:00
|
|
|
|
2017-10-12 11:13:20 +03:00
|
|
|
declare(strict_types=1);
|
2016-08-21 10:41:21 +03:00
|
|
|
|
|
|
|
namespace ShlinkMigrations;
|
|
|
|
|
|
|
|
use Doctrine\DBAL\Schema\Schema;
|
2020-01-07 01:08:14 +03:00
|
|
|
use Doctrine\DBAL\Types\Types;
|
2019-03-09 20:45:58 +03:00
|
|
|
use Doctrine\Migrations\AbstractMigration;
|
2016-08-21 10:41:21 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Auto-generated Migration: Please modify to your needs!
|
|
|
|
*/
|
|
|
|
class Version20160820191203 extends AbstractMigration
|
|
|
|
{
|
2019-03-09 20:45:58 +03:00
|
|
|
public function up(Schema $schema): void
|
2016-08-21 10:41:21 +03:00
|
|
|
{
|
2016-08-21 10:45:36 +03:00
|
|
|
// Check if the tables already exist
|
|
|
|
$tables = $schema->getTables();
|
|
|
|
foreach ($tables as $table) {
|
|
|
|
if ($table->getName() === 'tags') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-21 10:41:21 +03:00
|
|
|
$this->createTagsTable($schema);
|
|
|
|
$this->createShortUrlsInTagsTable($schema);
|
|
|
|
}
|
|
|
|
|
2019-03-09 20:45:58 +03:00
|
|
|
private function createTagsTable(Schema $schema): void
|
2016-08-21 10:41:21 +03:00
|
|
|
{
|
|
|
|
$table = $schema->createTable('tags');
|
2020-01-07 01:08:14 +03:00
|
|
|
$table->addColumn('id', Types::BIGINT, [
|
2016-08-21 10:41:21 +03:00
|
|
|
'unsigned' => true,
|
|
|
|
'autoincrement' => true,
|
|
|
|
'notnull' => true,
|
|
|
|
]);
|
2020-01-07 01:08:14 +03:00
|
|
|
$table->addColumn('name', Types::STRING, [
|
2016-08-21 10:41:21 +03:00
|
|
|
'length' => 255,
|
|
|
|
'notnull' => true,
|
|
|
|
]);
|
|
|
|
$table->addUniqueIndex(['name']);
|
|
|
|
|
|
|
|
$table->setPrimaryKey(['id']);
|
|
|
|
}
|
|
|
|
|
2019-03-09 20:45:58 +03:00
|
|
|
private function createShortUrlsInTagsTable(Schema $schema): void
|
2016-08-21 10:41:21 +03:00
|
|
|
{
|
|
|
|
$table = $schema->createTable('short_urls_in_tags');
|
2020-01-07 01:08:14 +03:00
|
|
|
$table->addColumn('short_url_id', Types::BIGINT, [
|
2016-08-21 10:41:21 +03:00
|
|
|
'unsigned' => true,
|
|
|
|
'notnull' => true,
|
|
|
|
]);
|
2020-01-07 01:08:14 +03:00
|
|
|
$table->addColumn('tag_id', Types::BIGINT, [
|
2016-08-21 10:41:21 +03:00
|
|
|
'unsigned' => true,
|
|
|
|
'notnull' => true,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$table->addForeignKeyConstraint('tags', ['tag_id'], ['id'], [
|
|
|
|
'onDelete' => 'CASCADE',
|
|
|
|
'onUpdate' => 'RESTRICT',
|
|
|
|
]);
|
|
|
|
$table->addForeignKeyConstraint('short_urls', ['short_url_id'], ['id'], [
|
|
|
|
'onDelete' => 'CASCADE',
|
|
|
|
'onUpdate' => 'RESTRICT',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$table->setPrimaryKey(['short_url_id', 'tag_id']);
|
|
|
|
}
|
|
|
|
|
2019-03-09 20:45:58 +03:00
|
|
|
public function down(Schema $schema): void
|
2016-08-21 10:41:21 +03:00
|
|
|
{
|
|
|
|
$schema->dropTable('short_urls_in_tags');
|
|
|
|
$schema->dropTable('tags');
|
|
|
|
}
|
|
|
|
}
|