Bug 1350302

Summary: beaker-init --check on an empty database creates alembic_version table, database cannot be populated
Product: [Retired] Beaker Reporter: Dan Callaghan <dcallagh>
Component: generalAssignee: Jon Orris <jorris>
Status: CLOSED CURRENTRELEASE QA Contact: tools-bugs <tools-bugs>
Severity: unspecified Docs Contact:
Priority: unspecified    
Version: 23CC: dcallagh, dowang, jorris, mjia, rjoost
Target Milestone: 23.3Keywords: Patch, Triaged
Target Release: ---   
Hardware: Unspecified   
OS: Unspecified   
Whiteboard:
Fixed In Version: Doc Type: If docs needed, set a value
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2016-11-07 06:44:34 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:

Description Dan Callaghan 2016-06-27 05:21:04 UTC
Description of problem:
In our Ansible playbooks we are doing a four-step process for db migrations, like this:

beaker-init --check
(if check indicates changes are needed)
beaker-init --user... --background
wait for pid file to disappear
beaker-init --check

This works well for migrating existing databases. But for deploying a fresh environment from scratch, it will fail on the final check step.

Version-Release number of selected component (if applicable):
beaker-server-23.0-0.git.248.ede6acc

How reproducible:
easily, with a fresh environment (empty db)

Steps to Reproduce:
1. Configure /etc/beaker/server.cfg to point at a new, empty database containing no tables
2. Run beaker-init --check
3. Run beaker-init --user admin --password '' --email something

Actual results:
Fails with an exception:
Traceback (most recent call last):
  File "/usr/bin/beaker-init", line 9, in <module>
    load_entry_point('bkr.server==23.0.git.248.ede6acc', 'console_scripts', 'beaker-init')()
  File "/usr/lib/python2.6/site-packages/bkr/server/tools/init.py", line 378, in main
    return doit(opts)
  File "/usr/lib/python2.6/site-packages/bkr/server/util.py", line 178, in decorated
    return func(*args, **kwargs)
  File "/usr/lib/python2.6/site-packages/bkr/server/tools/init.py", line 413, in doit
    upgrade_db(metadata)
  File "/usr/lib/python2.6/site-packages/bkr/server/tools/init.py", line 265, in upgrade_db
    run_alembic_operation(metadata, upgrade)
  File "/usr/lib/python2.6/site-packages/bkr/server/tools/init.py", line 317, in run_alembic_operation
    env_context.run_migrations()
[...]
sqlalchemy.exc.NoSuchTableError: `recipe_task`

Expected results:
Initial beaker-init --check should report that the database needs upgrading.
Subsequent beaker-init should then populate it correctly, not die with an exception.

Additional info:
The exception occurs because the initial beaker-init --check seems to silently create an alembic_version table (with no rows). This means the subsequent beaker-init invocation thinks the database is *not* empty and instead tries to follow the pre-0.19 inspection logic to determine the version, which then fails as above. In fact the database is just empty and should be populated from scratch.

So we either need to make beaker-init ignore the presence of alembic_version when considering whether the database is empty, or we need to make beaker-init --check not silently create the alembic_version table.

I would prefer the latter since beaker-init --check promises to make no changes to the database, although we are clearly violating that promise right now.

Comment 1 Jon Orris 2016-08-25 19:39:55 UTC
(In reply to Dan Callaghan from comment #0)
> So we either need to make beaker-init ignore the presence of alembic_version
> when considering whether the database is empty, or we need to make
> beaker-init --check not silently create the alembic_version table.
> 
> I would prefer the latter since beaker-init --check promises to make no
> changes to the database, although we are clearly violating that promise
> right now.

Unfortunately the second option is not possible, as alembic itself auto-creates this table as soon as you call get_current_revision(). This is, however, fixed in newer versions of alembic, so it will be possible to remove this workaround if and when alembic is upgraded.

https://gerrit.beaker-project.org/#/c/5161/

Comment 2 Dan Callaghan 2016-09-30 00:58:14 UTC
This bug fix is included in beaker-server-23.3-0.git.10.a898f12 which is currently available for download here:

https://beaker-project.org/nightlies/release-23/

Comment 4 Dan Callaghan 2016-11-07 06:44:34 UTC
Beaker 23.3 has been released.