Working on conversion to the obj table and everything related to that.

This commit is contained in:
Scott Idem
2024-07-31 17:23:28 -04:00
parent 4145f81850
commit e475ec6686
13 changed files with 1183 additions and 2 deletions

View File

@@ -929,6 +929,7 @@ def sql_select(
def run_sql_select(
sql: str|None = None,
data: dict|None = None,
commit: bool = False,
log_lvl: int = logging.WARNING, # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
) -> None|bool|dict|list:
log.setLevel(log_lvl)
@@ -953,12 +954,18 @@ def run_sql_select(
# https://docs.sqlalchemy.org/en/13/core/sqlelement.html#sqlalchemy.sql.expression.TextClause.columns
# https://docs.sqlalchemy.org/en/13/core/type_basics.html
sql = sql.columns(recurring_start_time=Time, recurring_end_time=Time)
if commit:
trans = db.begin()
if data:
log.info('Executing with SQL statement and data...')
result = db.execute(sql, data)
else:
log.info('Executing with SQL statement only...')
result = db.execute(sql)
if commit:
trans.commit()
except OperationalError as e:
log.error('An operational error exception happened. This is likely a "MySQL server has gone away" error. Going to try again...')
log.exception('**** *** ** * ### BEGIN ### Operational Exception Happened: Trying again... * ** *** ****')
@@ -1037,7 +1044,7 @@ def run_sql_select(
return False # Not successful
else:
log.debug('Successfully executed the SQL on the first try.')
log.info('Successfully executed the SQL on the first try.')
pass
return result
# ### END ### Core Help CRUD ### run_sql_select() ###

View File

@@ -15,7 +15,7 @@ from . import config
from app.log import log, logging
# Import the routers here first:
from app.routers import aether_cfg, api_crud, api_crud_v2, api, importing, sql, account, activity_log, address, archive, archive_content, contact, cont_edu_cert, cont_edu_cert_person, data_store, event, event_abstract, event_badge, event_badge_importing, event_badge_template, event_device, event_exhibit, event_exhibit_tracking, event_file, event_importing, event_location, event_person, event_person_detail, event_person_tracking, event_presentation, event_presenter, event_registration, event_session, flask_cfg, fundraising, grant, hosted_file, journal, journal_entry, log_client_viewing, lookup, membership_cfg, membership_group, membership_person_group, membership_person, membership_person_profile, membership_type, membership_person_type, order, order_v3, order_line, order_cart, organization, page, person, person_user, post, post_comment, product, qr, site, site_domain, user, util_email, websockets_redis, e_confex, e_cvent, c_idaa, e_impexium, e_stripe
from app.routers import ae_obj, aether_cfg, api_crud, api_crud_v2, api, importing, sql, account, activity_log, address, archive, archive_content, contact, cont_edu_cert, cont_edu_cert_person, data_store, event, event_abstract, event_badge, event_badge_importing, event_badge_template, event_device, event_exhibit, event_exhibit_tracking, event_file, event_importing, event_location, event_person, event_person_detail, event_person_tracking, event_presentation, event_presenter, event_registration, event_session, flask_cfg, fundraising, grant, hosted_file, journal, journal_entry, log_client_viewing, lookup, membership_cfg, membership_group, membership_person_group, membership_person, membership_person_profile, membership_type, membership_person_type, order, order_v3, order_line, order_cart, organization, page, person, person_user, post, post_comment, product, qr, site, site_domain, user, util_email, websockets_redis, e_confex, e_cvent, c_idaa, e_impexium, e_stripe
# from app.routers import aether_cfg, sql
@@ -88,6 +88,11 @@ app.mount('/static', StaticFiles(directory='static'), name='static')
# Set up each route once the router has been imported
app.include_router(
ae_obj.router,
prefix='/ae_obj',
tags=['AE Object'],
)
app.include_router(
aether_cfg.router,
tags=['Aether Config'],

663
app/routers/ae_obj.py Normal file
View File

@@ -0,0 +1,663 @@
import datetime, json, requests, time
from fastapi import APIRouter, Body, Depends, Header, HTTPException, Path, Query, Response, status
from fastapi.responses import FileResponse
from typing import Dict, List, Optional, Set, Union
from sqlalchemy import text
from app.lib_general import log, logging, common_route_params, Common_Route_Params
from app.db_sql import sql_insert, sql_update, sql_insert_or_update, sql_select, sql_delete, run_sql_select
from app.ae_obj_types_def import *
from app.models.core_object_models import Core_Std_Obj_Base, Core_Object_Base
router = APIRouter()
obj_type_map = {
'account': {
'tbl_name': 'account',
'obj_id': '`id_random`',
'obj_ext_uid': 'NULL',
'obj_ext_id': 'NULL',
'obj_code': '`code`',
'obj_type_ver_id': '`id`',
'obj_account_id': 'NULL',
'obj_parent_type': 'NULL',
'obj_parent_id': 'NULL',
'obj_passcode': 'NULL',
'obj_name': '`name`',
'obj_description': '`description`',
'obj_alert': 'NULL',
'obj_alert_msg': 'NULL',
'obj_status': 'NULL',
'obj_approve': 'NULL',
'obj_approved_on': 'NULL',
'obj_enable': '`enable`',
'obj_enable_on': '`enable_from`',
'obj_archive_on': '`enable_to`',
'obj_restricted': 'NULL',
'obj_hide': 'FALSE',
'obj_priority': 'NULL',
'obj_sort': 'NULL',
'obj_group': 'NULL',
'obj_ver': 'NULL',
'obj_staff_notes': 'NULL',
'obj_data_json': 'NULL',
'obj_cfg_json': 'NULL',
'obj_meta_json': 'NULL',
'obj_other_json': 'NULL',
'obj_notes': '`notes`',
'obj_created_on': '`created_on`',
'obj_updated_on': '`updated_on`',
},
'user': {
'tbl_name': 'user',
'obj_id': '`id_random`',
'obj_ext_uid': 'NULL',
'obj_ext_id': 'NULL',
'obj_code': '`email`',
'obj_type_ver_id': '`id`',
'obj_account_id': '`account_id`',
'obj_parent_type': 'NULL',
'obj_parent_id': 'NULL',
'obj_passcode': '`auth_key`',
'obj_name': '`name`',
'obj_description': 'NULL',
'obj_alert': 'NULL',
'obj_alert_msg': 'NULL',
'obj_status': 'NULL',
'obj_approve': 'NULL',
'obj_approved_on': 'NULL',
'obj_enable': '`enable`',
'obj_enable_on': '`enable_from`',
'obj_archive_on': '`enable_to`',
'obj_restricted': 'NULL',
'obj_hide': 'FALSE',
'obj_priority': 'NULL',
'obj_sort': 'NULL',
'obj_group': 'NULL',
'obj_ver': 'NULL',
'obj_staff_notes': 'NULL',
'obj_data_json': 'NULL',
'obj_cfg_json': 'NULL',
'obj_meta_json': 'NULL',
'obj_other_json': 'NULL',
'obj_notes': '`notes`',
'obj_created_on': '`created_on`',
'obj_updated_on': '`updated_on`',
},
'hosted_file': {
'tbl_name': 'hosted_file',
'obj_id': '`id_random`',
'obj_ext_uid': 'NULL',
'obj_ext_id': 'NULL',
'obj_code': 'NULL',
'obj_type_ver_id': '`id`',
'obj_account_id': '`account_id`',
'obj_parent_type': 'NULL',
'obj_parent_id': 'NULL',
'obj_passcode': 'NULL',
'obj_name': '`filename`',
'obj_description': 'NULL',
'obj_alert': 'NULL',
'obj_alert_msg': 'NULL',
'obj_status': 'NULL',
'obj_approve': 'NULL',
'obj_approved_on': 'NULL',
'obj_enable': '`enable`',
'obj_enable_on': 'NULL',
'obj_archive_on': 'NULL',
'obj_restricted': 'NULL',
'obj_hide': '`hide`',
'obj_priority': 'NULL',
'obj_sort': 'NULL',
'obj_group': 'NULL',
'obj_ver': 'NULL',
'obj_staff_notes': 'NULL',
'obj_data_json': 'NULL',
'obj_cfg_json': 'NULL',
'obj_meta_json': 'NULL',
'obj_other_json': 'NULL',
'obj_notes': '`notes`',
'obj_created_on': '`created_on`',
'obj_updated_on': '`updated_on`',
},
}
# Updated 2024-07-31
@router.get('/{obj_type}/convert')
def convert_obj_type(
obj_type: str = Path(min_length=2, max_length=50),
# for_obj_type: Optional[str] = Query(None, max_length=50),
# for_obj_id: Optional[str] = Query(None, max_length=22),
# tbl_alt: Optional[str] = Query('default', max_length=50), # This is used as a lookup for the real SQL database table or view name to use.
# mdl_alt: Optional[str] = Query('default', max_length=50), # This is used as a lookup for the real Python Pydantic model name to use.
# exp_alt: Optional[str] = Query('default', max_length=50), # This is used as a lookup for a list of column names to use.
# Get the "json" param from the query string. This is a JSON formatted string of the data to be inserted.
# jp: Optional[Union[str, None]] = None,
commons: Common_Route_Params = Depends(common_route_params),
):
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
# ### SECTION ### Get the field map for the object type
# Check the URL to read the JSON formatted mapping data.
url = 'https://dev-api.oneskyit.com/static/ae/obj_type_maps.json'
# url = f'https://dev-api.oneskyit.com/static/ae/obj_type_map__{obj_type}.json'
resp = requests.get(url=url, headers={'Content-Type': 'application/json', 'Cache-Control': 'no-cache'})
log.debug(f'Status Code: {resp.status_code}')
log.debug(f'Headers: {resp.headers}')
obj_type_maps = resp.json()
log.debug(obj_type_maps)
if obj_type not in obj_type_maps:
url = f'https://dev-api.oneskyit.com/static/ae/obj_type_map__{obj_type}.json'
resp = requests.get(url=url, headers={'Content-Type': 'application/json', 'Cache-Control': 'no-cache'})
log.debug(f'Status Code: {resp.status_code}')
log.debug(f'Headers: {resp.headers}')
if resp.status_code == 200:
obj_type_maps[obj_type] = resp.json()
else:
log.warning(f"Object type '{obj_type}' not found.")
raise HTTPException(status_code=404, detail=f"Object type '{obj_type}' not found.")
field_map = obj_type_maps[obj_type]
log.debug(field_map)
# ### SECTION ### Call generic function to convert the object type to the new model
result = handle_convert_obj_type(
obj_type=obj_type,
field_map=field_map,
)
data = {
'result_count': result.rowcount,
'result_id': result.lastrowid,
'obj_type': obj_type,
}
return mk_resp(data=data, response=commons.response)
# Example SQL statements
# -- Core to obj conversion
# INSERT INTO obj (`obj_id`, `obj_code`, `obj_account_id`, `obj_type`, `obj_type_ver_id`, `obj_name`, `obj_description`, `obj_enable`, `obj_enable_on`, `obj_archive_on`, `obj_hide`, `obj_notes`, `obj_created_on`, `obj_updated_on`)
# SELECT
# `id_random` AS `obj_id`,
# `code` AS `obj_code`,
# NULL AS `obj_account_id`,
# 'account' AS `obj_type`,
# `id` AS `obj_type_ver_id`,
# `name` AS `obj_name`,
# `description` AS `obj_description`,
# `enable` AS `obj_enable`,
# `enable_from` AS `obj_enable_on`,
# `enable_to` AS `obj_archive_on`,
# FALSE AS `obj_hide`,
# `notes` AS `obj_notes`,
# `created_on` AS `obj_created_on`,
# `updated_on` AS `obj_updated_on`
# FROM
# `account`
# ON DUPLICATE KEY UPDATE
# `obj_id` = VALUES(`obj_id`),
# `obj_code` = VALUES(`obj_code`),
# `obj_account_id` = VALUES(`obj_account_id`),
# `obj_name` = VALUES(`obj_name`),
# `obj_description` = VALUES(`obj_description`),
# `obj_enable` = VALUES(`obj_enable`),
# `obj_enable_on` = VALUES(`obj_enable_on`),
# `obj_archive_on` = VALUES(`obj_archive_on`),
# `obj_hide` = VALUES(`obj_hide`),
# `obj_notes` = VALUES(`obj_notes`),
# `obj_created_on` = VALUES(`obj_created_on`),
# `obj_updated_on` = VALUES(`obj_updated_on`);
# INSERT INTO obj (`obj_id`, `obj_ext_uid`, `obj_ext_id`, `obj_code`, `obj_type`, `obj_type_ver_id`, `obj_account_id`, `obj_passcode`, `obj_name`, `obj_description`, `obj_enable`, `obj_hide`, `obj_group`, `obj_notes`, `obj_created_on`, `obj_updated_on`)
# SELECT
# `id_random` AS `obj_id`,
# `external_sys_id` AS `obj_ext_uid`,
# `external_id` AS `obj_ext_id`,
# `primary_email` AS `obj_code`,
# 'person' AS `obj_type`,
# `id` AS `obj_type_ver_id`,
# `account_id` AS `obj_account_id`,
# `passcode` AS `obj_passcode`,
# `full_name` AS `obj_name`,
# `description` AS `obj_description`,
# `enable` AS `obj_enable`,
# `hide` AS `obj_hide`,
# `group` AS `obj_group`,
# `notes` AS `obj_notes`,
# `created_on` AS `obj_created_on`,
# `updated_on` AS `obj_updated_on`
# FROM
# `person`
# ON DUPLICATE KEY UPDATE
# `obj_id` = VALUES(`obj_id`),
# `obj_ext_uid` = VALUES(`obj_ext_uid`),
# `obj_ext_id` = VALUES(`obj_ext_id`),
# `obj_code` = VALUES(`obj_code`),
# `obj_account_id` = VALUES(`obj_account_id`),
# `obj_passcode` = VALUES(`obj_passcode`),
# `obj_name` = VALUES(`obj_name`),
# `obj_description` = VALUES(`obj_description`),
# `obj_enable` = VALUES(`obj_enable`),
# `obj_hide` = VALUES(`obj_hide`),
# `obj_group` = VALUES(`obj_group`),
# `obj_notes` = VALUES(`obj_notes`),
# `obj_created_on` = VALUES(`obj_created_on`),
# `obj_updated_on` = VALUES(`obj_updated_on`);
# INSERT INTO obj (`obj_id`, `obj_code`, `obj_type`, `obj_type_ver_id`, `obj_account_id`, `obj_passcode`, `obj_name`, `obj_enable`, `obj_enable_on`, `obj_archive_on`, `obj_hide`, `obj_priority`, `obj_sort`, `obj_group`, `obj_notes`, `obj_created_on`, `obj_updated_on`)
# SELECT
# `id_random` AS `obj_id`,
# `email` AS `obj_code`,
# 'user' AS `obj_type`,
# `id` AS `obj_type_ver_id`,
# `account_id` AS `obj_account_id`,
# `auth_key` AS `obj_passcode`,
# `name` AS `obj_name`,
# `enable` AS `obj_enable`,
# `enable_from` AS `obj_enable_on`,
# `enable_to` AS `obj_archive_on`,
# `hide` AS `obj_hide`,
# `priority` AS `obj_priority`,
# `sort` AS `obj_sort`,
# `group` AS `obj_group`,
# `notes` AS `obj_notes`,
# `created_on` AS `obj_created_on`,
# `updated_on` AS `obj_updated_on`
# FROM
# `user`
# ON DUPLICATE KEY UPDATE
# `obj_id` = VALUES(`obj_id`),
# `obj_code` = VALUES(`obj_code`),
# `obj_account_id` = VALUES(`obj_account_id`),
# `obj_passcode` = VALUES(`obj_passcode`),
# `obj_name` = VALUES(`obj_name`),
# `obj_enable` = VALUES(`obj_enable`),
# `obj_enable_on` = VALUES(`obj_enable_on`),
# `obj_archive_on` = VALUES(`obj_archive_on`),
# `obj_hide` = VALUES(`obj_hide`),
# `obj_priority` = VALUES(`obj_priority`),
# `obj_sort` = VALUES(`obj_sort`),
# `obj_group` = VALUES(`obj_group`),
# `obj_notes` = VALUES(`obj_notes`),
# `obj_created_on` = VALUES(`obj_created_on`),
# `obj_updated_on` = VALUES(`obj_updated_on`);
# INSERT INTO obj (`obj_id`, `obj_code`, `obj_type`, `obj_type_ver_id`, `obj_account_id`, `obj_passcode`, `obj_name`, `obj_description`, `obj_enable`, `obj_enable_on`, `obj_archive_on`, `obj_hide`, `obj_priority`, `obj_sort`, `obj_group`, `obj_cfg_json`, `obj_notes`, `obj_created_on`, `obj_updated_on`)
# SELECT
# `id_random` AS `obj_id`,
# NULL AS `obj_code`,
# 'hosted_file' AS `obj_type`,
# `id` AS `obj_type_ver_id`,
# `account_id` AS `obj_account_id`,
# NULL AS `obj_passcode`,
# `filename` AS `obj_name`,
# NULL AS `obj_description`,
# `enable` AS `obj_enable`,
# NULL AS `obj_enable_on`,
# NULL AS `obj_archive_on`,
# `hide` AS `obj_hide`,
# NULL AS `obj_priority`,
# NULL AS `obj_sort`,
# NULL AS `obj_group`,
# NULL AS `obj_cfg_json`,
# `notes` AS `obj_notes`,
# `created_on` AS `obj_created_on`,
# `updated_on` AS `obj_updated_on`
# FROM
# `hosted_file`
# ON DUPLICATE KEY UPDATE
# `obj_id` = VALUES(`obj_id`),
# `obj_code` = VALUES(`obj_code`),
# `obj_account_id` = VALUES(`obj_account_id`),
# `obj_passcode` = VALUES(`obj_passcode`),
# `obj_name` = VALUES(`obj_name`),
# `obj_description` = VALUES(`obj_description`),
# `obj_enable` = VALUES(`obj_enable`),
# `obj_enable_on` = VALUES(`obj_enable_on`),
# `obj_archive_on` = VALUES(`obj_archive_on`),
# `obj_hide` = VALUES(`obj_hide`),
# `obj_priority` = VALUES(`obj_priority`),
# `obj_sort` = VALUES(`obj_sort`),
# `obj_group` = VALUES(`obj_group`),
# `obj_cfg_json` = VALUES(`obj_cfg_json`),
# `obj_notes` = VALUES(`obj_notes`),
# `obj_created_on` = VALUES(`obj_created_on`),
# `obj_updated_on` = VALUES(`obj_updated_on`);
# -- Event to obj conversion
# INSERT INTO obj (`id_random`, `code`, `type_name`, `type_ver_id`, `account_id`, `passcode`, `name`, `description`, `enable`, `enable_on`, `archive_on`, `hide`, `priority`, `sort`, `group`, `cfg_json`, `notes`, `created_on`, `updated_on`)
# SELECT
# `id_random`,
# `code` AS `code`,
# 'event' AS `type_name`,
# `id` AS `type_ver_id`,
# `account_id` AS `account_id`,
# NULL AS `passcode`,
# `name` AS `name`,
# `description` AS `description`,
# `enable` AS `enable`,
# `enable_from` AS `enable_on`,
# `enable_to` AS `archive_on`,
# `hide` AS `hide`,
# `priority` AS `priority`,
# `sort` AS `sort`,
# `group` AS `group`,
# `cfg_json` AS `cfg_json`,
# `notes` AS `notes`,
# `created_on` AS `created_on`,
# `updated_on` AS `updated_on`
# FROM
# `event`
# ON DUPLICATE KEY UPDATE
# `id_random` = VALUES(`id_random`),
# `code` = VALUES(`code`),
# `account_id` = VALUES(`account_id`),
# `passcode` = VALUES(`passcode`),
# `name` = VALUES(`name`),
# `description` = VALUES(`description`),
# `enable` = VALUES(`enable`),
# `enable_on` = VALUES(`enable_on`),
# `archive_on` = VALUES(`archive_on`),
# `hide` = VALUES(`hide`),
# `priority` = VALUES(`priority`),
# `sort` = VALUES(`sort`),
# `group` = VALUES(`group`),
# `cfg_json` = VALUES(`cfg_json`),
# `notes` = VALUES(`notes`),
# `created_on` = VALUES(`created_on`),
# `updated_on` = VALUES(`updated_on`);
# INSERT INTO obj (`id_random`, `code`, `type_name`, `type_ver_id`, `account_id`, `parent_type`, `parent_id`, `passcode`, `name`, `description`, `status`, `enable`, `enable_on`, `archive_on`, `restricted`, `hide`, `priority`, `sort`, `group`, `cfg_json`, `notes`, `created_on`, `updated_on`)
# SELECT
# `id_random`,
# NULL AS `code`,
# 'event_file' AS `type_name`,
# `id` AS `type_ver_id`,
# `account_id` AS `account_id`,
# 'event' AS `parent_type`,
# `event_id` AS `parent_id`,
# NULL AS `passcode`,
# `filename` AS `name`,
# NULL AS `description`,
# `status` AS `status`,
# `enable` AS `enable`,
# `enable_after_datetime` AS `enable_on`,
# `enable_before_datetime` AS `archive_on`,
# `internal_use` AS `restricted`,
# `hide` AS `hide`,
# `priority` AS `priority`,
# `sort` AS `sort`,
# `group` AS `group`,
# NULL AS `cfg_json`,
# NULL AS `notes`,
# `created_on` AS `created_on`,
# `updated_on` AS `updated_on`
# FROM
# `v_event_file`
# ON DUPLICATE KEY UPDATE
# `id_random` = VALUES(`id_random`),
# `code` = VALUES(`code`),
# `account_id` = VALUES(`account_id`),
# `parent_type` = VALUES(`parent_type`),
# `parent_id` = VALUES(`parent_id`),
# `passcode` = VALUES(`passcode`),
# `name` = VALUES(`name`),
# `description` = VALUES(`description`),
# `status` = VALUES(`status`),
# `enable` = VALUES(`enable`),
# `enable_on` = VALUES(`enable_on`),
# `archive_on` = VALUES(`archive_on`),
# `restricted` = VALUES(`restricted`),
# `hide` = VALUES(`hide`),
# `priority` = VALUES(`priority`),
# `sort` = VALUES(`sort`),
# `group` = VALUES(`group`),
# `cfg_json` = VALUES(`cfg_json`),
# `notes` = VALUES(`notes`),
# `created_on` = VALUES(`created_on`),
# `updated_on` = VALUES(`updated_on`);
# INSERT INTO obj (`id_random`, `external_uid`, `external_id`, `code`, `type_name`, `type_ver_id`, `account_id`, `parent_type`, `parent_id`, `passcode`, `name`, `description`, `status`, `enable`, `enable_on`, `archive_on`, `restricted`, `hide`, `priority`, `sort`, `group`, `data_json`, `cfg_json`, `notes`, `created_on`, `updated_on`)
# SELECT
# `id_random`,
# NULL AS `external_uid`,
# `external_id` AS `external_id`,
# NULL AS `code`,
# 'event_badge' AS `type_name`,
# `id` AS `type_ver_id`,
# `account_id` AS `account_id`,
# 'event' AS `parent_type`,
# `event_id` AS `parent_id`,
# NULL AS `passcode`,
# `full_name` AS `name`,
# NULL AS `description`,
# NULL AS `status`,
# `enable` AS `enable`,
# NULL AS `enable_on`,
# NULL AS `archive_on`,
# NULL AS `restricted`,
# `hide` AS `hide`,
# `priority` AS `priority`,
# `sort` AS `sort`,
# `group` AS `group`,
# `data_json` AS `data_json`,
# `cfg_json` AS `cfg_json`,
# `notes` AS `notes`,
# `created_on` AS `created_on`,
# `updated_on` AS `updated_on`
# FROM
# `v_event_badge`
# ON DUPLICATE KEY UPDATE
# `id_random` = VALUES(`id_random`),
# `external_uid` = VALUES(`external_uid`),
# `external_id` = VALUES(`external_id`),
# `code` = VALUES(`code`),
# `account_id` = VALUES(`account_id`),
# `parent_type` = VALUES(`parent_type`),
# `parent_id` = VALUES(`parent_id`),
# `passcode` = VALUES(`passcode`),
# `name` = VALUES(`name`),
# `description` = VALUES(`description`),
# `status` = VALUES(`status`),
# `enable` = VALUES(`enable`),
# `enable_on` = VALUES(`enable_on`),
# `archive_on` = VALUES(`archive_on`),
# `restricted` = VALUES(`restricted`),
# `hide` = VALUES(`hide`),
# `priority` = VALUES(`priority`),
# `sort` = VALUES(`sort`),
# `group` = VALUES(`group`),
# `data_json` = VALUES(`data_json`),
# `cfg_json` = VALUES(`cfg_json`),
# `notes` = VALUES(`notes`),
# `created_on` = VALUES(`created_on`),
# `updated_on` = VALUES(`updated_on`);
# INSERT INTO obj (`id_random`, `external_uid`, `external_id`, `code`, `type_name`, `type_ver_id`, `account_id`, `parent_type`, `parent_id`, `passcode`, `name`, `description`, `status`, `enable`, `enable_on`, `archive_on`, `restricted`, `hide`, `priority`, `sort`, `group`, `data_json`, `cfg_json`, `notes`, `created_on`, `updated_on`)
# SELECT
# `id_random`,
# 'external_person_id' AS `external_uid`,
# `external_id` AS `external_id`,
# NULL AS `code`,
# 'event_person' AS `type_name`,
# `id` AS `type_ver_id`,
# `account_id` AS `account_id`,
# 'event' AS `parent_type`,
# `event_id` AS `parent_id`,
# NULL AS `passcode`,
# NULL AS `name`,
# NULL AS `description`,
# `status` AS `status`,
# `enable` AS `enable`,
# NULL AS `enable_on`,
# NULL AS `archive_on`,
# NULL AS `restricted`,
# `hide` AS `hide`,
# `priority` AS `priority`,
# `sort` AS `sort`,
# `group` AS `group`,
# NULL AS `data_json`,
# NULL AS `cfg_json`,
# `notes` AS `notes`,
# `created_on` AS `created_on`,
# `updated_on` AS `updated_on`
# FROM
# `v_event_person`
# ON DUPLICATE KEY UPDATE
# `id_random` = VALUES(`id_random`),
# `external_uid` = VALUES(`external_uid`),
# `external_id` = VALUES(`external_id`),
# `code` = VALUES(`code`),
# `account_id` = VALUES(`account_id`),
# `parent_type` = VALUES(`parent_type`),
# `parent_id` = VALUES(`parent_id`),
# `passcode` = VALUES(`passcode`),
# `name` = VALUES(`name`),
# `description` = VALUES(`description`),
# `status` = VALUES(`status`),
# `enable` = VALUES(`enable`),
# `enable_on` = VALUES(`enable_on`),
# `archive_on` = VALUES(`archive_on`),
# `restricted` = VALUES(`restricted`),
# `hide` = VALUES(`hide`),
# `priority` = VALUES(`priority`),
# `sort` = VALUES(`sort`),
# `group` = VALUES(`group`),
# `data_json` = VALUES(`data_json`),
# `cfg_json` = VALUES(`cfg_json`),
# `notes` = VALUES(`notes`),
# `created_on` = VALUES(`created_on`),
# `updated_on` = VALUES(`updated_on`);
# This will run the generated SQL statement based on the object type and field map provided.
def handle_convert_obj_type(
obj_type: str,
field_map: Dict[str, str]
) -> Dict[str, Union[str, int]]:
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
# ### SECTION ### Create the SQL statement to convert the object type to the new model
sql = text(f"""
INSERT INTO obj
(
`obj_id`, `obj_ext_uid`, `obj_ext_id`,
`obj_code`,
`obj_type`, `obj_type_ver_id`,
`obj_account_id`,
`obj_parent_type`, `obj_parent_id`,
`obj_passcode`,
`obj_name`, `obj_description`,
`obj_alert`, `obj_alert_msg`,
`obj_status`, `obj_approve`, `obj_approved_on`,
`obj_enable`, `obj_enable_on`, `obj_archive_on`,
`obj_restricted`, `obj_hide`, `obj_priority`, `obj_sort`, `obj_group`,
`obj_ver`, `obj_staff_notes`,
`obj_data_json`, `obj_cfg_json`, `obj_meta_json`, `obj_other_json`,
`obj_notes`, `obj_created_on`, `obj_updated_on`
)
SELECT
{field_map['obj_id']} AS `obj_id`,
{field_map['obj_ext_uid']} AS `obj_ext_uid`,
{field_map['obj_ext_id']} AS `obj_ext_id`,
{field_map['obj_code']} AS `obj_code`,
'{obj_type}' AS `obj_type`,
{field_map['obj_type_ver_id']} AS `obj_type_ver_id`,
{field_map['obj_account_id']} AS `obj_account_id`,
{field_map['obj_parent_type']} AS `obj_parent_type`,
{field_map['obj_parent_id']} AS `obj_parent_id`,
{field_map['obj_passcode']} AS `obj_passcode`,
{field_map['obj_name']} AS `obj_name`,
{field_map['obj_description']} AS `obj_description`,
{field_map['obj_alert']} AS `obj_alert`,
{field_map['obj_alert_msg']} AS `obj_alert_msg`,
{field_map['obj_status']} AS `obj_status`,
{field_map['obj_approve']} AS `obj_approve`,
{field_map['obj_approved_on']} AS `obj_approved_on`,
{field_map['obj_enable']} AS `obj_enable`,
{field_map['obj_enable_on']} AS `obj_enable_on`,
{field_map['obj_archive_on']} AS `obj_archive_on`,
{field_map['obj_restricted']} AS `obj_restricted`,
{field_map['obj_hide']} AS `obj_hide`,
{field_map['obj_priority']} AS `obj_priority`,
{field_map['obj_sort']} AS `obj_sort`,
{field_map['obj_group']} AS `obj_group`,
{field_map['obj_ver']} AS `obj_ver`,
{field_map['obj_staff_notes']} AS `obj_staff_notes`,
{field_map['obj_data_json']} AS `obj_data_json`,
{field_map['obj_cfg_json']} AS `obj_cfg_json`,
{field_map['obj_meta_json']} AS `obj_meta_json`,
{field_map['obj_other_json']} AS `obj_other_json`,
{field_map['obj_notes']} AS `obj_notes`,
{field_map['obj_created_on']} AS `obj_created_on`,
{field_map['obj_updated_on']} AS `obj_updated_on`
FROM
`{field_map['tbl_name']}`
ON DUPLICATE KEY UPDATE
`obj_id` = VALUES(`obj_id`),
`obj_ext_uid` = VALUES(`obj_ext_uid`),
`obj_ext_id` = VALUES(`obj_ext_id`),
`obj_code` = VALUES(`obj_code`),
`obj_type` = VALUES(`obj_type`),
`obj_type_ver_id` = VALUES(`obj_type_ver_id`),
`obj_account_id` = VALUES(`obj_account_id`),
`obj_parent_type` = VALUES(`obj_parent_type`),
`obj_parent_id` = VALUES(`obj_parent_id`),
`obj_passcode` = VALUES(`obj_passcode`),
`obj_name` = VALUES(`obj_name`),
`obj_description` = VALUES(`obj_description`),
`obj_alert` = VALUES(`obj_alert`),
`obj_alert_msg` = VALUES(`obj_alert_msg`),
`obj_status` = VALUES(`obj_status`),
`obj_approve` = VALUES(`obj_approve`),
`obj_approved_on` = VALUES(`obj_approved_on`),
`obj_enable` = VALUES(`obj_enable`),
`obj_enable_on` = VALUES(`obj_enable_on`),
`obj_archive_on` = VALUES(`obj_archive_on`),
`obj_restricted` = VALUES(`obj_restricted`),
`obj_hide` = VALUES(`obj_hide`),
`obj_priority` = VALUES(`obj_priority`),
`obj_sort` = VALUES(`obj_sort`),
`obj_group` = VALUES(`obj_group`),
`obj_ver` = VALUES(`obj_ver`),
`obj_staff_notes` = VALUES(`obj_staff_notes`),
`obj_data_json` = VALUES(`obj_data_json`),
`obj_cfg_json` = VALUES(`obj_cfg_json`),
`obj_meta_json` = VALUES(`obj_meta_json`),
`obj_other_json` = VALUES(`obj_other_json`),
`obj_notes` = VALUES(`obj_notes`),
`obj_created_on` = VALUES(`obj_created_on`),
`obj_updated_on` = VALUES(`obj_updated_on`)
;
""")
log.debug(sql)
# return False
# ### SECTION ### Execute the SQL statement
result = run_sql_select(sql, commit=True, log_lvl=logging.DEBUG)
log.info(result.rowcount)
log.debug(result.lastrowid)
return result