Files
Final-Year-Project/Backend/drizzle/0006_steady_control_plane.sql

41 lines
3.0 KiB
SQL

ALTER TABLE "devices" ADD COLUMN "role" varchar(32) DEFAULT 'client' NOT NULL;--> statement-breakpoint
ALTER TABLE "devices" ADD COLUMN "platform" varchar(32);--> statement-breakpoint
ALTER TABLE "devices" ADD COLUMN "app_version" varchar(64);--> statement-breakpoint
ALTER TABLE "devices" ADD COLUMN "push_token" text;--> statement-breakpoint
ALTER TABLE "devices" ADD COLUMN "status" varchar(32) DEFAULT 'offline' NOT NULL;--> statement-breakpoint
ALTER TABLE "devices" ADD COLUMN "updated_at" timestamp with time zone DEFAULT now() NOT NULL;--> statement-breakpoint
UPDATE "devices" SET "role" = CASE WHEN "is_camera" THEN 'camera' ELSE 'client' END;--> statement-breakpoint
CREATE TABLE "device_links" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"owner_user_id" uuid NOT NULL,
"camera_device_id" uuid NOT NULL,
"client_device_id" uuid NOT NULL,
"status" varchar(32) DEFAULT 'active' NOT NULL,
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
CONSTRAINT "device_links_camera_client_unique" UNIQUE("camera_device_id","client_device_id")
);
--> statement-breakpoint
CREATE TABLE "device_commands" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"owner_user_id" uuid NOT NULL,
"source_device_id" uuid NOT NULL,
"target_device_id" uuid NOT NULL,
"command_type" varchar(64) NOT NULL,
"payload" jsonb DEFAULT 'null'::jsonb,
"status" varchar(32) DEFAULT 'queued' NOT NULL,
"retry_count" integer DEFAULT 0 NOT NULL,
"last_dispatched_at" timestamp with time zone,
"acknowledged_at" timestamp with time zone,
"error" text,
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
ALTER TABLE "device_links" ADD CONSTRAINT "device_links_owner_user_id_users_id_fk" FOREIGN KEY ("owner_user_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "device_links" ADD CONSTRAINT "device_links_camera_device_id_devices_id_fk" FOREIGN KEY ("camera_device_id") REFERENCES "public"."devices"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "device_links" ADD CONSTRAINT "device_links_client_device_id_devices_id_fk" FOREIGN KEY ("client_device_id") REFERENCES "public"."devices"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "device_commands" ADD CONSTRAINT "device_commands_owner_user_id_users_id_fk" FOREIGN KEY ("owner_user_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "device_commands" ADD CONSTRAINT "device_commands_source_device_id_devices_id_fk" FOREIGN KEY ("source_device_id") REFERENCES "public"."devices"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "device_commands" ADD CONSTRAINT "device_commands_target_device_id_devices_id_fk" FOREIGN KEY ("target_device_id") REFERENCES "public"."devices"("id") ON DELETE no action ON UPDATE no action;