From adb0a0600636703ad3bff2b3cfc72e12b2ebbd66 Mon Sep 17 00:00:00 2001 From: Brian Cao Date: Fri, 30 Sep 2022 22:27:47 -0700 Subject: [PATCH] add uuid to account --- .../migrations/04_account_uuid/migration.sql | 12 ++++++++++++ db/postgresql/schema.prisma | 15 ++++++++------- pages/api/account/index.js | 7 ++++++- 3 files changed, 26 insertions(+), 8 deletions(-) create mode 100644 db/postgresql/migrations/04_account_uuid/migration.sql diff --git a/db/postgresql/migrations/04_account_uuid/migration.sql b/db/postgresql/migrations/04_account_uuid/migration.sql new file mode 100644 index 00000000..f95718d4 --- /dev/null +++ b/db/postgresql/migrations/04_account_uuid/migration.sql @@ -0,0 +1,12 @@ + +-- AlterTable +ALTER TABLE "account" ADD COLUMN "account_uuid" UUID NULL; + +-- Backfill UUID +UPDATE "account" SET account_uuid = gen_random_uuid(); + +-- AlterTable +ALTER TABLE "account" ALTER COLUMN "account_uuid" SET NOT NULL; + +-- CreateIndex +CREATE UNIQUE INDEX "account_account_uuid_key" ON "account"("account_uuid"); diff --git a/db/postgresql/schema.prisma b/db/postgresql/schema.prisma index a76a3da4..d1d346de 100644 --- a/db/postgresql/schema.prisma +++ b/db/postgresql/schema.prisma @@ -8,13 +8,14 @@ datasource db { } model account { - user_id Int @id @default(autoincrement()) - username String @unique @db.VarChar(255) - password String @db.VarChar(60) - is_admin Boolean @default(false) - created_at DateTime? @default(now()) @db.Timestamptz(6) - updated_at DateTime? @default(now()) @db.Timestamptz(6) - website website[] + user_id Int @id @default(autoincrement()) + username String @unique @db.VarChar(255) + password String @db.VarChar(60) + is_admin Boolean @default(false) + created_at DateTime? @default(now()) @db.Timestamptz(6) + updated_at DateTime? @default(now()) @db.Timestamptz(6) + account_uuid String @unique @db.Uuid + website website[] } model event { diff --git a/pages/api/account/index.js b/pages/api/account/index.js index fe9cafe1..a3fc6bc1 100644 --- a/pages/api/account/index.js +++ b/pages/api/account/index.js @@ -1,6 +1,7 @@ import { ok, unauthorized, methodNotAllowed, badRequest, hashPassword } from 'next-basics'; import { getAccountById, getAccountByUsername, updateAccount, createAccount } from 'queries'; import { useAuth } from 'lib/middleware'; +import { uuid } from 'lib/crypto'; export default async (req, res) => { await useAuth(req, res); @@ -47,7 +48,11 @@ export default async (req, res) => { return badRequest(res, 'Account already exists'); } - const created = await createAccount({ username, password: hashPassword(password) }); + const created = await createAccount({ + username, + password: hashPassword(password), + account_uuid: uuid(), + }); return ok(res, created); }