feat(constructeurs): introduce constructors management
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
-- Create constructeurs table
|
||||
CREATE TABLE "constructeurs" (
|
||||
"id" TEXT PRIMARY KEY,
|
||||
"name" TEXT NOT NULL,
|
||||
"email" TEXT,
|
||||
"phone" TEXT,
|
||||
"createdAt" TIMESTAMP NOT NULL DEFAULT NOW(),
|
||||
"updatedAt" TIMESTAMP NOT NULL DEFAULT NOW()
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX "constructeurs_name_unique_idx" ON "constructeurs"(LOWER("name"));
|
||||
|
||||
-- Add foreign key columns to machines, composants, pieces
|
||||
ALTER TABLE "machines" ADD COLUMN "constructeurId" TEXT;
|
||||
ALTER TABLE "composants" ADD COLUMN "constructeurId" TEXT;
|
||||
ALTER TABLE "pieces" ADD COLUMN "constructeurId" TEXT;
|
||||
|
||||
-- Populate constructeurs from existing data
|
||||
WITH existing_names AS (
|
||||
SELECT DISTINCT TRIM(constructeur) AS name FROM "machines" WHERE constructeur IS NOT NULL AND TRIM(constructeur) <> ''
|
||||
UNION
|
||||
SELECT DISTINCT TRIM(constructeur) FROM "composants" WHERE constructeur IS NOT NULL AND TRIM(constructeur) <> ''
|
||||
UNION
|
||||
SELECT DISTINCT TRIM(constructeur) FROM "pieces" WHERE constructeur IS NOT NULL AND TRIM(constructeur) <> ''
|
||||
),
|
||||
prepared AS (
|
||||
SELECT DISTINCT ON (LOWER(name)) name
|
||||
FROM existing_names
|
||||
WHERE name IS NOT NULL AND name <> ''
|
||||
ORDER BY LOWER(name), name
|
||||
)
|
||||
INSERT INTO "constructeurs" ("id", "name")
|
||||
SELECT substr(md5(random()::text || clock_timestamp()::text), 1, 25), name
|
||||
FROM prepared
|
||||
ON CONFLICT (LOWER("name")) DO NOTHING;
|
||||
|
||||
-- Link existing records to constructeurs
|
||||
UPDATE "machines" m
|
||||
SET "constructeurId" = c.id
|
||||
FROM "constructeurs" c
|
||||
WHERE m.constructeur IS NOT NULL
|
||||
AND TRIM(m.constructeur) <> ''
|
||||
AND LOWER(c.name) = LOWER(TRIM(m.constructeur));
|
||||
|
||||
UPDATE "composants" co
|
||||
SET "constructeurId" = c.id
|
||||
FROM "constructeurs" c
|
||||
WHERE co.constructeur IS NOT NULL
|
||||
AND TRIM(co.constructeur) <> ''
|
||||
AND LOWER(c.name) = LOWER(TRIM(co.constructeur));
|
||||
|
||||
UPDATE "pieces" p
|
||||
SET "constructeurId" = c.id
|
||||
FROM "constructeurs" c
|
||||
WHERE p.constructeur IS NOT NULL
|
||||
AND TRIM(p.constructeur) <> ''
|
||||
AND LOWER(c.name) = LOWER(TRIM(p.constructeur));
|
||||
|
||||
-- Drop legacy constructeur columns
|
||||
ALTER TABLE "machines" DROP COLUMN "constructeur";
|
||||
ALTER TABLE "composants" DROP COLUMN "constructeur";
|
||||
ALTER TABLE "pieces" DROP COLUMN "constructeur";
|
||||
|
||||
-- Add foreign key constraints
|
||||
ALTER TABLE "machines"
|
||||
ADD CONSTRAINT "machines_constructeurId_fkey"
|
||||
FOREIGN KEY ("constructeurId") REFERENCES "constructeurs"("id")
|
||||
ON DELETE SET NULL ON UPDATE CASCADE;
|
||||
|
||||
ALTER TABLE "composants"
|
||||
ADD CONSTRAINT "composants_constructeurId_fkey"
|
||||
FOREIGN KEY ("constructeurId") REFERENCES "constructeurs"("id")
|
||||
ON DELETE SET NULL ON UPDATE CASCADE;
|
||||
|
||||
ALTER TABLE "pieces"
|
||||
ADD CONSTRAINT "pieces_constructeurId_fkey"
|
||||
FOREIGN KEY ("constructeurId") REFERENCES "constructeurs"("id")
|
||||
ON DELETE SET NULL ON UPDATE CASCADE;
|
||||
Reference in New Issue
Block a user