migrations

This commit is contained in:
Daniel O'Connell 2021-03-13 12:09:35 +01:00
parent 4bd6f4122b
commit 072e0b3f4d
5 changed files with 66 additions and 13 deletions

View File

@ -7,7 +7,7 @@ The API for the chickens service.
1) Setup the development database:
docker run --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -p 5432:5432 -d postgres
psql 'postgresql://localhost/postgres?user=postgres&password=mysecretpassword' < resources/schema.sql
clojure -A:migrate up
2) Start the server:
@ -21,3 +21,13 @@ The API for the chickens service.
clojure -X:depstar uberjar
java -Dconfig=config/dev/config.edn -jar chickens.jar
## Migrations
To the newest migration
clojure -A:migrate up
Down one
clojure -A:migrate down

View File

@ -13,6 +13,9 @@
{:dev {:jvm-opts ["-Dconfig=config/dev/config.edn"]
:main-opts ["-m" "chicken-master.server"]}
:migrate {:extra-deps {ragtime/ragtime {:mvn/version "0.8.1"}}
:main-opts ["-m" "chicken-master.migrate"]}
:test {:extra-paths ["test"]
:extra-deps {lambdaisland/kaocha {:mvn/version "1.0.732"}}
:main-opts ["-m" "kaocha.runner"]}

View File

@ -1,13 +1,13 @@
CREATE EXTENSION pgcrypto;
CREATE TABLE users (
{:up ["CREATE EXTENSION pgcrypto;"
"CREATE TABLE users (
id SERIAL,
name VARCHAR(256) UNIQUE,
password VARCHAR(256),
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
PRIMARY KEY(id)
);
);"
CREATE TABLE customers (
"CREATE TABLE customers (
id SERIAL,
name VARCHAR(512),
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
@ -16,9 +16,9 @@ CREATE TABLE customers (
PRIMARY KEY(id),
UNIQUE(name, user_id),
CONSTRAINT fk_users FOREIGN KEY(user_id) REFERENCES users(id)
);
);"
CREATE TABLE products (
"CREATE TABLE products (
id SERIAL,
name VARCHAR(512),
amount NUMERIC,
@ -28,10 +28,10 @@ CREATE TABLE products (
PRIMARY KEY(id),
UNIQUE(name, user_id),
CONSTRAINT fk_users FOREIGN KEY(user_id) REFERENCES users(id)
);
);"
CREATE TYPE order_state AS ENUM('waiting', 'fulfilled', 'canceled');
CREATE TABLE orders (
"CREATE TYPE order_state AS ENUM('waiting', 'fulfilled', 'canceled');"
"CREATE TABLE orders (
id SERIAL,
customer_id INT,
notes TEXT,
@ -42,9 +42,9 @@ CREATE TABLE orders (
PRIMARY KEY(id),
CONSTRAINT fk_customer FOREIGN KEY(customer_id) REFERENCES customers(id),
CONSTRAINT fk_users FOREIGN KEY(user_id) REFERENCES users(id)
);
);"
CREATE TABLE order_products (
"CREATE TABLE order_products (
id SERIAL,
order_id INT,
product_id INT,
@ -52,4 +52,11 @@ CREATE TABLE order_products (
PRIMARY KEY(id),
CONSTRAINT fk_order FOREIGN KEY(order_id) REFERENCES orders(id),
CONSTRAINT fk_product FOREIGN KEY(product_id) REFERENCES products(id)
);
);"]
:down ["DROP TABLE order_products"
"DROP TABLE products"
"DROP TABLE orders"
"DROP TYPE order_state"
"DROP TABLE customers"
"DROP TABLE users"
"DROP EXTENSION pgcrypto"]}

View File

@ -0,0 +1,19 @@
{:up ["CREATE TABLE customer_groups (
id SERIAL,
customer_id INT,
name VARCHAR(512),
user_id INT,
PRIMARY KEY(id),
CONSTRAINT fk_customer FOREIGN KEY(customer_id) REFERENCES customers(id)
);"
"CREATE TABLE customer_group_products (
id SERIAL,
customer_group_id INT,
product_id INT,
amount NUMERIC,
PRIMARY KEY(id),
CONSTRAINT fk_customer_groups FOREIGN KEY(customer_group_id) REFERENCES customer_groups(id),
CONSTRAINT fk_product FOREIGN KEY(product_id) REFERENCES products(id)
);"]
:down ["DROP TABLE customer_group_products;"
"DROP TABLE customer_groups;"]}

View File

@ -0,0 +1,14 @@
(ns chicken-master.migrate
(:require [ragtime.jdbc :as jdbc]
[ragtime.repl :as repl]
[config.core :refer [env]]))
(def config
{:datastore (jdbc/sql-database {:connection-uri (-> env :db-uri :jdbcUrl)})
:migrations (jdbc/load-resources "migrations")})
(defn -main [command]
(condp = command
"up" (repl/migrate config)
"down" (repl/rollback config)
(println "up|migrate")))