From 388d312679a1c1e69bd963d2e02b7bdc6f79632b Mon Sep 17 00:00:00 2001 From: Sebastien Plante Date: Tue, 14 Oct 2025 13:21:34 -0400 Subject: [PATCH] update to the way we are tracking update timestamps --- FoldersToMySQL.py | 52 +++++++++++++++++++++++++++++----------------- README.md | Bin 3412 -> 4356 bytes 2 files changed, 33 insertions(+), 19 deletions(-) diff --git a/FoldersToMySQL.py b/FoldersToMySQL.py index 085a9b3..94c5f1b 100644 --- a/FoldersToMySQL.py +++ b/FoldersToMySQL.py @@ -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)}") diff --git a/README.md b/README.md index b0ba56ec776066f89ccf0b3180b9c1584d18557c..45b0061fbccaedb5825aaa5354288015633884ae 100644 GIT binary patch delta 914 zcmZ`&J5Iw;5S%m=AVdQNR8KU4f)FAQ5*-pfh?FQI!FGaCVuL>sqH)6wxCjzg;Q-K) zf&79|ud5TQqa7&EzL)3AKHX8pDSK^ceFDU~)5ejkkjGt;s8BD2tYTqnm$yAAz z?t@=ZRX@sFVtkojS?f=t!cLfO!ms+EetFfsB3+QRps7_KGJW=g|BP`?+qBU>N8*T! zjLyKQWEHi!=F(#q*Ocm3NlYQQw>3w0X9pIj3C7m(tF-UpSEJf;` oG(3h=CFS~Q-XlY*Hto-i(MZsEI7~