57 lines
1.5 KiB
Python
57 lines
1.5 KiB
Python
import os
|
|
import time
|
|
import pymysql
|
|
import csv
|
|
import logging
|
|
import time
|
|
|
|
logging.basicConfig(level=logging.INFO)
|
|
logger = logging.getLogger(__name__)
|
|
|
|
db_config = {
|
|
'host': 'db',
|
|
'user': os.getenv('MYSQL_USER'),
|
|
'password': os.getenv('MYSQL_PASSWORD'),
|
|
'db': os.getenv('MYSQL_DATABASE'),
|
|
}
|
|
|
|
logger.info("Waiting for the database to be ready...")
|
|
logger.info(f"Config: {db_config}")
|
|
|
|
while True:
|
|
logger.info("Try connect to db")
|
|
try:
|
|
connection = pymysql.connect(**db_config)
|
|
break
|
|
except pymysql.MySQLError:
|
|
time.sleep(3)
|
|
|
|
with connection.cursor() as cursor:
|
|
cursor.execute("""
|
|
CREATE TABLE IF NOT EXISTS data (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
name VARCHAR(255) NOT NULL,
|
|
age INT NOT NULL
|
|
);
|
|
""")
|
|
logger.info("Table 'data' is ready.")
|
|
|
|
logger.info("Start reading csv and insert to db")
|
|
|
|
with connection.cursor() as cursor, open('data.csv', 'r') as csv_file:
|
|
reader = csv.DictReader(csv_file)
|
|
for row in reader:
|
|
cursor.execute("INSERT INTO data (name, age) VALUES (%s, %s)", (row['name'], row['age']))
|
|
connection.commit()
|
|
|
|
logger.info("Database filled successfully.")
|
|
|
|
with connection.cursor() as cursor:
|
|
cursor.execute("SELECT * FROM data")
|
|
for row in cursor.fetchall():
|
|
logger.info(f"Data: {row}")
|
|
|
|
with open('/app/data_filled.signal', 'w') as signal_file:
|
|
signal_file.write('done')
|
|
logger.info("Filler task completed. Signal file created.")
|