-
Notifications
You must be signed in to change notification settings - Fork 3
Update sql-dump-splitter.py #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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 | ||||||||||||||||||||||||||||||||||||||||||||||
| 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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By enhancing the regex, we can support variations in spacing,
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||
| 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: | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -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 | ||||||||||||||||||||||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||
| save_file(current_content, output_dir, current_file_name) | ||||||||||||||||||||||||||||||||||||||||||||||
| file_count += 1 | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| # Set the new file name based on the extracted name | ||||||||||||||||||||||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||||||||||||||||||||||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+81
to
+82
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| 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}") | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.