Class: Sequent::Support::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/sequent/support/database.rb

Overview

Offers support operations for a postgres database.

Class methods do establish their own database connections (and therefore take in a database configuration). Instance methods assume that a database connection yet is established.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#db_configObject (readonly)

Returns the value of attribute db_config.



11
12
13
# File 'lib/sequent/support/database.rb', line 11

def db_config
  @db_config
end

Class Method Details

.connect!(env) ⇒ Object



13
14
15
16
# File 'lib/sequent/support/database.rb', line 13

def self.connect!(env)
  db_config = read_config(env)
  establish_connection(db_config)
end

.create!(db_config) ⇒ Object



25
26
27
28
# File 'lib/sequent/support/database.rb', line 25

def self.create!(db_config)
  ActiveRecord::Base.establish_connection(db_config.merge('database' => 'postgres'))
  ActiveRecord::Base.connection.create_database(db_config['database'])
end

.create_schema(schema) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/sequent/support/database.rb', line 47

def self.create_schema(schema)
  sql = "CREATE SCHEMA IF NOT EXISTS #{schema}"
  if user = ActiveRecord::Base.connection_config[:username]
    sql += " AUTHORIZATION #{user}"
  end
  execute_sql(sql)
end

.disconnect!Object



39
40
41
# File 'lib/sequent/support/database.rb', line 39

def self.disconnect!
  ActiveRecord::Base.connection_pool.disconnect!
end

.drop!(db_config) ⇒ Object



30
31
32
33
# File 'lib/sequent/support/database.rb', line 30

def self.drop!(db_config)
  ActiveRecord::Base.establish_connection(db_config.merge('database' => 'postgres'))
  ActiveRecord::Base.connection.drop_database(db_config['database'])
end

.drop_schema!(schema_name) ⇒ Object



55
56
57
# File 'lib/sequent/support/database.rb', line 55

def self.drop_schema!(schema_name)
  execute_sql "DROP SCHEMA if exists #{schema_name} cascade"
end

.establish_connection(db_config) ⇒ Object



35
36
37
# File 'lib/sequent/support/database.rb', line 35

def self.establish_connection(db_config)
  ActiveRecord::Base.establish_connection(db_config)
end

.execute_sql(sql) ⇒ Object



43
44
45
# File 'lib/sequent/support/database.rb', line 43

def self.execute_sql(sql)
  ActiveRecord::Base.connection.execute(sql)
end

.read_config(env) ⇒ Object



18
19
20
21
22
23
# File 'lib/sequent/support/database.rb', line 18

def self.read_config(env)
  fail ArgumentError.new("env is mandatory") unless env

  database_yml = File.join(Sequent.configuration.database_config_directory, 'database.yml')
  YAML.load(ERB.new(File.read(database_yml)).result)[env]
end

.schema_exists?(schema) ⇒ Boolean

Returns:



76
77
78
79
80
# File 'lib/sequent/support/database.rb', line 76

def self.schema_exists?(schema)
  ActiveRecord::Base.connection.execute(
    "SELECT schema_name FROM information_schema.schemata WHERE schema_name like '#{schema}'"
  ).count == 1
end

.with_schema_search_path(search_path, db_config, env = ENV['RACK_ENV']) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/sequent/support/database.rb', line 59

def self.with_schema_search_path(search_path, db_config, env = ENV['RACK_ENV'])
  fail ArgumentError.new("env is required") unless env

  disconnect!
  original_search_paths = db_config['schema_search_path'].dup
  ActiveRecord::Base.configurations[env.to_s] = ActiveSupport::HashWithIndifferentAccess.new(db_config).stringify_keys
  db_config['schema_search_path'] = search_path
  ActiveRecord::Base.establish_connection db_config

  yield

ensure
  disconnect!
  db_config['schema_search_path'] = original_search_paths
  establish_connection(db_config)
end

Instance Method Details

#create_schema!(schema) ⇒ Object



86
87
88
# File 'lib/sequent/support/database.rb', line 86

def create_schema!(schema)
  self.class.create_schema(schema)
end

#drop_schema!(schema) ⇒ Object



90
91
92
# File 'lib/sequent/support/database.rb', line 90

def drop_schema!(schema)
  self.class.drop_schema!(schema)
end

#execute_sql(sql) ⇒ Object



94
95
96
# File 'lib/sequent/support/database.rb', line 94

def execute_sql(sql)
  self.class.execute_sql(sql)
end

#migrate(migrations_path, verbose: true) ⇒ Object



98
99
100
101
102
103
104
105
106
# File 'lib/sequent/support/database.rb', line 98

def migrate(migrations_path, verbose: true)
  ActiveRecord::Migration.verbose = verbose
  if ActiveRecord::VERSION::MAJOR >= 5 && ActiveRecord::VERSION::MINOR >= 2
    ActiveRecord::MigrationContext.new([migrations_path]).up
  else
    ActiveRecord::Migrator.migrate(migrations_path)
  end

end

#schema_exists?(schema) ⇒ Boolean

Returns:



82
83
84
# File 'lib/sequent/support/database.rb', line 82

def schema_exists?(schema)
  self.class.schema_exists?(schema)
end