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