From 63fdff4e569b7f5f0f7d31ee041fcb30bce65868 Mon Sep 17 00:00:00 2001 From: Jace Manshadi Date: Sat, 9 Mar 2019 18:38:10 -0800 Subject: [PATCH] apparently, its not possible to create a database within a function as the following error indicates "CREATE DATABASE cannot be executed from a function", as a result I am manually checking for the existence of the required database and creating it if it doesn't exist --- main.py | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/main.py b/main.py index 5c1b36180..a40eed00e 100644 --- a/main.py +++ b/main.py @@ -80,22 +80,23 @@ def setupDB(): logger.info("[main.py setupDB] "+settings.WALL_E_DB_USER+" role created") if 'localhost' == settings.ENVIRONMENT or 'PRODUCTION' == settings.ENVIRONMENT: - sqlQuery="""DO - $do$ - BEGIN - IF NOT EXISTS ( - SELECT -- SELECT list can stay empty for this - FROM pg_database - WHERE datname = '"""+settings.WALL_E_DB_DBNAME+"""') THEN - CREATE DATABASE """+settings.WALL_E_DB_DBNAME+""" WITH OWNER """+settings.WALL_E_DB_USER+""" TEMPLATE = template0; - END IF; - END - $do$;""" + sqlQuery="""SELECT datname from pg_database""" postgresCurs.execute(sqlQuery) + results = postgresCurs.fetchall() + + #fetchAll returns [('postgres',), ('template0',), ('template1',), ('csss_discord_db',)] + #which the below line converts to ['postgres', 'template0', 'template1', 'csss_discord_db'] + results = [x for xs in results for x in xs] + + print(str(results)) + if settings.WALL_E_DB_DBNAME not in results: + postgresCurs.execute("CREATE DATABASE "+settings.WALL_E_DB_DBNAME+" WITH OWNER "+settings.WALL_E_DB_USER+" TEMPLATE = template0;") + logger.info("[main.py setupDB] "+settings.WALL_E_DB_DBNAME+" database created") + else: + logger.info("[main.py setupDB] "+settings.WALL_E_DB_DBNAME+" database already exists") else: postgresCurs.execute("CREATE DATABASE "+settings.WALL_E_DB_DBNAME+" WITH OWNER "+settings.WALL_E_DB_USER+" TEMPLATE = template0;") - - logger.info("[main.py setupDB] "+settings.WALL_E_DB_DBNAME+" database created") + logger.info("[main.py setupDB] "+settings.WALL_E_DB_DBNAME+" database created") #this section exists cause of this backup.sql that I had exported from an instance of a Postgres with which I had created the csss_discord_db #https://github.com/CSSS/wall_e/blob/implement_postgres/helper_files/backup.sql#L31