From a1a578c97f92346fa97d7779d1ff4b9deec68457 Mon Sep 17 00:00:00 2001 From: Khalim Conn-Kowlessar Date: Mon, 10 Jul 2023 19:14:06 +0100 Subject: [PATCH] Added create_user and also making users table schema changes --- src/app/db/create_user.ts | 40 +++++++++++ src/app/db/migrations/0001_fine_toad_men.sql | 7 ++ .../db/migrations/0002_hard_human_torch.sql | 1 + src/app/db/migrations/meta/0001_snapshot.json | 68 ++++++++++++++++++ src/app/db/migrations/meta/0002_snapshot.json | 70 +++++++++++++++++++ src/app/db/migrations/meta/_journal.json | 14 ++++ src/app/db/schema/users.ts | 19 +++-- tsconfig.json | 4 +- 8 files changed, 217 insertions(+), 6 deletions(-) create mode 100644 src/app/db/create_user.ts create mode 100644 src/app/db/migrations/0001_fine_toad_men.sql create mode 100644 src/app/db/migrations/0002_hard_human_torch.sql create mode 100644 src/app/db/migrations/meta/0001_snapshot.json create mode 100644 src/app/db/migrations/meta/0002_snapshot.json diff --git a/src/app/db/create_user.ts b/src/app/db/create_user.ts new file mode 100644 index 00000000..cab108ce --- /dev/null +++ b/src/app/db/create_user.ts @@ -0,0 +1,40 @@ +import dotenv from "dotenv"; +import { user } from "./schema/users"; +import { drizzle } from "drizzle-orm/node-postgres"; +import { Pool } from "pg"; + +dotenv.config({ path: ".env.local" }); + +// Get email from command line arguments +const email = process.argv[2]; +const name = process.argv[3]; + +// Check if email is provided +if (!email) { + console.error("You must provide an email address"); + process.exit(1); +} + +// Connect to the database +const pool = new Pool({ + host: process.env.DB_HOST, + port: Number(process.env.DB_PORT), + user: process.env.DB_USERNAME, + password: process.env.DB_PASSWORD, + database: process.env.DB_NAME, +}); + +const db = drizzle(pool); + +async function createUser(email: string, name: string) { + try { + await db.insert(user).values({ email: email, firstName: name }); + console.log(`User with email ${email} created successfully`); + process.exit(0); + } catch (err) { + console.error("Failed to create user:", err); + process.exit(1); + } +} + +createUser(email, name); diff --git a/src/app/db/migrations/0001_fine_toad_men.sql b/src/app/db/migrations/0001_fine_toad_men.sql new file mode 100644 index 00000000..db8938a9 --- /dev/null +++ b/src/app/db/migrations/0001_fine_toad_men.sql @@ -0,0 +1,7 @@ +ALTER TABLE "user" ALTER COLUMN "email" SET NOT NULL;--> statement-breakpoint +ALTER TABLE "user" ALTER COLUMN "created_at" SET DATA TYPE timestamp (6) with time zone;--> statement-breakpoint +ALTER TABLE "user" ALTER COLUMN "created_at" SET DEFAULT now();--> statement-breakpoint +ALTER TABLE "user" ALTER COLUMN "created_at" SET NOT NULL;--> statement-breakpoint +ALTER TABLE "user" ALTER COLUMN "updated_at" SET DATA TYPE timestamp (6) with time zone;--> statement-breakpoint +ALTER TABLE "user" ALTER COLUMN "updated_at" SET DEFAULT now();--> statement-breakpoint +ALTER TABLE "user" ALTER COLUMN "updated_at" SET NOT NULL; \ No newline at end of file diff --git a/src/app/db/migrations/0002_hard_human_torch.sql b/src/app/db/migrations/0002_hard_human_torch.sql new file mode 100644 index 00000000..f3f6893e --- /dev/null +++ b/src/app/db/migrations/0002_hard_human_torch.sql @@ -0,0 +1 @@ +ALTER TABLE "user" RENAME COLUMN "name" TO "firstName"; \ No newline at end of file diff --git a/src/app/db/migrations/meta/0001_snapshot.json b/src/app/db/migrations/meta/0001_snapshot.json new file mode 100644 index 00000000..a1bdfa89 --- /dev/null +++ b/src/app/db/migrations/meta/0001_snapshot.json @@ -0,0 +1,68 @@ +{ + "version": "5", + "dialect": "pg", + "id": "1f801509-86d5-48dc-b038-b2319fff0c13", + "prevId": "9cd7955c-6b58-415a-baab-bab2d5e5ff12", + "tables": { + "user": { + "name": "user", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "serial", + "primaryKey": true, + "notNull": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "email": { + "name": "email", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "oauth_id": { + "name": "oauth_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "oauth_provider": { + "name": "oauth_provider", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp (6) with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp (6) with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {} + } + }, + "enums": {}, + "schemas": {}, + "_meta": { + "schemas": {}, + "tables": {}, + "columns": {} + } +} \ No newline at end of file diff --git a/src/app/db/migrations/meta/0002_snapshot.json b/src/app/db/migrations/meta/0002_snapshot.json new file mode 100644 index 00000000..8ebb67b0 --- /dev/null +++ b/src/app/db/migrations/meta/0002_snapshot.json @@ -0,0 +1,70 @@ +{ + "version": "5", + "dialect": "pg", + "id": "4347e9a7-f388-4bac-8860-a482db2a5c8b", + "prevId": "1f801509-86d5-48dc-b038-b2319fff0c13", + "tables": { + "user": { + "name": "user", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "serial", + "primaryKey": true, + "notNull": true + }, + "firstName": { + "name": "firstName", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "email": { + "name": "email", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "oauth_id": { + "name": "oauth_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "oauth_provider": { + "name": "oauth_provider", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp (6) with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp (6) with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {} + } + }, + "enums": {}, + "schemas": {}, + "_meta": { + "schemas": {}, + "tables": {}, + "columns": { + "\"user\".\"name\"": "\"user\".\"firstName\"" + } + } +} \ No newline at end of file diff --git a/src/app/db/migrations/meta/_journal.json b/src/app/db/migrations/meta/_journal.json index 43e1270a..8504b374 100644 --- a/src/app/db/migrations/meta/_journal.json +++ b/src/app/db/migrations/meta/_journal.json @@ -8,6 +8,20 @@ "when": 1689006611034, "tag": "0000_magenta_clea", "breakpoints": true + }, + { + "idx": 1, + "version": "5", + "when": 1689011877245, + "tag": "0001_fine_toad_men", + "breakpoints": true + }, + { + "idx": 2, + "version": "5", + "when": 1689012318015, + "tag": "0002_hard_human_torch", + "breakpoints": true } ] } \ No newline at end of file diff --git a/src/app/db/schema/users.ts b/src/app/db/schema/users.ts index a7f1326a..12268628 100644 --- a/src/app/db/schema/users.ts +++ b/src/app/db/schema/users.ts @@ -2,11 +2,22 @@ import { serial, text, timestamp, pgTable } from "drizzle-orm/pg-core"; export const user = pgTable("user", { id: serial("id").primaryKey(), - name: text("name"), - email: text("email"), + firstName: text("firstName"), + // At the moment, Drizzle doesn't support unique constraints + email: text("email").notNull(), oauthId: text("oauth_id"), oauthProvider: text("oauth_provider").$type<"google">(), // role: text("role").$type<"admin" | "write" | "read">(), - createdAt: timestamp("created_at"), - updatedAt: timestamp("updated_at"), + createdAt: timestamp("created_at", { + precision: 6, + withTimezone: true, + }) + .defaultNow() + .notNull(), + updatedAt: timestamp("updated_at", { + precision: 6, + withTimezone: true, + }) + .defaultNow() + .notNull(), }); diff --git a/tsconfig.json b/tsconfig.json index bda10728..c10dbbcd 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,8 +1,8 @@ { "compilerOptions": { "allowSyntheticDefaultImports": true, - "target": "es5", - // "target": "ESNext", + // "target": "es5", + "target": "ESNext", "lib": ["dom", "dom.iterable", "esnext"], "allowJs": true, "skipLibCheck": true,