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.



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

def db_config
  @db_config
end

Class Method Details

.configuration_hashObject



111
112
113
114
115
116
117
# File 'lib/sequent/support/database.rb', line 111

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



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

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

.create!(db_config) ⇒ Object



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

def self.create!(db_config)
  establish_connection(db_config, {database: 'postgres'})
  ActiveRecord::Base.connection.create_database(get_db_config_attribute(db_config, :database))
end

.create_schema(schema) ⇒ Object



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

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



69
70
71
# File 'lib/sequent/support/database.rb', line 69

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

.drop!(db_config) ⇒ Object



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

def self.drop!(db_config)
  establish_connection(db_config, {database: 'postgres'})
  ActiveRecord::Base.connection.drop_database(get_db_config_attribute(db_config, :database))
end

.drop_schema!(schema_name) ⇒ Object



84
85
86
# File 'lib/sequent/support/database.rb', line 84

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

.establish_connection(db_config, db_config_overrides = {}) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/sequent/support/database.rb', line 51

def self.establish_connection(db_config, db_config_overrides = {})
  if Sequent.configuration.can_use_multiple_databases?
    db_config = db_config.deep_merge(
      Sequent.configuration.primary_database_key => db_config_overrides,
    ).stringify_keys
    if Gem.loaded_specs['activerecord'].version < Gem::Version.create('7.1.0')
      ActiveRecord.legacy_connection_handling = false
    end
    ActiveRecord::Base.configurations = db_config.stringify_keys
    ActiveRecord::Base.connects_to database: {
      Sequent.configuration.primary_database_role => Sequent.configuration.primary_database_key,
    }
  else
    db_config = db_config.merge(db_config_overrides)
    ActiveRecord::Base.establish_connection(db_config)
  end
end

.execute_sql(sql) ⇒ Object



73
74
75
# File 'lib/sequent/support/database.rb', line 73

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

.get_db_config_attribute(db_config, attribute) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/sequent/support/database.rb', line 43

def self.get_db_config_attribute(db_config, attribute)
  if Sequent.configuration.can_use_multiple_databases?
    db_config[Sequent.configuration.primary_database_key][attribute]
  else
    db_config[attribute]
  end
end

.read_config(env) ⇒ Object



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

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:



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

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['SEQUENT_ENV']) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/sequent/support/database.rb', line 88

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

  disconnect!

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

  establish_connection(db_config, {schema_search_path: search_path})
  yield
ensure
  disconnect!
  establish_connection(db_config)
end

Instance Method Details

#create_schema!(schema) ⇒ Object



123
124
125
# File 'lib/sequent/support/database.rb', line 123

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

#drop_schema!(schema) ⇒ Object



127
128
129
# File 'lib/sequent/support/database.rb', line 127

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

#execute_sql(sql) ⇒ Object



131
132
133
# File 'lib/sequent/support/database.rb', line 131

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

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



135
136
137
138
139
140
141
142
143
144
# File 'lib/sequent/support/database.rb', line 135

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:



119
120
121
# File 'lib/sequent/support/database.rb', line 119

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