CREATE TABLE "basic_contract" (
	"id" integer PRIMARY KEY GENERATED ALWAYS AS IDENTITY (sequence name "basic_contract_id_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 2147483647 START WITH 1 CACHE 1),
	"template_id" integer,
	"vendor_id" integer,
	"requested_by" integer,
	"status" text DEFAULT 'PENDING' NOT NULL,
	"file_name" varchar(255) NOT NULL,
	"file_path" varchar(1024) NOT NULL,
	"created_at" timestamp DEFAULT now(),
	"updated_at" timestamp DEFAULT now()
);
--> statement-breakpoint
CREATE TABLE "basic_contract_templates" (
	"id" integer PRIMARY KEY GENERATED ALWAYS AS IDENTITY (sequence name "basic_contract_templates_id_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 2147483647 START WITH 1 CACHE 1),
	"template_name" text NOT NULL,
	"document_number" integer NOT NULL,
	"status" text DEFAULT 'ACTIVE' NOT NULL,
	"file_name" varchar(255) NOT NULL,
	"file_path" varchar(1024) NOT NULL,
	"created_at" timestamp DEFAULT now(),
	"updated_at" timestamp DEFAULT now()
);
--> statement-breakpoint
/* 
    Unfortunately in current drizzle-kit version we can't automatically get name for primary key.
    We are working on making it available!

    Meanwhile you can:
        1. Check pk name in your database, by running
            SELECT constraint_name FROM information_schema.table_constraints
            WHERE table_schema = 'public'
                AND table_name = 'bidding-projects'
                AND constraint_type = 'PRIMARY KEY';
        2. Uncomment code below and paste pk name manually
        
    Hope to release this update as soon as possible
*/

-- ALTER TABLE "bidding-projects" DROP CONSTRAINT "<constraint_name>";--> statement-breakpoint
ALTER TABLE "basic_contract" ADD CONSTRAINT "basic_contract_template_id_basic_contract_templates_id_fk" FOREIGN KEY ("template_id") REFERENCES "public"."basic_contract_templates"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "basic_contract" ADD CONSTRAINT "basic_contract_vendor_id_vendors_id_fk" FOREIGN KEY ("vendor_id") REFERENCES "public"."vendors"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "basic_contract" ADD CONSTRAINT "basic_contract_requested_by_users_id_fk" FOREIGN KEY ("requested_by") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "bidding-projects" ADD CONSTRAINT "bidding-projects_pspid_unique" UNIQUE("pspid");--> statement-breakpoint
CREATE VIEW "public"."basic_contract_templates_view" AS (select "id" as "id", "template_name" as "template_name", "document_number" as "document_number", "status" as "status", "file_name" as "file_name", "file_path" as "file_path", "created_at" as "created_at", "updated_at" as "updated_at" from "basic_contract_templates");--> statement-breakpoint
CREATE VIEW "public"."basic_contract_view" AS (select "basic_contract"."id" as "id", "basic_contract"."template_id" as "template_id", "basic_contract"."vendor_id" as "vendor_id", "basic_contract"."requested_by" as "requested_by", "basic_contract"."status" as "basic_contract_status", "basic_contract"."created_at" as "created_at", "basic_contract"."updated_at" as "updated_at", "vendors"."vendor_code" as "vendor_code", "vendors"."email" as "vendor_email", "users"."name" as "user_name", "basic_contract_templates"."template_name" as "template_name", "basic_contract_templates"."document_number" as "document_number", "basic_contract"."file_name" as "file_name", "basic_contract"."file_path" as "file_path" from "basic_contract" left join "vendors" on "basic_contract"."vendor_id" = "vendors"."id" left join "users" on "basic_contract"."requested_by" = "users"."id" left join "basic_contract_templates" on "basic_contract"."template_id" = "basic_contract_templates"."id");