Skip to content

Commit

Permalink
fixed migrate script
Browse files Browse the repository at this point in the history
  • Loading branch information
trevorpfiz committed May 8, 2024
1 parent f37eae3 commit 00219bc
Show file tree
Hide file tree
Showing 11 changed files with 501 additions and 40 deletions.
31 changes: 31 additions & 0 deletions packages/db/migrations/0000_brainy_lila_cheney.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
CREATE TABLE IF NOT EXISTS "auth"."users" (
"id" uuid PRIMARY KEY NOT NULL
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "wc_profile" (
"id" uuid PRIMARY KEY NOT NULL,
"name" varchar(256) NOT NULL,
"image" varchar(256),
"email" varchar(256)
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "wc_report" (
"id" serial PRIMARY KEY NOT NULL,
"title" varchar(256) NOT NULL,
"content" text NOT NULL,
"profile_id" uuid NOT NULL,
"created_at" timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL,
"updated_at" timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL
);
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "wc_profile" ADD CONSTRAINT "wc_profile_id_users_id_fk" FOREIGN KEY ("id") REFERENCES "auth"."users"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "wc_report" ADD CONSTRAINT "wc_report_profile_id_wc_profile_id_fk" FOREIGN KEY ("profile_id") REFERENCES "wc_profile"("id") ON DELETE no action ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
142 changes: 142 additions & 0 deletions packages/db/migrations/meta/0000_snapshot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
{
"id": "225a9ba0-acad-4d5c-ac65-73a6d7d8af6a",
"prevId": "00000000-0000-0000-0000-000000000000",
"version": "5",
"dialect": "pg",
"tables": {
"users": {
"name": "users",
"schema": "auth",
"columns": {
"id": {
"name": "id",
"type": "uuid",
"primaryKey": true,
"notNull": true
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
},
"wc_profile": {
"name": "wc_profile",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "uuid",
"primaryKey": true,
"notNull": true
},
"name": {
"name": "name",
"type": "varchar(256)",
"primaryKey": false,
"notNull": true
},
"image": {
"name": "image",
"type": "varchar(256)",
"primaryKey": false,
"notNull": false
},
"email": {
"name": "email",
"type": "varchar(256)",
"primaryKey": false,
"notNull": false
}
},
"indexes": {},
"foreignKeys": {
"wc_profile_id_users_id_fk": {
"name": "wc_profile_id_users_id_fk",
"tableFrom": "wc_profile",
"tableTo": "users",
"schemaTo": "auth",
"columnsFrom": [
"id"
],
"columnsTo": [
"id"
],
"onDelete": "cascade",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
},
"wc_report": {
"name": "wc_report",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "serial",
"primaryKey": true,
"notNull": true
},
"title": {
"name": "title",
"type": "varchar(256)",
"primaryKey": false,
"notNull": true
},
"content": {
"name": "content",
"type": "text",
"primaryKey": false,
"notNull": true
},
"profile_id": {
"name": "profile_id",
"type": "uuid",
"primaryKey": false,
"notNull": true
},
"created_at": {
"name": "created_at",
"type": "timestamp",
"primaryKey": false,
"notNull": true,
"default": "CURRENT_TIMESTAMP"
},
"updated_at": {
"name": "updated_at",
"type": "timestamp",
"primaryKey": false,
"notNull": true,
"default": "CURRENT_TIMESTAMP"
}
},
"indexes": {},
"foreignKeys": {
"wc_report_profile_id_wc_profile_id_fk": {
"name": "wc_report_profile_id_wc_profile_id_fk",
"tableFrom": "wc_report",
"tableTo": "wc_profile",
"columnsFrom": [
"profile_id"
],
"columnsTo": [
"id"
],
"onDelete": "no action",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
}
},
"enums": {},
"schemas": {},
"_meta": {
"columns": {},
"schemas": {},
"tables": {}
}
}
13 changes: 13 additions & 0 deletions packages/db/migrations/meta/_journal.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"version": "5",
"dialect": "pg",
"entries": [
{
"idx": 0,
"version": "5",
"when": 1715131176564,
"tag": "0000_brainy_lila_cheney",
"breakpoints": true
}
]
}
6 changes: 4 additions & 2 deletions packages/db/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"format": "prettier --check . --ignore-path ../../.gitignore",
"lint": "eslint",
"generate": "pnpm with-env drizzle-kit generate:pg",
"migrate": "tsx src/migrate.ts",
"migrate": "pnpm with-env tsx src/migrate.ts",
"push": "pnpm with-env drizzle-kit push:pg",
"studio": "pnpm with-env drizzle-kit studio",
"typecheck": "tsc --noEmit --emitDeclarationOnly false",
Expand All @@ -34,7 +34,8 @@
"dependencies": {
"@vercel/postgres": "^0.8.0",
"drizzle-orm": "^0.30.10",
"drizzle-zod": "^0.5.1"
"drizzle-zod": "^0.5.1",
"nanoid": "^5.0.7"
},
"devDependencies": {
"@wellchart/eslint-config": "workspace:*",
Expand All @@ -44,6 +45,7 @@
"drizzle-kit": "^0.20.18",
"eslint": "^9.2.0",
"prettier": "^3.2.5",
"tsx": "^4.9.3",
"typescript": "^5.4.5"
},
"prettier": "@wellchart/prettier-config"
Expand Down
8 changes: 8 additions & 0 deletions packages/db/src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { customAlphabet } from "nanoid";

export const nanoid = customAlphabet("abcdefghijklmnopqrstuvwxyz0123456789");

export const timestamps: { createdAt: true; updatedAt: true } = {
createdAt: true,
updatedAt: true,
};
4 changes: 2 additions & 2 deletions packages/db/src/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { sql } from "@vercel/postgres";
import { drizzle } from "drizzle-orm/vercel-postgres";
import { migrate } from "drizzle-orm/vercel-postgres/migrator";

import { env } from "./config";
import { env } from "./client";

const runMigrate = async () => {
if (!env.POSTGRES_URL) {
Expand All @@ -15,7 +15,7 @@ const runMigrate = async () => {

const start = Date.now();

await migrate(db, { migrationsFolder: "src/migrations" });
await migrate(db, { migrationsFolder: "migrations" });

const end = Date.now();

Expand Down
2 changes: 1 addition & 1 deletion packages/db/src/schema/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from "./profile";
export * from "./post";
export * from "./report";
31 changes: 0 additions & 31 deletions packages/db/src/schema/post.ts

This file was deleted.

12 changes: 8 additions & 4 deletions packages/db/src/schema/profile.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import { relations } from "drizzle-orm";
import { varchar } from "drizzle-orm/pg-core";
import { uuid, varchar } from "drizzle-orm/pg-core";

import { createTable } from "./_table";
import { post } from "./post";
import { users } from "./auth";
import { report } from "./report";

export const profile = createTable("profile", {
id: varchar("id", { length: 256 }).primaryKey(),
// Matches id from auth.users table in Supabase
id: uuid("id")
.primaryKey()
.references(() => users.id, { onDelete: "cascade" }),
name: varchar("name", { length: 256 }).notNull(),
image: varchar("image", { length: 256 }),
email: varchar("email", { length: 256 }),
});

export const profileRelations = relations(profile, ({ many }) => ({
posts: many(post),
reports: many(report),
}));
36 changes: 36 additions & 0 deletions packages/db/src/schema/report.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { relations, sql } from "drizzle-orm";
import { serial, text, timestamp, uuid, varchar } from "drizzle-orm/pg-core";
import { createInsertSchema } from "drizzle-zod";
import { z } from "zod";

import { timestamps } from "../lib/utils";
import { createTable } from "./_table";
import { profile } from "./profile";

export const report = createTable("report", {
id: serial("id").primaryKey(),
title: varchar("title", { length: 256 }).notNull(),
content: text("content").notNull(),
profileId: uuid("profile_id")
.notNull()
.references(() => profile.id),
createdAt: timestamp("created_at")
.notNull()
.default(sql`CURRENT_TIMESTAMP`),
updatedAt: timestamp("updated_at")
.notNull()
.default(sql`CURRENT_TIMESTAMP`),
});

export const reportRelations = relations(report, ({ one }) => ({
profile: one(profile, {
fields: [report.profileId],
references: [profile.id],
}),
}));

export const createReportSchema = createInsertSchema(report).omit({
id: true,
...timestamps,
});
export type CreateReportInput = z.infer<typeof createReportSchema>;
Loading

0 comments on commit 00219bc

Please sign in to comment.