chore(migrations): normaliser le schema
This commit is contained in:
28
migrations/Version20261120120000.php
Normal file
28
migrations/Version20261120120000.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace DoctrineMigrations;
|
||||
|
||||
use Doctrine\DBAL\Schema\Schema;
|
||||
use Doctrine\Migrations\AbstractMigration;
|
||||
|
||||
final class Version20261120120000 extends AbstractMigration
|
||||
{
|
||||
public function getDescription(): string
|
||||
{
|
||||
return 'Add email column to profiles when missing.';
|
||||
}
|
||||
|
||||
public function up(Schema $schema): void
|
||||
{
|
||||
$this->addSql('ALTER TABLE profiles ADD COLUMN IF NOT EXISTS email VARCHAR(180) DEFAULT NULL');
|
||||
$this->addSql('CREATE UNIQUE INDEX IF NOT EXISTS uniq_profiles_email ON profiles (email)');
|
||||
}
|
||||
|
||||
public function down(Schema $schema): void
|
||||
{
|
||||
$this->addSql('DROP INDEX IF EXISTS uniq_profiles_email');
|
||||
$this->addSql('ALTER TABLE profiles DROP COLUMN IF EXISTS email');
|
||||
}
|
||||
}
|
||||
28
migrations/Version20261120123000.php
Normal file
28
migrations/Version20261120123000.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace DoctrineMigrations;
|
||||
|
||||
use Doctrine\DBAL\Schema\Schema;
|
||||
use Doctrine\Migrations\AbstractMigration;
|
||||
|
||||
final class Version20261120123000 extends AbstractMigration
|
||||
{
|
||||
public function getDescription(): string
|
||||
{
|
||||
return 'Ensure profiles.email exists (camelCase schema).';
|
||||
}
|
||||
|
||||
public function up(Schema $schema): void
|
||||
{
|
||||
$this->addSql('ALTER TABLE public.profiles ADD COLUMN IF NOT EXISTS email VARCHAR(180) DEFAULT NULL');
|
||||
$this->addSql('CREATE UNIQUE INDEX IF NOT EXISTS uniq_profiles_email ON public.profiles (email)');
|
||||
}
|
||||
|
||||
public function down(Schema $schema): void
|
||||
{
|
||||
$this->addSql('DROP INDEX IF EXISTS uniq_profiles_email');
|
||||
$this->addSql('ALTER TABLE public.profiles DROP COLUMN IF EXISTS email');
|
||||
}
|
||||
}
|
||||
94
migrations/Version20261120124000.php
Normal file
94
migrations/Version20261120124000.php
Normal file
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace DoctrineMigrations;
|
||||
|
||||
use Doctrine\DBAL\Schema\Schema;
|
||||
use Doctrine\Migrations\AbstractMigration;
|
||||
|
||||
final class Version20261120124000 extends AbstractMigration
|
||||
{
|
||||
public function getDescription(): string
|
||||
{
|
||||
return 'Normalize profiles columns to camelCase (quoted identifiers).';
|
||||
}
|
||||
|
||||
public function up(Schema $schema): void
|
||||
{
|
||||
$this->addSql(<<<'SQL'
|
||||
DO $$
|
||||
BEGIN
|
||||
IF EXISTS (
|
||||
SELECT 1 FROM information_schema.columns
|
||||
WHERE table_schema = 'public' AND table_name = 'profiles' AND column_name = 'firstname'
|
||||
) THEN
|
||||
ALTER TABLE public.profiles RENAME COLUMN firstname TO "firstName";
|
||||
END IF;
|
||||
IF EXISTS (
|
||||
SELECT 1 FROM information_schema.columns
|
||||
WHERE table_schema = 'public' AND table_name = 'profiles' AND column_name = 'lastname'
|
||||
) THEN
|
||||
ALTER TABLE public.profiles RENAME COLUMN lastname TO "lastName";
|
||||
END IF;
|
||||
IF EXISTS (
|
||||
SELECT 1 FROM information_schema.columns
|
||||
WHERE table_schema = 'public' AND table_name = 'profiles' AND column_name = 'isactive'
|
||||
) THEN
|
||||
ALTER TABLE public.profiles RENAME COLUMN isactive TO "isActive";
|
||||
END IF;
|
||||
IF EXISTS (
|
||||
SELECT 1 FROM information_schema.columns
|
||||
WHERE table_schema = 'public' AND table_name = 'profiles' AND column_name = 'createdat'
|
||||
) THEN
|
||||
ALTER TABLE public.profiles RENAME COLUMN createdat TO "createdAt";
|
||||
END IF;
|
||||
IF EXISTS (
|
||||
SELECT 1 FROM information_schema.columns
|
||||
WHERE table_schema = 'public' AND table_name = 'profiles' AND column_name = 'updatedat'
|
||||
) THEN
|
||||
ALTER TABLE public.profiles RENAME COLUMN updatedat TO "updatedAt";
|
||||
END IF;
|
||||
END $$;
|
||||
SQL);
|
||||
}
|
||||
|
||||
public function down(Schema $schema): void
|
||||
{
|
||||
$this->addSql(<<<'SQL'
|
||||
DO $$
|
||||
BEGIN
|
||||
IF EXISTS (
|
||||
SELECT 1 FROM information_schema.columns
|
||||
WHERE table_schema = 'public' AND table_name = 'profiles' AND column_name = 'firstName'
|
||||
) THEN
|
||||
ALTER TABLE public.profiles RENAME COLUMN "firstName" TO firstname;
|
||||
END IF;
|
||||
IF EXISTS (
|
||||
SELECT 1 FROM information_schema.columns
|
||||
WHERE table_schema = 'public' AND table_name = 'profiles' AND column_name = 'lastName'
|
||||
) THEN
|
||||
ALTER TABLE public.profiles RENAME COLUMN "lastName" TO lastname;
|
||||
END IF;
|
||||
IF EXISTS (
|
||||
SELECT 1 FROM information_schema.columns
|
||||
WHERE table_schema = 'public' AND table_name = 'profiles' AND column_name = 'isActive'
|
||||
) THEN
|
||||
ALTER TABLE public.profiles RENAME COLUMN "isActive" TO isactive;
|
||||
END IF;
|
||||
IF EXISTS (
|
||||
SELECT 1 FROM information_schema.columns
|
||||
WHERE table_schema = 'public' AND table_name = 'profiles' AND column_name = 'createdAt'
|
||||
) THEN
|
||||
ALTER TABLE public.profiles RENAME COLUMN "createdAt" TO createdat;
|
||||
END IF;
|
||||
IF EXISTS (
|
||||
SELECT 1 FROM information_schema.columns
|
||||
WHERE table_schema = 'public' AND table_name = 'profiles' AND column_name = 'updatedAt'
|
||||
) THEN
|
||||
ALTER TABLE public.profiles RENAME COLUMN "updatedAt" TO updatedat;
|
||||
END IF;
|
||||
END $$;
|
||||
SQL);
|
||||
}
|
||||
}
|
||||
32
migrations/Version20261120125000.php
Normal file
32
migrations/Version20261120125000.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace DoctrineMigrations;
|
||||
|
||||
use Doctrine\DBAL\Schema\Schema;
|
||||
use Doctrine\Migrations\AbstractMigration;
|
||||
|
||||
final class Version20261120125000 extends AbstractMigration
|
||||
{
|
||||
public function getDescription(): string
|
||||
{
|
||||
return 'Add missing profile auth columns (email, roles, password).';
|
||||
}
|
||||
|
||||
public function up(Schema $schema): void
|
||||
{
|
||||
$this->addSql('ALTER TABLE public.profiles ADD COLUMN IF NOT EXISTS email VARCHAR(180) DEFAULT NULL');
|
||||
$this->addSql('ALTER TABLE public.profiles ADD COLUMN IF NOT EXISTS roles JSON DEFAULT \'["ROLE_USER"]\' NOT NULL');
|
||||
$this->addSql('ALTER TABLE public.profiles ADD COLUMN IF NOT EXISTS password VARCHAR(255) DEFAULT NULL');
|
||||
$this->addSql('CREATE UNIQUE INDEX IF NOT EXISTS uniq_profiles_email ON public.profiles (email)');
|
||||
}
|
||||
|
||||
public function down(Schema $schema): void
|
||||
{
|
||||
$this->addSql('DROP INDEX IF EXISTS uniq_profiles_email');
|
||||
$this->addSql('ALTER TABLE public.profiles DROP COLUMN IF EXISTS password');
|
||||
$this->addSql('ALTER TABLE public.profiles DROP COLUMN IF EXISTS roles');
|
||||
$this->addSql('ALTER TABLE public.profiles DROP COLUMN IF EXISTS email');
|
||||
}
|
||||
}
|
||||
66
migrations/Version20261120131000.php
Normal file
66
migrations/Version20261120131000.php
Normal file
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace DoctrineMigrations;
|
||||
|
||||
use Doctrine\DBAL\Schema\Schema;
|
||||
use Doctrine\Migrations\AbstractMigration;
|
||||
|
||||
final class Version20261120131000 extends AbstractMigration
|
||||
{
|
||||
public function getDescription(): string
|
||||
{
|
||||
return 'Normalize public schema identifiers to lowercase (tables + columns).';
|
||||
}
|
||||
|
||||
public function up(Schema $schema): void
|
||||
{
|
||||
$this->addSql(<<<'SQL'
|
||||
DO $$
|
||||
DECLARE
|
||||
r RECORD;
|
||||
BEGIN
|
||||
-- Special-case legacy table name from Prisma.
|
||||
IF EXISTS (
|
||||
SELECT 1 FROM information_schema.tables
|
||||
WHERE table_schema = 'public' AND table_name = 'ModelType'
|
||||
) THEN
|
||||
EXECUTE 'ALTER TABLE public."ModelType" RENAME TO model_types';
|
||||
END IF;
|
||||
|
||||
-- Rename columns containing uppercase letters.
|
||||
FOR r IN
|
||||
SELECT table_name, column_name
|
||||
FROM information_schema.columns
|
||||
WHERE table_schema = 'public' AND column_name ~ '[A-Z]'
|
||||
LOOP
|
||||
EXECUTE format(
|
||||
'ALTER TABLE public.%I RENAME COLUMN %I TO %I',
|
||||
r.table_name,
|
||||
r.column_name,
|
||||
lower(r.column_name)
|
||||
);
|
||||
END LOOP;
|
||||
|
||||
-- Rename tables containing uppercase letters.
|
||||
FOR r IN
|
||||
SELECT table_name
|
||||
FROM information_schema.tables
|
||||
WHERE table_schema = 'public' AND table_name ~ '[A-Z]'
|
||||
LOOP
|
||||
EXECUTE format(
|
||||
'ALTER TABLE public.%I RENAME TO %I',
|
||||
r.table_name,
|
||||
lower(r.table_name)
|
||||
);
|
||||
END LOOP;
|
||||
END $$;
|
||||
SQL);
|
||||
}
|
||||
|
||||
public function down(Schema $schema): void
|
||||
{
|
||||
// Irreversible: cannot restore original casing reliably.
|
||||
}
|
||||
}
|
||||
26
migrations/Version20261120140000.php
Normal file
26
migrations/Version20261120140000.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace DoctrineMigrations;
|
||||
|
||||
use Doctrine\DBAL\Schema\Schema;
|
||||
use Doctrine\Migrations\AbstractMigration;
|
||||
|
||||
final class Version20261120140000 extends AbstractMigration
|
||||
{
|
||||
public function getDescription(): string
|
||||
{
|
||||
return 'Convert custom_fields.options from text[] to json.';
|
||||
}
|
||||
|
||||
public function up(Schema $schema): void
|
||||
{
|
||||
$this->addSql("ALTER TABLE custom_fields ALTER COLUMN options TYPE JSON USING to_json(options)");
|
||||
}
|
||||
|
||||
public function down(Schema $schema): void
|
||||
{
|
||||
$this->addSql("ALTER TABLE custom_fields ALTER COLUMN options TYPE TEXT[] USING ARRAY(SELECT json_array_elements_text(options))");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user