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 d354f26
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
15 changes: 13 additions & 2 deletions worek/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,16 @@ 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)
__import__('pdb').set_trace()
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 d354f26

Please sign in to comment.