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

Raise when DB schema version comment is missing #379

Merged
merged 4 commits into from
Aug 29, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
33 changes: 30 additions & 3 deletions lib/que/migrations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,41 @@ def db_version
# No table in the database at all.
0
elsif (d = result.first[:description]).nil?
# There's a table, it was just created before the migration system
# existed.
1
# The table exists but the version comment is missing
if _db_schema_matches_db_version_1?
1
else
_raise_db_version_comment_missing_error
end
ZimbiX marked this conversation as resolved.
Show resolved Hide resolved
else
d.to_i
end
end

# The que_jobs table could be missing the schema version comment either due to:
# - Being created before the migration system existed (matching DB version 1); or
# - A bug in Rails schema dump in some versions of Rails
# So to determine which is the case, here we check an aspect of the schema that's only like that if just the first migration has been applied
def _db_schema_matches_db_version_1?
result =
Que.execute <<-SQL
SELECT column_default AS default_priority
FROM information_schema.columns
WHERE (table_schema, table_name, column_name) = ('public', 'que_jobs', 'priority');
SQL
result.first[:default_priority] == '1'
end

def _raise_db_version_comment_missing_error
raise Error, <<~ERROR
Cannot determine Que DB schema version.

The que_jobs table is abnormally missing its comment recording the Que DB schema version. This is likely due to a bug in Rails schema dump in Rails 7 versions prior to 7.0.3, omitting comments - see https://github.com/que-rb/que/issues/363. Please determine the appropriate schema version from your migrations and record it manually by running the following SQL (replacing version as appropriate):

COMMENT ON TABLE que_jobs IS 'version';
ERROR
end

def set_db_version(version)
i = version.to_i
Que.execute "COMMENT ON TABLE que_jobs IS '#{i}'" unless i.zero?
Expand Down
30 changes: 23 additions & 7 deletions spec/que/migrations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,25 +63,41 @@
assert DB.table_exists?(:que_jobs)
end

it "should be able to recognize a que_jobs table at version 0" do
it "should be able to honor the original behavior of Que.create!" do
Que.migrate! version: 0
DB.create_table(:que_jobs){serial :id} # Dummy Table.
Que.create!
assert DB.table_exists?(:que_jobs)
assert_equal 1, Que::Migrations.db_version
DB.drop_table(:que_jobs)
Que.migrate!(version: Que::Migrations::CURRENT_VERSION)

# Clean up.
Que::Migrations.migrate!(version: Que::Migrations::CURRENT_VERSION)
assert DB.table_exists?(:que_jobs)
end

it "should be able to honor the original behavior of Que.create!" do
Que.migrate! version: 0
it "should be able to determine the db version when the que_jobs table was created prior to the existence of the migrations system" do
Que.migrate!(version: 0)
Que.create!
assert DB.table_exists?(:que_jobs)
Que.execute("COMMENT ON TABLE que_jobs IS NULL") # Simulate migrations system not existing yet
assert_equal 1, Que::Migrations.db_version

# Clean up.
Que::Migrations.migrate!(version: Que::Migrations::CURRENT_VERSION)
assert DB.table_exists?(:que_jobs)
end

it "should raise if the db version comment is missing abnormally due to a bug in Rails schema dump" do
Que::Migrations.migrate!(version: Que::Migrations::CURRENT_VERSION)
Que.execute("COMMENT ON TABLE que_jobs IS NULL") # Simulate bug in Rails schema dump

assert_raises_with_message(Que::Error, /^Cannot determine Que DB schema version./) do
Que::Migrations.migrate!(version: Que::Migrations::CURRENT_VERSION)
end

# Clean up.
Que.execute("COMMENT ON TABLE que_jobs IS '#{Que::Migrations::CURRENT_VERSION}'")
assert_equal Que::Migrations::CURRENT_VERSION, Que::Migrations.db_version
end

it "down migrations should precisely undo what the up migrations did" do
Que.migrate! version: 0
versions = (1..Que::Migrations::CURRENT_VERSION).to_a
Expand Down