Class: Sequent::Support::Database

Inherits:
Object
  • Object
show all
Includes:
ActiveRecord::Tasks
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.



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

def db_config
  @db_config
end

Class Method Details

.configuration_hashObject



103
104
105
# File 'lib/sequent/support/database.rb', line 103

def self.configuration_hash
  ActiveRecord::Base.connection_db_config.configuration_hash
end

.connect!(env) ⇒ Object



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

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

.create!(db_config) ⇒ Object



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

def self.create!(db_config)
  DatabaseTasks.create(db_config)
end

.create_schema(schema) ⇒ Object



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

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



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

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

.drop!(db_config) ⇒ Object



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

def self.drop!(db_config)
  DatabaseTasks.drop(db_config)
end

.drop_schema!(schema_name) ⇒ Object



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

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

.establish_connection(db_config) ⇒ Object



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

def self.establish_connection(db_config)
  if Sequent.configuration.can_use_multiple_databases?
    ActiveRecord::Base.configurations = db_config.stringify_keys
    ActiveRecord::Base.connects_to database: {
      Sequent.configuration.primary_database_role => Sequent.configuration.primary_database_key,
    }
  else
    ActiveRecord::Base.establish_connection(db_config)
  end
end

.execute_sql(sql) ⇒ Object



59
60
61
# File 'lib/sequent/support/database.rb', line 59

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

.read_config(env) ⇒ Object



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

def self.read_config(env)
  read_database_config(env).configuration_hash.with_indifferent_access
end

.read_database_config(env) ⇒ Object



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

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

  DatabaseTasks.db_dir = Sequent.configuration.database_schema_directory unless defined?(Rails)
  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)
end

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

Returns:



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

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_search_path(search_path) ⇒ Object



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

def self.with_search_path(search_path)
  old_search_path = ActiveRecord::Base.connection.select_value("SELECT current_setting('search_path')")
  begin
    ActiveRecord::Base.connection.exec_update("SET search_path TO #{search_path}", 'with_search_path')
    yield
  ensure
    ActiveRecord::Base.connection.exec_update("SET search_path TO #{old_search_path}", 'with_search_path')
  end
end

Instance Method Details

#create_schema!(schema) ⇒ Object



111
112
113
# File 'lib/sequent/support/database.rb', line 111

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

#drop_schema!(schema) ⇒ Object



115
116
117
# File 'lib/sequent/support/database.rb', line 115

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

#execute_sql(sql) ⇒ Object



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

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

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

Returns:



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

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