Files
pn-new-crm/src/lib/db/migrations/0002_groovy_excalibur.sql

67 lines
3.2 KiB
SQL

CREATE TABLE "yacht_notes" (
"id" text PRIMARY KEY NOT NULL,
"yacht_id" text NOT NULL,
"author_id" text NOT NULL,
"content" text NOT NULL,
"mentions" text[],
"is_locked" boolean DEFAULT false NOT NULL,
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "yacht_ownership_history" (
"id" text PRIMARY KEY NOT NULL,
"yacht_id" text NOT NULL,
"owner_type" text NOT NULL,
"owner_id" text NOT NULL,
"start_date" timestamp with time zone NOT NULL,
"end_date" timestamp with time zone,
"transfer_reason" text,
"transfer_notes" text,
"created_by" text NOT NULL,
"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "yacht_tags" (
"yacht_id" text NOT NULL,
"tag_id" text NOT NULL,
CONSTRAINT "yacht_tags_yacht_id_tag_id_pk" PRIMARY KEY("yacht_id","tag_id")
);
--> statement-breakpoint
CREATE TABLE "yachts" (
"id" text PRIMARY KEY NOT NULL,
"port_id" text NOT NULL,
"name" text NOT NULL,
"hull_number" text,
"registration" text,
"flag" text,
"year_built" integer,
"builder" text,
"model" text,
"hull_material" text,
"length_ft" numeric,
"width_ft" numeric,
"draft_ft" numeric,
"length_m" numeric,
"width_m" numeric,
"draft_m" numeric,
"current_owner_type" text NOT NULL,
"current_owner_id" text NOT NULL,
"status" text DEFAULT 'active' NOT NULL,
"notes" text,
"archived_at" timestamp with time zone,
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
ALTER TABLE "yacht_notes" ADD CONSTRAINT "yacht_notes_yacht_id_yachts_id_fk" FOREIGN KEY ("yacht_id") REFERENCES "public"."yachts"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "yacht_ownership_history" ADD CONSTRAINT "yacht_ownership_history_yacht_id_yachts_id_fk" FOREIGN KEY ("yacht_id") REFERENCES "public"."yachts"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "yacht_tags" ADD CONSTRAINT "yacht_tags_yacht_id_yachts_id_fk" FOREIGN KEY ("yacht_id") REFERENCES "public"."yachts"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "yachts" ADD CONSTRAINT "yachts_port_id_ports_id_fk" FOREIGN KEY ("port_id") REFERENCES "public"."ports"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
CREATE INDEX "idx_yn_yacht" ON "yacht_notes" USING btree ("yacht_id");--> statement-breakpoint
CREATE INDEX "idx_yoh_yacht" ON "yacht_ownership_history" USING btree ("yacht_id");--> statement-breakpoint
CREATE UNIQUE INDEX "idx_yoh_active" ON "yacht_ownership_history" USING btree ("yacht_id") WHERE "yacht_ownership_history"."end_date" IS NULL;--> statement-breakpoint
CREATE INDEX "idx_yachts_port" ON "yachts" USING btree ("port_id");--> statement-breakpoint
CREATE INDEX "idx_yachts_current_owner" ON "yachts" USING btree ("port_id","current_owner_type","current_owner_id");--> statement-breakpoint
CREATE INDEX "idx_yachts_name" ON "yachts" USING btree ("port_id","name");--> statement-breakpoint
CREATE INDEX "idx_yachts_archived" ON "yachts" USING btree ("port_id","archived_at");