fix: update database migration script

This commit is contained in:
JustSong
2023-05-06 17:24:22 +08:00
parent 054c4719c1
commit 9e9f643aad
2 changed files with 18 additions and 22 deletions
+2 -2
View File
@@ -548,10 +548,10 @@ send_message('标题', '描述', '**Markdown 内容**')
此处均以 SQLite 为例,其他数据库请自行修改。我已经让 ChatGPT 翻译成对应的 SQL 版本,见 `bin` 文件夹,供参考。 此处均以 SQLite 为例,其他数据库请自行修改。我已经让 ChatGPT 翻译成对应的 SQL 版本,见 `bin` 文件夹,供参考。
### 从 `v0.3` 迁移到 `v0.4` ### 从 `v0.3` 迁移到 `v0.4`
1. 首先备份你的数据库文件,复制一份,命名为 `message-pusher-v0.3.db` 1. 首先备份你的数据库文件。
2. 下载最新的 `v0.4` 版本,启动程序,程序会自动进行数据库表结构的迁移。 2. 下载最新的 `v0.4` 版本,启动程序,程序会自动进行数据库表结构的迁移。
3. 终止程序。 3. 终止程序。
4. 之后执行脚本:`./bin/migrate_v3_to_v4.py --v4_db_path ./message-pusher.db`,进行数据的迁移。 4. 之后执行脚本:`./bin/migrate_v3_to_v4.py`,进行数据的迁移。
5. 重新启动程序即可。 5. 重新启动程序即可。
## 其他 ## 其他
+16 -20
View File
@@ -10,77 +10,73 @@ def get_timestamp():
def main(args): def main(args):
old_conn = sqlite3.connect(args.v3_db_path) conn = sqlite3.connect(args.db_path)
old_cursor = old_conn.cursor() cursor = conn.cursor()
new_conn = sqlite3.connect(args.v4_db_path) res = cursor.execute("SELECT * FROM users")
new_cursor = new_conn.cursor()
new_cursor.execute("DELETE FROM users")
res = old_cursor.execute("SELECT * FROM users")
users = res.fetchall() 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: 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," "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(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", "send_email_to_others,save_message_to_database) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
(id, username, password, display_name, role, status, token, email, github_id, wechat_id, channel, (id, username, password, display_name, role, status, token, email, github_id, wechat_id, channel,
send_email_to_others, save_message_to_database)) send_email_to_others, save_message_to_database))
if email: if email:
new_cursor.execute( cursor.execute(
"INSERT INTO channels " "INSERT INTO channels "
"(type,user_id,name,description,status,secret,app_id,account_id,url,other,created_time) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", "(type,user_id,name,description,status,secret,app_id,account_id,url,other,created_time) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
('email', id, 'email', '', 1, '', '', '', '', '', get_timestamp())) ('email', id, 'email', '', 1, '', '', '', '', '', get_timestamp()))
if wechat_test_account_id: if wechat_test_account_id:
new_cursor.execute( cursor.execute(
"INSERT INTO channels " "INSERT INTO channels "
"(type,user_id,name,description,status,secret,app_id,account_id,url,other,created_time) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", "(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, ('test', id, 'test', '', 1, wechat_test_account_secret, wechat_test_account_id,
wechat_test_account_open_id, '', wechat_test_account_template_id, get_timestamp())) wechat_test_account_open_id, '', wechat_test_account_template_id, get_timestamp()))
if wechat_corp_account_id: if wechat_corp_account_id:
new_cursor.execute( cursor.execute(
"INSERT INTO channels " "INSERT INTO channels "
"(type,user_id,name,description,status,secret,app_id,account_id,url,other,created_time) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", "(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, ('corp_app', id, 'corp_app', '', 1, wechat_corp_account_agent_secret,
f"{wechat_corp_account_id}|{wechat_corp_account_agent_id}", f"{wechat_corp_account_id}|{wechat_corp_account_agent_id}",
wechat_corp_account_user_id, '', wechat_corp_account_client_type, get_timestamp())) wechat_corp_account_user_id, '', wechat_corp_account_client_type, get_timestamp()))
if lark_webhook_url: if lark_webhook_url:
new_cursor.execute( cursor.execute(
"INSERT INTO channels " "INSERT INTO channels "
"(type,user_id,name,description,status,secret,app_id,account_id,url,other,created_time) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", "(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())) ('lark', id, 'lark', '', 1, lark_webhook_secret, '', '', lark_webhook_url, '', get_timestamp()))
if ding_webhook_url: if ding_webhook_url:
new_cursor.execute( cursor.execute(
"INSERT INTO channels " "INSERT INTO channels "
"(type,user_id,name,description,status,secret,app_id,account_id,url,other,created_time) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", "(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())) ('ding', id, 'ding', '', 1, ding_webhook_secret, '', '', ding_webhook_url, '', get_timestamp()))
if corp_webhook_url: if corp_webhook_url:
new_cursor.execute( cursor.execute(
"INSERT INTO channels " "INSERT INTO channels "
"(type,user_id,name,description,status,secret,app_id,account_id,url,other,created_time) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", "(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())) ('corp', id, 'corp', '', 1, '', '', '', corp_webhook_url, '', get_timestamp()))
if bark_server: if bark_server:
new_cursor.execute( cursor.execute(
"INSERT INTO channels " "INSERT INTO channels "
"(type,user_id,name,description,status,secret,app_id,account_id,url,other,created_time) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", "(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())) ('bark', id, 'bark', '', 1, bark_secret, '', '', bark_server, '', get_timestamp()))
if telegram_bot_token: if telegram_bot_token:
new_cursor.execute( cursor.execute(
"INSERT INTO channels " "INSERT INTO channels "
"(type,user_id,name,description,status,secret,app_id,account_id,url,other,created_time) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", "(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())) ('telegram', id, 'telegram', '', 1, telegram_bot_token, '', telegram_chat_id, '', '', get_timestamp()))
if discord_webhook_url: if discord_webhook_url:
new_cursor.execute( cursor.execute(
"INSERT INTO channels " "INSERT INTO channels "
"(type,user_id,name,description,status,secret,app_id,account_id,url,other,created_time) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", "(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())) ('discord', id, 'discord', '', 1, '', '', '', discord_webhook_url, '', get_timestamp()))
if client_secret: if client_secret:
new_cursor.execute( cursor.execute(
"INSERT INTO channels " "INSERT INTO channels "
"(type,user_id,name,description,status,secret,app_id,account_id,url,other,created_time) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", "(type,user_id,name,description,status,secret,app_id,account_id,url,other,created_time) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
('client', id, 'client', '', 1, client_secret, '', '', '', '', get_timestamp())) ('client', id, 'client', '', 1, client_secret, '', '', '', '', get_timestamp()))
new_conn.commit() conn.commit()
if __name__ == '__main__': if __name__ == '__main__':
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument('--v3_db_path', type=str, default='./message-pusher-v0.3.db') parser.add_argument('--db_path', type=str, default='./message-pusher.db')
parser.add_argument('--v4_db_path', type=str, default='./message-pusher-v0.4.db')
main(parser.parse_args()) main(parser.parse_args())