Updated table name to be configurable via DB_CONFIG
This commit is contained in:
@@ -3,21 +3,24 @@ import sys
|
||||
import datetime
|
||||
import mysql.connector
|
||||
|
||||
# Version 1.0
|
||||
# Author: Sébastien Plante
|
||||
# Date: 2025-10-07
|
||||
# Description:
|
||||
# This script scans a specified directory for folders 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.
|
||||
# This script scans a specified directory for {table} and synchronizes their names and last modified dates
|
||||
# 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:
|
||||
# Install python 3.x from https://www.python.org/downloads/
|
||||
# pip install mysql-connector-python
|
||||
|
||||
# Usage:
|
||||
# python foldersToSQL.py
|
||||
# python {table}ToSQL.py
|
||||
# Optionally, you can specify a different path to scan:
|
||||
# python foldersToSQL.py "C:\another folder"
|
||||
# python {table}ToSQL.py "C:\another folder"
|
||||
|
||||
# ================== CONFIGURATION ==================
|
||||
SCAN_PATH = r"C:\test folder"
|
||||
@@ -26,11 +29,12 @@ DB_CONFIG = {
|
||||
"user": "root",
|
||||
"password": "qwerty1234",
|
||||
"database": "testdb",
|
||||
"table": "folders"
|
||||
}
|
||||
# ===================================================
|
||||
|
||||
TABLE_SQL = """
|
||||
CREATE TABLE IF NOT EXISTS folders (
|
||||
CREATE TABLE IF NOT EXISTS {table} (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
name VARCHAR(255) NOT NULL UNIQUE,
|
||||
last_modified DATETIME NOT NULL,
|
||||
@@ -39,22 +43,25 @@ CREATE TABLE IF NOT EXISTS folders (
|
||||
"""
|
||||
|
||||
UPSERT_SQL = """
|
||||
INSERT INTO folders (name, last_modified, active)
|
||||
INSERT INTO {table} (name, last_modified, active)
|
||||
VALUES (%s, %s, 1)
|
||||
ON DUPLICATE KEY UPDATE
|
||||
last_modified = VALUES(last_modified);
|
||||
"""
|
||||
|
||||
MARK_INACTIVE_SQL_TEMPLATE = "UPDATE folders SET active = 0 WHERE name NOT IN ({placeholders});"
|
||||
MARK_ALL_INACTIVE_SQL = "UPDATE folders SET active = 0;"
|
||||
MARK_INACTIVE_SQL_TEMPLATE = "UPDATE {table} SET active = 0 WHERE name NOT IN ({placeholders});"
|
||||
MARK_ALL_INACTIVE_SQL = "UPDATE {table} SET active = 0;"
|
||||
|
||||
|
||||
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):
|
||||
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):
|
||||
@@ -82,18 +89,21 @@ def sync(base_path):
|
||||
current_names = {name for name, _ 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:
|
||||
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))
|
||||
else:
|
||||
cur.execute(MARK_ALL_INACTIVE_SQL)
|
||||
cur.execute(MARK_ALL_INACTIVE_SQL.format(table=DB_CONFIG["table"]))
|
||||
|
||||
conn.commit()
|
||||
print(
|
||||
f"Synchronization complete. Active folders: {len(current_dirs)}")
|
||||
print(f"Synchronization complete. Active folders: {len(current_dirs)}")
|
||||
except mysql.connector.Error as err:
|
||||
print(f"MySQL error: {err}")
|
||||
finally:
|
||||
|
||||
Reference in New Issue
Block a user