Skip to content

Instantly share code, notes, and snippets.

@augustl
Created June 6, 2014 19:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save augustl/41ecaea6e257e5e2fe7e to your computer and use it in GitHub Desktop.
Save augustl/41ecaea6e257e5e2fe7e to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
<changeSet id="1402072059" author="augustl">
<createTable tableName="apps">
<column name="id" type="int" autoIncrement="true">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="app_name" type="varchar">
<constraints nullable="false"/>
</column>
<column name="app_ident" type="varchar">
<constraints nullable="false"/>
</column>
<column name="app_icon" type="blob">
</column>
</createTable>
<createTable tableName="permissions">
<column name="id" type="int" autoIncrement="true">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="auth_token" type="binary(64)">
<constraints nullable="false"/>
</column>
<column name="app_id" type="int">
<constraints nullable="false"/>
</column>
</createTable>
<addForeignKeyConstraint
baseTableName="permissions"
baseColumnNames="app_id"
referencedTableName="apps"
referencedColumnNames="id"
constraintName="fk_permission_app"></addForeignKeyConstraint>
</changeSet>
</databaseChangeLog>
(ns my-thing
(:import [java.sql Connection DriverManager]
[java.util Properties]
[liquibase Liquibase]
[liquibase.resource ClassLoaderResourceAccessor]
[liquibase.database DatabaseFactory]
[liquibase.database.jvm JdbcConnection]))
(def default-properties
(doto (Properties.)
(.put "user" "ua")
(.put "password" "")))
(defn get-connection [db-name props]
(DriverManager/getConnection (str"jdbc:h2:" db-name) props))
(defn get-liquibase [conn changelog-path]
(Liquibase.
changelog-path
(ClassLoaderResourceAccessor. (.getContextClassLoader (Thread/currentThread)))
(.findCorrectDatabaseImplementation (DatabaseFactory/getInstance) (JdbcConnection. conn))))
(defn ^Connection get-h2-database [db-name liquibase-schema]
(let [conn (get-connection db-name default-properties)]
(-> (get-liquibase conn liquibase-schema)
(.update nil))
conn))
;; test
(deftest a-test
(let [conn (get-database "mem:" "h2_schema.xml")]
(doto (.prepareStatement conn "INSERT INTO apps (app_name, app_ident) VALUES (?, ?)")
(.setString 1 "My app")
(.setString 2 "com.augustl.myapp")
(.execute))
(let [rs (-> (.prepareStatement conn "SELECT * FROM apps")
(.executeQuery))]
(.next rs)
(is (= "My app" (.getString rs (.findColumn rs "app_name"))))
(is (= "com.augustl.myapp" (.getString rs (.findColumn rs "app_ident")))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment