diff --git a/drizzle/migrations/0008_wooden_purple_man.sql b/drizzle/migrations/0008_wooden_purple_man.sql new file mode 100644 index 0000000..a3d134d --- /dev/null +++ b/drizzle/migrations/0008_wooden_purple_man.sql @@ -0,0 +1,7 @@ +ALTER TABLE "schedule" DROP CONSTRAINT "schedule_station_origin_id_station_id_fk"; +--> statement-breakpoint +DO $$ BEGIN + ALTER TABLE "schedule" ADD CONSTRAINT "schedule_station_origin_id_station_id_fk" FOREIGN KEY ("station_origin_id") REFERENCES "public"."station"("id") ON DELETE cascade ON UPDATE no action; +EXCEPTION + WHEN duplicate_object THEN null; +END $$; diff --git a/drizzle/migrations/meta/0008_snapshot.json b/drizzle/migrations/meta/0008_snapshot.json new file mode 100644 index 0000000..9fbaaa7 --- /dev/null +++ b/drizzle/migrations/meta/0008_snapshot.json @@ -0,0 +1,323 @@ +{ + "id": "ae343a69-44a3-4c74-9916-0e7ac4f8f116", + "prevId": "511ffc79-573f-46fb-adaf-1de89c7f7d69", + "version": "7", + "dialect": "postgresql", + "tables": { + "public.station": { + "name": "station", + "schema": "", + "columns": { + "uid": { + "name": "uid", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "id": { + "name": "id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "type": { + "name": "type", + "type": "station_type", + "typeSchema": "public", + "primaryKey": false, + "notNull": true + }, + "metadata": { + "name": "metadata", + "type": "jsonb", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "station_uidx": { + "name": "station_uidx", + "columns": [ + { + "expression": "uid", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + }, + "station_idx": { + "name": "station_idx", + "columns": [ + { + "expression": "id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "station_type_idx": { + "name": "station_type_idx", + "columns": [ + { + "expression": "type", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": { + "station_uid_unique": { + "name": "station_uid_unique", + "nullsNotDistinct": false, + "columns": [ + "uid" + ] + }, + "station_id_unique": { + "name": "station_id_unique", + "nullsNotDistinct": false, + "columns": [ + "id" + ] + } + } + }, + "public.schedule": { + "name": "schedule", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "station_id": { + "name": "station_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "station_origin_id": { + "name": "station_origin_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "station_destination_id": { + "name": "station_destination_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "train_id": { + "name": "train_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "line": { + "name": "line", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "route": { + "name": "route", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "departs_at": { + "name": "departs_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "arrives_at": { + "name": "arrives_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "metadata": { + "name": "metadata", + "type": "jsonb", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "schedule_idx": { + "name": "schedule_idx", + "columns": [ + { + "expression": "id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + }, + "schedule_station_idx": { + "name": "schedule_station_idx", + "columns": [ + { + "expression": "station_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "schedule_train_idx": { + "name": "schedule_train_idx", + "columns": [ + { + "expression": "train_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "schedule_station_id_station_id_fk": { + "name": "schedule_station_id_station_id_fk", + "tableFrom": "schedule", + "tableTo": "station", + "columnsFrom": [ + "station_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "schedule_station_origin_id_station_id_fk": { + "name": "schedule_station_origin_id_station_id_fk", + "tableFrom": "schedule", + "tableTo": "station", + "columnsFrom": [ + "station_origin_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "schedule_station_destination_id_station_id_fk": { + "name": "schedule_station_destination_id_station_id_fk", + "tableFrom": "schedule", + "tableTo": "station", + "columnsFrom": [ + "station_destination_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": { + "schedule_id_unique": { + "name": "schedule_id_unique", + "nullsNotDistinct": false, + "columns": [ + "id" + ] + } + } + } + }, + "enums": { + "public.station_type": { + "name": "station_type", + "schema": "public", + "values": [ + "KRL", + "MRT", + "LRT", + "LOCAL" + ] + } + }, + "schemas": {}, + "sequences": {}, + "_meta": { + "columns": {}, + "schemas": {}, + "tables": {} + } +} \ No newline at end of file diff --git a/drizzle/migrations/meta/_journal.json b/drizzle/migrations/meta/_journal.json index 7b19e5b..4eba11e 100644 --- a/drizzle/migrations/meta/_journal.json +++ b/drizzle/migrations/meta/_journal.json @@ -57,6 +57,13 @@ "when": 1732445107060, "tag": "0007_naive_pepper_potts", "breakpoints": true + }, + { + "idx": 8, + "version": "7", + "when": 1743344871226, + "tag": "0008_wooden_purple_man", + "breakpoints": true } ] } \ No newline at end of file diff --git a/src/db/schema/schedule.table.ts b/src/db/schema/schedule.table.ts index 04cbd9e..9700599 100644 --- a/src/db/schema/schedule.table.ts +++ b/src/db/schema/schedule.table.ts @@ -31,10 +31,10 @@ export const scheduleTable = pgTable( onDelete: "cascade", }), station_origin_id: text("station_origin_id") + .notNull() .references(() => stationTable.id, { - onDelete: "set null", - }) - .notNull(), + onDelete: "cascade", + }), station_destination_id: text("station_destination_id") .references(() => stationTable.id, { onDelete: "set null",