CREATE TABLE "asset_field_defs" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "asset_type_id" uuid NOT NULL, "key" varchar(64) NOT NULL, "label" varchar(128) NOT NULL, "type" "field_type" NOT NULL, "required" boolean DEFAULT false NOT NULL, "order" integer DEFAULT 0 NOT NULL, "enum_values" text[], "unit" varchar(32), "placeholder" varchar(255), "help_text" text, "deprecated_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 CREATE TABLE "asset_location_history" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "asset_id" uuid NOT NULL, "from_kind" "container_kind", "from_project_id" uuid, "from_property_id" uuid, "to_kind" "container_kind" NOT NULL, "to_project_id" uuid, "to_property_id" uuid, "moved_by" uuid, "moved_at" timestamp with time zone DEFAULT now() NOT NULL, "reason" text ); --> statement-breakpoint CREATE TABLE "asset_logs" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "asset_id" uuid NOT NULL, "author_id" uuid, "body" text NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "asset_types" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "company_id" uuid, "parent_id" uuid, "name" varchar(128) NOT NULL, "slug" varchar(128) NOT NULL, "icon" varchar(64), "description" text, "schema_version" integer DEFAULT 1 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 "assets" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "company_id" uuid NOT NULL, "asset_type_id" uuid NOT NULL, "name" varchar(255) NOT NULL, "tag" varchar(64), "serial_number" varchar(128), "manufacturer" varchar(128), "model" varchar(128), "purchased_at" timestamp with time zone, "current_container_kind" "container_kind" NOT NULL, "current_project_id" uuid, "current_property_id" uuid, "custom_fields" jsonb DEFAULT '{}'::jsonb NOT NULL, "search_tsv" text, "created_by" uuid, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "updated_at" timestamp with time zone DEFAULT now() NOT NULL, "deleted_at" timestamp with time zone ); --> statement-breakpoint CREATE TABLE "documents" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "company_id" uuid NOT NULL, "scope_type" "doc_scope" NOT NULL, "scope_id" uuid NOT NULL, "filename" varchar(512) NOT NULL, "mime_type" varchar(128) NOT NULL, "size_bytes" bigint NOT NULL, "sha256" varchar(64) NOT NULL, "storage_key" varchar(512) NOT NULL, "uploaded_by" uuid, "uploaded_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "properties" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "company_id" uuid NOT NULL, "name" varchar(255) NOT NULL, "kind" varchar(64), "address_line1" varchar(255), "address_line2" varchar(255), "city" varchar(128), "region" varchar(128), "postal_code" varchar(32), "country_code" varchar(2), "lat" numeric(9, 6), "lng" numeric(9, 6), "notes" text, "created_by" uuid, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "updated_at" timestamp with time zone DEFAULT now() NOT NULL, "deleted_at" timestamp with time zone ); --> statement-breakpoint ALTER TABLE "asset_field_defs" ADD CONSTRAINT "asset_field_defs_asset_type_id_asset_types_id_fk" FOREIGN KEY ("asset_type_id") REFERENCES "public"."asset_types"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "asset_location_history" ADD CONSTRAINT "asset_location_history_asset_id_assets_id_fk" FOREIGN KEY ("asset_id") REFERENCES "public"."assets"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "asset_location_history" ADD CONSTRAINT "asset_location_history_from_property_id_properties_id_fk" FOREIGN KEY ("from_property_id") REFERENCES "public"."properties"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint ALTER TABLE "asset_location_history" ADD CONSTRAINT "asset_location_history_to_property_id_properties_id_fk" FOREIGN KEY ("to_property_id") REFERENCES "public"."properties"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint ALTER TABLE "asset_location_history" ADD CONSTRAINT "asset_location_history_moved_by_users_id_fk" FOREIGN KEY ("moved_by") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint ALTER TABLE "asset_logs" ADD CONSTRAINT "asset_logs_asset_id_assets_id_fk" FOREIGN KEY ("asset_id") REFERENCES "public"."assets"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "asset_logs" ADD CONSTRAINT "asset_logs_author_id_users_id_fk" FOREIGN KEY ("author_id") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint ALTER TABLE "asset_types" ADD CONSTRAINT "asset_types_company_id_companies_id_fk" FOREIGN KEY ("company_id") REFERENCES "public"."companies"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "assets" ADD CONSTRAINT "assets_company_id_companies_id_fk" FOREIGN KEY ("company_id") REFERENCES "public"."companies"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "assets" ADD CONSTRAINT "assets_asset_type_id_asset_types_id_fk" FOREIGN KEY ("asset_type_id") REFERENCES "public"."asset_types"("id") ON DELETE restrict ON UPDATE no action;--> statement-breakpoint ALTER TABLE "assets" ADD CONSTRAINT "assets_current_property_id_properties_id_fk" FOREIGN KEY ("current_property_id") REFERENCES "public"."properties"("id") ON DELETE restrict ON UPDATE no action;--> statement-breakpoint ALTER TABLE "assets" ADD CONSTRAINT "assets_created_by_users_id_fk" FOREIGN KEY ("created_by") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint ALTER TABLE "documents" ADD CONSTRAINT "documents_company_id_companies_id_fk" FOREIGN KEY ("company_id") REFERENCES "public"."companies"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "documents" ADD CONSTRAINT "documents_uploaded_by_users_id_fk" FOREIGN KEY ("uploaded_by") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint ALTER TABLE "properties" ADD CONSTRAINT "properties_company_id_companies_id_fk" FOREIGN KEY ("company_id") REFERENCES "public"."companies"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "properties" ADD CONSTRAINT "properties_created_by_users_id_fk" FOREIGN KEY ("created_by") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint CREATE UNIQUE INDEX "asset_field_defs_type_key_uq" ON "asset_field_defs" USING btree ("asset_type_id","key");--> statement-breakpoint CREATE INDEX "asset_field_defs_by_type" ON "asset_field_defs" USING btree ("asset_type_id","order");--> statement-breakpoint CREATE INDEX "alh_by_asset" ON "asset_location_history" USING btree ("asset_id","moved_at");--> statement-breakpoint CREATE INDEX "asset_logs_by_asset" ON "asset_logs" USING btree ("asset_id","created_at");--> statement-breakpoint CREATE UNIQUE INDEX "asset_types_company_slug_uq" ON "asset_types" USING btree ("company_id","slug");--> statement-breakpoint CREATE INDEX "asset_types_by_parent" ON "asset_types" USING btree ("parent_id");--> statement-breakpoint CREATE INDEX "assets_by_company" ON "assets" USING btree ("company_id");--> statement-breakpoint CREATE INDEX "assets_by_type" ON "assets" USING btree ("asset_type_id");--> statement-breakpoint CREATE INDEX "assets_by_project" ON "assets" USING btree ("current_project_id");--> statement-breakpoint CREATE INDEX "assets_by_property" ON "assets" USING btree ("current_property_id");--> statement-breakpoint CREATE UNIQUE INDEX "assets_company_tag_uq" ON "assets" USING btree ("company_id","tag");--> statement-breakpoint CREATE UNIQUE INDEX "assets_company_serial_uq" ON "assets" USING btree ("company_id","serial_number");--> statement-breakpoint CREATE INDEX "docs_by_scope" ON "documents" USING btree ("scope_type","scope_id");--> statement-breakpoint CREATE INDEX "docs_by_company" ON "documents" USING btree ("company_id");--> statement-breakpoint CREATE INDEX "docs_by_hash" ON "documents" USING btree ("sha256");--> statement-breakpoint CREATE UNIQUE INDEX "docs_storage_key_uq" ON "documents" USING btree ("storage_key");--> statement-breakpoint CREATE INDEX "properties_by_company" ON "properties" USING btree ("company_id");