-
Notifications
You must be signed in to change notification settings - Fork 45
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
use provided dname if available for _noproc
#660
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,7 @@ def __init__( | |
password: Optional[str] = None, | ||
isolation_level: "Optional[psycopg.IsolationLevel]" = None, | ||
connection_timeout: int = 60, | ||
use_database: bool = False, | ||
) -> None: | ||
""" | ||
Initialize janitor. | ||
|
@@ -44,13 +45,16 @@ def __init__( | |
defaults to server's default | ||
:param connection_timeout: how long to retry connection before | ||
raising a TimeoutError | ||
:param use_database: whether to use an existing database | ||
(may imply manual cleanup) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this makes the janitor effectively using only a loader method that can do something 🤔 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm okay for now, just thinking out loud whether this should be extracted to something separate for this single responsibility later now 🤔 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok. Indeed this parameter makes the janitor out of a job, but I do not think it is worth changing the code structure to avoid it, I'd just let it be. |
||
""" | ||
self.user = user | ||
self.password = password | ||
self.host = host | ||
self.port = port | ||
self.dbname = dbname | ||
self._connection_timeout = connection_timeout | ||
self._use_database = use_database | ||
check_for_psycopg() | ||
self.isolation_level = isolation_level | ||
if not isinstance(version, Version): | ||
|
@@ -60,6 +64,8 @@ def __init__( | |
|
||
def init(self) -> None: | ||
"""Create database in postgresql.""" | ||
if self._use_database: | ||
return | ||
template_name = f"{self.dbname}_tmpl" | ||
with self.cursor() as cur: | ||
if self.dbname.endswith("_tmpl"): | ||
|
@@ -86,13 +92,17 @@ def drop(self) -> None: | |
"""Drop database in postgresql.""" | ||
# We cannot drop the database while there are connections to it, so we | ||
# terminate all connections first while not allowing new connections. | ||
if self._use_database: | ||
return | ||
with self.cursor() as cur: | ||
self._dont_datallowconn(cur, self.dbname) | ||
self._terminate_connection(cur, self.dbname) | ||
cur.execute(f'DROP DATABASE IF EXISTS "{self.dbname}";') | ||
|
||
@staticmethod | ||
def _dont_datallowconn(cur: cursor, dbname: str) -> None: | ||
if self._use_database: | ||
return | ||
cur.execute(f'ALTER DATABASE "{dbname}" with allow_connections false;') | ||
|
||
@staticmethod | ||
|
@@ -136,7 +146,7 @@ def cursor(self) -> Iterator[cursor]: | |
|
||
def connect() -> connection: | ||
return psycopg.connect( | ||
dbname="postgres", | ||
dbname=self.dbname or "postgres", | ||
user=self.user, | ||
password=self.password, | ||
host=self.host, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but dbname will never be empty 🤔 And we'll attempt to create the database in question.... most probably prior to it's creation 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmm. Indeed,
_noproc
tries to create a database, and fails. I'd like to be able to use it with a basic database account, but the problem goes deeper than just connecting to thepostgres
database :-/There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With test file
test.py
:Run test against an existing database:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be dependant on the new flag you've added. rather than the self.dbname being null which it will not 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure. This means that the option has somehow two effects, but why not.
I've updated the patch.