Module: Waxx::Database
Overview
Waxx Copyright © 2016 ePark labs Inc. & Daniel J. Fitzpatrick <dan@eparklabs.com> All rights reserved. Released under the Apache Version 2 License. See LICENSE.txt.
Instance Method Summary collapse
- #[](name) ⇒ Object
- #collection(name) ⇒ Object
- #connect(conf = Waxx['database']) ⇒ Object
-
#connections(dbs = Waxx['databases']) ⇒ Object
Define database connections in config.yaml or pass in a hash { app: connection_string, blog: connection_string }.
- #migrate(db_only = nil, opts = {}) ⇒ Object
- #parse_uri(uri) ⇒ Object
Instance Method Details
#[](name) ⇒ Object
80 81 82 |
# File 'waxx/database.rb', line 80 def [](name) app[name] end |
#collection(name) ⇒ Object
84 85 86 |
# File 'waxx/database.rb', line 84 def collection(name) app[name] end |
#connect(conf = Waxx['database']) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'waxx/database.rb', line 19 def connect(conf=Waxx['database']) # Parse the conf string to load the correct db engine engine = conf.split(":").first case engine.downcase when 'postgresql', 'pg' Waxx::Pg.connect(conf) when 'mysql2', 'mysql' # Parse the string uri = parse_uri(conf) # Merge the opts into the params config = { username: uri/:user, password: uri/:pass, host: uri/:host, port: uri/:port, database: uri/:database } config.merge!(uri/:opts) Waxx::Mysql2.connect(config) when 'sqlite3', 'sqlite' Waxx::Sqlite3.connect(conf.sub('sqlite3://','')) when 'mongodb', 'mongo' Waxx::Mongodb.connect(conf) else raise 'Unknown Database Type' end end |
#connections(dbs = Waxx['databases']) ⇒ Object
Define database connections in config.yaml or pass in a hash
{
app: connection_string,
blog: connection_string
}
52 53 54 55 56 57 58 59 60 |
# File 'waxx/database.rb', line 52 def connections(dbs=Waxx['databases']) c = {} return c if dbs.nil? dbs.each{|name, conf| c[name.to_sym] = connect(conf) c.define_singleton_method(name){self[name.to_sym]} } c end |
#migrate(db_only = nil, opts = {}) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'waxx/database.rb', line 62 def migrate(db_only=nil, opts={}) dbs = connections dbs.each{|name, db| next if db_only and db_only.to_sym != name puts "Migrating: db.#{name}" # get the latest version latest = db.exec("SELECT value FROM waxx WHERE name = 'db.#{name}.migration.last'").first['value'] Dir.entries("#{opts[:base]}/db/#{name}/").sort.each{|f| if f =~ /\.sql$/ and f > latest puts " #{f}" db.exec(File.read("#{opts[:base]}/db/#{name}/#{f}")) db.exec("UPDATE waxx SET value = $1 WHERE name = 'db.#{name}.migration.last'",[f]) end } } puts "Migration complete" end |
#parse_uri(uri) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 |
# File 'waxx/database.rb', line 7 def parse_uri(uri) _, schema, user, pass, host, port, database, opts = uri.split(/^(\w*):\/\/(\w*):?(.*)@([\w\-]*):?([0-9]*)?\/([\w\-]*)\??(.*)?$/) { type: schema, user: user, pass: pass, host: host, database: database, opts: Waxx::Http.query_string_to_hash(opts) } end |