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
- .create!(db_config) ⇒ Object
- .disconnect! ⇒ Object
- .drop!(db_config) ⇒ Object
- .establish_connection(db_config) ⇒ Object
Instance Method Summary collapse
- #create_schema!(schema) ⇒ Object
- #drop_schema!(schema) ⇒ Object
- #migrate(migrations_path, verbose: true) ⇒ Object
- #schema_exists?(schema) ⇒ Boolean
Instance Attribute Details
#db_config ⇒ Object (readonly)
Returns the value of attribute db_config.
9 10 11 |
# File 'lib/sequent/support/database.rb', line 9 def db_config @db_config end |
Class Method Details
.create!(db_config) ⇒ Object
11 12 13 14 |
# File 'lib/sequent/support/database.rb', line 11 def self.create!(db_config) ActiveRecord::Base.establish_connection(db_config.merge('database' => 'postgres')) ActiveRecord::Base.connection.create_database(db_config['database']) end |
.disconnect! ⇒ Object
25 26 27 |
# File 'lib/sequent/support/database.rb', line 25 def self.disconnect! ActiveRecord::Base.connection_pool.disconnect! end |
.drop!(db_config) ⇒ Object
16 17 18 19 |
# File 'lib/sequent/support/database.rb', line 16 def self.drop!(db_config) ActiveRecord::Base.establish_connection(db_config.merge('database' => 'postgres')) ActiveRecord::Base.connection.drop_database(db_config['database']) end |
.establish_connection(db_config) ⇒ Object
21 22 23 |
# File 'lib/sequent/support/database.rb', line 21 def self.establish_connection(db_config) ActiveRecord::Base.establish_connection(db_config) end |
Instance Method Details
#create_schema!(schema) ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/sequent/support/database.rb', line 35 def create_schema!(schema) sql = "CREATE SCHEMA IF NOT EXISTS #{schema}" if user = ActiveRecord::Base.connection_config[:username] sql += " AUTHORIZATION #{user}" end ActiveRecord::Base.connection.execute(sql) end |
#drop_schema!(schema) ⇒ Object
43 44 45 46 47 |
# File 'lib/sequent/support/database.rb', line 43 def drop_schema!(schema) ActiveRecord::Base.connection.execute( "DROP SCHEMA IF EXISTS #{schema} CASCADE" ) end |
#migrate(migrations_path, verbose: true) ⇒ Object
49 50 51 52 |
# File 'lib/sequent/support/database.rb', line 49 def migrate(migrations_path, verbose: true) ActiveRecord::Migration.verbose = verbose ActiveRecord::Migrator.migrate(migrations_path) end |
#schema_exists?(schema) ⇒ Boolean
29 30 31 32 33 |
# File 'lib/sequent/support/database.rb', line 29 def schema_exists?(schema) ActiveRecord::Base.connection.execute( "SELECT schema_name FROM information_schema.schemata WHERE schema_name like '#{schema}'" ).count == 1 end |