Skip to content

Commit 2bfe3b8

Browse files
committed
add migrations for new docker build
1 parent 8d76a11 commit 2bfe3b8

24 files changed

Lines changed: 6482 additions & 10160 deletions

apps/web/app/api/selfhosted/checkout/route.ts

Lines changed: 0 additions & 55 deletions
This file was deleted.

apps/web/app/api/selfhosted/migrations/route.ts

Lines changed: 0 additions & 50 deletions
This file was deleted.

apps/web/instrumentation.node.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@ import {
88
PutBucketPolicyCommand,
99
S3Client,
1010
} from "@aws-sdk/client-s3";
11-
import { db } from "@cap/database";
1211
import { buildEnv, serverEnv } from "@cap/env";
13-
import { migrate } from "drizzle-orm/mysql2/migrator";
14-
import path from "path";
12+
import { migrateDb } from "@cap/database/migrate";
1513

1614
export async function register() {
1715
if (process.env.NEXT_PUBLIC_IS_CAP) return;
@@ -92,9 +90,8 @@ async function runMigrations() {
9290
console.log("🔍 DB migrations triggered");
9391
console.log("💿 Running DB migrations...");
9492

95-
await migrate(db() as any, {
96-
migrationsFolder: path.join(process.cwd(), "/migrations"),
97-
});
93+
await migrateDb()
94+
9895
console.log("💿 Migrations run successfully!");
9996
} catch (error) {
10097
console.error("🚨 MIGRATION_FAILED", { error });

apps/web/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"dev": "dotenv -e ../../.env -- next dev",
77
"build": "next build --turbopack",
88
"build:web": "next build --turbopack",
9-
"build:web:docker": "cd ../.. && docker build -t cap-web-docker . --no-cache --progress=plain",
9+
"build:docker": "cd ../.. && docker build -t cap-web-docker . --no-cache --progress=plain",
1010
"start": "next start",
1111
"compress-images": "bash tools/compress-images.sh"
1212
},

package.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
"scripts": {
44
"build": "dotenv -e .env -- turbo run build",
55
"build:web": "turbo run build:web",
6-
"build:web:docker": "pnpm run --filter=@cap/web build:web:docker",
6+
"build:web:docker": "pnpm run --filter=@cap/web build:docker",
77
"cap-setup": "dotenv -e .env -- node scripts/setup.js",
8-
"db:generate": "dotenv -e .env -- pnpm --dir packages/database db:generate",
9-
"db:push": "dotenv -e .env -- pnpm --dir packages/database db:push",
10-
"db:studio": "dotenv -e .env -- pnpm --dir packages/database db:studio",
8+
"db:generate": "dotenv -e .env -- pnpm --filter @cap/database db:generate",
9+
"db:push": "dotenv -e .env -- pnpm --filter @cap/database db:push",
10+
"db:studio": "dotenv -e .env -- pnpm --filter @cap/database db:studio",
1111
"dev": "(pnpm run docker:up > /dev/null &) && sleep 5 && trap 'pnpm run docker:stop' EXIT && dotenv -e .env -- turbo run dev --env-mode=loose --ui tui",
1212
"dev:desktop": "pnpm run --filter=@cap/desktop dev",
1313
"dev:manual": "pnpm run docker:up && trap 'pnpm run docker:stop' EXIT && dotenv -e .env -- turbo run dev --filter=!@cap/storybook --no-cache --concurrency 1",
@@ -24,7 +24,8 @@
2424
"env-setup": "node scripts/env-cli.js",
2525
"check-tauri-versions": "node scripts/check-tauri-plugin-versions.js",
2626
"clean": "find . -name node_modules -o -name .next -o -name .output -o -name .turbo -o -name dist -type d -prune | xargs rm -rf",
27-
"lgtm-otel": "docker run -p 3010:3000 -p 4317:4317 -p 4318:4318 --rm -it docker.io/grafana/otel-lgtm"
27+
"lgtm-otel": "docker run -p 3010:3000 -p 4317:4317 -p 4318:4318 --rm -it docker.io/grafana/otel-lgtm",
28+
"with-env": "dotenv -e .env --"
2829
},
2930
"devDependencies": {
3031
"@biomejs/biome": "2.2.0",

packages/database/migrate.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { migrate } from "drizzle-orm/mysql2/migrator";
2+
import { db } from "@cap/database";
3+
import path from "node:path";
4+
import { DrizzleQueryError } from "drizzle-orm";
5+
6+
import { runOrgIdBackfill } from "./migrations/orgid_backfill.ts";
7+
8+
async function runMigrate() {
9+
await migrate(db() as any, {
10+
migrationsFolder: path.join(process.cwd(), "/migrations"),
11+
});
12+
}
13+
14+
function errorIsOrgIdMigration(e: unknown): e is DrizzleQueryError {
15+
return e instanceof DrizzleQueryError &&
16+
e.query === "ALTER TABLE `videos` MODIFY COLUMN `orgId` varchar(15) NOT NULL;";
17+
}
18+
19+
export async function migrateDb() {
20+
runMigrate()
21+
.catch(async (e) => {
22+
if(errorIsOrgIdMigration(e)) {
23+
console.log("non-null videos.orgId migration failed, running backfill");
24+
25+
await runOrgIdBackfill();
26+
await runMigrate()
27+
} else throw e
28+
}).catch(e =>{
29+
if(errorIsOrgIdMigration(e)) throw new Error("videos.orgId backfill failed, you will need to manually update the videos.orgId column before attempting to migrate again.")
30+
})
31+
}

packages/database/migrations/0003_outstanding_kylun.sql

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
CREATE TABLE `folders` (
2+
`id` varchar(15) NOT NULL,
3+
`name` varchar(255) NOT NULL,
4+
`color` varchar(16) NOT NULL DEFAULT 'normal',
5+
`organizationId` varchar(15) NOT NULL,
6+
`createdById` varchar(15) NOT NULL,
7+
`parentId` varchar(15),
8+
`spaceId` varchar(15),
9+
`createdAt` timestamp NOT NULL DEFAULT (now()),
10+
`updatedAt` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
11+
CONSTRAINT `folders_id` PRIMARY KEY(`id`),
12+
CONSTRAINT `folders_id_unique` UNIQUE(`id`)
13+
);
14+
--> statement-breakpoint
15+
CREATE TABLE `imported_videos` (
16+
`id` varchar(15) NOT NULL,
17+
`orgId` varchar(15) NOT NULL,
18+
`source` varchar(255) NOT NULL,
19+
`source_id` varchar(255) NOT NULL,
20+
CONSTRAINT `imported_videos_orgId_source_source_id_pk` PRIMARY KEY(`orgId`,`source`,`source_id`)
21+
);
22+
--> statement-breakpoint
23+
CREATE TABLE `notifications` (
24+
`id` varchar(15) NOT NULL,
25+
`orgId` varchar(15) NOT NULL,
26+
`recipientId` varchar(15) NOT NULL,
27+
`type` varchar(10) NOT NULL,
28+
`data` json NOT NULL,
29+
`readAt` timestamp,
30+
`createdAt` timestamp NOT NULL DEFAULT (now()),
31+
CONSTRAINT `notifications_id` PRIMARY KEY(`id`),
32+
CONSTRAINT `notifications_id_unique` UNIQUE(`id`)
33+
);
34+
--> statement-breakpoint
35+
CREATE TABLE `video_uploads` (
36+
`video_id` varchar(15) NOT NULL,
37+
`uploaded` int NOT NULL DEFAULT 0,
38+
`total` int NOT NULL DEFAULT 0,
39+
`started_at` timestamp NOT NULL DEFAULT (now()),
40+
`updated_at` timestamp NOT NULL DEFAULT (now()),
41+
`mode` varchar(255),
42+
CONSTRAINT `video_uploads_video_id` PRIMARY KEY(`video_id`)
43+
);
44+
--> statement-breakpoint
45+
ALTER TABLE `organizations` ADD `settings` json;--> statement-breakpoint
46+
ALTER TABLE `shared_videos` ADD `folderId` varchar(15);--> statement-breakpoint
47+
ALTER TABLE `space_videos` ADD `folderId` varchar(15);--> statement-breakpoint
48+
ALTER TABLE `users` ADD `preferences` json DEFAULT ('null');--> statement-breakpoint
49+
ALTER TABLE `users` ADD `onboardingSteps` json;--> statement-breakpoint
50+
ALTER TABLE `users` ADD `defaultOrgId` varchar(15);--> statement-breakpoint
51+
ALTER TABLE `videos` ADD `duration` float;--> statement-breakpoint
52+
ALTER TABLE `videos` ADD `width` int;--> statement-breakpoint
53+
ALTER TABLE `videos` ADD `height` int;--> statement-breakpoint
54+
ALTER TABLE `videos` ADD `fps` int;--> statement-breakpoint
55+
ALTER TABLE `videos` ADD `settings` json;--> statement-breakpoint
56+
ALTER TABLE `videos` ADD `folderId` varchar(15);--> statement-breakpoint
57+
ALTER TABLE `space_members` ADD CONSTRAINT `space_id_user_id_unique` UNIQUE(`spaceId`,`userId`);--> statement-breakpoint
58+
CREATE INDEX `organization_id_idx` ON `folders` (`organizationId`);--> statement-breakpoint
59+
CREATE INDEX `created_by_id_idx` ON `folders` (`createdById`);--> statement-breakpoint
60+
CREATE INDEX `parent_id_idx` ON `folders` (`parentId`);--> statement-breakpoint
61+
CREATE INDEX `space_id_idx` ON `folders` (`spaceId`);--> statement-breakpoint
62+
CREATE INDEX `recipient_id_idx` ON `notifications` (`recipientId`);--> statement-breakpoint
63+
CREATE INDEX `org_id_idx` ON `notifications` (`orgId`);--> statement-breakpoint
64+
CREATE INDEX `type_idx` ON `notifications` (`type`);--> statement-breakpoint
65+
CREATE INDEX `read_at_idx` ON `notifications` (`readAt`);--> statement-breakpoint
66+
CREATE INDEX `created_at_idx` ON `notifications` (`createdAt`);--> statement-breakpoint
67+
CREATE INDEX `recipient_read_idx` ON `notifications` (`recipientId`,`readAt`);--> statement-breakpoint
68+
CREATE INDEX `recipient_created_idx` ON `notifications` (`recipientId`,`createdAt`);--> statement-breakpoint
69+
CREATE INDEX `folder_id_idx` ON `shared_videos` (`folderId`);--> statement-breakpoint
70+
CREATE INDEX `video_id_folder_id_idx` ON `shared_videos` (`videoId`,`folderId`);--> statement-breakpoint
71+
CREATE INDEX `folder_id_idx` ON `space_videos` (`folderId`);--> statement-breakpoint
72+
CREATE INDEX `folder_id_idx` ON `videos` (`folderId`);

packages/database/migrations/0004_optimal_eddie_brock.sql

Lines changed: 0 additions & 2 deletions
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE `videos` ADD `orgId` varchar(15);

0 commit comments

Comments
 (0)