Updated table name to be configurable via DB_CONFIG

This commit is contained in:
2025-10-07 12:44:56 -04:00
parent 4969eebe28
commit d4509af986

View File

@@ -3,21 +3,24 @@ import sys
import datetime import datetime
import mysql.connector import mysql.connector
# Version 1.0
# Author: Sébastien Plante # Author: Sébastien Plante
# Date: 2025-10-07 # Date: 2025-10-07
# Description: # Description:
# This script scans a specified directory for folders and synchronizes their names and last modified dates # This script scans a specified directory for {table} and synchronizes their names and last modified dates
# with a MySQL database. It marks folders as inactive if they are no longer present in the directory. # with a MySQL database. It marks {table} as inactive if they are no longer present in the directory.
# Version 1.1
# Changes:
# - Updated table name to be configurable via DB_CONFIG
# Requirements: # Requirements:
# Install python 3.x from https://www.python.org/downloads/ # Install python 3.x from https://www.python.org/downloads/
# pip install mysql-connector-python # pip install mysql-connector-python
# Usage: # Usage:
# python foldersToSQL.py # python {table}ToSQL.py
# Optionally, you can specify a different path to scan: # Optionally, you can specify a different path to scan:
# python foldersToSQL.py "C:\another folder" # python {table}ToSQL.py "C:\another folder"
# ================== CONFIGURATION ================== # ================== CONFIGURATION ==================
SCAN_PATH = r"C:\test folder" SCAN_PATH = r"C:\test folder"
@@ -26,11 +29,12 @@ DB_CONFIG = {
"user": "root", "user": "root",
"password": "qwerty1234", "password": "qwerty1234",
"database": "testdb", "database": "testdb",
"table": "folders"
} }
# =================================================== # ===================================================
TABLE_SQL = """ TABLE_SQL = """
CREATE TABLE IF NOT EXISTS folders ( CREATE TABLE IF NOT EXISTS {table} (
id INT AUTO_INCREMENT PRIMARY KEY, id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL UNIQUE, name VARCHAR(255) NOT NULL UNIQUE,
last_modified DATETIME NOT NULL, last_modified DATETIME NOT NULL,
@@ -39,22 +43,25 @@ CREATE TABLE IF NOT EXISTS folders (
""" """
UPSERT_SQL = """ UPSERT_SQL = """
INSERT INTO folders (name, last_modified, active) INSERT INTO {table} (name, last_modified, active)
VALUES (%s, %s, 1) VALUES (%s, %s, 1)
ON DUPLICATE KEY UPDATE ON DUPLICATE KEY UPDATE
last_modified = VALUES(last_modified); last_modified = VALUES(last_modified);
""" """
MARK_INACTIVE_SQL_TEMPLATE = "UPDATE folders SET active = 0 WHERE name NOT IN ({placeholders});" MARK_INACTIVE_SQL_TEMPLATE = "UPDATE {table} SET active = 0 WHERE name NOT IN ({placeholders});"
MARK_ALL_INACTIVE_SQL = "UPDATE folders SET active = 0;" MARK_ALL_INACTIVE_SQL = "UPDATE {table} SET active = 0;"
def get_connection(): def get_connection():
return mysql.connector.connect(**DB_CONFIG) # Exclure la clé 'table' des paramètres de connexion
conn_params = {k: v for k, v in DB_CONFIG.items() if k != "table"}
return mysql.connector.connect(**conn_params)
def ensure_table(cursor): def ensure_table(cursor):
cursor.execute(TABLE_SQL) # Formatter le SQL de création avec le nom de table configuré
cursor.execute(TABLE_SQL.format(table=DB_CONFIG["table"]))
def list_directories(base_path): def list_directories(base_path):
@@ -82,18 +89,21 @@ def sync(base_path):
current_names = {name for name, _ in current_dirs} current_names = {name for name, _ in current_dirs}
for name, mtime in current_dirs: for name, mtime in current_dirs:
cur.execute(UPSERT_SQL, (name, mtime)) # Formatter l'UPSERT avec le nom de table
cur.execute(UPSERT_SQL.format(
table=DB_CONFIG["table"]), (name, mtime))
if current_names: if current_names:
placeholders = ",".join(["%s"] * len(current_names)) placeholders = ",".join(["%s"] * len(current_names))
sql = MARK_INACTIVE_SQL_TEMPLATE.format(placeholders=placeholders) # Formatter la requête de mise à jour avec le nom de table
sql = MARK_INACTIVE_SQL_TEMPLATE.format(
table=DB_CONFIG["table"], placeholders=placeholders)
cur.execute(sql, list(current_names)) cur.execute(sql, list(current_names))
else: else:
cur.execute(MARK_ALL_INACTIVE_SQL) cur.execute(MARK_ALL_INACTIVE_SQL.format(table=DB_CONFIG["table"]))
conn.commit() conn.commit()
print( print(f"Synchronization complete. Active folders: {len(current_dirs)}")
f"Synchronization complete. Active folders: {len(current_dirs)}")
except mysql.connector.Error as err: except mysql.connector.Error as err:
print(f"MySQL error: {err}") print(f"MySQL error: {err}")
finally: finally: