migrate_v3_to_v4.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/usr/bin/env python3
  2. import argparse
  3. import sqlite3
  4. import time
  5. def get_timestamp():
  6. return int(time.time())
  7. def main(args):
  8. conn = sqlite3.connect(args.db_path)
  9. cursor = conn.cursor()
  10. cursor.execute("DELETE FROM channels")
  11. res = cursor.execute("SELECT * FROM users")
  12. users = res.fetchall()
  13. 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:
  14. if email:
  15. cursor.execute(
  16. "INSERT INTO channels "
  17. "(type,user_id,name,description,status,secret,app_id,account_id,url,other,created_time) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
  18. ('email', id, 'email', '', 1, '', '', '', '', '', get_timestamp()))
  19. if wechat_test_account_id:
  20. cursor.execute(
  21. "INSERT INTO channels "
  22. "(type,user_id,name,description,status,secret,app_id,account_id,url,other,created_time) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
  23. ('test', id, 'test', '', 1, wechat_test_account_secret, wechat_test_account_id,
  24. wechat_test_account_open_id, '', wechat_test_account_template_id, get_timestamp()))
  25. if wechat_corp_account_id:
  26. cursor.execute(
  27. "INSERT INTO channels "
  28. "(type,user_id,name,description,status,secret,app_id,account_id,url,other,created_time) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
  29. ('corp_app', id, 'corp_app', '', 1, wechat_corp_account_agent_secret,
  30. f"{wechat_corp_account_id}|{wechat_corp_account_agent_id}",
  31. wechat_corp_account_user_id, '', wechat_corp_account_client_type, get_timestamp()))
  32. if lark_webhook_url:
  33. cursor.execute(
  34. "INSERT INTO channels "
  35. "(type,user_id,name,description,status,secret,app_id,account_id,url,other,created_time) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
  36. ('lark', id, 'lark', '', 1, lark_webhook_secret, '', '', lark_webhook_url, '', get_timestamp()))
  37. if ding_webhook_url:
  38. cursor.execute(
  39. "INSERT INTO channels "
  40. "(type,user_id,name,description,status,secret,app_id,account_id,url,other,created_time) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
  41. ('ding', id, 'ding', '', 1, ding_webhook_secret, '', '', ding_webhook_url, '', get_timestamp()))
  42. if corp_webhook_url:
  43. cursor.execute(
  44. "INSERT INTO channels "
  45. "(type,user_id,name,description,status,secret,app_id,account_id,url,other,created_time) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
  46. ('corp', id, 'corp', '', 1, '', '', '', corp_webhook_url, '', get_timestamp()))
  47. if bark_server:
  48. cursor.execute(
  49. "INSERT INTO channels "
  50. "(type,user_id,name,description,status,secret,app_id,account_id,url,other,created_time) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
  51. ('bark', id, 'bark', '', 1, bark_secret, '', '', bark_server, '', get_timestamp()))
  52. if telegram_bot_token:
  53. cursor.execute(
  54. "INSERT INTO channels "
  55. "(type,user_id,name,description,status,secret,app_id,account_id,url,other,created_time) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
  56. ('telegram', id, 'telegram', '', 1, telegram_bot_token, '', telegram_chat_id, '', '', get_timestamp()))
  57. if discord_webhook_url:
  58. cursor.execute(
  59. "INSERT INTO channels "
  60. "(type,user_id,name,description,status,secret,app_id,account_id,url,other,created_time) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
  61. ('discord', id, 'discord', '', 1, '', '', '', discord_webhook_url, '', get_timestamp()))
  62. if client_secret:
  63. cursor.execute(
  64. "INSERT INTO channels "
  65. "(type,user_id,name,description,status,secret,app_id,account_id,url,other,created_time) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
  66. ('client', id, 'client', '', 1, client_secret, '', '', '', '', get_timestamp()))
  67. conn.commit()
  68. if __name__ == '__main__':
  69. parser = argparse.ArgumentParser()
  70. parser.add_argument('--db_path', type=str, default='./message-pusher.db')
  71. main(parser.parse_args())