61 lines
1.7 KiB
Python
61 lines
1.7 KiB
Python
|
import os
|
||
|
from urllib.parse import urlparse
|
||
|
|
||
|
import psycopg
|
||
|
from psycopg import sql
|
||
|
|
||
|
|
||
|
def generate_test_data() -> None:
|
||
|
"""
|
||
|
Generate the test data in PostgreSQL database
|
||
|
|
||
|
Watch for PostgreSQL version changes
|
||
|
"""
|
||
|
url_parts = urlparse(os.environ["TODO_DB_DATABASE_URL"])
|
||
|
|
||
|
# Extract connection parameters
|
||
|
dbname = url_parts.path[1:]
|
||
|
user_name = url_parts.username
|
||
|
password = url_parts.password
|
||
|
host = url_parts.hostname
|
||
|
port = url_parts.port
|
||
|
|
||
|
print(
|
||
|
f"The database name is {dbname}, the username is {user_name}, "
|
||
|
f"the password is {password}, the host is {host}, the port is {port}"
|
||
|
)
|
||
|
|
||
|
with psycopg.connect(
|
||
|
dbname=dbname, user=user_name, password=password, host=host, port=port
|
||
|
) as conn:
|
||
|
with conn.cursor() as cur:
|
||
|
# create the members table
|
||
|
cur.execute(
|
||
|
sql.SQL(
|
||
|
"""
|
||
|
INSERT INTO members (email, password_hash)
|
||
|
VALUES ('member@todo.test',
|
||
|
'$2b$14$6yXjNza30kPCg3LhzZJfqeCWOLM.zyTiQFD4rdWlFHBTfYzzKJMJe')
|
||
|
"""
|
||
|
)
|
||
|
)
|
||
|
|
||
|
cur.execute(
|
||
|
sql.SQL(
|
||
|
"""
|
||
|
INSERT INTO todos (member_id, task)
|
||
|
VALUES (1, 'Test Task')
|
||
|
"""
|
||
|
)
|
||
|
)
|
||
|
|
||
|
# Commit the changes
|
||
|
conn.commit()
|
||
|
|
||
|
print("Finished PostgreSQL test data generation")
|
||
|
print("=================================================================")
|
||
|
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
generate_test_data()
|