mirror of
https://github.com/mruwnik/chicken-master.git
synced 2025-06-08 13:24:42 +02:00
migrations
This commit is contained in:
parent
4bd6f4122b
commit
072e0b3f4d
@ -7,7 +7,7 @@ The API for the chickens service.
|
|||||||
1) Setup the development database:
|
1) Setup the development database:
|
||||||
|
|
||||||
docker run --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -p 5432:5432 -d postgres
|
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:
|
2) Start the server:
|
||||||
|
|
||||||
@ -21,3 +21,13 @@ The API for the chickens service.
|
|||||||
|
|
||||||
clojure -X:depstar uberjar
|
clojure -X:depstar uberjar
|
||||||
java -Dconfig=config/dev/config.edn -jar chickens.jar
|
java -Dconfig=config/dev/config.edn -jar chickens.jar
|
||||||
|
|
||||||
|
## Migrations
|
||||||
|
|
||||||
|
To the newest migration
|
||||||
|
|
||||||
|
clojure -A:migrate up
|
||||||
|
|
||||||
|
Down one
|
||||||
|
|
||||||
|
clojure -A:migrate down
|
||||||
|
@ -13,6 +13,9 @@
|
|||||||
{:dev {:jvm-opts ["-Dconfig=config/dev/config.edn"]
|
{:dev {:jvm-opts ["-Dconfig=config/dev/config.edn"]
|
||||||
:main-opts ["-m" "chicken-master.server"]}
|
: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"]
|
:test {:extra-paths ["test"]
|
||||||
:extra-deps {lambdaisland/kaocha {:mvn/version "1.0.732"}}
|
:extra-deps {lambdaisland/kaocha {:mvn/version "1.0.732"}}
|
||||||
:main-opts ["-m" "kaocha.runner"]}
|
:main-opts ["-m" "kaocha.runner"]}
|
||||||
|
@ -1,13 +1,13 @@
|
|||||||
CREATE EXTENSION pgcrypto;
|
{:up ["CREATE EXTENSION pgcrypto;"
|
||||||
CREATE TABLE users (
|
"CREATE TABLE users (
|
||||||
id SERIAL,
|
id SERIAL,
|
||||||
name VARCHAR(256) UNIQUE,
|
name VARCHAR(256) UNIQUE,
|
||||||
password VARCHAR(256),
|
password VARCHAR(256),
|
||||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||||
PRIMARY KEY(id)
|
PRIMARY KEY(id)
|
||||||
);
|
);"
|
||||||
|
|
||||||
CREATE TABLE customers (
|
"CREATE TABLE customers (
|
||||||
id SERIAL,
|
id SERIAL,
|
||||||
name VARCHAR(512),
|
name VARCHAR(512),
|
||||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||||
@ -16,9 +16,9 @@ CREATE TABLE customers (
|
|||||||
PRIMARY KEY(id),
|
PRIMARY KEY(id),
|
||||||
UNIQUE(name, user_id),
|
UNIQUE(name, user_id),
|
||||||
CONSTRAINT fk_users FOREIGN KEY(user_id) REFERENCES users(id)
|
CONSTRAINT fk_users FOREIGN KEY(user_id) REFERENCES users(id)
|
||||||
);
|
);"
|
||||||
|
|
||||||
CREATE TABLE products (
|
"CREATE TABLE products (
|
||||||
id SERIAL,
|
id SERIAL,
|
||||||
name VARCHAR(512),
|
name VARCHAR(512),
|
||||||
amount NUMERIC,
|
amount NUMERIC,
|
||||||
@ -28,10 +28,10 @@ CREATE TABLE products (
|
|||||||
PRIMARY KEY(id),
|
PRIMARY KEY(id),
|
||||||
UNIQUE(name, user_id),
|
UNIQUE(name, user_id),
|
||||||
CONSTRAINT fk_users FOREIGN KEY(user_id) REFERENCES users(id)
|
CONSTRAINT fk_users FOREIGN KEY(user_id) REFERENCES users(id)
|
||||||
);
|
);"
|
||||||
|
|
||||||
CREATE TYPE order_state AS ENUM('waiting', 'fulfilled', 'canceled');
|
"CREATE TYPE order_state AS ENUM('waiting', 'fulfilled', 'canceled');"
|
||||||
CREATE TABLE orders (
|
"CREATE TABLE orders (
|
||||||
id SERIAL,
|
id SERIAL,
|
||||||
customer_id INT,
|
customer_id INT,
|
||||||
notes TEXT,
|
notes TEXT,
|
||||||
@ -42,9 +42,9 @@ CREATE TABLE orders (
|
|||||||
PRIMARY KEY(id),
|
PRIMARY KEY(id),
|
||||||
CONSTRAINT fk_customer FOREIGN KEY(customer_id) REFERENCES customers(id),
|
CONSTRAINT fk_customer FOREIGN KEY(customer_id) REFERENCES customers(id),
|
||||||
CONSTRAINT fk_users FOREIGN KEY(user_id) REFERENCES users(id)
|
CONSTRAINT fk_users FOREIGN KEY(user_id) REFERENCES users(id)
|
||||||
);
|
);"
|
||||||
|
|
||||||
CREATE TABLE order_products (
|
"CREATE TABLE order_products (
|
||||||
id SERIAL,
|
id SERIAL,
|
||||||
order_id INT,
|
order_id INT,
|
||||||
product_id INT,
|
product_id INT,
|
||||||
@ -52,4 +52,11 @@ CREATE TABLE order_products (
|
|||||||
PRIMARY KEY(id),
|
PRIMARY KEY(id),
|
||||||
CONSTRAINT fk_order FOREIGN KEY(order_id) REFERENCES orders(id),
|
CONSTRAINT fk_order FOREIGN KEY(order_id) REFERENCES orders(id),
|
||||||
CONSTRAINT fk_product FOREIGN KEY(product_id) REFERENCES products(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"]}
|
19
backend/resources/migrations/002-customer-groups.edn
Normal file
19
backend/resources/migrations/002-customer-groups.edn
Normal 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;"]}
|
14
backend/src/chicken_master/migrate.clj
Normal file
14
backend/src/chicken_master/migrate.clj
Normal 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")))
|
Loading…
x
Reference in New Issue
Block a user