Move to one postgres secret (#33)
This commit is contained in:
parent
bbe0fd99d2
commit
b068dcf347
|
@ -2,16 +2,14 @@
|
|||
|
||||
## Commands
|
||||
|
||||
All commands require passing in the following parameters:
|
||||
All commands require passing in the postgress connection string:
|
||||
|
||||
```
|
||||
database_name: str
|
||||
database_host: str
|
||||
database_port: int
|
||||
database_user: str
|
||||
database_password: str
|
||||
database_connection_str: str
|
||||
```
|
||||
|
||||
Which is passed directly to `pyscopg2`. eg: `dbname=dev user=dev password=dev host=postgres-db port=5432`
|
||||
|
||||
All commands except for `DoSQL` require `table_name: str` as well.
|
||||
|
||||
### CreateTable
|
||||
|
|
|
@ -12,9 +12,9 @@ class ConnectionConfig:
|
|||
class BaseCommand:
|
||||
"""BaseCommand."""
|
||||
|
||||
def _execute(self, sql, config, handler):
|
||||
def _execute(self, sql, conn_str, handler):
|
||||
try:
|
||||
conn = psycopg2.connect(self._get_db_connection_str(config))
|
||||
conn = psycopg2.connect(conn_str)
|
||||
with conn.cursor() as cursor:
|
||||
response = handler(conn, cursor)
|
||||
if response is None:
|
||||
|
@ -31,14 +31,14 @@ class BaseCommand:
|
|||
|
||||
return {"response": response, "status": status, "mimetype": "application/json"}
|
||||
|
||||
def execute_query(self, sql, config, values=None):
|
||||
def execute_query(self, sql, conn_str, values=None):
|
||||
def handler(conn, cursor):
|
||||
cursor.execute(sql, values)
|
||||
conn.commit()
|
||||
|
||||
return self._execute(sql, config, handler)
|
||||
return self._execute(sql, conn_str, handler)
|
||||
|
||||
def execute_batch(self, sql, config, vars_list):
|
||||
def execute_batch(self, sql, conn_str, vars_list):
|
||||
def handler(conn, cursor):
|
||||
cursor.executemany(sql, vars_list)
|
||||
# TODO: look more into getting this to work instead
|
||||
|
@ -46,16 +46,16 @@ class BaseCommand:
|
|||
# https://www.psycopg.org/docs/extras.html#fast-exec
|
||||
conn.commit()
|
||||
|
||||
return self._execute(sql, config, handler)
|
||||
return self._execute(sql, conn_str, handler)
|
||||
|
||||
def fetchall(self, sql, config, values):
|
||||
def fetchall(self, sql, conn_str, values):
|
||||
def prep_results(results):
|
||||
return list(map(list, results))
|
||||
def handler(conn, cursor):
|
||||
cursor.execute(sql, values)
|
||||
return json.dumps(prep_results(cursor.fetchall()))
|
||||
|
||||
return self._execute(sql, config, handler)
|
||||
return self._execute(sql, conn_str, handler)
|
||||
|
||||
def build_where_clause(self, schema):
|
||||
where_configs = schema.get("where", [])
|
||||
|
@ -74,6 +74,3 @@ class BaseCommand:
|
|||
columns, values = zip(*where_parts)
|
||||
|
||||
return f"WHERE {' AND '.join(columns)}", values
|
||||
|
||||
def _get_db_connection_str(self, config):
|
||||
return f"dbname={config.database} user={config.user} password={config.password} host={config.host} port={config.port}"
|
||||
|
|
|
@ -9,21 +9,12 @@ class CreateTable(BaseCommand):
|
|||
"""CreateTable."""
|
||||
|
||||
def __init__(self,
|
||||
database_name: str,
|
||||
database_host: str,
|
||||
database_port: int,
|
||||
database_user: str,
|
||||
database_password: str,
|
||||
database_connection_str: str,
|
||||
table_name: str,
|
||||
schema: Dict[str, Any]
|
||||
):
|
||||
"""__init__."""
|
||||
self.connection_config = ConnectionConfig(
|
||||
database_name,
|
||||
database_host,
|
||||
database_port,
|
||||
database_user,
|
||||
database_password)
|
||||
self.database_connection_str = database_connection_str
|
||||
self.table_name = table_name
|
||||
self.schema = schema
|
||||
|
||||
|
@ -34,7 +25,7 @@ class CreateTable(BaseCommand):
|
|||
# https://www.psycopg.org/docs/usage.html#passing-parameters-to-sql-queries
|
||||
sql = f"CREATE TABLE IF NOT EXISTS {self.table_name} ({columns});"
|
||||
|
||||
return self.execute_query(sql, self.connection_config)
|
||||
return self.execute_query(sql, self.database_connection_str)
|
||||
|
||||
def _column_definitions(self, schema):
|
||||
def column_defintion(column):
|
||||
|
|
|
@ -9,21 +9,12 @@ class DeleteValues(BaseCommand):
|
|||
"""DeleteValues."""
|
||||
|
||||
def __init__(self,
|
||||
database_name: str,
|
||||
database_host: str,
|
||||
database_port: int,
|
||||
database_user: str,
|
||||
database_password: str,
|
||||
database_connection_str: str,
|
||||
table_name: str,
|
||||
schema: Dict[str, Any]
|
||||
):
|
||||
"""__init__."""
|
||||
self.connection_config = ConnectionConfig(
|
||||
database_name,
|
||||
database_host,
|
||||
database_port,
|
||||
database_user,
|
||||
database_password)
|
||||
self.database_connection_str = database_connection_str
|
||||
self.table_name = table_name
|
||||
self.schema = schema
|
||||
|
||||
|
@ -34,5 +25,5 @@ class DeleteValues(BaseCommand):
|
|||
# https://www.psycopg.org/docs/usage.html#passing-parameters-to-sql-queries
|
||||
sql = f"DELETE FROM {self.table_name} {where_clause};"
|
||||
|
||||
return self.execute_query(sql, self.connection_config, values)
|
||||
return self.execute_query(sql, self.database_connection_str, values)
|
||||
|
||||
|
|
|
@ -14,20 +14,11 @@ class DoSQL(BaseCommand):
|
|||
"""DoSQL."""
|
||||
|
||||
def __init__(self,
|
||||
database_name: str,
|
||||
database_host: str,
|
||||
database_port: int,
|
||||
database_user: str,
|
||||
database_password: str,
|
||||
database_connection_str: str,
|
||||
schema: Dict[str, Any]
|
||||
):
|
||||
"""__init__."""
|
||||
self.connection_config = ConnectionConfig(
|
||||
database_name,
|
||||
database_host,
|
||||
database_port,
|
||||
database_user,
|
||||
database_password)
|
||||
self.database_connection_str = database_connection_str
|
||||
self.schema = schema
|
||||
|
||||
def execute(self, config, task_data):
|
||||
|
@ -37,6 +28,6 @@ class DoSQL(BaseCommand):
|
|||
fetch_results = self.schema.get("fetch_results", False)
|
||||
|
||||
if fetch_results:
|
||||
return self.fetchall(sql, self.connection_config, values)
|
||||
return self.fetchall(sql, self.database_connection_str, values)
|
||||
|
||||
return self.execute_query(sql, self.connection_config, values)
|
||||
return self.execute_query(sql, self.database_connection_str, values)
|
||||
|
|
|
@ -6,20 +6,11 @@ class DropTable(BaseCommand):
|
|||
"""DropTable."""
|
||||
|
||||
def __init__(self,
|
||||
database_name: str,
|
||||
database_host: str,
|
||||
database_port: int,
|
||||
database_user: str,
|
||||
database_password: str,
|
||||
database_connection_str: str,
|
||||
table_name: str
|
||||
):
|
||||
"""__init__."""
|
||||
self.connection_config = ConnectionConfig(
|
||||
database_name,
|
||||
database_host,
|
||||
database_port,
|
||||
database_user,
|
||||
database_password)
|
||||
self.database_connection_str = database_connection_str
|
||||
self.table_name = table_name
|
||||
|
||||
def execute(self, config, task_data):
|
||||
|
@ -28,5 +19,5 @@ class DropTable(BaseCommand):
|
|||
# https://www.psycopg.org/docs/usage.html#passing-parameters-to-sql-queries
|
||||
sql = f"DROP TABLE IF EXISTS {self.table_name};"
|
||||
|
||||
return self.execute_query(sql, self.connection_config)
|
||||
return self.execute_query(sql, self.database_connection_str)
|
||||
|
||||
|
|
|
@ -9,21 +9,12 @@ class InsertValues(BaseCommand):
|
|||
"""InsertValues."""
|
||||
|
||||
def __init__(self,
|
||||
database_name: str,
|
||||
database_host: str,
|
||||
database_port: int,
|
||||
database_user: str,
|
||||
database_password: str,
|
||||
database_connection_str: str,
|
||||
table_name: str,
|
||||
schema: Dict[str, Any]
|
||||
):
|
||||
"""__init__."""
|
||||
self.connection_config = ConnectionConfig(
|
||||
database_name,
|
||||
database_host,
|
||||
database_port,
|
||||
database_user,
|
||||
database_password)
|
||||
self.database_connection_str = database_connection_str
|
||||
self.table_name = table_name
|
||||
self.schema = schema
|
||||
|
||||
|
@ -36,4 +27,4 @@ class InsertValues(BaseCommand):
|
|||
# https://www.psycopg.org/docs/usage.html#passing-parameters-to-sql-queries
|
||||
sql = f"INSERT INTO {self.table_name} ({columns}) VALUES {placeholders};"
|
||||
|
||||
return self.execute_batch(sql, self.connection_config, value_lists)
|
||||
return self.execute_batch(sql, self.database_connection_str, value_lists)
|
||||
|
|
|
@ -9,21 +9,12 @@ class SelectValues(BaseCommand):
|
|||
"""SelectValues."""
|
||||
|
||||
def __init__(self,
|
||||
database_name: str,
|
||||
database_host: str,
|
||||
database_port: int,
|
||||
database_user: str,
|
||||
database_password: str,
|
||||
database_connection_str: str,
|
||||
table_name: str,
|
||||
schema: Dict[str, Any]
|
||||
):
|
||||
"""__init__."""
|
||||
self.connection_config = ConnectionConfig(
|
||||
database_name,
|
||||
database_host,
|
||||
database_port,
|
||||
database_user,
|
||||
database_password)
|
||||
self.database_connection_str = database_connection_str
|
||||
self.table_name = table_name
|
||||
self.schema = schema
|
||||
|
||||
|
@ -36,4 +27,4 @@ class SelectValues(BaseCommand):
|
|||
# https://www.psycopg.org/docs/usage.html#passing-parameters-to-sql-queries
|
||||
sql = f"SELECT {columns} FROM {self.table_name} {where_clause};"
|
||||
|
||||
return self.fetchall(sql, self.connection_config, values)
|
||||
return self.fetchall(sql, self.database_connection_str, values)
|
||||
|
|
|
@ -9,21 +9,12 @@ class UpdateValues(BaseCommand):
|
|||
"""UpdateValues."""
|
||||
|
||||
def __init__(self,
|
||||
database_name: str,
|
||||
database_host: str,
|
||||
database_port: int,
|
||||
database_user: str,
|
||||
database_password: str,
|
||||
database_connection_str: str,
|
||||
table_name: str,
|
||||
schema: Dict[str, Any]
|
||||
):
|
||||
"""__init__."""
|
||||
self.connection_config = ConnectionConfig(
|
||||
database_name,
|
||||
database_host,
|
||||
database_port,
|
||||
database_user,
|
||||
database_password)
|
||||
self.database_connection_str = database_connection_str
|
||||
self.table_name = table_name
|
||||
self.schema = schema
|
||||
|
||||
|
@ -38,7 +29,7 @@ class UpdateValues(BaseCommand):
|
|||
# https://www.psycopg.org/docs/usage.html#passing-parameters-to-sql-queries
|
||||
sql = f"UPDATE {self.table_name} {set_clause} {where_clause};"
|
||||
|
||||
return self.execute_query(sql, self.connection_config, values)
|
||||
return self.execute_query(sql, self.database_connection_str, values)
|
||||
|
||||
def _build_set_clause(self, schema):
|
||||
columns_to_values = schema["set"]
|
||||
|
|
Loading…
Reference in New Issue