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



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

def self.configuration_hash
  ActiveRecord::Base.connection_db_config.configuration_hash
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



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

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



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

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



65
66
67
# File 'lib/sequent/support/database.rb', line 65

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

.drop!(db_config) ⇒ Object



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

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



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

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

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



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/sequent/support/database.rb', line 47

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 ActiveRecord::VERSION::MAJOR >= 7 && ActiveRecord::VERSION::MINOR < 1
      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



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

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

.get_db_config_attribute(db_config, attribute) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/sequent/support/database.rb', line 39

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
# 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]
  ActiveRecord::Base.configurations.resolve(config).configuration_hash.with_indifferent_access
end

.schema_exists?(schema, event_records_table = nil) ⇒ Boolean

Returns:



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/sequent/support/database.rb', line 101

def self.schema_exists?(schema, event_records_table = nil)
  schema_exists = ActiveRecord::Base.connection.exec_query(
    'SELECT 1 FROM information_schema.schemata WHERE schema_name LIKE $1',
    'schema_exists?',
    [schema],
  ).count == 1

  # The ActiveRecord 7.1 schema_dumper.rb now also adds `create_schema` statements for any schema that
  # is not named `public`, and in this case the schema may already be created so we check for the
  # existence of the `event_records` table (or view) as well.
  return schema_exists unless event_records_table

  ActiveRecord::Base.connection.exec_query(
    'SELECT 1 FROM information_schema.tables WHERE table_schema LIKE $1 AND table_name LIKE $2',
    'schema_exists?',
    [schema, event_records_table],
  ).count == 1
end

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



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/sequent/support/database.rb', line 84

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



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

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

#drop_schema!(schema) ⇒ Object



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

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

#execute_sql(sql) ⇒ Object



136
137
138
# File 'lib/sequent/support/database.rb', line 136

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

#schema_exists?(schema, event_records_table = nil) ⇒ Boolean

Returns:



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

def schema_exists?(schema, event_records_table = nil)
  self.class.schema_exists?(schema, event_records_table)
end