cube-studio/myapp/check_tables.py

37 lines
1.6 KiB
Python
Raw Normal View History

2022-05-13 20:47:22 +08:00
#!/usr/bin/env python
import os
2023-09-03 21:17:55 +08:00
2022-05-13 20:47:22 +08:00
# @pysnooper.snoop()
def check_tables():
2023-09-03 21:17:55 +08:00
SQLALCHEMY_DATABASE_URI = os.getenv('MYSQL_SERVICE', '')
2022-05-13 20:47:22 +08:00
if SQLALCHEMY_DATABASE_URI:
import sqlalchemy.engine.url as url
uri = url.make_url(SQLALCHEMY_DATABASE_URI)
"""Inits the Myapp application"""
import pymysql
# 创建连接
2023-09-03 21:17:55 +08:00
conn = pymysql.connect(host=uri.host, port=uri.port, user=uri.username, password=uri.password, charset='utf8')
2022-05-13 20:47:22 +08:00
# 创建游标
cursor = conn.cursor()
# 创建数据库的sql(如果数据库存在就不创建,防止异常)
2023-09-03 21:17:55 +08:00
sql = f"SELECT table_name FROM information_schema.tables WHERE table_schema='{uri.database}'"
2022-05-13 20:47:22 +08:00
cursor.execute(sql)
results = list(cursor.fetchall())
results = [item[0] for item in results]
print(results)
2023-09-03 21:17:55 +08:00
for table_name in ['ab_permission', 'ab_permission_view', 'ab_permission_view_role', 'ab_register_user',
'ab_role', 'ab_user', 'ab_user_role', 'ab_view_menu', 'alembic_version', 'dimension',
'docker', 'images', 'inferenceservice', 'job_template', 'logs', 'metadata_metric', 'model',
'nni', 'notebook', 'pipeline', 'project', 'project_user', 'repository', 'run',
2023-12-11 13:25:07 +08:00
'service', 'task', 'metadata_table', 'workflow']:
2022-05-13 20:47:22 +08:00
if table_name not in results:
2023-09-03 21:17:55 +08:00
print('%s db下table %s不完整,请\n1、mysql: drop dabatase %s\n2、重启当前pod' % (uri.database,table_name,uri.database))
2022-05-13 20:47:22 +08:00
exit(1)
2023-09-03 21:17:55 +08:00
2022-05-13 20:47:22 +08:00
check_tables()