Created
December 23, 2017 20:57
-
-
Save nihirash/390e41e75987c6e926814449b8665b73 to your computer and use it in GitHub Desktop.
Simplest ever migrations for clojure project.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (ns config) | |
| (def db-spec (or (System/getenv "DATABASE_URL") | |
| "postgres://pin:password@localhost:5432/pin")) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (ns migration | |
| (:require [clojure.java.jdbc :as sql] | |
| [config])) | |
| (defn- db-do | |
| "Do something with db connection" | |
| [fn] | |
| (sql/db-do-commands config/db-spec fn)) | |
| (defn init! | |
| "Inits migration engine - creates migration table" | |
| [] | |
| (try | |
| (db-do (sql/create-table-ddl :migrations [[:name "VARCHAR(80) PRIMARY KEY"]])) | |
| (catch Exception _))) | |
| (defn migrated? | |
| "Is migration already performed?" | |
| [name] | |
| (-> (sql/query config/db-spec ["select count(*) from migrations where name = ?" name]) | |
| first | |
| :count | |
| pos?)) | |
| (defn migration-applied! | |
| [name] | |
| (sql/insert! config/db-spec | |
| :migrations | |
| [:name] | |
| [name])) | |
| (defn add-table! | |
| "Simple adds table to database. If migration already applied - will return false and no makes changes to database" | |
| [migration-name table spec] | |
| (cond | |
| (migrated? migration-name) false | |
| :else (do | |
| (db-do (sql/create-table-ddl table spec)) | |
| (migration-applied! migration-name)))) | |
| (defn sql-request! | |
| "Plain sql-request migration. Applies once(checks by name of migration)" | |
| [migration-name query] | |
| (cond | |
| (migrated? migration-name) false | |
| :else (do | |
| (migration-applied! migration-name) | |
| (sql/query config/db-spec query)))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (ns project.migrations | |
| (:require [migration :as m])) | |
| (declare create-users create-chains create-posts) | |
| (defn migrate | |
| "Perform all migrations" | |
| [] | |
| (m/init!) | |
| (create-users) | |
| (create-chains) | |
| (create-posts)) | |
| ;; --------------- Here goes migrations ----------------------------------- | |
| (defn- create-users | |
| [] | |
| (m/add-table! "Create table users" :users [[:uuid "UUID PRIMARY KEY"] | |
| [:name :varchar] | |
| [:hash :varchar] | |
| [:local :boolean]])) | |
| (defn- create-chains | |
| [] | |
| (m/add-table! "Create table chains" :chains [[:uuid "UUID PRIMARY KEY"] | |
| [:name :varchar]])) | |
| (defn- create-posts | |
| [] | |
| (m/add-table! "Create table posts" :posts [[:uuid "UUID PRIMARY KEY"] | |
| [:creator_uuid :uuid] | |
| [:chain_uuid :uuid] | |
| [:title :varchar] | |
| [:body :text] | |
| [:branch :uuid]])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment