Skip to content

Commit

Permalink
Cleanup error message when database does not exist
Browse files Browse the repository at this point in the history
Fixes GH-5
  • Loading branch information
Nick Zaccardi committed Oct 31, 2019
1 parent 26d2b7d commit 5fd3b74
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 7 deletions.
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ whitelist_externals = *
passenv = PGUSER PGPASSWORD

commands =
pip install -e .[ci]
pip install --progress-bar=off -e .[ci]
# Output installed versions to compare with previous test runs in case a dependency's change
# breaks things for our build.
pip freeze
Expand Down
14 changes: 12 additions & 2 deletions worek/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,15 @@ def restore(host, port, user, dbname, engine, schema, restore_file, file_format)
' automatically determine the file format when using a pipe.',
)

core.restore(file_name, schema=schema, host=host, port=port, user=user, dbname=dbname,
file_format=file_format)
try:
core.restore(
file_name,
schema=schema,
host=host,
port=port,
user=user,
dbname=dbname,
file_format=file_format,
)
except core.WorekOperationException as e:
click.echo(str(e), err=True)
8 changes: 6 additions & 2 deletions worek/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ def backup(backup_file, backup_type='full', **params):
schemas=params.get('schemas'))

if not PG.engine_can_connect:
raise WorekOperationException('Can\'t connect to the database.')
raise WorekOperationException(
"Can't connect to the database. Trying to connect to {}.".format(repr(PG.engine.url))
)

if backup_type == 'full':
PG.backup_binary(backup_file)
Expand Down Expand Up @@ -58,7 +60,9 @@ def restore(restore_file, file_format=None, clean_existing_database=True, **para
schemas=params.get('schemas'))

if not PG.engine_can_connect:
raise WorekOperationException('Can\'t connect to the database.')
raise WorekOperationException(
"Can't connect to the database. Trying to connect to {}.".format(repr(PG.engine.url))
)

if clean_existing_database:
PG.clean_existing_database()
Expand Down
2 changes: 1 addition & 1 deletion worek/dialects/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def engine_can_connect(self):
try:
self.engine.execute("SELECT 1")
return True
except psycopg2.Error:
except (psycopg2.Error, sa.exc.OperationalError):
return False

@classmethod
Expand Down
19 changes: 18 additions & 1 deletion worek/tests/test_core.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import io

import pytest
import sqlalchemy as sa
import worek

import worek
from worek.tests.helpers import PostgresDialectTestBase


Expand All @@ -25,3 +28,17 @@ def test_backup_creates_full_restoreable_backup(self, tmpdir, pg_clean_engine):
pg_clean_engine.execute('SELECT * FROM should_be_removed;')
except sa.exc.ProgrammingError as e:
assert 'relation "should_be_removed" does not exist' in str(e)

def test_exception_with_no_database_backup(self):
with pytest.raises(worek.core.WorekOperationException) as e:
worek.core.backup('back.txt')

assert "Can't connect to the database. Trying to connect to " in str(e.value)


class TestCorePGRestore(PostgresDialectTestBase):
def test_exception_with_no_database_restore(self,):
with pytest.raises(worek.core.WorekOperationException) as e:
worek.core.restore(io.StringIO('test'))

assert "Can't connect to the database. Trying to connect to " in str(e.value)

0 comments on commit 5fd3b74

Please sign in to comment.