Class: Sequent::Migrations::SequentSchema

Inherits:
Object
  • Object
show all
Defined in:
lib/sequent/migrations/sequent_schema.rb

Constant Summary collapse

FAIL_IF_EXISTS =
->(schema_name) { fail "Schema #{schema_name} already exists in the database" }

Class Method Summary collapse

Class Method Details

.create_sequent_schema_if_not_exists(env:, fail_if_exists: true) ⇒ Object

Creates the sequent schema if it does not exist yet

Parameters:

  • env (String)

    The environment used to setup database connection

  • fail_if_exists (Boolean) (defaults to: true)

    When true fails if the schema exists, otherwise just return.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/sequent/migrations/sequent_schema.rb', line 14

def create_sequent_schema_if_not_exists(env:, fail_if_exists: true)
  fail ArgumentError, 'env is required' if env.blank?

  db_config = Sequent::Support::Database.read_database_config(env)
  Sequent::Support::Database.establish_connection(db_config)

  event_store_schema = Sequent.configuration.event_store_schema_name
  event_records_table = Sequent.configuration.event_record_class.table_name
  schema_exists = Sequent::Support::Database.schema_exists?(event_store_schema, event_records_table)

  FAIL_IF_EXISTS.call(event_store_schema) if schema_exists && fail_if_exists
  return if schema_exists

  ActiveRecord::Tasks::DatabaseTasks.load_schema(db_config, :sql)
end