Skip to content

baoliang/rui-mig

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

rui-mig

A Leiningen plugin for relational-database migration

Support

PostgreSQL and MySQL

Usage

Leiningen (via Clojars)

Put one of the following into the :plugins vector of the :user profile in your ~/.lein/profiles.clj:

Clojars Project

At first please create a file named config.json to store database infomation e.g MySQL

{
  "db": {
    "db-name": "eg",
    "user": "root",
    "host": "127.0.0.1",
    "port": "3306",
    "password": "123",
    "subprotocol": "mysql"
  }
}  

PostgreSQL

{
  "db": {
    "db-name": "eg",
    "user": "postgres",
    "host": "localhost",
    "port": "5433",
    "password": "123456",
    "subprotocol": "postgresql"
  }
}

If you want add a migration:

lein rui-mig create

It will generat a script of src/projectname/migrations/dataetimexxxx.clj and it's content like follow

(ns projectname.migrations.m20150618164213
            (:require [clojure.java.jdbc :as sql]
                      [clojure.data.json :as json]))
              (def config
                (json/read-str (slurp "./config.json")))

            (def database
                {:subprotocol  (get-in config ["db" "subprotocol"])
                 :subname  (str  (str "//" (get-in config ["db"  "host"]) ":" (get-in config ["db"  "port"]) "/" (get-in config ["db" "db-name"]) (if (= "mysql" (get-in config ["db" "subprotocol"])) "?useUnicode=true&characterEncoding=UTF-8" "")))
                 :user (get-in config ["db" "user"])
                 :password (get-in config ["db" "password"])})
          
            (defn execute-in-db! [ & queries]
              (doseq [q queries]
                (sql/with-db-transaction [db database]
                                         (sql/execute! db   (if (string? q) [q] q)))))

            (defn up[]
              (execute-in-db! ""))
          

You can write your sql-scripts into the generated up function

If you want run migration:

lein rui-mig #Default to run the new migration

If you want deploy on jar (lein uberjar of the project to generate the file of jar)

At first you must add follow to project.clj on :dependencies

[org.clojure/java.jdbc "0.3.3"]
[org.clojure/data.json "0.2.4"]
[org.clojars.kbarber/postgresql "9.2-1002.jdbc4"] ;If your database is PostgreSQL
[mysql/mysql-connector-java "5.1.25"] ;If your database is MySQL

And then run

lein rui-mig deploy 

It will generate the file of projectName.main.clj and add this projectName.main to project.clj :main

And then you run

lein  uberjar

It will generate a file of jar and you run

 java -cp target/xxx.jar xxx.main  # To run migration by jar

It will run migration by file of jar

Thanks

Thanks for Xiang tao Zhou and 中瑞财富 for authorizing me to share this lein plugin.

About

A Leiningen plugin for relational-database migration

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors