From 4a38ce4e7b842438e101fc65e28893ec68b8e8c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20Mielcarek?= Date: Fri, 31 May 2024 13:21:34 +0200 Subject: [PATCH] Adding database creation with GitHub Actions --- .github/workflows/database.yml | 5 ++-- database/db-creation.py | 47 +++++++++++----------------------- 2 files changed, 18 insertions(+), 34 deletions(-) diff --git a/.github/workflows/database.yml b/.github/workflows/database.yml index 91ea2ef..0c969e3 100644 --- a/.github/workflows/database.yml +++ b/.github/workflows/database.yml @@ -10,6 +10,7 @@ jobs: image: postgres env: POSTGRES_PASSWORD: postgres + POSTGRES_DB: big-brother # Set health checks to wait until postgres has started options: >- --health-cmd pg_isready @@ -25,10 +26,10 @@ jobs: uses: actions/checkout@v4 - name: Install dependencies - run: pip3 install mariadb + run: pip3 install psycopg2 - name: Connect to PostgreSQL run: python3 database/db-creation.py env: POSTGRES_HOST: postgres - POSTGRES_PORT: 5432 + POSTGRES_PORT: 5432 \ No newline at end of file diff --git a/database/db-creation.py b/database/db-creation.py index f68bebf..04ca796 100644 --- a/database/db-creation.py +++ b/database/db-creation.py @@ -1,35 +1,18 @@ -import os -import mariadb +import psycopg2 -env_user = os.getenv('MARIADB_USER') -env_password = os.getenv('MARIADB_PASSWORD') -env_database = os.getenv('MARIADB_DATABASE') +conn = psycopg2.connect( + database="big-brother", user='postgres', password='postgres', host='127.0.0.1', port= '5432' +) +cursor = conn.cursor() -def executeDBQuery(query): - conn = mariadb.connect( - user=env_user, - password=env_password, - host="felixmielcarek-bigbrotherdb", - database=env_database - ) - conn.autocommit = True - cur = conn.cursor() - cur.execute(query) - cur.close() - conn.close() +cursor.execute("DROP TABLE IF EXISTS USERS") -grant_privileges_query = ''' -GRANT ALL PRIVILEGES ON {database}.* TO '{user}'@'%' IDENTIFIED BY '{password}'; -FLUSH PRIVILEGES; -'''.format(database=env_database, user=env_user, password=env_password) - -create_table_query = ''' -CREATE TABLE IF NOT EXISTS users ( - spotifyid VARCHAR(255) NOT NULL, - accesstoken VARCHAR(255), - refreshtoken VARCHAR(255), - PRIMARY KEY (spotifyid) -); -''' - -executeDBQuery(create_table_query) \ No newline at end of file +sql ='''CREATE TABLE USERS( + SPOTIFY_ID VARCHAR(255) PRIMARY KEY, + ACCESS_TOKEN VARCHAR(255), + REFRESH_TOKEN VARCHAR(255) +)''' +cursor.execute(sql) +print("Table created successfully........") +conn.commit() +conn.close() \ No newline at end of file