From 49f2f6a9c2922742d84219e5ba19e84bbcea78f5 Mon Sep 17 00:00:00 2001 From: Tarun Date: Thu, 30 Nov 2023 16:36:44 +0530 Subject: [PATCH] Add migration with ragtime --- swift-ticketing/project.clj | 5 +- .../migrations/001-swift-ticketing.down.sql | 9 ++ .../migrations/001-swift-ticketing.up.sql | 106 ++++++++++++++++++ .../src/swift_ticketing/migrations.clj | 29 +++++ 4 files changed, 148 insertions(+), 1 deletion(-) create mode 100644 swift-ticketing/resources/migrations/001-swift-ticketing.down.sql create mode 100644 swift-ticketing/resources/migrations/001-swift-ticketing.up.sql create mode 100644 swift-ticketing/src/swift_ticketing/migrations.clj diff --git a/swift-ticketing/project.clj b/swift-ticketing/project.clj index 08e9c66..6928742 100644 --- a/swift-ticketing/project.clj +++ b/swift-ticketing/project.clj @@ -19,10 +19,13 @@ [org.clojure/core.async "1.6.681"] [metosin/compojure-api "2.0.0-alpha31"] [org.clojure/data.json "2.4.0"] - [com.taoensso/carmine "3.3.2"]] + [com.taoensso/carmine "3.3.2"] + [ragtime "0.8.0"]] :main ^:skip-aot swift-ticketing.core :target-path "target/%s" :plugins [[lein-ring "0.12.6"]] :ring {:handler swift-ticketing.app/swift-ticketing-app} + :aliases {"migrate" ["run" "-m" "swift-ticketing.migrations/migrate"] + "rollback" ["run" "-m" "swift-ticketing.migrations/rollback"]} :profiles {:uberjar {:aot :all :jvm-opts ["-Dclojure.compiler.direct-linking=true"]}}) diff --git a/swift-ticketing/resources/migrations/001-swift-ticketing.down.sql b/swift-ticketing/resources/migrations/001-swift-ticketing.down.sql new file mode 100644 index 0000000..961491b --- /dev/null +++ b/swift-ticketing/resources/migrations/001-swift-ticketing.down.sql @@ -0,0 +1,9 @@ +DROP TABLE swift_ticketing.ticket; +DROP TABLE swift_ticketing.booking; +DROP TABLE swift_ticketing.ticket_type; +DROP TABLE swift_ticketing.event; +DROP TABLE swift_ticketing.user_account; + +DROP TYPE swift_ticketing.booking_status; +DROP TYPE swift_ticketing.ticket_status; +DROP TYPE swift_ticketing.seat_type; diff --git a/swift-ticketing/resources/migrations/001-swift-ticketing.up.sql b/swift-ticketing/resources/migrations/001-swift-ticketing.up.sql new file mode 100644 index 0000000..b5d5943 --- /dev/null +++ b/swift-ticketing/resources/migrations/001-swift-ticketing.up.sql @@ -0,0 +1,106 @@ +CREATE SCHEMA IF NOT EXISTS swift_ticketing; +--;; +CREATE TABLE swift_ticketing.user_account ( + user_id UUID PRIMARY KEY, + name TEXT NOT NULL, + created_at timestamptz NOT NULL DEFAULT now(), + updated_at timestamptz NOT NULL DEFAULT now() +); + +-- CREATE TABLE swift_ticketing.venue ( +-- venue_id UUID PRIMARY KEY, +-- venue_name TEXT NOT NULL, +-- venue_description TEXT, +-- location POINT NOT NULL, -- Add lat lng here +-- created_at timestamptz NOT NULL DEFAULT now(), +-- updated_at timestamptz NOT NULL DEFAULT now() +-- ); +-- +-- CREATE TABLE swift_ticketing.event ( +-- event_id UUID PRIMARY KEY, +-- event_name TEXT NOT NULL, +-- event_description TEXT, +-- event_date_from timestamptz NOT NULL, +-- event_date_to timestamptz NOT NULL, +-- venue_id UUID NOT NULL, +-- organizer_id UUID NOT NULL, +-- created_at timestamptz NOT NULL DEFAULT now(), +-- updated_at timestamptz NOT NULL DEFAULT now(), +-- FOREIGN KEY(venue_id) REFERENCES swift_ticketing.venue(venue_id), +-- FOREIGN KEY(organizer_id) REFERENCES swift_ticketing.user(user_id) +-- ); +CREATE TABLE swift_ticketing.event ( + event_id UUID PRIMARY KEY, + event_name TEXT NOT NULL, + event_description TEXT, + event_date timestamptz NOT NULL, + venue TEXT NOT NULL, + organizer_id UUID NOT NULL, + created_at timestamptz NOT NULL DEFAULT now(), + updated_at timestamptz NOT NULL DEFAULT now() + -- FOREIGN KEY(organizer_id) REFERENCES swift_ticketing.user_account(user_id) +); + +-- CREATE TABLE swift_ticketing.category ( +-- category_id UUID PRIMARY KEY, +-- category_name TEXT NOT NULL, +-- category_parent UUID, +-- created_at timestamptz NOT NULL DEFAULT now(), +-- updated_at timestamptz NOT NULL DEFAULT now(), +-- FOREIGN KEY(category_parent) REFERENCES swift_ticketing.category(category_id) +-- ); + +-- CREATE TABLE swift_ticketing.event_category ( +-- category_id UUID PRIMARY KEY, +-- event_id UUID NOT NULL, +-- created_at timestamptz NOT NULL DEFAULT now(), +-- updated_at timestamptz NOT NULL DEFAULT now(), +-- FOREIGN KEY(category_id) REFERENCES swift_ticketing.category(category_id), +-- FOREIGN KEY(event_id) REFERENCES swift_ticketing.event(event_id) +-- ); + +CREATE TYPE swift_ticketing.booking_status AS ENUM ('Confirmed', 'Canceled', 'InProcess', 'PaymentPending', 'Rejected'); +CREATE TYPE swift_ticketing.ticket_status AS ENUM ('Available', 'Booked', 'Reserved'); +CREATE TYPE swift_ticketing.seat_type AS ENUM ('General', 'Named'); + +CREATE TABLE swift_ticketing.booking ( + booking_id UUID PRIMARY KEY, + user_id UUID NOT NULL, + booking_status swift_ticketing.booking_status NOT NULL, + -- ticket_count INT NOT NULL, + -- amount NUMERIC NOT NULL, + created_at timestamptz NOT NULL DEFAULT now(), + updated_at timestamptz NOT NULL DEFAULT now() + -- FOREIGN KEY(ticket_id) REFERENCES swift_ticketing.ticket(ticket_id), + -- FOREIGN KEY(user_id) REFERENCES swift_ticketing.user_account(user_id) +); + +CREATE TABLE swift_ticketing.ticket_type ( + ticket_type_id UUID PRIMARY KEY, + ticket_type TEXT NOT NULL, + ticket_type_description TEXT, + event_id UUID NOT NULL, + reservation_timelimit_seconds INT, + seat_type swift_ticketing.seat_type NOT NULL, + created_at timestamptz NOT NULL DEFAULT now(), + updated_at timestamptz NOT NULL DEFAULT now(), + FOREIGN KEY(event_id) REFERENCES swift_ticketing.event(event_id) +); + +CREATE TABLE swift_ticketing.ticket ( + ticket_id UUID PRIMARY KEY, + ticket_name TEXT NULL, + ticket_type_id UUID NOT NULL, + -- ticket_timing_from timestamptz NOT NULL, + -- ticket_timing_to timestamptz NOT NULL, + ticket_price NUMERIC Not NULL, + reservation_expiration_time timestamptz, + ticket_status swift_ticketing.ticket_status NOT NULL, + -- ticket_total INT NOT NULL, + booking_id UUID, + created_at timestamptz NOT NULL DEFAULT now(), + updated_at timestamptz NOT NULL DEFAULT now(), + FOREIGN KEY(ticket_type_id) REFERENCES swift_ticketing.ticket_type(ticket_type_id), + FOREIGN KEY(booking_id) REFERENCES swift_ticketing.booking(booking_id) +); + diff --git a/swift-ticketing/src/swift_ticketing/migrations.clj b/swift-ticketing/src/swift_ticketing/migrations.clj new file mode 100644 index 0000000..3b44f1c --- /dev/null +++ b/swift-ticketing/src/swift_ticketing/migrations.clj @@ -0,0 +1,29 @@ +(ns swift-ticketing.migrations + (:require [ragtime.jdbc :as jdbc] + [ragtime.repl :as repl] + [swift-ticketing.config :as config])) + +(defn make-connection-uri [db-config] + (str "jdbc:postgresql://" (:host db-config) ":" (:port db-config) + "/" (:dbname db-config) "?user=" (:username db-config) + "&password=" (:password db-config))) + +(defn migration-config [db-config] + {:datastore (jdbc/sql-database + {:connection-uri (make-connection-uri db-config)}) + :migrations (jdbc/load-resources "migrations")}) + +(defn migrate-with [db-config] + (repl/migrate (migration-config db-config))) + +(defn migrate [] + (migrate-with (:database (config/read-app-config)))) + +(defn rollback-with [db-config] + (repl/rollback (migration-config db-config))) + +(defn rollback [] + (rollback-with (:database (config/read-app-config)))) + +(comment (migrate-with (:database (config/read-test-config))) + (rollback-with (:database (config/read-test-config))))