Adding database creation with GitHub Actions

main
Félix MIELCAREK 11 months ago
parent b5b1753d7d
commit 4a38ce4e7b

@ -10,6 +10,7 @@ jobs:
image: postgres image: postgres
env: env:
POSTGRES_PASSWORD: postgres POSTGRES_PASSWORD: postgres
POSTGRES_DB: big-brother
# Set health checks to wait until postgres has started # Set health checks to wait until postgres has started
options: >- options: >-
--health-cmd pg_isready --health-cmd pg_isready
@ -25,7 +26,7 @@ jobs:
uses: actions/checkout@v4 uses: actions/checkout@v4
- name: Install dependencies - name: Install dependencies
run: pip3 install mariadb run: pip3 install psycopg2
- name: Connect to PostgreSQL - name: Connect to PostgreSQL
run: python3 database/db-creation.py run: python3 database/db-creation.py

@ -1,35 +1,18 @@
import os import psycopg2
import mariadb
env_user = os.getenv('MARIADB_USER') conn = psycopg2.connect(
env_password = os.getenv('MARIADB_PASSWORD') database="big-brother", user='postgres', password='postgres', host='127.0.0.1', port= '5432'
env_database = os.getenv('MARIADB_DATABASE') )
cursor = conn.cursor()
def executeDBQuery(query): cursor.execute("DROP TABLE IF EXISTS USERS")
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()
grant_privileges_query = ''' sql ='''CREATE TABLE USERS(
GRANT ALL PRIVILEGES ON {database}.* TO '{user}'@'%' IDENTIFIED BY '{password}'; SPOTIFY_ID VARCHAR(255) PRIMARY KEY,
FLUSH PRIVILEGES; ACCESS_TOKEN VARCHAR(255),
'''.format(database=env_database, user=env_user, password=env_password) REFRESH_TOKEN VARCHAR(255)
)'''
create_table_query = ''' cursor.execute(sql)
CREATE TABLE IF NOT EXISTS users ( print("Table created successfully........")
spotifyid VARCHAR(255) NOT NULL, conn.commit()
accesstoken VARCHAR(255), conn.close()
refreshtoken VARCHAR(255),
PRIMARY KEY (spotifyid)
);
'''
executeDBQuery(create_table_query)
Loading…
Cancel
Save