mirror of
https://github.com/shlinkio/shlink.git
synced 2024-12-22 23:23:42 -06:00
Created migrations to rename camel case columns to snake case
This commit is contained in:
parent
46482522bb
commit
26fd61a3ed
1
build.sh
1
build.sh
@ -18,6 +18,7 @@ rm -rf "${builtcontent}"
|
|||||||
mkdir -p "${builtcontent}"
|
mkdir -p "${builtcontent}"
|
||||||
rsync -av * "${builtcontent}" \
|
rsync -av * "${builtcontent}" \
|
||||||
--exclude=data/infra \
|
--exclude=data/infra \
|
||||||
|
--exclude=data/migrations_template.txt \
|
||||||
--exclude=**/.gitignore \
|
--exclude=**/.gitignore \
|
||||||
--exclude=CHANGELOG.md \
|
--exclude=CHANGELOG.md \
|
||||||
--exclude=composer.lock \
|
--exclude=composer.lock \
|
||||||
|
68
data/migrations/Version20181020060559.php
Normal file
68
data/migrations/Version20181020060559.php
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace ShlinkMigrations;
|
||||||
|
|
||||||
|
use Doctrine\DBAL\DBALException;
|
||||||
|
use Doctrine\DBAL\Schema\Schema;
|
||||||
|
use Doctrine\DBAL\Schema\SchemaException;
|
||||||
|
use Doctrine\DBAL\Schema\Table;
|
||||||
|
use Doctrine\DBAL\Types\Type;
|
||||||
|
use Doctrine\Migrations\AbstractMigration;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Auto-generated Migration: Please modify to your needs!
|
||||||
|
*/
|
||||||
|
final class Version20181020060559 extends AbstractMigration
|
||||||
|
{
|
||||||
|
private const COLUMNS = [
|
||||||
|
'countryCode' => 'country_code',
|
||||||
|
'countryName' => 'country_name',
|
||||||
|
'regionName' => 'region_name',
|
||||||
|
'cityName' => 'city_name',
|
||||||
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Schema $schema
|
||||||
|
* @throws SchemaException
|
||||||
|
*/
|
||||||
|
public function up(Schema $schema): void
|
||||||
|
{
|
||||||
|
$this->createColumns($schema->getTable('visit_locations'), self::COLUMNS);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function createColumns(Table $visitLocations, array $columnNames): void
|
||||||
|
{
|
||||||
|
foreach ($columnNames as $name) {
|
||||||
|
if (! $visitLocations->hasColumn($name)) {
|
||||||
|
$visitLocations->addColumn($name, Type::STRING, ['notnull' => false]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws SchemaException
|
||||||
|
* @throws DBALException
|
||||||
|
*/
|
||||||
|
public function postUp(Schema $schema): void
|
||||||
|
{
|
||||||
|
$visitLocations = $schema->getTable('visit_locations');
|
||||||
|
|
||||||
|
// If the camel case columns do not exist, do nothing
|
||||||
|
if (! $visitLocations->hasColumn('countryCode')) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$qb = $this->connection->createQueryBuilder();
|
||||||
|
$qb->update('visit_locations');
|
||||||
|
foreach (self::COLUMNS as $camelCaseName => $snakeCaseName) {
|
||||||
|
$qb->set($snakeCaseName, $camelCaseName);
|
||||||
|
}
|
||||||
|
$qb->execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function down(Schema $schema): void
|
||||||
|
{
|
||||||
|
// No down
|
||||||
|
}
|
||||||
|
}
|
41
data/migrations/Version20181020065148.php
Normal file
41
data/migrations/Version20181020065148.php
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace ShlinkMigrations;
|
||||||
|
|
||||||
|
use Doctrine\DBAL\Schema\Schema;
|
||||||
|
use Doctrine\DBAL\Schema\SchemaException;
|
||||||
|
use Doctrine\DBAL\Schema\Table;
|
||||||
|
use Doctrine\Migrations\AbstractMigration;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Auto-generated Migration: Please modify to your needs!
|
||||||
|
*/
|
||||||
|
final class Version20181020065148 extends AbstractMigration
|
||||||
|
{
|
||||||
|
private const CAMEL_CASE_COLUMNS = [
|
||||||
|
'countryCode',
|
||||||
|
'countryName',
|
||||||
|
'regionName',
|
||||||
|
'cityName',
|
||||||
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws SchemaException
|
||||||
|
*/
|
||||||
|
public function up(Schema $schema): void
|
||||||
|
{
|
||||||
|
$visitLocations = $schema->getTable('visit_locations');
|
||||||
|
|
||||||
|
foreach (self::CAMEL_CASE_COLUMNS as $name) {
|
||||||
|
if ($visitLocations->hasColumn($name)) {
|
||||||
|
$visitLocations->dropColumn($name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function down(Schema $schema): void
|
||||||
|
{
|
||||||
|
// No down
|
||||||
|
}
|
||||||
|
}
|
23
data/migrations_template.txt
Normal file
23
data/migrations_template.txt
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace <namespace>;
|
||||||
|
|
||||||
|
use Doctrine\DBAL\Schema\Schema;
|
||||||
|
use Doctrine\Migrations\AbstractMigration;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Auto-generated Migration: Please modify to your needs!
|
||||||
|
*/
|
||||||
|
final class Version<version> extends AbstractMigration
|
||||||
|
{
|
||||||
|
public function up(Schema $schema): void
|
||||||
|
{
|
||||||
|
<up>
|
||||||
|
}
|
||||||
|
|
||||||
|
public function down(Schema $schema): void
|
||||||
|
{
|
||||||
|
<down>
|
||||||
|
}
|
||||||
|
}
|
@ -2,3 +2,4 @@ name: ShlinkMigrations
|
|||||||
migrations_namespace: ShlinkMigrations
|
migrations_namespace: ShlinkMigrations
|
||||||
table_name: migrations
|
table_name: migrations
|
||||||
migrations_directory: data/migrations
|
migrations_directory: data/migrations
|
||||||
|
custom_template: data/migrations_template.txt
|
||||||
|
Loading…
Reference in New Issue
Block a user