-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCHECK_FIELD_EXIST_SQL.py
52 lines (42 loc) · 2.06 KB
/
CHECK_FIELD_EXIST_SQL.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import configparser
import pymysql
from LIST_FIELDS import LIST_FIELDS
from CREATE_FIELD import CREATE_FIELD
def CHECK_FIELD_EXIST_SQL(app_token=None, table_id=None, view_id=None, page_token=None, page_size=None, config_file=None):
if config_file is None:
config_file = 'feishu-config.ini'
config = configparser.ConfigParser()
config.read(config_file, encoding='utf-8')
if not app_token:
app_token = config.get('TOKEN', 'app_token')
if not table_id:
table_id = config.get('ID', 'table_id')
if not view_id:
view_id = config.get('ID', 'view_id')
if not page_token:
page_token = config.get('LIST_FIELDS', 'page_token', fallback=None)
if not page_size:
page_size = config.get('LIST_FIELDS', 'page_size', fallback=100)
# 获取飞书中的字段列表
response = LIST_FIELDS(app_token=app_token, table_id=table_id, view_id=view_id, page_token=page_token, page_size=page_size, config_file=config_file)
feishu_fields = [item['field_name'] for item in response['data']['items']]
# 获取数据库表的列名
db_host = config.get('DB', 'host')
db_user = config.get('DB', 'user')
db_password = config.get('DB', 'password')
db_database = config.get('DB', 'database')
db_port = config.getint('DB', 'port')
conn = pymysql.connect(host=db_host, user=db_user, password=db_password, database=db_database, port=db_port)
cursor = conn.cursor()
check_query = config.get('SQL', 'check_query')
cursor.execute(check_query)
db_columns = [column[0] for column in cursor.fetchall()]
conn.close()
# 检查每个需要检查的字段是否在飞书字段列表中,如果不在,就创建新的字段
for column in db_columns:
if column not in feishu_fields:
CREATE_FIELD(field_name=column, field_type=1, app_token=app_token, table_id=table_id, config_file=config_file)
print(f"字段 {column} 已成功创建")
if __name__ == "__main__":
# 调用函数进行字段检查和创建
CHECK_FIELD_EXIST_SQL(config_file='feishu-config.ini')