Skip to content

Commit

Permalink
exec sql one by one
Browse files Browse the repository at this point in the history
anyongjin committed Sep 24, 2024
1 parent 549d03f commit ee95b82
Showing 2 changed files with 22 additions and 2 deletions.
1 change: 1 addition & 0 deletions config.yml
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@ apply_sql: false # apply sql files to dest (to connection_info)
skip_tbls: [] # exclude some tables
only_tbls: [] # only execute for specified tables
skip_nonempty_tbl: false # if true, skip tables in new database if not empty while loading data from ibd
continue_on_error: false # if true, continue on sql execute error
connection_info:
host: localhost
port: 3306
23 changes: 21 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
@@ -160,10 +160,29 @@ def ibd2sql(args: dict):
logger.info(f"sql generated at: {sql_path}")
if args["apply_sql"]:
logger.info("applying output to dest")
continue_on_error = args.get("continue_on_error", False)
with open(sql_path, "r") as outputfile:
db_config = config["connection_info"]
cursor = pymysql.connect(**db_config).cursor()
cursor.executemany(outputfile.read(), ())
with pymysql.connect(**db_config) as connection:
with connection.cursor() as cursor:
sql_commands = outputfile.read().split(';')

rollback = False
for command in sql_commands:
command = command.strip() # Remove leading/trailing whitespace
if command: # Ensure the command is not empty
try:
cursor.execute(command) # Execute each command
except pymysql.MySQLError as e:
print(f"Error executing command: {command} - {e}")
if not continue_on_error:
rollback = True
break

if rollback:
connection.rollback()
else:
connection.commit()


def link_tables_ibd(config: dict):

0 comments on commit ee95b82

Please sign in to comment.