Class: Sequent::Support::Database
- Inherits:
-
Object
- Object
- Sequent::Support::Database
- 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
-
#db_config ⇒ Object
readonly
Returns the value of attribute db_config.
Class Method Summary collapse
- .configuration_hash ⇒ Object
- .connect!(env) ⇒ Object
- .create!(db_config) ⇒ Object
- .create_schema(schema) ⇒ Object
- .disconnect! ⇒ Object
- .drop!(db_config) ⇒ Object
- .drop_schema!(schema_name) ⇒ Object
- .establish_connection(db_config, db_config_overrides = {}) ⇒ Object
- .execute_sql(sql) ⇒ Object
- .get_db_config_attribute(db_config, attribute) ⇒ Object
- .read_config(env) ⇒ Object
- .schema_exists?(schema) ⇒ Boolean
- .with_schema_search_path(search_path, db_config, env = ENV['SEQUENT_ENV']) ⇒ Object
Instance Method Summary collapse
- #create_schema!(schema) ⇒ Object
- #drop_schema!(schema) ⇒ Object
- #execute_sql(sql) ⇒ Object
- #migrate(migrations_path, schema_migration: ActiveRecord::SchemaMigration, verbose: true) ⇒ Object
- #schema_exists?(schema) ⇒ Boolean
Instance Attribute Details
#db_config ⇒ Object (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_hash ⇒ Object
109 110 111 112 113 114 115 |
# File 'lib/sequent/support/database.rb', line 109 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
75 76 77 78 79 80 |
# File 'lib/sequent/support/database.rb', line 75 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
67 68 69 |
# File 'lib/sequent/support/database.rb', line 67 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
82 83 84 |
# File 'lib/sequent/support/database.rb', line 82 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 |
# 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 ActiveRecord.legacy_connection_handling = false 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
71 72 73 |
# File 'lib/sequent/support/database.rb', line 71 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
103 104 105 106 107 |
# File 'lib/sequent/support/database.rb', line 103 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
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/sequent/support/database.rb', line 86 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
121 122 123 |
# File 'lib/sequent/support/database.rb', line 121 def create_schema!(schema) self.class.create_schema(schema) end |
#drop_schema!(schema) ⇒ Object
125 126 127 |
# File 'lib/sequent/support/database.rb', line 125 def drop_schema!(schema) self.class.drop_schema!(schema) end |
#execute_sql(sql) ⇒ Object
129 130 131 |
# File 'lib/sequent/support/database.rb', line 129 def execute_sql(sql) self.class.execute_sql(sql) end |
#migrate(migrations_path, schema_migration: ActiveRecord::SchemaMigration, verbose: true) ⇒ Object
133 134 135 136 137 138 139 140 141 142 |
# File 'lib/sequent/support/database.rb', line 133 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
117 118 119 |
# File 'lib/sequent/support/database.rb', line 117 def schema_exists?(schema) self.class.schema_exists?(schema) end |