From 9e9f643aada443536b3fada3a5d2f633e5e55064 Mon Sep 17 00:00:00 2001 From: JustSong Date: Sat, 6 May 2023 17:24:22 +0800 Subject: [PATCH] fix: update database migration script --- README.md | 4 ++-- bin/migrate_v3_to_v4.py | 36 ++++++++++++++++-------------------- 2 files changed, 18 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 5b98d96..bff77e7 100644 --- a/README.md +++ b/README.md @@ -548,10 +548,10 @@ send_message('标题', '描述', '**Markdown 内容**') 此处均以 SQLite 为例,其他数据库请自行修改。我已经让 ChatGPT 翻译成对应的 SQL 版本,见 `bin` 文件夹,供参考。 ### 从 `v0.3` 迁移到 `v0.4` -1. 首先备份你的数据库文件,复制一份,命名为 `message-pusher-v0.3.db`。 +1. 首先备份你的数据库文件。 2. 下载最新的 `v0.4` 版本,启动程序,程序会自动进行数据库表结构的迁移。 3. 终止程序。 -4. 之后执行脚本:`./bin/migrate_v3_to_v4.py --v4_db_path ./message-pusher.db`,进行数据的迁移。 +4. 之后执行脚本:`./bin/migrate_v3_to_v4.py`,进行数据的迁移。 5. 重新启动程序即可。 ## 其他 diff --git a/bin/migrate_v3_to_v4.py b/bin/migrate_v3_to_v4.py index 8be1ce6..91dfafa 100644 --- a/bin/migrate_v3_to_v4.py +++ b/bin/migrate_v3_to_v4.py @@ -10,77 +10,73 @@ def get_timestamp(): def main(args): - old_conn = sqlite3.connect(args.v3_db_path) - old_cursor = old_conn.cursor() - new_conn = sqlite3.connect(args.v4_db_path) - new_cursor = new_conn.cursor() - new_cursor.execute("DELETE FROM users") - res = old_cursor.execute("SELECT * FROM users") + conn = sqlite3.connect(args.db_path) + cursor = conn.cursor() + res = cursor.execute("SELECT * FROM users") users = res.fetchall() for id, username, password, display_name, role, status, token, email, github_id, wechat_id, channel, wechat_test_account_id, wechat_test_account_secret, wechat_test_account_template_id, wechat_test_account_open_id, wechat_test_account_verification_token, wechat_corp_account_id, wechat_corp_account_agent_secret, wechat_corp_account_agent_id, wechat_corp_account_user_id, wechat_corp_account_client_type, corp_webhook_url, lark_webhook_url, lark_webhook_secret, ding_webhook_url, ding_webhook_secret, bark_server, bark_secret, client_secret, telegram_bot_token, telegram_chat_id, discord_webhook_url, send_email_to_others, save_message_to_database in users: - new_cursor.execute( + cursor.execute( "INSERT INTO users (id,username,password,display_name,role,status,token,email,github_id,wechat_id,channel," "send_email_to_others,save_message_to_database) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", (id, username, password, display_name, role, status, token, email, github_id, wechat_id, channel, send_email_to_others, save_message_to_database)) if email: - new_cursor.execute( + cursor.execute( "INSERT INTO channels " "(type,user_id,name,description,status,secret,app_id,account_id,url,other,created_time) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", ('email', id, 'email', '', 1, '', '', '', '', '', get_timestamp())) if wechat_test_account_id: - new_cursor.execute( + cursor.execute( "INSERT INTO channels " "(type,user_id,name,description,status,secret,app_id,account_id,url,other,created_time) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", ('test', id, 'test', '', 1, wechat_test_account_secret, wechat_test_account_id, wechat_test_account_open_id, '', wechat_test_account_template_id, get_timestamp())) if wechat_corp_account_id: - new_cursor.execute( + cursor.execute( "INSERT INTO channels " "(type,user_id,name,description,status,secret,app_id,account_id,url,other,created_time) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", ('corp_app', id, 'corp_app', '', 1, wechat_corp_account_agent_secret, f"{wechat_corp_account_id}|{wechat_corp_account_agent_id}", wechat_corp_account_user_id, '', wechat_corp_account_client_type, get_timestamp())) if lark_webhook_url: - new_cursor.execute( + cursor.execute( "INSERT INTO channels " "(type,user_id,name,description,status,secret,app_id,account_id,url,other,created_time) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", ('lark', id, 'lark', '', 1, lark_webhook_secret, '', '', lark_webhook_url, '', get_timestamp())) if ding_webhook_url: - new_cursor.execute( + cursor.execute( "INSERT INTO channels " "(type,user_id,name,description,status,secret,app_id,account_id,url,other,created_time) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", ('ding', id, 'ding', '', 1, ding_webhook_secret, '', '', ding_webhook_url, '', get_timestamp())) if corp_webhook_url: - new_cursor.execute( + cursor.execute( "INSERT INTO channels " "(type,user_id,name,description,status,secret,app_id,account_id,url,other,created_time) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", ('corp', id, 'corp', '', 1, '', '', '', corp_webhook_url, '', get_timestamp())) if bark_server: - new_cursor.execute( + cursor.execute( "INSERT INTO channels " "(type,user_id,name,description,status,secret,app_id,account_id,url,other,created_time) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", ('bark', id, 'bark', '', 1, bark_secret, '', '', bark_server, '', get_timestamp())) if telegram_bot_token: - new_cursor.execute( + cursor.execute( "INSERT INTO channels " "(type,user_id,name,description,status,secret,app_id,account_id,url,other,created_time) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", ('telegram', id, 'telegram', '', 1, telegram_bot_token, '', telegram_chat_id, '', '', get_timestamp())) if discord_webhook_url: - new_cursor.execute( + cursor.execute( "INSERT INTO channels " "(type,user_id,name,description,status,secret,app_id,account_id,url,other,created_time) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", ('discord', id, 'discord', '', 1, '', '', '', discord_webhook_url, '', get_timestamp())) if client_secret: - new_cursor.execute( + cursor.execute( "INSERT INTO channels " "(type,user_id,name,description,status,secret,app_id,account_id,url,other,created_time) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", ('client', id, 'client', '', 1, client_secret, '', '', '', '', get_timestamp())) - new_conn.commit() + conn.commit() if __name__ == '__main__': parser = argparse.ArgumentParser() - parser.add_argument('--v3_db_path', type=str, default='./message-pusher-v0.3.db') - parser.add_argument('--v4_db_path', type=str, default='./message-pusher-v0.4.db') + parser.add_argument('--db_path', type=str, default='./message-pusher.db') main(parser.parse_args())