-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example db migration file for aggregate unique keys
- Loading branch information
1 parent
a4a6790
commit 8775b6f
Showing
2 changed files
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# frozen_string_literal: true | ||
|
||
class AggregateUniqueKeys < ActiveRecord::Migration[7.2] | ||
def up | ||
say 'Setting up search path', true | ||
suppress_messages do | ||
execute <<~SQL | ||
SELECT set_config('tmp.search_path', current_setting('search_path'), true); | ||
SELECT set_config('search_path', '#{Sequent.configuration.event_store_schema_name}', true); | ||
SQL | ||
end | ||
|
||
say 'Creating aggregate_unique_keys table', true | ||
suppress_messages do | ||
execute <<~SQL | ||
CREATE TABLE IF NOT EXISTS aggregate_unique_keys ( | ||
aggregate_id uuid NOT NULL, | ||
scope text NOT NULL, | ||
key jsonb NOT NULL, | ||
PRIMARY KEY (aggregate_id, scope), | ||
UNIQUE (scope, key), | ||
FOREIGN KEY (aggregate_id) REFERENCES aggregates (aggregate_id) ON UPDATE CASCADE ON DELETE CASCADE | ||
) | ||
SQL | ||
end | ||
|
||
say 'Creating event store stored procedures and views', true | ||
suppress_messages do | ||
sequent_pgsql_filename = File.join(Sequent.configuration.database_schema_directory, 'sequent_pgsql.sql') | ||
execute File.read(sequent_pgsql_filename) | ||
end | ||
|
||
say 'Restoring search path', true | ||
suppress_messages do | ||
execute "SELECT set_config('search_path', current_setting('tmp.search_path'), true)" | ||
end | ||
end | ||
|
||
def down | ||
fail ActiveRecord::IrreversibleMigration | ||
end | ||
end |