CREATE TYPE "router_v1.11"."position_type" AS ENUM('erc20', 'vault_v1', 'debtOf', 'collateralOf');--> statement-breakpoint
CREATE TYPE "router_v1.11"."status_code" AS ENUM('VALID', 'SIMULATION_ERROR');--> statement-breakpoint
CREATE TABLE "router_v1.11"."callbacks" (
	"id" varchar(66) PRIMARY KEY NOT NULL,
	"position_chain_id" bigint NOT NULL,
	"position_contract" varchar(42) NOT NULL,
	"position_user" varchar(42) NOT NULL,
	"position_type_id" integer NOT NULL,
	"amount" numeric(78, 0)
);
--> statement-breakpoint
CREATE TABLE "router_v1.11"."chains" (
	"chain_id" bigint NOT NULL,
	"block_number" bigint NOT NULL,
	"epoch" numeric(78, 0) DEFAULT '0' NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "router_v1.11"."collectors" (
	"chain_id" bigint NOT NULL,
	"name" text NOT NULL,
	"block_number" bigint NOT NULL,
	"epoch" numeric(78, 0) DEFAULT '0' NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "router_v1.11"."consumed_events" (
	"event_id" varchar(128) PRIMARY KEY NOT NULL,
	"chain_id" bigint NOT NULL,
	"maker" varchar(42) NOT NULL,
	"group" varchar(66) NOT NULL,
	"amount" numeric(78, 0) NOT NULL,
	"block_number" bigint NOT NULL,
	"created_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "router_v1.11"."groups" (
	"chain_id" bigint NOT NULL,
	"maker" varchar(42) NOT NULL,
	"group" varchar(66) NOT NULL,
	"consumed" numeric(78, 0) NOT NULL,
	"block_number" bigint NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL,
	CONSTRAINT "groups_pk" PRIMARY KEY("chain_id","maker","group")
);
--> statement-breakpoint
CREATE TABLE "router_v1.11"."lots" (
	"chain_id" bigint NOT NULL,
	"user" varchar(42) NOT NULL,
	"contract" varchar(42) NOT NULL,
	"group" varchar(66) NOT NULL,
	"obligation_id" varchar(66) NOT NULL,
	"lower" numeric(78, 0) NOT NULL,
	"upper" numeric(78, 0) NOT NULL,
	CONSTRAINT "lots_pk" PRIMARY KEY("chain_id","user","contract","group","obligation_id")
);
--> statement-breakpoint
CREATE TABLE "router_v1.11"."lots_positions" (
	"chain_id" bigint NOT NULL,
	"contract" varchar(42) NOT NULL,
	"user" varchar(42) NOT NULL,
	"position_type_id" integer NOT NULL,
	CONSTRAINT "lots_positions_pk" PRIMARY KEY("chain_id","contract","user")
);
--> statement-breakpoint
CREATE TABLE "router_v1.11"."merkle_paths" (
	"offer_hash" varchar(66) PRIMARY KEY NOT NULL,
	"tree_root" varchar(66) NOT NULL,
	"proof_nodes" text NOT NULL,
	"created_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "router_v1.11"."obligation_collaterals_v2" (
	"obligation_key" varchar(66) NOT NULL,
	"asset" varchar(42) NOT NULL,
	"oracle_address" varchar(42) NOT NULL,
	"lltv" bigint NOT NULL,
	"collateral_index" integer NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL,
	CONSTRAINT "obligation_collaterals_v2_pk" PRIMARY KEY("obligation_key","asset")
);
--> statement-breakpoint
CREATE TABLE "router_v1.11"."obligation_id_keys" (
	"obligation_id" varchar(66) PRIMARY KEY NOT NULL,
	"obligation_key" varchar(66) NOT NULL,
	"chain_id" bigint NOT NULL,
	"morpho_v2" varchar(42) NOT NULL
);
--> statement-breakpoint
CREATE TABLE "router_v1.11"."obligations" (
	"obligation_key" varchar(66) PRIMARY KEY NOT NULL,
	"loan_token" varchar(42) NOT NULL,
	"maturity" integer NOT NULL
);
--> statement-breakpoint
CREATE TABLE "router_v1.11"."offers" (
	"hash" varchar(66) PRIMARY KEY NOT NULL,
	"obligation_id" varchar(66) NOT NULL,
	"assets" numeric(78, 0) NOT NULL,
	"obligation_units" numeric(78, 0) DEFAULT '0' NOT NULL,
	"obligation_shares" numeric(78, 0) DEFAULT '0' NOT NULL,
	"tick" integer NOT NULL,
	"maturity" integer NOT NULL,
	"expiry" integer NOT NULL,
	"start" integer NOT NULL,
	"group_chain_id" bigint NOT NULL,
	"group_maker" varchar(42) NOT NULL,
	"group_group" varchar(66) NOT NULL,
	"session" varchar(66) NOT NULL,
	"buy" boolean NOT NULL,
	"callback_address" varchar(42) NOT NULL,
	"callback_data" text NOT NULL,
	"receiver_if_maker_is_seller" varchar(42),
	"block_number" bigint NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "router_v1.11"."offers_callbacks" (
	"offer_hash" varchar(66) NOT NULL,
	"callback_id" varchar(66),
	CONSTRAINT "offers_callbacks_pk" PRIMARY KEY("offer_hash","callback_id")
);
--> statement-breakpoint
CREATE TABLE "router_v1.11"."offsets" (
	"chain_id" bigint NOT NULL,
	"user" varchar(42) NOT NULL,
	"contract" varchar(42) NOT NULL,
	"group" varchar(66) NOT NULL,
	"obligation_id" varchar(66) NOT NULL,
	"value" numeric(78, 0) NOT NULL,
	CONSTRAINT "offsets_pk" PRIMARY KEY("chain_id","user","contract","group","obligation_id")
);
--> statement-breakpoint
CREATE TABLE "router_v1.11"."oracles" (
	"chain_id" bigint NOT NULL,
	"address" varchar(42) NOT NULL,
	"price" numeric(78, 0),
	"block_number" bigint NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL,
	CONSTRAINT "oracles_pk" PRIMARY KEY("chain_id","address")
);
--> statement-breakpoint
CREATE TABLE "router_v1.11"."position_types" (
	"id" serial PRIMARY KEY NOT NULL,
	"type" "router_v1.11"."position_type" NOT NULL
);
--> statement-breakpoint
CREATE TABLE "router_v1.11"."positions" (
	"chain_id" bigint NOT NULL,
	"contract" varchar(66) NOT NULL,
	"user" varchar(42) NOT NULL,
	"position_type_id" integer NOT NULL,
	"balance" numeric(78, 0),
	"asset" varchar(42) NOT NULL,
	"block_number" bigint NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL,
	CONSTRAINT "positions_pk" PRIMARY KEY("chain_id","contract","user","position_type_id","asset")
);
--> statement-breakpoint
CREATE TABLE "router_v1.11"."status" (
	"id" serial PRIMARY KEY NOT NULL,
	"code" "router_v1.11"."status_code",
	CONSTRAINT "status_code_unique" UNIQUE("code")
);
--> statement-breakpoint
CREATE TABLE "router_v1.11"."transfers" (
	"event_id" varchar(128) PRIMARY KEY NOT NULL,
	"chain_id" bigint NOT NULL,
	"contract" varchar(66) NOT NULL,
	"from" varchar(42) NOT NULL,
	"to" varchar(42) NOT NULL,
	"value" numeric(78, 0) NOT NULL,
	"position_type_id" integer NOT NULL,
	"asset" varchar(42) NOT NULL,
	"block_number" bigint NOT NULL,
	"created_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "router_v1.11"."trees" (
	"root" varchar(66) PRIMARY KEY NOT NULL,
	"root_signature" varchar(132) NOT NULL,
	"created_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "router_v1.11"."validations" (
	"offer_hash" varchar(66) PRIMARY KEY NOT NULL,
	"status_id" integer NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
DROP TABLE "router_v1.10"."callbacks" CASCADE;--> statement-breakpoint
DROP TABLE "router_v1.10"."chains" CASCADE;--> statement-breakpoint
DROP TABLE "router_v1.10"."collectors" CASCADE;--> statement-breakpoint
DROP TABLE "router_v1.10"."consumed_events" CASCADE;--> statement-breakpoint
DROP TABLE "router_v1.10"."groups" CASCADE;--> statement-breakpoint
DROP TABLE "router_v1.10"."lots" CASCADE;--> statement-breakpoint
DROP TABLE "router_v1.10"."lots_positions" CASCADE;--> statement-breakpoint
DROP TABLE "router_v1.10"."merkle_paths" CASCADE;--> statement-breakpoint
DROP TABLE "router_v1.10"."obligation_collaterals_v2" CASCADE;--> statement-breakpoint
DROP TABLE "router_v1.10"."obligation_id_keys" CASCADE;--> statement-breakpoint
DROP TABLE "router_v1.10"."obligations" CASCADE;--> statement-breakpoint
DROP TABLE "router_v1.10"."offers" CASCADE;--> statement-breakpoint
DROP TABLE "router_v1.10"."offers_callbacks" CASCADE;--> statement-breakpoint
DROP TABLE "router_v1.10"."offsets" CASCADE;--> statement-breakpoint
DROP TABLE "router_v1.10"."oracles" CASCADE;--> statement-breakpoint
DROP TABLE "router_v1.10"."position_types" CASCADE;--> statement-breakpoint
DROP TABLE "router_v1.10"."positions" CASCADE;--> statement-breakpoint
DROP TABLE "router_v1.10"."status" CASCADE;--> statement-breakpoint
DROP TABLE "router_v1.10"."transfers" CASCADE;--> statement-breakpoint
DROP TABLE "router_v1.10"."trees" CASCADE;--> statement-breakpoint
DROP TABLE "router_v1.10"."validations" CASCADE;--> statement-breakpoint
CREATE UNIQUE INDEX "chains_chain_id_idx" ON "router_v1.11"."chains" USING btree ("chain_id");--> statement-breakpoint
ALTER TABLE "router_v1.11"."callbacks" ADD CONSTRAINT "callbacks_position_type_id_position_types_id_fk" FOREIGN KEY ("position_type_id") REFERENCES "router_v1.11"."position_types"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "router_v1.11"."collectors" ADD CONSTRAINT "collectors_chain_id_chains_chain_id_fk" FOREIGN KEY ("chain_id") REFERENCES "router_v1.11"."chains"("chain_id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "router_v1.11"."consumed_events" ADD CONSTRAINT "consumed_events_groups_fk" FOREIGN KEY ("chain_id","maker","group") REFERENCES "router_v1.11"."groups"("chain_id","maker","group") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "router_v1.11"."lots" ADD CONSTRAINT "lots_lots_positions_fk" FOREIGN KEY ("chain_id","contract","user") REFERENCES "router_v1.11"."lots_positions"("chain_id","contract","user") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "router_v1.11"."lots" ADD CONSTRAINT "lots_groups_fk" FOREIGN KEY ("chain_id","user","group") REFERENCES "router_v1.11"."groups"("chain_id","maker","group") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "router_v1.11"."lots_positions" ADD CONSTRAINT "lots_positions_position_type_id_position_types_id_fk" FOREIGN KEY ("position_type_id") REFERENCES "router_v1.11"."position_types"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "router_v1.11"."merkle_paths" ADD CONSTRAINT "merkle_paths_offer_hash_offers_hash_fk" FOREIGN KEY ("offer_hash") REFERENCES "router_v1.11"."offers"("hash") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "router_v1.11"."merkle_paths" ADD CONSTRAINT "merkle_paths_tree_root_trees_root_fk" FOREIGN KEY ("tree_root") REFERENCES "router_v1.11"."trees"("root") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "router_v1.11"."obligation_collaterals_v2" ADD CONSTRAINT "obligation_collaterals_v2_obligation_key_obligations_obligation_key_fk" FOREIGN KEY ("obligation_key") REFERENCES "router_v1.11"."obligations"("obligation_key") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "router_v1.11"."obligation_id_keys" ADD CONSTRAINT "obligation_id_keys_obligation_key_obligations_obligation_key_fk" FOREIGN KEY ("obligation_key") REFERENCES "router_v1.11"."obligations"("obligation_key") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "router_v1.11"."offers" ADD CONSTRAINT "offers_obligation_id_obligation_id_keys_obligation_id_fk" FOREIGN KEY ("obligation_id") REFERENCES "router_v1.11"."obligation_id_keys"("obligation_id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "router_v1.11"."offers" ADD CONSTRAINT "offers_groups_fk" FOREIGN KEY ("group_chain_id","group_maker","group_group") REFERENCES "router_v1.11"."groups"("chain_id","maker","group") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "router_v1.11"."offers_callbacks" ADD CONSTRAINT "offers_callbacks_offer_hash_offers_hash_fk" FOREIGN KEY ("offer_hash") REFERENCES "router_v1.11"."offers"("hash") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "router_v1.11"."offsets" ADD CONSTRAINT "offsets_lots_positions_fk" FOREIGN KEY ("chain_id","contract","user") REFERENCES "router_v1.11"."lots_positions"("chain_id","contract","user") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "router_v1.11"."positions" ADD CONSTRAINT "positions_position_type_id_position_types_id_fk" FOREIGN KEY ("position_type_id") REFERENCES "router_v1.11"."position_types"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "router_v1.11"."transfers" ADD CONSTRAINT "transfers_position_type_id_position_types_id_fk" FOREIGN KEY ("position_type_id") REFERENCES "router_v1.11"."position_types"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "router_v1.11"."transfers" ADD CONSTRAINT "transfers_positions_from_fk" FOREIGN KEY ("chain_id","contract","from","position_type_id","asset") REFERENCES "router_v1.11"."positions"("chain_id","contract","user","position_type_id","asset") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "router_v1.11"."transfers" ADD CONSTRAINT "transfers_positions_to_fk" FOREIGN KEY ("chain_id","contract","to","position_type_id","asset") REFERENCES "router_v1.11"."positions"("chain_id","contract","user","position_type_id","asset") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "router_v1.11"."validations" ADD CONSTRAINT "validations_offer_hash_offers_hash_fk" FOREIGN KEY ("offer_hash") REFERENCES "router_v1.11"."offers"("hash") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "router_v1.11"."validations" ADD CONSTRAINT "validations_status_id_status_id_fk" FOREIGN KEY ("status_id") REFERENCES "router_v1.11"."status"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
CREATE UNIQUE INDEX "collectors_chain_name_idx" ON "router_v1.11"."collectors" USING btree ("chain_id","name");--> statement-breakpoint
CREATE INDEX "consumed_events_group_idx" ON "router_v1.11"."consumed_events" USING btree ("chain_id","maker","group");--> statement-breakpoint
CREATE INDEX "consumed_events_block_number_idx" ON "router_v1.11"."consumed_events" USING btree ("block_number");--> statement-breakpoint
CREATE INDEX "groups_chain_id_maker_group_consumed_idx" ON "router_v1.11"."groups" USING btree ("chain_id","maker","group","consumed");--> statement-breakpoint
CREATE INDEX "merkle_paths_tree_root_idx" ON "router_v1.11"."merkle_paths" USING btree ("tree_root");--> statement-breakpoint
CREATE INDEX "obligation_collaterals_v2_obligation_key_idx" ON "router_v1.11"."obligation_collaterals_v2" USING btree ("obligation_key");--> statement-breakpoint
CREATE INDEX "obligation_collaterals_v2_oracle_address_idx" ON "router_v1.11"."obligation_collaterals_v2" USING btree ("oracle_address");--> statement-breakpoint
CREATE INDEX "obligation_id_keys_obligation_key_idx" ON "router_v1.11"."obligation_id_keys" USING btree ("obligation_key");--> statement-breakpoint
CREATE INDEX "obligation_id_keys_chain_id_idx" ON "router_v1.11"."obligation_id_keys" USING btree ("chain_id");--> statement-breakpoint
CREATE INDEX "offers_group_fk_idx" ON "router_v1.11"."offers" USING btree ("group_chain_id","group_maker","group_group");--> statement-breakpoint
CREATE INDEX "offers_group_and_hash_idx" ON "router_v1.11"."offers" USING btree ("group_chain_id","group_maker","group_group","hash");--> statement-breakpoint
CREATE INDEX "offers_obligation_id_side_idx" ON "router_v1.11"."offers" USING btree ("obligation_id","buy");--> statement-breakpoint
CREATE INDEX "transfers_chain_contract_user_idx" ON "router_v1.11"."transfers" USING btree ("chain_id","contract","from","to","block_number");--> statement-breakpoint
CREATE INDEX "transfers_chain_type_block_idx" ON "router_v1.11"."transfers" USING btree ("chain_id","position_type_id","block_number");--> statement-breakpoint
DROP TYPE "router_v1.10"."position_type";--> statement-breakpoint
DROP TYPE "router_v1.10"."status_code";