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

Conversation

ZimbiX
Copy link
Member

@ZimbiX ZimbiX commented Aug 26, 2022

Resolves #377.

PostgreSQL comment docs

This wasn't as simple as I first thought it'd be, since we also have to cater for the scenario of migrating from before DB schema version was recorded. To detect that case, I'm checking the default for the priority column, since that was changed to a much more sensible value in migration 2.

I did some code archaeology, and determined that, prior to migration 2, the default for the priority column was 1 since 1050850#diff-f5ed5e5d3159a4a7d9d7ea114936cb2147d07e27ce81dcce79c2814e299d2456R8, which (according to the tags that commit appears in) covers way back to v0.1.0. So this is a safe proxy to detect DB schemas pre version 2. However, the que_jobs table schema at the linked commit differs from the current migration 1 - to consider it as schema version 1 and migrate up would produce an incorrect result, if it even works. But regardless, this check against the priority column's default preserves the DB schema version 1 detection result as it was before.

Migrating when the DB schema version comment is missing now looks like:

➜ docker compose up -d db

➜ export DATABASE_URL=postgres://que:que@localhost:5697/que-test

➜ ruby -r bundler/setup -r que -r sequel -e "Que.connection = Sequel.connect(ENV['DATABASE_URL']); Que.migrate!(version: Que::Migrations::CURRENT_VERSION)"

➜ ./auto/psql -c "comment on table que_jobs is null;" 
[+] Running 1/0
 ⠿ Container que-db-1  Running  0.0s
COMMENT

➜ ruby -r bundler/setup -r que -r sequel -e "Que.connection = Sequel.connect(ENV['DATABASE_URL']); Que.migrate!(version: Que::Migrations::CURRENT_VERSION)"
/home/brendan/Projects/que/lib/que/migrations.rb:78:in `_raise_db_version_comment_missing_error': Cannot determine Que DB schema version. (Que::Error)

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/377. 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';
  from /home/brendan/Projects/que/lib/que/migrations.rb:55:in `db_version'
  from /home/brendan/Projects/que/lib/que/migrations.rb:12:in `block in migrate!'
  from /home/brendan/Projects/que/lib/que/utils/transactions.rb:18:in `block in transaction'
  from /home/brendan/Projects/que/lib/que/connection_pool.rb:39:in `block in checkout'
  from /home/brendan/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/sequel-5.58.0/lib/sequel/connection_pool/threaded.rb:92:in `hold'
  from /home/brendan/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/sequel-5.58.0/lib/sequel/database/connecting.rb:269:in `synchronize'
  from /home/brendan/Projects/que/lib/que/connection_pool.rb:18:in `checkout'
  from /home/brendan/Projects/que/lib/que/utils/transactions.rb:12:in `transaction'
  from /home/brendan/Projects/que/lib/que/migrations.rb:11:in `migrate!'
  from /home/brendan/.rbenv/versions/3.1.2/lib/ruby/3.1.0/forwardable.rb:238:in `migrate!'
  from -e:1:in `<main>'

➜ docker compose down && docker volume rm -f que_db-data

lib/que/migrations.rb Outdated Show resolved Hide resolved
@ZimbiX ZimbiX requested a review from oeoeaio August 27, 2022 05:29
@ZimbiX ZimbiX changed the title Raise when DB schema version comment is missing abnormally Raise when DB schema version comment is missing Aug 29, 2022
@ZimbiX ZimbiX merged commit ce9c3ba into master Aug 29, 2022
@ZimbiX ZimbiX deleted the detect-missing-db-schema-version-comment branch August 29, 2022 01:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants