Compare commits

..

3 Commits

Author SHA1 Message Date
c00d0a8d43 typo 2025-10-14 13:22:08 -04:00
388d312679 update to the way we are tracking update timestamps 2025-10-14 13:21:34 -04:00
293b56afa6 typo 2025-10-07 12:49:32 -04:00
2 changed files with 33 additions and 19 deletions

View File

@@ -9,7 +9,14 @@ import mysql.connector
# 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.
# Version 1.1
# Version 1.2 [2025-10-14]
# Changes:
# - folder_created_date now uses the folder's (OS) creation time.
# - folder_last_seen now uses the folder's last time it has been seen by the script.
# - Added last_updated field track when the record was last modified.
# ** This version is not backward compatible with previous versions, you will need to drop the existing table.
# Version 1.1 [2025-10-07]
# Changes:
# - Updated table name to be configurable via DB_CONFIG
@@ -37,30 +44,33 @@ TABLE_SQL = """
CREATE TABLE IF NOT EXISTS {table} (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL UNIQUE,
last_modified DATETIME NOT NULL,
active TINYINT(1) NOT NULL DEFAULT 1
folder_created_date DATETIME NOT NULL,
folder_last_seen DATETIME NOT NULL,
active TINYINT(1) NOT NULL DEFAULT 1,
last_updated DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
"""
UPSERT_SQL = """
INSERT INTO {table} (name, last_modified, active)
VALUES (%s, %s, 1)
INSERT INTO {table} (name, folder_created_date, folder_last_seen, active, last_updated)
VALUES (%s, %s, %s, 1, %s)
ON DUPLICATE KEY UPDATE
last_modified = VALUES(last_modified);
folder_created_date = VALUES(folder_created_date),
folder_last_seen = VALUES(folder_last_seen),
active = 1,
last_updated = VALUES(last_updated);
"""
MARK_INACTIVE_SQL_TEMPLATE = "UPDATE {table} SET active = 0 WHERE name NOT IN ({placeholders});"
MARK_ALL_INACTIVE_SQL = "UPDATE {table} SET active = 0;"
MARK_INACTIVE_SQL_TEMPLATE = "UPDATE {table} SET active = 0, last_updated = %s WHERE name NOT IN ({placeholders});"
MARK_ALL_INACTIVE_SQL = "UPDATE {table} SET active = 0, last_updated = %s;"
def get_connection():
# 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):
# Formatter le SQL de création avec le nom de table configuré
cursor.execute(TABLE_SQL.format(table=DB_CONFIG["table"]))
@@ -74,8 +84,10 @@ def list_directories(base_path):
for name in entries:
full = os.path.join(base_path, name)
if os.path.isdir(full):
mtime = datetime.datetime.fromtimestamp(os.path.getmtime(full))
dirs.append((name, mtime))
ctime = datetime.datetime.fromtimestamp(
os.path.getctime(full)) # creation time
# mtime = datetime.datetime.fromtimestamp(os.path.getmtime(full)) # last modified time
dirs.append((name, ctime))
return dirs
@@ -88,19 +100,21 @@ def sync(base_path):
current_dirs = list_directories(base_path)
current_names = {name for name, _ in current_dirs}
for name, mtime in current_dirs:
# Formatter l'UPSERT avec le nom de table
cur.execute(UPSERT_SQL.format(
table=DB_CONFIG["table"]), (name, mtime))
for name, ctime in current_dirs:
cur.execute(
UPSERT_SQL.format(table=DB_CONFIG["table"]),
(name, ctime, datetime.datetime.now(), datetime.datetime.now()),
)
if current_names:
placeholders = ",".join(["%s"] * len(current_names))
# 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))
params = [datetime.datetime.now()] + list(current_names)
cur.execute(sql, params)
else:
cur.execute(MARK_ALL_INACTIVE_SQL.format(table=DB_CONFIG["table"]))
sql = MARK_ALL_INACTIVE_SQL.format(table=DB_CONFIG["table"])
cur.execute(sql, (datetime.datetime.now(),))
conn.commit()
print(f"Synchronization complete. Active folders: {len(current_dirs)}")

BIN
README.md

Binary file not shown.