From 52da0fb23b59f774e4a5f4f38bf5ff749540fd7d Mon Sep 17 00:00:00 2001 From: JustSong Date: Sat, 6 May 2023 16:35:23 +0800 Subject: [PATCH] feat: add database migration script --- README.md | 11 ++++++ bin/migrate_v3_to_v4.py | 86 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 bin/migrate_v3_to_v4.py diff --git a/README.md b/README.md index d4e0ed6..90c3d56 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,8 @@ _✨ 搭建专属于你的消息推送服务,支持多种消息推送方式, > 公告:官方部署站 https://msgpusher.com 现已上线,当前开放注册,欢迎使用。如果收到积极反馈未来可以考虑换用延迟更低的服务器。 +> 注意:从 `v0.3` 版本升级到 `v0.4` 版本需要手动迁移数据库,具体方法见 [迁移数据库](#迁移数据库)。 + ## 描述 1. 多种消息推送方式: + 邮件消息, @@ -542,6 +544,15 @@ send_message('标题', '描述', '**Markdown 内容**') 欢迎 PR 添加更多语言的示例。 +## 迁移数据库 +此处均以 SQLite 为例,其他数据库请自行修改。 + +### 从 `v0.3` 迁移到 `v0.4` +1. 首先备份你的数据库文件,修改原本的数据库文件名称为 `message-pusher-v0.3.db`。 +2. 下载最新的 `v0.4` 版本,启动程序,程序会生成新的数据库文件,手动重命名为 `message-pusher-v0.4.db`。 +3. 执行脚本:`./bin/migrate_v3_to_v4.py`。 +4. 执行完毕后将 `message-pusher-v0.4.db` 重命名为 `message-pusher.db` 即可。 + ## 其他 1. `v0.3` 之前的版本基于 Node.js,你可以切换到 [`nodejs`](https://github.com/songquanpeng/message-pusher/tree/nodejs) 分支查看,该版本不再有功能性更新。 2. `v0.3` 以及后续版本基于 Gin Template [`v0.2.1`](https://github.com/songquanpeng/gin-template) 版本开发。 diff --git a/bin/migrate_v3_to_v4.py b/bin/migrate_v3_to_v4.py new file mode 100644 index 0000000..8be1ce6 --- /dev/null +++ b/bin/migrate_v3_to_v4.py @@ -0,0 +1,86 @@ +#!/usr/bin/env python3 + +import argparse +import sqlite3 +import time + + +def get_timestamp(): + return int(time.time()) + + +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") + 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( + "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( + "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( + "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( + "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( + "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( + "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( + "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( + "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( + "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( + "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( + "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() + + +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') + main(parser.parse_args())