Skip to content

Commit

Permalink
Add migrating metadata tables before running migration
Browse files Browse the repository at this point in the history
  • Loading branch information
lvonk committed Nov 13, 2023
1 parent f5e4569 commit faaf9dd
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/sequent/migrations/replayed_ids.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Sequent
module Migrations
class ReplayedIds < Sequent::ApplicationRecord
def self.sql
def self.migration_sql
<<~SQL.chomp
CREATE TABLE IF NOT EXISTS #{table_name} (event_id bigint NOT NULL, CONSTRAINT event_id_pk PRIMARY KEY(event_id));
SQL
Expand Down
2 changes: 1 addition & 1 deletion lib/sequent/migrations/versions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Versions < Sequent::ApplicationRecord
MIGRATE_OFFLINE_RUNNING = 3
DONE = nil

def self.sql
def self.migration_sql
<<~SQL.chomp
CREATE TABLE IF NOT EXISTS #{table_name} (version integer NOT NULL, CONSTRAINT version_pk PRIMARY KEY(version));
ALTER TABLE #{table_name} ADD COLUMN IF NOT EXISTS status INTEGER DEFAULT NULL CONSTRAINT only_one_running CHECK (status in (1,2,3));
Expand Down
16 changes: 11 additions & 5 deletions lib/sequent/migrations/view_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,7 @@ def replay_all!(group_exponent: 1)
# This method is mainly useful during an initial setup of the view schema
def create_view_schema_if_not_exists
exec_sql(%(CREATE SCHEMA IF NOT EXISTS #{view_schema}))
Sequent::ApplicationRecord.transaction do
in_view_schema do
exec_sql([ReplayedIds.sql, Versions.sql].join("\n"))
end
end
migrate_metadata_tables
end

def plan
Expand Down Expand Up @@ -195,6 +191,8 @@ def executor
#
# @raise ConcurrentMigrationError if migration is already running
def migrate_online
migrate_metadata_tables

return if Sequent.new_version == current_version

ensure_version_correct!
Expand Down Expand Up @@ -287,6 +285,14 @@ def migrate_offline

private

def migrate_metadata_tables
Sequent::ApplicationRecord.transaction do
in_view_schema do
exec_sql([ReplayedIds.migration_sql, Versions.migration_sql].join("\n"))
end
end
end

def ensure_version_correct!
create_view_schema_if_not_exists
new_version = Sequent.new_version
Expand Down

0 comments on commit faaf9dd

Please sign in to comment.