Skip to content

Commit

Permalink
Merge pull request #5388 from hypothesis/stamp-new-databases
Browse files Browse the repository at this point in the history
Stamp new databases with alembic version
  • Loading branch information
robertknight committed Oct 18, 2018
2 parents 5fbea16 + a60f9f2 commit dbc2830
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
18 changes: 2 additions & 16 deletions docs/developing/making-changes-to-model-code.rst
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,7 @@ steps to create a new migration script for h are:
downgrade that adds the constraint back may not work with data created
using the updated schema.

3. Stamp your database.

Before running any upgrades or downgrades you need to stamp the database
with its current revision, so Alembic knows which migration scripts to run:

.. code-block:: bash
tox -e py27-dev -- sh bin/hypothesis migrate stamp <revision_id>
``<revision_id>`` should be the revision corresponding to the version of the
code that was present when the current database was created. The will
usually be the ``down_revision`` from the migration script that you've just
generated.

4. Test your ``upgrade()`` function by upgrading your database to the most
3. Test your ``upgrade()`` function by upgrading your database to the most
recent revision. This will run all migration scripts newer than the revision
that your db is currently stamped with, which usually means just your new
revision script:
Expand All @@ -96,7 +82,7 @@ steps to create a new migration script for h are:
columns of the database before testing upgrading and downgrading it.
Some migration script crashes will only happen when there's data present.

5. Test your ``downgrade()`` function:
4. Test your ``downgrade()`` function:

.. code-block:: bash
Expand Down
7 changes: 7 additions & 0 deletions h/cli/commands/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import logging
import os

import alembic.config
import alembic.command
import click
import sqlalchemy

Expand Down Expand Up @@ -37,6 +39,11 @@ def _init_db(settings):
except sqlalchemy.exc.ProgrammingError:
log.info("initializing database")
db.init(engine, should_create=True, authority=text_type(settings['h.authority']))

# Stamp the database with the current schema version so that future
# migrations start from the correct point.
alembic_cfg = alembic.config.Config('conf/alembic.ini')
alembic.command.stamp(alembic_cfg, 'head')
else:
log.info("detected alembic_version table, skipping db initialization")

Expand Down
24 changes: 23 additions & 1 deletion tests/h/cli/commands/init_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from h.cli.commands import init as init_cli


@pytest.mark.usefixtures('db', 'search')
@pytest.mark.usefixtures('alembic_config', 'alembic_stamp', 'db', 'search')
class TestInitCommand(object):
def test_initialises_database(self,
cli,
Expand Down Expand Up @@ -44,6 +44,18 @@ def _cleanup():
assert not db.init.called
assert result.exit_code == 0

def test_stamps_alembic_version(self, alembic_config, alembic_stamp, cli,
cliconfig, db, db_engine, pyramid_settings):
db.make_engine.return_value = db_engine
Config = alembic_config.Config
pyramid_settings['h.authority'] = 'foobar.org'

result = cli.invoke(init_cli.init, obj=cliconfig)

Config.assert_called_once_with('conf/alembic.ini')
alembic_stamp.assert_called_once_with(Config.return_value, 'head')
assert result.exit_code == 0

def test_initialises_search(self,
cli,
cliconfig,
Expand Down Expand Up @@ -71,3 +83,13 @@ def db(patch):
@pytest.fixture
def search(patch):
return patch('h.cli.commands.init.search')


@pytest.fixture
def alembic_config(patch):
return patch('h.cli.commands.init.alembic.config')


@pytest.fixture
def alembic_stamp(patch):
return patch('h.cli.commands.init.alembic.command.stamp')

0 comments on commit dbc2830

Please sign in to comment.