Skip to content

Commit

Permalink
Add migration with ragtime
Browse files Browse the repository at this point in the history
  • Loading branch information
tarun-nil committed Nov 30, 2023
1 parent 21d9a76 commit 49f2f6a
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 1 deletion.
5 changes: 4 additions & 1 deletion swift-ticketing/project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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"]}})
Original file line number Diff line number Diff line change
@@ -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;
106 changes: 106 additions & 0 deletions swift-ticketing/resources/migrations/001-swift-ticketing.up.sql
Original file line number Diff line number Diff line change
@@ -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)
);

29 changes: 29 additions & 0 deletions swift-ticketing/src/swift_ticketing/migrations.clj
Original file line number Diff line number Diff line change
@@ -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))))

0 comments on commit 49f2f6a

Please sign in to comment.