Skip to content
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

fixing issue with how the script was checking if the database already existed #160

Merged
merged 3 commits into from
Mar 10, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down