|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +""" |
| 4 | +直接使用SQL语句重置Card和Todo模块的数据库表,避开循环导入和模型重复定义问题 |
| 5 | +""" |
| 6 | +import os |
| 7 | +import sys |
| 8 | +import datetime |
| 9 | +import pymysql |
| 10 | +import yaml |
| 11 | + |
| 12 | +# 直接读取配置文件,避免导入应用模块 |
| 13 | +def direct_read_config(): |
| 14 | + """直接从配置文件读取数据库信息,避免导入应用模块""" |
| 15 | + config_path = os.path.join('woniunote', 'configs', 'config.py') |
| 16 | + try: |
| 17 | + # 读取Python配置文件内容 |
| 18 | + config_content = {} |
| 19 | + with open(config_path, 'r', encoding='utf-8') as f: |
| 20 | + # 提取数据库连接字符串 |
| 21 | + for line in f: |
| 22 | + if 'SQLALCHEMY_DATABASE_URI' in line: |
| 23 | + # 提取引号内的内容 |
| 24 | + import re |
| 25 | + match = re.search(r'["\'](.+?)["\']', line) |
| 26 | + if match: |
| 27 | + db_uri = match.group(1) |
| 28 | + return {'database': {'SQLALCHEMY_DATABASE_URI': db_uri}} |
| 29 | + |
| 30 | + # 如果没有找到连接字符串,返回None |
| 31 | + print("在配置文件中未找到数据库连接字符串") |
| 32 | + return None |
| 33 | + except Exception as e: |
| 34 | + print(f"读取配置文件出错: {str(e)}") |
| 35 | + return None |
| 36 | + |
| 37 | +# 解析数据库URI |
| 38 | +def direct_parse_db_uri(uri): |
| 39 | + """解析数据库URI获取连接信息""" |
| 40 | + # mysql://username:password@host:port/database |
| 41 | + if not uri.startswith('mysql://'): |
| 42 | + raise ValueError("仅支持MySQL数据库") |
| 43 | + |
| 44 | + # 去除协议前缀 |
| 45 | + uri = uri.replace('mysql://', '') |
| 46 | + |
| 47 | + # 提取用户名和密码 |
| 48 | + auth, rest = uri.split('@', 1) |
| 49 | + user, password = auth.split(':', 1) |
| 50 | + |
| 51 | + # 提取主机、端口和数据库名 |
| 52 | + host_port, database = rest.split('/', 1) |
| 53 | + |
| 54 | + if ':' in host_port: |
| 55 | + host, port = host_port.split(':', 1) |
| 56 | + else: |
| 57 | + host = host_port |
| 58 | + port = 3306 # 默认MySQL端口 |
| 59 | + |
| 60 | + return { |
| 61 | + 'host': host, |
| 62 | + 'port': port, |
| 63 | + 'user': user, |
| 64 | + 'password': password, |
| 65 | + 'database': database |
| 66 | + } |
| 67 | + |
| 68 | +# 直接创建数据库连接 |
| 69 | +def get_connection(): |
| 70 | + """直接创建数据库连接""" |
| 71 | + config = direct_read_config() |
| 72 | + if not config: |
| 73 | + print("无法读取配置") |
| 74 | + sys.exit(1) |
| 75 | + |
| 76 | + db_uri = config.get('database', {}).get('SQLALCHEMY_DATABASE_URI') |
| 77 | + if not db_uri: |
| 78 | + print("无法找到数据库URI") |
| 79 | + sys.exit(1) |
| 80 | + |
| 81 | + db_info = direct_parse_db_uri(db_uri) |
| 82 | + print(f"数据库连接信息: {db_info}") |
| 83 | + |
| 84 | + try: |
| 85 | + return pymysql.connect( |
| 86 | + host=db_info['host'], |
| 87 | + port=int(db_info['port']), |
| 88 | + user=db_info['user'], |
| 89 | + password=db_info['password'], |
| 90 | + database=db_info['database'], |
| 91 | + charset='utf8mb4' |
| 92 | + ) |
| 93 | + except Exception as e: |
| 94 | + print(f"连接数据库失败: {str(e)}") |
| 95 | + sys.exit(1) |
| 96 | + |
| 97 | +def reset_card_tables(): |
| 98 | + """重置Card模块的数据库表""" |
| 99 | + conn = get_connection() |
| 100 | + cursor = conn.cursor() |
| 101 | + |
| 102 | + try: |
| 103 | + # 删除表(如果存在) |
| 104 | + cursor.execute("DROP TABLE IF EXISTS card") |
| 105 | + cursor.execute("DROP TABLE IF EXISTS cardcategory") |
| 106 | + print("已删除Card相关表") |
| 107 | + |
| 108 | + # 创建cardcategory表 |
| 109 | + cursor.execute(""" |
| 110 | + CREATE TABLE cardcategory ( |
| 111 | + id INT PRIMARY KEY, |
| 112 | + name VARCHAR(64) NOT NULL |
| 113 | + ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; |
| 114 | + """) |
| 115 | + print("成功创建cardcategory表") |
| 116 | + |
| 117 | + # 创建card表 |
| 118 | + cursor.execute(""" |
| 119 | + CREATE TABLE card ( |
| 120 | + id INT AUTO_INCREMENT PRIMARY KEY, |
| 121 | + type INT DEFAULT 1, |
| 122 | + headline TEXT NOT NULL, |
| 123 | + content TEXT, |
| 124 | + createtime DATETIME, |
| 125 | + updatetime DATETIME, |
| 126 | + donetime DATETIME, |
| 127 | + usedtime INT DEFAULT 0, |
| 128 | + begintime DATETIME, |
| 129 | + endtime DATETIME, |
| 130 | + cardcategory_id INT DEFAULT 1, |
| 131 | + FOREIGN KEY (cardcategory_id) REFERENCES cardcategory(id) |
| 132 | + ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; |
| 133 | + """) |
| 134 | + print("成功创建card表") |
| 135 | + |
| 136 | + # 添加默认分类 |
| 137 | + cursor.execute("INSERT INTO cardcategory (id, name) VALUES (1, '待办卡片')") |
| 138 | + cursor.execute("INSERT INTO cardcategory (id, name) VALUES (2, '已完成')") |
| 139 | + conn.commit() |
| 140 | + print("成功添加默认分类") |
| 141 | + |
| 142 | + # 添加示例卡片 |
| 143 | + now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') |
| 144 | + cursor.execute(f""" |
| 145 | + INSERT INTO card (headline, type, createtime, cardcategory_id) |
| 146 | + VALUES ('这是一个示例卡片', 1, '{now}', 1) |
| 147 | + """) |
| 148 | + conn.commit() |
| 149 | + print("成功添加示例卡片") |
| 150 | + except Exception as e: |
| 151 | + print(f"重置Card表出错: {str(e)}") |
| 152 | + conn.rollback() |
| 153 | + |
| 154 | + cursor.close() |
| 155 | + conn.close() |
| 156 | + |
| 157 | +def reset_todo_tables(): |
| 158 | + """重置Todo模块的数据库表""" |
| 159 | + conn = get_connection() |
| 160 | + cursor = conn.cursor() |
| 161 | + |
| 162 | + try: |
| 163 | + # 删除表(如果存在) |
| 164 | + cursor.execute("DROP TABLE IF EXISTS todo_item") |
| 165 | + cursor.execute("DROP TABLE IF EXISTS todo_category") |
| 166 | + print("已删除Todo相关表") |
| 167 | + |
| 168 | + # 创建todo_category表 |
| 169 | + cursor.execute(""" |
| 170 | + CREATE TABLE todo_category ( |
| 171 | + id INT PRIMARY KEY, |
| 172 | + name VARCHAR(64) NOT NULL |
| 173 | + ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; |
| 174 | + """) |
| 175 | + print("成功创建todo_category表") |
| 176 | + |
| 177 | + # 创建todo_item表 |
| 178 | + cursor.execute(""" |
| 179 | + CREATE TABLE todo_item ( |
| 180 | + id INT AUTO_INCREMENT PRIMARY KEY, |
| 181 | + body TEXT NOT NULL, |
| 182 | + category_id INT DEFAULT 1, |
| 183 | + FOREIGN KEY (category_id) REFERENCES todo_category(id) |
| 184 | + ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; |
| 185 | + """) |
| 186 | + print("成功创建todo_item表") |
| 187 | + |
| 188 | + # 添加默认分类 |
| 189 | + cursor.execute("INSERT INTO todo_category (id, name) VALUES (1, '待办事项')") |
| 190 | + cursor.execute("INSERT INTO todo_category (id, name) VALUES (2, '已完成')") |
| 191 | + conn.commit() |
| 192 | + print("成功添加默认分类") |
| 193 | + |
| 194 | + # 添加示例待办事项 |
| 195 | + cursor.execute(""" |
| 196 | + INSERT INTO todo_item (body, category_id) |
| 197 | + VALUES ('这是一个示例待办事项', 1) |
| 198 | + """) |
| 199 | + conn.commit() |
| 200 | + print("成功添加示例待办事项") |
| 201 | + except Exception as e: |
| 202 | + print(f"重置Todo表出错: {str(e)}") |
| 203 | + conn.rollback() |
| 204 | + |
| 205 | + cursor.close() |
| 206 | + conn.close() |
| 207 | + |
| 208 | +if __name__ == "__main__": |
| 209 | + # 询问用户要重置哪些表 |
| 210 | + print("请选择要重置的表:") |
| 211 | + print("1. 重置Card相关表") |
| 212 | + print("2. 重置Todo相关表") |
| 213 | + print("3. 重置所有表") |
| 214 | + |
| 215 | + choice = input("请输入选项(1/2/3): ") |
| 216 | + |
| 217 | + if choice == '1': |
| 218 | + reset_card_tables() |
| 219 | + print("Card表重置完成") |
| 220 | + elif choice == '2': |
| 221 | + reset_todo_tables() |
| 222 | + print("Todo表重置完成") |
| 223 | + elif choice == '3': |
| 224 | + reset_card_tables() |
| 225 | + reset_todo_tables() |
| 226 | + print("所有表重置完成") |
| 227 | + else: |
| 228 | + print("无效选项") |
0 commit comments