Files
Inventory/migrations/Version20261120131000.php

67 lines
1.6 KiB
PHP

<?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.
}
}