From 45025139992ac8168af70fbf913ee0a6d735ff1e Mon Sep 17 00:00:00 2001 From: Pontus Abrahamsson Date: Mon, 30 Dec 2024 14:07:09 +0100 Subject: [PATCH] Cascade --- .github/workflows/production-api.yml | 2 +- apps/api/drizzle/0002_hesitant_amazoness.sql | 29 ++ apps/api/drizzle/meta/0002_snapshot.json | 276 +++++++++++++++++++ apps/api/drizzle/meta/_journal.json | 7 + apps/api/src/db/schema.ts | 4 +- 5 files changed, 315 insertions(+), 3 deletions(-) create mode 100644 apps/api/drizzle/0002_hesitant_amazoness.sql create mode 100644 apps/api/drizzle/meta/0002_snapshot.json diff --git a/.github/workflows/production-api.yml b/.github/workflows/production-api.yml index bc0a2fa..ad5669d 100644 --- a/.github/workflows/production-api.yml +++ b/.github/workflows/production-api.yml @@ -29,7 +29,7 @@ jobs: apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} workingDirectory: "apps/api" wranglerVersion: "latest" - command: d1 migrations apply languine-production --env production + command: d1 migrations apply languine-production --env production --remote - name: 🚀 Deploy Project Artifacts to Cloudflare uses: cloudflare/wrangler-action@v3 with: diff --git a/apps/api/drizzle/0002_hesitant_amazoness.sql b/apps/api/drizzle/0002_hesitant_amazoness.sql new file mode 100644 index 0000000..aa9c472 --- /dev/null +++ b/apps/api/drizzle/0002_hesitant_amazoness.sql @@ -0,0 +1,29 @@ +PRAGMA foreign_keys=OFF;--> statement-breakpoint +CREATE TABLE `__new_projects` ( + `id` text PRIMARY KEY NOT NULL, + `team_id` text NOT NULL, + `name` text NOT NULL, + `description` text, + `created_at` text DEFAULT CURRENT_TIMESTAMP NOT NULL, + `updated_at` text DEFAULT CURRENT_TIMESTAMP NOT NULL, + FOREIGN KEY (`team_id`) REFERENCES `teams`(`id`) ON UPDATE no action ON DELETE cascade +); +--> statement-breakpoint +INSERT INTO `__new_projects`("id", "team_id", "name", "description", "created_at", "updated_at") SELECT "id", "team_id", "name", "description", "created_at", "updated_at" FROM `projects`;--> statement-breakpoint +DROP TABLE `projects`;--> statement-breakpoint +ALTER TABLE `__new_projects` RENAME TO `projects`;--> statement-breakpoint +PRAGMA foreign_keys=ON;--> statement-breakpoint +CREATE TABLE `__new_team_members` ( + `id` text PRIMARY KEY NOT NULL, + `team_id` text NOT NULL, + `user_id` text NOT NULL, + `role` text NOT NULL, + `created_at` text DEFAULT CURRENT_TIMESTAMP NOT NULL, + `updated_at` text DEFAULT CURRENT_TIMESTAMP NOT NULL, + FOREIGN KEY (`team_id`) REFERENCES `teams`(`id`) ON UPDATE no action ON DELETE cascade, + FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE no action +); +--> statement-breakpoint +INSERT INTO `__new_team_members`("id", "team_id", "user_id", "role", "created_at", "updated_at") SELECT "id", "team_id", "user_id", "role", "created_at", "updated_at" FROM `team_members`;--> statement-breakpoint +DROP TABLE `team_members`;--> statement-breakpoint +ALTER TABLE `__new_team_members` RENAME TO `team_members`; \ No newline at end of file diff --git a/apps/api/drizzle/meta/0002_snapshot.json b/apps/api/drizzle/meta/0002_snapshot.json new file mode 100644 index 0000000..6b596c9 --- /dev/null +++ b/apps/api/drizzle/meta/0002_snapshot.json @@ -0,0 +1,276 @@ +{ + "version": "6", + "dialect": "sqlite", + "id": "c1e50742-3003-4e1c-82e0-237e3e6c2876", + "prevId": "358d4d4a-8540-4b87-b0d4-478cbea7c2fc", + "tables": { + "projects": { + "name": "projects", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "team_id": { + "name": "team_id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "description": { + "name": "description", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "created_at": { + "name": "created_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "CURRENT_TIMESTAMP" + }, + "updated_at": { + "name": "updated_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "CURRENT_TIMESTAMP" + } + }, + "indexes": {}, + "foreignKeys": { + "projects_team_id_teams_id_fk": { + "name": "projects_team_id_teams_id_fk", + "tableFrom": "projects", + "tableTo": "teams", + "columnsFrom": [ + "team_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "team_members": { + "name": "team_members", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "team_id": { + "name": "team_id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "user_id": { + "name": "user_id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "role": { + "name": "role", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "created_at": { + "name": "created_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "CURRENT_TIMESTAMP" + }, + "updated_at": { + "name": "updated_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "CURRENT_TIMESTAMP" + } + }, + "indexes": {}, + "foreignKeys": { + "team_members_team_id_teams_id_fk": { + "name": "team_members_team_id_teams_id_fk", + "tableFrom": "team_members", + "tableTo": "teams", + "columnsFrom": [ + "team_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "team_members_user_id_users_id_fk": { + "name": "team_members_user_id_users_id_fk", + "tableFrom": "team_members", + "tableTo": "users", + "columnsFrom": [ + "user_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "teams": { + "name": "teams", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "created_at": { + "name": "created_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "CURRENT_TIMESTAMP" + }, + "updated_at": { + "name": "updated_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "CURRENT_TIMESTAMP" + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "users": { + "name": "users", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "email": { + "name": "email", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "avatar": { + "name": "avatar", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "provider": { + "name": "provider", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "created_at": { + "name": "created_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "CURRENT_TIMESTAMP" + }, + "updated_at": { + "name": "updated_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "CURRENT_TIMESTAMP" + } + }, + "indexes": { + "users_email_unique": { + "name": "users_email_unique", + "columns": [ + "email" + ], + "isUnique": true + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + } + }, + "views": {}, + "enums": {}, + "_meta": { + "schemas": {}, + "tables": {}, + "columns": {} + }, + "internal": { + "indexes": {} + } +} \ No newline at end of file diff --git a/apps/api/drizzle/meta/_journal.json b/apps/api/drizzle/meta/_journal.json index fcce48f..973fb5e 100644 --- a/apps/api/drizzle/meta/_journal.json +++ b/apps/api/drizzle/meta/_journal.json @@ -15,6 +15,13 @@ "when": 1735563592170, "tag": "0001_warm_pepper_potts", "breakpoints": true + }, + { + "idx": 2, + "version": "6", + "when": 1735563924272, + "tag": "0002_hesitant_amazoness", + "breakpoints": true } ] } \ No newline at end of file diff --git a/apps/api/src/db/schema.ts b/apps/api/src/db/schema.ts index 98bdc54..9523e5a 100644 --- a/apps/api/src/db/schema.ts +++ b/apps/api/src/db/schema.ts @@ -22,7 +22,7 @@ export const teamMembers = sqliteTable("team_members", { id: text("id").primaryKey(), teamId: text("team_id") .notNull() - .references(() => teams.id), + .references(() => teams.id, { onDelete: "cascade" }), userId: text("user_id") .notNull() .references(() => users.id), @@ -35,7 +35,7 @@ export const projects = sqliteTable("projects", { id: text("id").primaryKey(), teamId: text("team_id") .notNull() - .references(() => teams.id), + .references(() => teams.id, { onDelete: "cascade" }), name: text("name").notNull(), description: text("description"), createdAt: text("created_at").notNull().default(sql`CURRENT_TIMESTAMP`),