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
29
30
31
32
33
34
35
36
# 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_config(env)

  Sequent::Support::Database.establish_connection(db_config)

  event_store_schema = Sequent.configuration.event_store_schema_name
  schema_exists = Sequent::Support::Database.schema_exists?(event_store_schema)

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

  sequent_schema = File.join(Sequent.configuration.database_schema_directory, "#{event_store_schema}.rb")
  unless File.exist?(sequent_schema)
    fail "File '#{sequent_schema}' does not exist. Check your Sequent configuration."
  end

  Sequent::Support::Database.create_schema(event_store_schema)
  Sequent::Support::Database.with_schema_search_path(event_store_schema, db_config, env) do
    load(sequent_schema)
  end
end