Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix versions migration for postgres 12 #397

Merged
merged 1 commit into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/sequent/migrations/versions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ class Versions < Sequent::ApplicationRecord
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} drop constraint if exists only_one_running;
ALTER TABLE #{table_name} ADD COLUMN IF NOT EXISTS status INTEGER DEFAULT NULL CONSTRAINT only_one_running CHECK (status in (1,2,3));
CREATE UNIQUE INDEX IF NOT EXISTS single_migration_running ON #{table_name} (status);
DROP INDEX IF EXISTS single_migration_running;
CREATE UNIQUE INDEX single_migration_running ON #{table_name} ((status * 0)) where status is not null;
SQL
end

Expand Down
11 changes: 11 additions & 0 deletions spec/lib/sequent/migrations/view_schema_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@

expect(Sequent::ApplicationRecord.connection).to have_schema(view_schema)
end

it 'can not insert two versions with a status' do
migrator.create_view_schema_if_not_exists
migrator.create_view_schema_if_not_exists

Sequent::Migrations::Versions.create!(version: 1, status: nil)
Sequent::Migrations::Versions.create!(version: 2, status: 1)
expect do
Sequent::Migrations::Versions.create!(version: 3, status: 2)
end.to raise_error(ActiveRecord::RecordNotUnique)
end
end

context '#create_view_tables' do
Expand Down
Loading