add timestamp field to mysql

This commit is contained in:
cooper 2024-05-17 16:25:45 +08:00
parent 1f20355d9d
commit 2903506991
3 changed files with 18 additions and 4 deletions

2
.gitignore vendored
View File

@ -161,3 +161,5 @@ cython_debug/
# option (not recommended) you can uncomment the following to ignore the entire idea folder. # option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/ #.idea/
# My File
.secret

View File

@ -1 +1 @@
o36r0lgw71mdzm9rkwrv3wi1wn|3600|1715909100.492765|oadugxyl9fhoqsamqopc ontipj0py431xul5zm4kj6gzzo|3600|1715933457.543795|oadugxyl9fhoqsamqopc

View File

@ -24,7 +24,9 @@ class DecspiderPipeline:
# 检查表是否存在,如果不存在就创建表 # 检查表是否存在,如果不存在就创建表
self.cursor.execute(f""" self.cursor.execute(f"""
CREATE TABLE IF NOT EXISTS `{self.table_name}` ( CREATE TABLE IF NOT EXISTS `{self.table_name}` (
id INT AUTO_INCREMENT PRIMARY KEY id INT AUTO_INCREMENT PRIMARY KEY,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) )
""") """)
@ -41,15 +43,25 @@ class DecspiderPipeline:
self.cursor.execute(f"ALTER TABLE `{self.table_name}` ADD COLUMN `{column}` TEXT") self.cursor.execute(f"ALTER TABLE `{self.table_name}` ADD COLUMN `{column}` TEXT")
spider.log(f'Added column `{column}` to `{self.table_name}` table') spider.log(f'Added column `{column}` to `{self.table_name}` table')
# 添加 created_at 和 updated_at 字段,如果它们不存在
if 'created_at' not in existing_columns:
self.cursor.execute(f"ALTER TABLE `{self.table_name}` ADD COLUMN `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP")
spider.log(f'Added column `created_at` to `{self.table_name}` table')
if 'updated_at' not in existing_columns:
self.cursor.execute(f"ALTER TABLE `{self.table_name}` ADD COLUMN `updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP")
spider.log(f'Added column `updated_at` to `{self.table_name}` table')
# 删除表中不存在于 NewsItem 中的字段 # 删除表中不存在于 NewsItem 中的字段
for column in existing_columns: for column in existing_columns:
if column not in item_columns and column != 'id': if column not in item_columns and column not in {'id', 'created_at', 'updated_at'}:
self.cursor.execute(f"ALTER TABLE `{self.table_name}` DROP COLUMN `{column}`") self.cursor.execute(f"ALTER TABLE `{self.table_name}` DROP COLUMN `{column}`")
spider.log(f'Dropped column `{column}` from `{self.table_name}` table') spider.log(f'Dropped column `{column}` from `{self.table_name}` table')
self.conn.commit() self.conn.commit()
def close_spider(self, spider): def close_spider(self, spider):
self.conn.close() self.conn.close()