|
| 1 | +# Generated by Django 4.2.15 on 2025-03-13 07:21 |
| 2 | + |
| 3 | +from django.db import migrations |
| 4 | +from django.db.models import Q |
| 5 | + |
| 6 | +mysql_template = """ |
| 7 | +def query_mysql(host,port, user, password, database, sql): |
| 8 | + import pymysql |
| 9 | + import json |
| 10 | + from pymysql.cursors import DictCursor |
| 11 | + from datetime import datetime, date |
| 12 | +
|
| 13 | + def default_serializer(obj): |
| 14 | + from decimal import Decimal |
| 15 | + if isinstance(obj, (datetime, date)): |
| 16 | + return obj.isoformat() # 将 datetime/date 转换为 ISO 格式字符串 |
| 17 | + elif isinstance(obj, Decimal): |
| 18 | + return float(obj) # 将 Decimal 转换为 float |
| 19 | + raise TypeError(f"Type {type(obj)} not serializable") |
| 20 | +
|
| 21 | + try: |
| 22 | + # 创建连接 |
| 23 | + db = pymysql.connect( |
| 24 | + host=host, |
| 25 | + port=int(port), |
| 26 | + user=user, |
| 27 | + password=password, |
| 28 | + database=database, |
| 29 | + cursorclass=DictCursor # 使用字典游标 |
| 30 | + ) |
| 31 | +
|
| 32 | + # 使用 cursor() 方法创建一个游标对象 cursor |
| 33 | + cursor = db.cursor() |
| 34 | +
|
| 35 | + # 使用 execute() 方法执行 SQL 查询 |
| 36 | + cursor.execute(sql) |
| 37 | +
|
| 38 | + # 使用 fetchall() 方法获取所有数据 |
| 39 | + data = cursor.fetchall() |
| 40 | +
|
| 41 | + # 处理 bytes 类型的数据 |
| 42 | + for row in data: |
| 43 | + for key, value in row.items(): |
| 44 | + if isinstance(value, bytes): |
| 45 | + row[key] = value.decode("utf-8") # 转换为字符串 |
| 46 | +
|
| 47 | + # 将数据序列化为 JSON |
| 48 | + json_data = json.dumps(data, default=default_serializer, ensure_ascii=False) |
| 49 | + return json_data |
| 50 | +
|
| 51 | + # 关闭数据库连接 |
| 52 | + db.close() |
| 53 | +
|
| 54 | + except Exception as e: |
| 55 | + print(f"Error while connecting to MySQL: {e}") |
| 56 | + raise e |
| 57 | +""" |
| 58 | + |
| 59 | +pgsql_template = """ |
| 60 | +def queryPgSQL(database, user, password, host, port, query): |
| 61 | + import psycopg2 |
| 62 | + import json |
| 63 | + from datetime import datetime |
| 64 | +
|
| 65 | + # 自定义 JSON 序列化函数 |
| 66 | + def default_serializer(obj): |
| 67 | + from decimal import Decimal |
| 68 | + if isinstance(obj, datetime): |
| 69 | + return obj.isoformat() # 将 datetime 转换为 ISO 格式字符串 |
| 70 | + elif isinstance(obj, Decimal): |
| 71 | + return float(obj) # 将 Decimal 转换为 float |
| 72 | + raise TypeError(f"Type {type(obj)} not serializable") |
| 73 | +
|
| 74 | + # 数据库连接信息 |
| 75 | + conn_params = { |
| 76 | + "dbname": database, |
| 77 | + "user": user, |
| 78 | + "password": password, |
| 79 | + "host": host, |
| 80 | + "port": port |
| 81 | + } |
| 82 | + try: |
| 83 | + # 建立连接 |
| 84 | + conn = psycopg2.connect(**conn_params) |
| 85 | + print("连接成功!") |
| 86 | + # 创建游标对象 |
| 87 | + cursor = conn.cursor() |
| 88 | + # 执行查询语句 |
| 89 | + cursor.execute(query) |
| 90 | + # 获取查询结果 |
| 91 | + rows = cursor.fetchall() |
| 92 | + # 处理 bytes 类型的数据 |
| 93 | + columns = [desc[0] for desc in cursor.description] |
| 94 | + result = [dict(zip(columns, row)) for row in rows] |
| 95 | + # 转换为 JSON 格式 |
| 96 | + json_result = json.dumps(result, default=default_serializer, ensure_ascii=False) |
| 97 | + return json_result |
| 98 | + except Exception as e: |
| 99 | + print(f"发生错误:{e}") |
| 100 | + raise e |
| 101 | + finally: |
| 102 | + # 关闭游标和连接 |
| 103 | + if cursor: |
| 104 | + cursor.close() |
| 105 | + if conn: |
| 106 | + conn.close() |
| 107 | +""" |
| 108 | + |
| 109 | + |
| 110 | +def fix_type(apps, schema_editor): |
| 111 | + FunctionLib = apps.get_model('function_lib', 'FunctionLib') |
| 112 | + FunctionLib.objects.filter( |
| 113 | + Q(id='22c21b76-0308-11f0-9694-5618c4394482') | Q(template_id='22c21b76-0308-11f0-9694-5618c4394482') |
| 114 | + ).update(code=mysql_template) |
| 115 | + FunctionLib.objects.filter( |
| 116 | + Q(id='bd1e8b88-0302-11f0-87bb-5618c4394482') | Q(template_id='bd1e8b88-0302-11f0-87bb-5618c4394482') |
| 117 | + ).update(code=pgsql_template) |
| 118 | + |
| 119 | + |
| 120 | +class Migration(migrations.Migration): |
| 121 | + dependencies = [ |
| 122 | + ('function_lib', '0003_functionlib_function_type_functionlib_icon_and_more'), |
| 123 | + ] |
| 124 | + |
| 125 | + operations = [ |
| 126 | + migrations.RunPython(fix_type) |
| 127 | + ] |
0 commit comments