Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 43 additions & 15 deletions sql-dump-splitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,34 @@
# -*- coding: utf-8 -*-

__author__ = "Davyd Maker"
__version__ = "1.0"
__version__ = "1.2"

import os
import argparse
import time

DEFAULT_SQL_CONDITIONS = ["DROP TABLE", "CREATE TABLE IF NOT EXISTS"]

def save_file(content, directory, file_index):
import re

DEFAULT_SQL_CONDITIONS = ["DROP TABLE", "CREATE TABLE IF NOT EXISTS", "CREATE VIEW", "CREATE FUNCTION", "CREATE PROCEDURE"]

def extract_name(line):
# Regex patterns to match table/view/function/procedure names
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Regex patterns to match table/view/function/procedure names

patterns = [
r'CREATE TABLE (\w+)', # Match CREATE TABLE statements
r'CREATE VIEW (\w+)', # Match CREATE VIEW statements
r'CREATE FUNCTION (\w+)', # Match CREATE FUNCTION statements
r'CREATE PROCEDURE (\w+)', # Match CREATE PROCEDURE statements
r'DROP TABLE (\w+)', # Match DROP TABLE statements
]

for pattern in patterns:
match = re.search(pattern, line)
if match:
return match.group(1)
Comment on lines +17 to +27
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By enhancing the regex, we can support variations in spacing, IF NOT EXISTS statements, names with/without quotes, and case sensitivity:

Suggested change
r'CREATE TABLE (\w+)', # Match CREATE TABLE statements
r'CREATE VIEW (\w+)', # Match CREATE VIEW statements
r'CREATE FUNCTION (\w+)', # Match CREATE FUNCTION statements
r'CREATE PROCEDURE (\w+)', # Match CREATE PROCEDURE statements
r'DROP TABLE (\w+)', # Match DROP TABLE statements
]
for pattern in patterns:
match = re.search(pattern, line)
if match:
return match.group(1)
r'CREATE\s+TABLE\s+(?:IF\s+NOT\s+EXISTS\s+)?(?:`?(\w+)`?|"(\w+)"|\'(\w+)\')',
r'CREATE\s+VIEW\s+(?:`?(\w+)`?|"(\w+)"|\'(\w+)\')',
r'CREATE\s+FUNCTION\s+(?:`?(\w+)`?|"(\w+)"|\'(\w+)\')',
r'CREATE\s+PROCEDURE\s+(?:`?(\w+)`?|"(\w+)"|\'(\w+)\')',
r'DROP\s+TABLE\s+(?:`?(\w+)`?|"(\w+)"|\'(\w+)\')',
]
for pattern in patterns:
match = re.search(pattern, line, re.IGNORECASE)
if match:
return next((g for g in match.groups() if g), None)

return None

def save_file(content, directory, file_name):
try:
file_path = os.path.join(directory, f'{file_index}.sql')
file_path = os.path.join(directory, f'{file_name}.sql')
with open(file_path, 'w', encoding='utf-8') as file:
file.write(''.join(content))
except IOError as e:
Expand All @@ -37,29 +54,40 @@ def handle_line(line, ignore_blank_lines):
def process_file(input_file_path, output_dir, trigger_count, ignore_blank_lines, sql_conditions):
prepare_directory(output_dir)
file_count = 0
current_content = []
current_file_name = None
condition_hit_count = 0

start_time = time.time()

try:
with open(input_file_path, 'r', encoding='utf-8') as file:
current_content = []
condition_hit_count = 0

for line in file:
processed_line = handle_line(line, ignore_blank_lines)
if processed_line is None:
continue

name = extract_name(processed_line)
split, condition_hit_count = should_split(processed_line, sql_conditions, condition_hit_count, trigger_count)

if split:
save_file(current_content, output_dir, file_count)
file_count += 1
current_content = []
condition_hit_count = 0
if current_content:
# Save the current content to the previous file name
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Save the current content to the previous file name

save_file(current_content, output_dir, current_file_name)
file_count += 1

# Set the new file name based on the extracted name
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Set the new file name based on the extracted name

current_file_name = name if name else f"file_{file_count}" # Fallback if name extraction fails
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
current_file_name = name if name else f"file_{file_count}" # Fallback if name extraction fails
current_file_name = name if name else f"file_{file_count}"

current_content = [] # Reset current content for the new file
condition_hit_count = 0 # Reset hit count
Comment on lines +81 to +82
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
current_content = [] # Reset current content for the new file
condition_hit_count = 0 # Reset hit count
current_content = []
condition_hit_count = 0


current_content.append(processed_line)

if current_content:
save_file(current_content, output_dir, file_count)
# Save any remaining content in the last file
if current_content and current_file_name:
save_file(current_content, output_dir, current_file_name)
file_count += 1

except Exception as e:
print(f"Error reading file {input_file_path}: {e}")

Expand Down