fix: simplify setup route
This commit is contained in:
parent
30ac75beda
commit
64aa687b8b
1 changed files with 46 additions and 63 deletions
|
|
@ -1,100 +1,83 @@
|
||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
import { db } from "@/db";
|
import postgres from "postgres";
|
||||||
import { sql } from "drizzle-orm/postgres";
|
|
||||||
|
const connectionString = process.env.DATABASE_URL!;
|
||||||
|
|
||||||
export async function GET() {
|
export async function GET() {
|
||||||
try {
|
const client = postgres(connectionString);
|
||||||
// Create types
|
|
||||||
await db.execute(sql`CREATE TYPE IF NOT EXISTS child_sex AS ENUM('male', 'female', 'other')`);
|
|
||||||
await db.execute(sql`CREATE TYPE IF NOT EXISTS child_stage AS ENUM('newborn', 'infant', 'solids_start', 'toddler_early', 'toddler_late', 'preschool')`);
|
|
||||||
await db.execute(sql`CREATE TYPE IF NOT EXISTS member_role AS ENUM('admin', 'caregiver', 'viewer')`);
|
|
||||||
|
|
||||||
// Create tables
|
try {
|
||||||
await db.execute(sql`
|
await client.unsafe(`
|
||||||
|
CREATE TYPE IF NOT EXISTS child_sex AS ENUM('male', 'female', 'other');
|
||||||
|
CREATE TYPE IF NOT EXISTS child_stage AS ENUM('newborn', 'infant', 'solids_start', 'toddler_early', 'toddler_late', 'preschool');
|
||||||
|
CREATE TYPE IF NOT EXISTS member_role AS ENUM('admin', 'caregiver', 'viewer');
|
||||||
|
`);
|
||||||
|
|
||||||
|
await client.unsafe(`
|
||||||
CREATE TABLE IF NOT EXISTS users (
|
CREATE TABLE IF NOT EXISTS users (
|
||||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||||
name text,
|
name text, email text NOT NULL UNIQUE,
|
||||||
email text NOT NULL UNIQUE,
|
email_verified timestamp, image text,
|
||||||
email_verified timestamp,
|
created_at timestamp DEFAULT now(),
|
||||||
image text,
|
updated_at timestamp DEFAULT now()
|
||||||
created_at timestamp DEFAULT now() NOT NULL,
|
|
||||||
updated_at timestamp DEFAULT now() NOT NULL
|
|
||||||
)
|
)
|
||||||
`);
|
`);
|
||||||
|
|
||||||
await db.execute(sql`
|
await client.unsafe(`
|
||||||
CREATE TABLE IF NOT EXISTS accounts (
|
|
||||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
||||||
user_id uuid NOT NULL,
|
|
||||||
type text NOT NULL,
|
|
||||||
provider text NOT NULL,
|
|
||||||
provider_account_id text NOT NULL,
|
|
||||||
refresh_token text,
|
|
||||||
access_token text,
|
|
||||||
expires_at timestamp,
|
|
||||||
token_type text,
|
|
||||||
scope text,
|
|
||||||
id_token text,
|
|
||||||
session_state text
|
|
||||||
)
|
|
||||||
`);
|
|
||||||
|
|
||||||
await db.execute(sql`
|
|
||||||
CREATE TABLE IF NOT EXISTS sessions (
|
CREATE TABLE IF NOT EXISTS sessions (
|
||||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||||
session_token text NOT NULL UNIQUE,
|
session_token text NOT NULL UNIQUE,
|
||||||
user_id uuid NOT NULL,
|
user_id uuid NOT NULL, expires timestamp NOT NULL
|
||||||
expires timestamp NOT NULL
|
|
||||||
)
|
)
|
||||||
`);
|
`);
|
||||||
|
|
||||||
await db.execute(sql`
|
await client.unsafe(`
|
||||||
|
CREATE TABLE IF NOT EXISTS accounts (
|
||||||
|
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||||
|
user_id uuid NOT NULL, type text NOT NULL,
|
||||||
|
provider text NOT NULL, provider_account_id text NOT NULL,
|
||||||
|
refresh_token text, access_token text, expires_at timestamp,
|
||||||
|
token_type text, scope text, id_token text, session_state text
|
||||||
|
)
|
||||||
|
`);
|
||||||
|
|
||||||
|
await client.unsafe(`
|
||||||
CREATE TABLE IF NOT EXISTS verification_tokens (
|
CREATE TABLE IF NOT EXISTS verification_tokens (
|
||||||
identifier text NOT NULL,
|
identifier text NOT NULL, token text NOT NULL,
|
||||||
token text NOT NULL,
|
expires timestamp NOT NULL, PRIMARY KEY (identifier, token)
|
||||||
expires timestamp NOT NULL,
|
|
||||||
PRIMARY KEY (identifier, token)
|
|
||||||
)
|
)
|
||||||
`);
|
`);
|
||||||
|
|
||||||
await db.execute(sql`
|
await client.unsafe(`
|
||||||
CREATE TABLE IF NOT EXISTS families (
|
CREATE TABLE IF NOT EXISTS families (
|
||||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||||
name text DEFAULT 'The Gupta Family' NOT NULL,
|
name text DEFAULT 'The Gupta Family', pediatrician_phone text,
|
||||||
pediatrician_phone text,
|
created_at timestamp DEFAULT now(), updated_at timestamp DEFAULT now()
|
||||||
created_at timestamp DEFAULT now() NOT NULL,
|
|
||||||
updated_at timestamp DEFAULT now() NOT NULL
|
|
||||||
)
|
)
|
||||||
`);
|
`);
|
||||||
|
|
||||||
await db.execute(sql`
|
await client.unsafe(`
|
||||||
CREATE TABLE IF NOT EXISTS family_members (
|
CREATE TABLE IF NOT EXISTS family_members (
|
||||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||||
family_id uuid NOT NULL,
|
family_id uuid NOT NULL, user_id uuid NOT NULL,
|
||||||
user_id uuid NOT NULL,
|
role member_role DEFAULT 'caregiver', display_name text NOT NULL,
|
||||||
role member_role DEFAULT 'caregiver' NOT NULL,
|
created_at timestamp DEFAULT now()
|
||||||
display_name text NOT NULL,
|
|
||||||
created_at timestamp DEFAULT now() NOT NULL,
|
|
||||||
UNIQUE(family_id, user_id)
|
|
||||||
)
|
)
|
||||||
`);
|
`);
|
||||||
|
|
||||||
await db.execute(sql`
|
await client.unsafe(`
|
||||||
CREATE TABLE IF NOT EXISTS children (
|
CREATE TABLE IF NOT EXISTS children (
|
||||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||||
family_id uuid NOT NULL,
|
family_id uuid NOT NULL, name text NOT NULL,
|
||||||
name text NOT NULL,
|
birth_date timestamp NOT NULL, sex child_sex NOT NULL,
|
||||||
birth_date timestamp NOT NULL,
|
current_stage child_stage, stage_overrides jsonb DEFAULT '{}',
|
||||||
sex child_sex NOT NULL,
|
profile_photo_url text, created_at timestamp DEFAULT now(),
|
||||||
current_stage child_stage,
|
updated_at timestamp DEFAULT now()
|
||||||
stage_overrides jsonb DEFAULT '{}',
|
|
||||||
profile_photo_url text,
|
|
||||||
created_at timestamp DEFAULT now() NOT NULL,
|
|
||||||
updated_at timestamp DEFAULT now() NOT NULL
|
|
||||||
)
|
)
|
||||||
`);
|
`);
|
||||||
|
|
||||||
return NextResponse.json({ success: true, message: "Tables created" });
|
await client.end();
|
||||||
|
return NextResponse.json({ success: true });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
return NextResponse.json({ error: String(error) }, { status: 500 });
|
return NextResponse.json({ error: String(error) }, { status: 500 });
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue