refactor: update

This commit is contained in:
Mauricio Siu
2025-02-15 20:06:33 -06:00
parent 8b71f963cc
commit 87b12ff6e9
9 changed files with 10740 additions and 84 deletions

View File

@@ -0,0 +1,2 @@
ALTER TABLE "project" ADD COLUMN "organizationId" text;--> statement-breakpoint
ALTER TABLE "project" ADD CONSTRAINT "project_organizationId_organization_id_fk" FOREIGN KEY ("organizationId") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;

View File

@@ -0,0 +1,27 @@
-- Custom SQL migration file, put your code below! --
-- Primero, actualizamos los proyectos con la organización del usuario
UPDATE "project" p
SET "organizationId" = (
SELECT m."organization_id"
FROM "member" m
WHERE m."user_id" = p."userId"
AND m."role" = 'owner'
LIMIT 1
)
WHERE p."organizationId" IS NULL;
-- Verificamos que todos los proyectos tengan una organización
DO $$
BEGIN
IF EXISTS (
SELECT 1
FROM "project"
WHERE "organizationId" IS NULL
) THEN
RAISE EXCEPTION 'Hay proyectos sin organización asignada';
END IF;
END $$;
-- Hacemos organization_id NOT NULL después de la migración
ALTER TABLE "project"
ALTER COLUMN "organizationId" SET NOT NULL;

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -491,6 +491,20 @@
"when": 1739664410814,
"tag": "0069_broad_ken_ellis",
"breakpoints": true
},
{
"idx": 70,
"version": "7",
"when": 1739671371444,
"tag": "0070_dusty_wind_dancer",
"breakpoints": true
},
{
"idx": 71,
"version": "7",
"when": 1739671387634,
"tag": "0071_migrate-data-projects",
"breakpoints": true
}
]
}

View File

@@ -124,3 +124,27 @@ await db
.catch((error) => {
console.error(error);
});
await db
.transaction(async (db) => {
const projects = await db.query.projects.findMany({
with: {
user: {
with: {
organizations: true,
},
},
},
});
for (const project of projects) {
const user = await db.update(schema.projects).set({
organizationId: project.user.organizations[0]?.id || "",
});
}
})
.then(() => {
console.log("Migration finished");
})
.catch((error) => {
console.error(error);
});