Files
kidarr-server/drizzle/0000_wide_gravity.sql
2024-01-15 23:36:07 +00:00

83 lines
2.4 KiB
SQL

CREATE TABLE IF NOT EXISTS "account" (
"userId" uuid NOT NULL,
"type" text NOT NULL,
"provider" text NOT NULL,
"providerAccountId" text NOT NULL,
"refresh_token" text,
"access_token" text,
"expires_at" integer,
"token_type" text,
"scope" text,
"id_token" text,
"session_state" text,
CONSTRAINT "account_provider_providerAccountId_pk" PRIMARY KEY("provider","providerAccountId")
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "child" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid
() NOT NULL,
"name" varchar(256) NOT NULL,
"email" varchar(256),
"phone" varchar(256),
"avatar" varchar(256),
"key" varchar(256),
"parent_id" uuid NOT NULL
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "device" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid
() NOT NULL,
"device_id" varchar NOT NULL,
"child_id" uuid NOT NULL,
"device_name" varchar NOT NULL,
"api_key" varchar NOT NULL,
"pin" integer NOT NULL,
"expires" timestamp DEFAULT now
() + interval '1 hour',
CONSTRAINT "device_device_id_unique" UNIQUE("device_id"),
CONSTRAINT "device_api_key_unique" UNIQUE("api_key")
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "ping" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid
() NOT NULL,
"device_id" uuid NOT NULL,
"latitude" double precision NOT NULL,
"longitude" double precision NOT NULL,
"timestamp" timestamp NOT NULL
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "session" (
"sessionToken" text PRIMARY KEY NOT NULL,
"userId" uuid NOT NULL,
"expires" timestamp NOT NULL
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "user" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid
() NOT NULL,
"name" text,
"email" text NOT NULL,
"emailVerified" timestamp,
"image" text
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "verification_token" (
"identifier" varchar(255) NOT NULL,
"token" varchar(255) NOT NULL,
"expires" timestamp NOT NULL,
CONSTRAINT "verification_token_identifier_token_pk" PRIMARY KEY("identifier","token")
);
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "account" ADD CONSTRAINT "account_userId_user_id_fk" FOREIGN KEY ("userId") REFERENCES "user"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "session" ADD CONSTRAINT "session_userId_user_id_fk" FOREIGN KEY ("userId") REFERENCES "user"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;