module JunglePath::DBAccess::Meta::DB

Public Class Methods

create(config) click to toggle source
# File lib/jungle_path/db_access/meta/db.rb, line 8
def self.create(config)
  puts "JunglePath::DBAccess::Meta::DB.create: #{config.name}."
  db = JunglePath::DBAccess::IO.connection_from_config_use_postgres_db(config)
  sql = "create database #{config.name}"
  db.run sql
end
drop!(config) click to toggle source
# File lib/jungle_path/db_access/meta/db.rb, line 15
def self.drop!(config)
  puts "JunglePath::DBAccess::Meta::DB.drop: #{config.name}."
  kill_connections! config
  db = JunglePath::DBAccess::IO.connection_from_config_use_postgres_db(config)
  sql = "drop database #{config.name}"
  db.run sql
end
drop?(config) click to toggle source
# File lib/jungle_path/db_access/meta/db.rb, line 23
def self.drop?(config)
  if exists?(config)
    drop! config
  end
end
exists?(config) click to toggle source
# File lib/jungle_path/db_access/meta/db.rb, line 34
def self.exists?(config) # todo: fix to also work for ms sql server.
  puts "JunglePath::DBAccess::Meta::DB.exists? #{config.name}."
  exists = false
  db = JunglePath::DBAccess::IO.connection_from_config_unknown_database(config)
  sql = sql_query_db_existence(config)
  db.fetch(sql) do |row|
    exists = true
  end
  exists
end
kill_connections!(config) click to toggle source
# File lib/jungle_path/db_access/meta/db.rb, line 59
def self.kill_connections!(config)
  db = JunglePath::DBAccess::IO.connection_from_config_use_postgres_db(config)
  sql = "select pg_terminate_backend(pid) from pg_stat_activity where datname = '#{config.name}'"
  db.run sql
end
rename(config_from, to_name) click to toggle source
# File lib/jungle_path/db_access/meta/db.rb, line 45
def self.rename(config_from, to_name)
  puts "JunglePath::DBAccess::Meta::DB.rename: #{config_from.name} to #{to_name}."
  kill_connections! config_from
  db = JunglePath::DBAccess::IO.connection_from_config_use_postgres_db(config_from)
  sql = "alter database #{config_from.name} rename to #{to_name}"
  db.run sql
end
rename?(config_from, to_name) click to toggle source
# File lib/jungle_path/db_access/meta/db.rb, line 53
def self.rename?(config_from, to_name)
  if exists?(config_from)
    rename config_from, to_name
  end
end
reset!(config) click to toggle source
# File lib/jungle_path/db_access/meta/db.rb, line 29
def self.reset!(config)
  drop? config
  create config
end

Private Class Methods

sql_query_db_existence(config) click to toggle source
# File lib/jungle_path/db_access/meta/db.rb, line 67
def self.sql_query_db_existence(config)
  if config.type == 'postgres'
    "select datname from pg_database where datname = '#{config.name}'"
  elsif config.type == 'tinytds'
    "select * from master.sys.databases where name = '#{config.name}'"
  else
    throw "Unknown database type: #{config.type}."
  end
end