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

prepare: warn but don't fail on initialised db (bug 1956987) #57

Merged
merged 2 commits into from
Mar 30, 2025
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
1 change: 1 addition & 0 deletions phabricatoremails/alembic/versions/3fc2902fe464_.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
Create Date: 2020-04-09 09:57:09.401710

"""

import sqlalchemy as sa
from alembic import op

Expand Down
4 changes: 0 additions & 4 deletions phabricatoremails/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@
from sqlalchemy.orm import sessionmaker


class DBInitializedError(Exception):
pass


class DBNotInitializedError(Exception):
pass

Expand Down
4 changes: 2 additions & 2 deletions phabricatoremails/prepare.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from phabricatoremails.db import DBInitializedError
from phabricatoremails.query_position_store import DBQueryPositionStore
from phabricatoremails.settings import Settings

Expand All @@ -20,11 +19,12 @@ def prepare(settings: Settings):
# want to invoke multiple times. This check asserts that "prepare" isn't
# run more than one time.
if db.is_initialized():
raise DBInitializedError(
settings.logger.warning(
"Database has already been initialized! Run "
"`phabricator-emails migrate` to upgrade an existing "
"database"
)
return

# We fetch the end key at the beginning here so that we fail early if
# we can't successfully communicate with Phabricator
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

[flake8]
max-line-length = 88
ignore = W503

# [mypy] section is now required in setup.cfg due to this issue:
# https://github.com/python/mypy/issues/9940
Expand Down
8 changes: 3 additions & 5 deletions tests/test_prepare.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,21 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import pytest
from kgb import spy_on

from phabricatoremails.db import DBInitializedError
from phabricatoremails.prepare import prepare
from tests.mock_db import MockDB
from tests.mock_settings import MockSettings
from tests.mock_source import MockSource
from tests.mock_worker import MockWorker


def test_prepare_doesnt_upgrade_schema_if_already_initialized():
def test_prepare_doesnt_upgrade_schema_if_already_initialized(caplog):
db = MockDB(is_initialized=True)
settings = MockSettings(db=db)
with spy_on(db.upgrade_schema) as spy:
with pytest.raises(DBInitializedError):
prepare(settings)
prepare(settings)
assert "already been initialized!" in caplog.text
assert not spy.called


Expand Down