class DBConnection
Public Class Methods
async_exec(*args)
click to toggle source
# File lib/activeleopard/db_connection.rb, line 16 def self.async_exec(*args) print_query(*args) instance.send_query(*args) end
execute(*args)
click to toggle source
# File lib/activeleopard/db_connection.rb, line 11 def self.execute(*args) print_query(*args) instance.exec(*args) end
get_first_row(*args)
click to toggle source
# File lib/activeleopard/db_connection.rb, line 21 def self.get_first_row(*args) print_query(*args) instance.exec(*args).first end
instance()
click to toggle source
# File lib/activeleopard/db_connection.rb, line 5 def self.instance open if @db.nil? @db end
run_migrations()
click to toggle source
# File lib/activeleopard/db_connection.rb, line 26 def self.run_migrations ensure_migrations_table! migrations = Dir.entries("db/migrations").reject { |fname| fname.start_with?('.') } migrations.sort_by! { |fname| Integer(fname[0..1]) } migrations.each do |file_name| run_migration_file(file_name) end end
Private Class Methods
already_run?(migration_name)
click to toggle source
# File lib/activeleopard/db_connection.rb, line 112 def self.already_run?(migration_name) !!execute(<<-SQL, [migration_name]).first SELECT * FROM migrations WHERE name = $1 SQL end
create_database!()
click to toggle source
# File lib/activeleopard/db_connection.rb, line 120 def self.create_database! master_conn = PG::Connection.connect(dbname: 'postgres') master_conn.exec("CREATE DATABASE #{database_name}") end
database_name()
click to toggle source
# File lib/activeleopard/db_connection.rb, line 50 def self.database_name Gazebo::ROOT.split('/').last.gsub("-", "_") + '_development' end
ensure_migrations_table!()
click to toggle source
# File lib/activeleopard/db_connection.rb, line 86 def self.ensure_migrations_table! begin execute("SELECT * FROM migrations") rescue PG::UndefinedTable execute(<<-SQL) CREATE TABLE MIGRATIONS( ID SERIAL PRIMARY KEY NOT NULL, NAME CHAR(50) NOT NULL, CREATED_AT CHAR(50) NOT NULL ) SQL end end
open()
click to toggle source
# File lib/activeleopard/db_connection.rb, line 54 def self.open if ENV['DATABASE_URL'] open_production else open_development end run_migrations end
open_development()
click to toggle source
# File lib/activeleopard/db_connection.rb, line 75 def self.open_development begin @db = PG::Connection.open(dbname: self.database_name) rescue PG::ConnectionBad => e create_database! retry end @db end
open_production()
click to toggle source
# File lib/activeleopard/db_connection.rb, line 63 def self.open_production uri = URI.parse(ENV['DATABASE_URL']) @db = PG::Connection.new( user: uri.user, password: uri.password, host: uri.host, port: uri.port, dbname: uri.path[1..-1] ) end
print_query(query, bind_params = [])
click to toggle source
# File lib/activeleopard/db_connection.rb, line 129 def self.print_query(query, bind_params = []) return unless PRINT_QUERIES output = query.gsub(/\s+/, ' ') unless bind_params.empty? output += " #{bind_params.inspect}" end puts output.colorize(random_color) end
random_color()
click to toggle source
# File lib/activeleopard/db_connection.rb, line 125 def self.random_color [:blue, :light_blue, :red, :green, :yellow].sample end
record_migration!(migration_name)
click to toggle source
# File lib/activeleopard/db_connection.rb, line 100 def self.record_migration!(migration_name) time = Time.new.strftime("%Y%m%dT%H%M") here_doc = <<-SQL INSERT INTO migrations (name, created_at) VALUES ($1, $2) SQL execute(here_doc, [migration_name, time]) end
run_migration_file(file_name)
click to toggle source
# File lib/activeleopard/db_connection.rb, line 38 def self.run_migration_file(file_name) migration_name = file_name.match(/\w+/).to_s return if migration_name.empty? || already_run?(migration_name) file = File.join(Gazebo::ROOT, "db/migrations", file_name) migration_sql = File.read(file) execute(migration_sql) record_migration!(migration_name) end