Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
21 lines
1.5 KiB
SQL
21 lines
1.5 KiB
SQL
CREATE TABLE "listing_phones" (
|
|
"id" serial PRIMARY KEY NOT NULL,
|
|
"listing_id" integer NOT NULL,
|
|
"company_id" integer NOT NULL,
|
|
"user_id" integer,
|
|
"phone" text NOT NULL,
|
|
"created_at" timestamp with time zone DEFAULT now() NOT NULL
|
|
);
|
|
--> statement-breakpoint
|
|
ALTER TABLE "listing_phones" ADD CONSTRAINT "listing_phones_listing_id_listings_id_fk" FOREIGN KEY ("listing_id") REFERENCES "public"."listings"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
|
ALTER TABLE "listing_phones" ADD CONSTRAINT "listing_phones_company_id_companies_id_fk" FOREIGN KEY ("company_id") REFERENCES "public"."companies"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
|
|
ALTER TABLE "listing_phones" ADD CONSTRAINT "listing_phones_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
|
|
CREATE UNIQUE INDEX "listing_phones_uq" ON "listing_phones" USING btree ("listing_id","company_id");--> statement-breakpoint
|
|
CREATE INDEX "listing_phones_phone_idx" ON "listing_phones" USING btree ("listing_id","phone");--> statement-breakpoint
|
|
-- Номера, уже внесённые агентами, становятся вариантами в общей базе.
|
|
INSERT INTO "listing_phones" ("listing_id", "company_id", "phone")
|
|
SELECT DISTINCT ON (l."listing_id", l."company_id") l."listing_id", l."company_id", l."phone"
|
|
FROM "leads" l WHERE l."listing_id" IS NOT NULL AND l."phone" IS NOT NULL
|
|
ORDER BY l."listing_id", l."company_id", l."id" DESC
|
|
ON CONFLICT DO NOTHING;
|