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.



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

def db_config
  @db_config
end

Class Method Details

.configuration_hashObject



93
94
95
96
97
98
99
# File 'lib/sequent/support/database.rb', line 93

def self.configuration_hash
  if Gem.loaded_specs['activesupport'].version >= Gem::Version.create('6.1.0')
    ActiveRecord::Base.connection_db_config.configuration_hash
  else
    ActiveRecord::Base.connection_config
  end
end

.connect!(env) ⇒ Object



15
16
17
18
# File 'lib/sequent/support/database.rb', line 15

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

.create!(db_config) ⇒ Object



32
33
34
35
# File 'lib/sequent/support/database.rb', line 32

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



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

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

.disconnect!Object



46
47
48
# File 'lib/sequent/support/database.rb', line 46

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

.drop!(db_config) ⇒ Object



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

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



61
62
63
# File 'lib/sequent/support/database.rb', line 61

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

.establish_connection(db_config) ⇒ Object



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

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

.execute_sql(sql) ⇒ Object



50
51
52
# File 'lib/sequent/support/database.rb', line 50

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

.read_config(env) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/sequent/support/database.rb', line 20

def self.read_config(env)
  fail ArgumentError, 'env is mandatory' unless env

  database_yml = File.join(Sequent.configuration.database_config_directory, 'database.yml')
  config = YAML.safe_load(ERB.new(File.read(database_yml)).result, aliases: true)[env]
  if Gem.loaded_specs['activerecord'].version >= Gem::Version.create('6.1.0')
    ActiveRecord::Base.configurations.resolve(config).configuration_hash.with_indifferent_access
  else
    ActiveRecord::Base.resolve_config_for_connection(config)
  end
end

.schema_exists?(schema) ⇒ Boolean

Returns:



87
88
89
90
91
# File 'lib/sequent/support/database.rb', line 87

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



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/sequent/support/database.rb', line 65

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

  disconnect!
  original_search_paths = db_config[:schema_search_path].dup

  if ActiveRecord::VERSION::MAJOR < 6
    ActiveRecord::Base.configurations[env.to_s] =
      ActiveSupport::HashWithIndifferentAccess.new(db_config).stringify_keys
  end

  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



105
106
107
# File 'lib/sequent/support/database.rb', line 105

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

#drop_schema!(schema) ⇒ Object



109
110
111
# File 'lib/sequent/support/database.rb', line 109

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

#execute_sql(sql) ⇒ Object



113
114
115
# File 'lib/sequent/support/database.rb', line 113

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

#migrate(migrations_path, schema_migration: ActiveRecord::SchemaMigration, verbose: true) ⇒ Object



117
118
119
120
121
122
123
124
125
126
# File 'lib/sequent/support/database.rb', line 117

def migrate(migrations_path, schema_migration: ActiveRecord::SchemaMigration, verbose: true)
  ActiveRecord::Migration.verbose = verbose
  if ActiveRecord::VERSION::MAJOR >= 6
    ActiveRecord::MigrationContext.new([migrations_path], schema_migration).up
  elsif 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:



101
102
103
# File 'lib/sequent/support/database.rb', line 101

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