class Sequent::Support::Database
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.
Attributes
Public Class Methods
Source
# File lib/sequent/support/database.rb, line 103 def self.configuration_hash ActiveRecord::Base.connection_db_config.configuration_hash end
Source
# File lib/sequent/support/database.rb, line 18 def self.connect!(env) db_config = read_config(env) establish_connection(db_config) end
Source
# File lib/sequent/support/database.rb, line 36 def self.create!(db_config) DatabaseTasks.create(db_config) end
Source
# 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
Source
# File lib/sequent/support/database.rb, line 55 def self.disconnect! ActiveRecord::Base.connection_pool.disconnect! end
Source
# File lib/sequent/support/database.rb, line 40 def self.drop!(db_config) DatabaseTasks.drop(db_config) end
Source
# File lib/sequent/support/database.rb, line 70 def self.drop_schema!(schema_name) execute_sql "DROP SCHEMA if exists #{schema_name} cascade" end
Source
# 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
Source
# File lib/sequent/support/database.rb, line 59 def self.execute_sql(sql) ActiveRecord::Base.connection.execute(sql) end
Source
# File lib/sequent/support/database.rb, line 32 def self.read_config(env) read_database_config(env).configuration_hash.with_indifferent_access end
Source
# 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
Source
# 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
Source
# 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
Public Instance Methods
Source
# File lib/sequent/support/database.rb, line 111 def create_schema!(schema) self.class.create_schema(schema) end
Source
# File lib/sequent/support/database.rb, line 115 def drop_schema!(schema) self.class.drop_schema!(schema) end
Source
# File lib/sequent/support/database.rb, line 119 def execute_sql(sql) self.class.execute_sql(sql) end
Source
# 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