-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from juanedi/crystal-db
Use Crystal-DB API
- Loading branch information
Showing
13 changed files
with
169 additions
and
76 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
/doc/ | ||
/libs/ | ||
/.crystal/ | ||
/lib/ | ||
/bin/ | ||
/.shards/ | ||
|
||
|
||
# Libraries don't need dependency lock | ||
# Dependencies will be locked in application that uses them | ||
/shard.lock | ||
|
This file contains 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
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#! /usr/bin/env crystal | ||
|
||
# | ||
# To build a standalone command line client, require the | ||
# driver you wish to use and use `Micrate::Cli`. | ||
# | ||
|
||
require "../src/micrate" | ||
require "pg" | ||
|
||
Micrate::DB.connection_url = "postgresql://..." | ||
Micrate::Cli.run |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
name: micrate | ||
version: 0.1.0 | ||
version: 0.3.0 | ||
|
||
authors: | ||
- Juan Edi <jedi11235@gmail.com> | ||
|
||
dependencies: | ||
pg: | ||
github: will/crystal-pg | ||
db: | ||
github: crystal-lang/crystal-db |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,67 @@ | ||
require "pg" | ||
require "db" | ||
require "./db/*" | ||
|
||
module Micrate | ||
module DB | ||
@@connection_url = ENV["PG_URL"]? | ||
@@connection_url = ENV["DB_URL"]? | ||
|
||
def self.connection_url | ||
@@connection_url | ||
end | ||
|
||
def self.connection_url=(connection_url) | ||
@@dialect = nil | ||
@@connection_url = connection_url | ||
end | ||
|
||
def self.connect | ||
if !@@connection_url | ||
raise "No postgresql connection URL is configured. Please set the PG_URL environment variable." | ||
end | ||
|
||
PG.connect(@@connection_url.not_nil!) | ||
validate_connection_url | ||
::DB.connect(@@connection_url.not_nil!) | ||
end | ||
|
||
def self.connect(&block) | ||
db = connect | ||
begin | ||
validate_connection_url | ||
::DB.open @@connection_url.not_nil! do |db| | ||
yield db | ||
ensure | ||
db.close | ||
end | ||
end | ||
|
||
def self.get_versions_last_first_order(db) | ||
db.exec({Int64, Bool}, "SELECT version_id, is_applied from micrate_db_version ORDER BY id DESC").rows | ||
db.query_all "SELECT version_id, is_applied from micrate_db_version ORDER BY id DESC", as: {Int64, Bool} | ||
end | ||
|
||
def self.create_migrations_table(db) | ||
db.exec("CREATE TABLE micrate_db_version ( | ||
id serial NOT NULL, | ||
version_id bigint NOT NULL, | ||
is_applied boolean NOT NULL, | ||
tstamp timestamp NULL default now(), | ||
PRIMARY KEY(id) | ||
);") | ||
dialect.query_create_migrations_table(db) | ||
end | ||
|
||
def self.record_migration(migration, direction, db) | ||
is_applied = direction == :forward | ||
db.exec("INSERT INTO micrate_db_version (version_id, is_applied) VALUES ($1, $2);", [migration.version, is_applied]) | ||
dialect.query_record_migration(migration, is_applied, db) | ||
end | ||
|
||
def self.exec(statement, db) | ||
db.exec(statement) | ||
end | ||
|
||
def self.get_migration_status(migration, db) : Time? | ||
rows = db.exec({Time, Bool}, "SELECT tstamp, is_applied FROM micrate_db_version WHERE version_id=$1 ORDER BY tstamp DESC LIMIT 1", [migration.version]).rows | ||
rows = dialect.query_migration_status(migration, db) | ||
|
||
if !rows.empty? && rows[0][1] | ||
rows[0][0] | ||
else | ||
nil | ||
end | ||
end | ||
|
||
private def self.dialect | ||
validate_connection_url | ||
@@dialect ||= Dialect.from_connection_url(@@connection_url.not_nil!) | ||
end | ||
|
||
private def self.validate_connection_url | ||
if !@@connection_url | ||
raise "No database connection URL is configured. Please set the DB_URL environment variable." | ||
end | ||
end | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
module Micrate::DB | ||
abstract class Dialect | ||
abstract def query_create_migrations_table(db) | ||
abstract def query_migration_status(migration, db) | ||
abstract def query_record_migration(migration, is_applied, db) | ||
|
||
def self.from_connection_url(connection_url : String) | ||
uri = URI.parse(connection_url) | ||
case uri.scheme | ||
when "postgresql", "postgres" | ||
Postgres.new | ||
when "mysql" | ||
Mysql.new | ||
when "sqlite3" | ||
Sqlite3.new | ||
else | ||
raise "Could not infer SQL dialect from connection url #{connection_url}" | ||
end | ||
end | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
module Micrate::DB | ||
class Mysql < Dialect | ||
def query_create_migrations_table(db) | ||
db.exec("CREATE TABLE micrate_db_version ( | ||
id serial NOT NULL, | ||
version_id bigint NOT NULL, | ||
is_applied boolean NOT NULL, | ||
tstamp timestamp NULL default now(), | ||
PRIMARY KEY(id) | ||
);") | ||
end | ||
|
||
def query_migration_status(migration, db) | ||
db.query_all "SELECT tstamp, is_applied FROM micrate_db_version WHERE version_id=? ORDER BY tstamp DESC LIMIT 1", migration.version, as: {Time, Bool} | ||
end | ||
|
||
def query_record_migration(migration, is_applied, db) | ||
db.exec("INSERT INTO micrate_db_version (version_id, is_applied) VALUES (?, ?);", [migration.version, is_applied]) | ||
end | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
module Micrate::DB | ||
class Postgres < Dialect | ||
def query_create_migrations_table(db) | ||
db.exec("CREATE TABLE micrate_db_version ( | ||
id serial NOT NULL, | ||
version_id bigint NOT NULL, | ||
is_applied boolean NOT NULL, | ||
tstamp timestamp NULL default now(), | ||
PRIMARY KEY(id) | ||
);") | ||
end | ||
|
||
def query_migration_status(migration, db) | ||
db.query_all "SELECT tstamp, is_applied FROM micrate_db_version WHERE version_id=$1 ORDER BY tstamp DESC LIMIT 1", migration.version, as: {Time, Bool} | ||
end | ||
|
||
def query_record_migration(migration, is_applied, db) | ||
db.exec("INSERT INTO micrate_db_version (version_id, is_applied) VALUES ($1, $2);", [migration.version, is_applied]) | ||
end | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module Micrate::DB | ||
class Sqlite3 < Dialect | ||
def query_create_migrations_table(db) | ||
# The current sqlite drive implementation does not store timestamps in the same | ||
# format as the ones autogenerated by sqlite. | ||
# | ||
# As a workaround, we create timestamps locally so that the driver decides timestamp | ||
# formats when writing and reading. | ||
db.exec("CREATE TABLE micrate_db_version ( | ||
id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
version_id INTEGER NOT NULL, | ||
is_applied INTEGER NOT NULL, | ||
tstamp TIMESTAMP | ||
);") | ||
end | ||
|
||
def query_migration_status(migration, db) | ||
db.query_all "SELECT tstamp, is_applied FROM micrate_db_version WHERE version_id=? ORDER BY tstamp DESC LIMIT 1", migration.version, as: {Time, Bool} | ||
end | ||
|
||
def query_record_migration(migration, is_applied, db) | ||
db.exec("INSERT INTO micrate_db_version (version_id, is_applied, tstamp) VALUES (?, ?, ?);", [migration.version, is_applied, Time.now]) | ||
end | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module Micrate | ||
VERSION = "0.2.2" | ||
VERSION = "0.3.0" | ||
end |