Skip to content

Commit

Permalink
database_exists: don't connect to 'postgres' data base for database e…
Browse files Browse the repository at this point in the history
…xistence check (#372)

* database_exists: remove rewriting of database

for connection strings starting with 'postgres' the function
tested for the database with the name 'postgres' instead of the
database specified by the url.

This has been introduced here (the if else branch probably made
sense in dro and create, but it doesn't here in my opinion)

5d741c1
d7f2905

* fix
  • Loading branch information
bernt-matthias authored Jul 8, 2020
1 parent 73ea41d commit 5d99747
Showing 1 changed file with 1 addition and 6 deletions.
7 changes: 1 addition & 6 deletions sqlalchemy_utils/functions/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,12 +458,7 @@ def sqlite_file_exists(database):
return header[:16] == b'SQLite format 3\x00'

url = copy(make_url(url))
database = url.database
if url.drivername.startswith('postgres'):
url.database = 'postgres'
else:
url.database = None

database, url.database = url.database, None
engine = sa.create_engine(url)

if engine.dialect.name == 'postgresql':
Expand Down

1 comment on commit 5d99747

@YSelfTool
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit is wrong and should be reverted.

The changed function (database_exists) intends to check if the url points to an existing database
There are two ways to do that:

  1. connect to the uri and check for exceptions
  2. connect to the metadata database postgres and query the table pg_database for the database of the uri.
    Before this commit, the function did 2. But now, it does neither.
    Now, it connects to the default database of the user specified in the uri (not to the database in the uri). If that user is postgres, it connects to the correct database. If it isn't, it either connects to the wrong database or (if the default database of that user doesn't exist) fails.

Please sign in to comment.