|
| 1 | +import os |
| 2 | +import psycopg |
| 3 | +from psycopg import sql |
| 4 | + |
| 5 | +# Database connection setup |
| 6 | +def connect_to_db(db_name, user, password, host="localhost", port=5432): |
| 7 | + return psycopg.connect(dbname=db_name, user=user, password=password, host=host, port=port) |
| 8 | + |
| 9 | +# Initialize database schema |
| 10 | +def initialize_schema(connection): |
| 11 | + with connection.cursor() as cursor: |
| 12 | + cursor.execute(""" |
| 13 | + CREATE TABLE IF NOT EXISTS study ( |
| 14 | + id SERIAL PRIMARY KEY, |
| 15 | + name VARCHAR(50) UNIQUE NOT NULL |
| 16 | + ); |
| 17 | +
|
| 18 | + CREATE TABLE IF NOT EXISTS site ( |
| 19 | + id SERIAL PRIMARY KEY, |
| 20 | + name VARCHAR(50) NOT NULL, |
| 21 | + study_id INT REFERENCES study(id) ON DELETE CASCADE |
| 22 | + ); |
| 23 | +
|
| 24 | + CREATE TABLE IF NOT EXISTS subject ( |
| 25 | + id SERIAL PRIMARY KEY, |
| 26 | + name VARCHAR(50) NOT NULL, |
| 27 | + site_id INT REFERENCES site(id) ON DELETE CASCADE |
| 28 | + ); |
| 29 | +
|
| 30 | + CREATE TABLE IF NOT EXISTS task ( |
| 31 | + id SERIAL PRIMARY KEY, |
| 32 | + name VARCHAR(50) NOT NULL, |
| 33 | + subject_id INT REFERENCES subject(id) ON DELETE CASCADE |
| 34 | + ); |
| 35 | +
|
| 36 | + CREATE TABLE IF NOT EXISTS session ( |
| 37 | + id SERIAL PRIMARY KEY, |
| 38 | + session_name VARCHAR(50) NOT NULL, |
| 39 | + category INT NOT NULL, |
| 40 | + csv_path TEXT, |
| 41 | + plot_paths TEXT[], |
| 42 | + task_id INT REFERENCES task(id) ON DELETE CASCADE |
| 43 | + ); |
| 44 | + """) |
| 45 | + connection.commit() |
| 46 | + |
| 47 | +# Populate the database from the folder structure |
| 48 | +def populate_database(connection, data_folder): |
| 49 | + for study_name in os.listdir(data_folder): |
| 50 | + study_path = os.path.join(data_folder, study_name) |
| 51 | + if not os.path.isdir(study_path): |
| 52 | + continue |
| 53 | + |
| 54 | + with connection.cursor() as cursor: |
| 55 | + cursor.execute("INSERT INTO study (name) VALUES (%s) ON CONFLICT (name) DO NOTHING RETURNING id;", (study_name,)) |
| 56 | + study_id = cursor.fetchone() or (cursor.execute("SELECT id FROM study WHERE name = %s;", (study_name,)), cursor.fetchone()[0]) |
| 57 | + |
| 58 | + for site_name in os.listdir(study_path): |
| 59 | + site_path = os.path.join(study_path, site_name) |
| 60 | + if not os.path.isdir(site_path): |
| 61 | + continue |
| 62 | + |
| 63 | + with connection.cursor() as cursor: |
| 64 | + cursor.execute("INSERT INTO site (name, study_id) VALUES (%s, %s) ON CONFLICT DO NOTHING RETURNING id;", (site_name, study_id)) |
| 65 | + site_id = cursor.fetchone() or (cursor.execute("SELECT id FROM site WHERE name = %s AND study_id = %s;", (site_name, study_id)), cursor.fetchone()[0]) |
| 66 | + |
| 67 | + for subject_name in os.listdir(site_path): |
| 68 | + subject_path = os.path.join(site_path, subject_name) |
| 69 | + if not os.path.isdir(subject_path): |
| 70 | + continue |
| 71 | + |
| 72 | + with connection.cursor() as cursor: |
| 73 | + cursor.execute("INSERT INTO subject (name, site_id) VALUES (%s, %s) ON CONFLICT DO NOTHING RETURNING id;", (subject_name, site_id)) |
| 74 | + subject_id = cursor.fetchone() or (cursor.execute("SELECT id FROM subject WHERE name = %s AND site_id = %s;", (subject_name, site_id)), cursor.fetchone()[0]) |
| 75 | + |
| 76 | + for task_name in os.listdir(subject_path): |
| 77 | + task_path = os.path.join(subject_path, task_name) |
| 78 | + if not os.path.isdir(task_path): |
| 79 | + continue |
| 80 | + |
| 81 | + with connection.cursor() as cursor: |
| 82 | + cursor.execute("INSERT INTO task (name, subject_id) VALUES (%s, %s) ON CONFLICT DO NOTHING RETURNING id;", (task_name, subject_id)) |
| 83 | + task_id = cursor.fetchone() or (cursor.execute("SELECT id FROM task WHERE name = %s AND subject_id = %s;", (task_name, subject_id)), cursor.fetchone()[0]) |
| 84 | + |
| 85 | + for folder in ["data", "plot"]: |
| 86 | + folder_path = os.path.join(task_path, folder) |
| 87 | + if not os.path.exists(folder_path): |
| 88 | + continue |
| 89 | + |
| 90 | + if folder == "data": |
| 91 | + for file in os.listdir(folder_path): |
| 92 | + if file.endswith(".csv"): |
| 93 | + parts = file.split("_") |
| 94 | + session_name = parts[1].split("-")[1] |
| 95 | + category = int(parts[2].split("-")[1].split(".")[0]) |
| 96 | + |
| 97 | + with connection.cursor() as cursor: |
| 98 | + cursor.execute(""" |
| 99 | + INSERT INTO session (session_name, category, csv_path, task_id) |
| 100 | + VALUES (%s, %s, %s, %s) |
| 101 | + ON CONFLICT DO NOTHING; |
| 102 | + """, (session_name, category, os.path.join(folder_path, file), task_id)) |
| 103 | + |
| 104 | + elif folder == "plot": |
| 105 | + plots = [] |
| 106 | + for file in os.listdir(folder_path): |
| 107 | + if file.endswith(".png"): |
| 108 | + plots.append(os.path.join(folder_path, file)) |
| 109 | + |
| 110 | + with connection.cursor() as cursor: |
| 111 | + cursor.execute(""" |
| 112 | + UPDATE session |
| 113 | + SET plot_paths = %s |
| 114 | + WHERE task_id = %s; |
| 115 | + """, (plots, task_id)) |
| 116 | + connection.commit() |
| 117 | +import psycopg |
| 118 | +from psycopg import sql |
| 119 | + |
| 120 | +def initialize_postgres_db(host, user, password, port, db_name): |
| 121 | + try: |
| 122 | + # Connect to PostgreSQL server (default database is 'postgres') |
| 123 | + connection = psycopg.connect( |
| 124 | + host=host, |
| 125 | + user=user, |
| 126 | + password=password, |
| 127 | + port=port, |
| 128 | + dbname="postgres" # Connect to the default database |
| 129 | + ) |
| 130 | + connection.autocommit = True # To allow database creation outside transactions |
| 131 | + cursor = connection.cursor() |
| 132 | + |
| 133 | + # Create the new database |
| 134 | + cursor.execute(sql.SQL("CREATE DATABASE {}").format(sql.Identifier(db_name))) |
| 135 | + print(f"Database {db_name} created successfully.") |
| 136 | + |
| 137 | + # Close the connection to 'postgres' |
| 138 | + cursor.close() |
| 139 | + connection.close() |
| 140 | + |
| 141 | + # Connect to the new database |
| 142 | + connection = psycopg.connect( |
| 143 | + host=host, |
| 144 | + user=user, |
| 145 | + password=password, |
| 146 | + port=port, |
| 147 | + dbname=db_name |
| 148 | + ) |
| 149 | + cursor = connection.cursor() |
| 150 | + |
| 151 | + # Create a sample table |
| 152 | + cursor.execute(""" |
| 153 | + CREATE TABLE IF NOT EXISTS users ( |
| 154 | + id SERIAL PRIMARY KEY, |
| 155 | + name VARCHAR(100) NOT NULL, |
| 156 | + email VARCHAR(100) UNIQUE NOT NULL, |
| 157 | + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP |
| 158 | + ) |
| 159 | + """) |
| 160 | + print("Table 'users' created successfully.") |
| 161 | + |
| 162 | + # Commit and close |
| 163 | + connection.commit() |
| 164 | + cursor.close() |
| 165 | + connection.close() |
| 166 | + |
| 167 | + except psycopg.Error as e: |
| 168 | + print(f"An error occurred: {e}") |
| 169 | + finally: |
| 170 | + if connection: |
| 171 | + connection.close() |
| 172 | + |
| 173 | +# Main entry point |
| 174 | +if __name__ == "__main__": |
| 175 | + db_name = "main_db" |
| 176 | + user = "zgdev" |
| 177 | + password = "*mIloisfAT23*123*" |
| 178 | + data_folder = "../data" |
| 179 | + # Example usage |
| 180 | + initialize_postgres_db( |
| 181 | + host="localhost", |
| 182 | + user="zgdev", |
| 183 | + password="*mIloisfAT23*123*", |
| 184 | + port=5432, |
| 185 | + db_name="main_db" |
| 186 | + ) |
| 187 | + |
| 188 | + """conn = connect_to_db(db_name, user, password) |
| 189 | + try: |
| 190 | + initialize_schema(conn) |
| 191 | + populate_database(conn, data_folder) |
| 192 | + print("Database initialized and populated successfully.") |
| 193 | + finally: |
| 194 | + conn.close()""" |
0 commit comments