|
| 1 | +""" |
| 2 | +SQL Code Block Prepend Tool |
| 3 | +
|
| 4 | +Description: |
| 5 | + This script automates the process of prepending a specific SQL code block to all .sql files within |
| 6 | + specified directories. It supports multiple predefined code block options, allowing the user to |
| 7 | + select which block to prepend based on their requirements. The script also handles file encoding |
| 8 | + intelligently, ensuring that the prepend action respects the original file's encoding. This feature |
| 9 | + is particularly useful for preparing SQL files with necessary configurations or statements before |
| 10 | + execution in different environments. |
| 11 | +
|
| 12 | +Usage: |
| 13 | + - Define the SQL code block options within the 'options' dictionary. Each option should be a complete, |
| 14 | + ready-to-prepend SQL statement or set of statements. |
| 15 | + - Set the 'option_to_use' variable to the key of the desired code block from the 'options' dictionary. |
| 16 | + - Specify the directories to be processed in the 'directories' list. All .sql files within these directories |
| 17 | + (and their subdirectories) will be processed. |
| 18 | + - Run the script. It will prepend the chosen SQL code block to each .sql file, handling various encodings |
| 19 | + and ensuring that the file's original encoding is preserved. |
| 20 | +
|
| 21 | +Features: |
| 22 | + - Allows specification of multiple SQL code block options for different use cases. |
| 23 | + - Prepends chosen SQL code block to the beginning of each .sql file in the specified directories. |
| 24 | + - Handles file encodings ('utf-8-sig', 'utf-8', 'latin-1') to accommodate files from different sources. |
| 25 | + - Checks if a file already starts with one of the options and, if so, replaces it with the chosen option to avoid duplication. |
| 26 | + - Provides feedback on the processing of each file, including the file path and encoding used. |
| 27 | +
|
| 28 | +Note: |
| 29 | + Before running this script, ensure that you have backups of your .sql files, as this process modifies the files in place. |
| 30 | + Modify the 'options' dictionary and the 'directories' list according to your specific |
| 31 | +
|
| 32 | +import os |
| 33 | +import re |
| 34 | +""" |
| 35 | + |
| 36 | +# Define your options here |
| 37 | +options = { |
| 38 | + 'option1': """ |
| 39 | +USE bsav2_MyTest_DB; --Babelfish addition: Setting this for testing. |
| 40 | +GO |
| 41 | +SET NOCOUNT ON; |
| 42 | +GO |
| 43 | +""", |
| 44 | + 'option2': """ |
| 45 | +USE bsav2_MyTest_DB; --Babelfish addition: Setting this for testing. |
| 46 | +GO |
| 47 | +EXECUTE sp_babelfish_configure '%', 'ignore', 'server'--Babelfish addition: Setting this for testing. |
| 48 | +GO |
| 49 | +SET NOCOUNT ON; |
| 50 | +GO |
| 51 | +""", |
| 52 | + 'option3': """ |
| 53 | +EXECUTE sp_babelfish_configure '%', 'ignore', 'server'--Babelfish addition: Setting this for testing. |
| 54 | +GO |
| 55 | +""" |
| 56 | +} |
| 57 | +import os |
| 58 | +import re |
| 59 | + |
| 60 | +def prepend_code_block_with_encoding_handling(directories, chosen_option, encodings, options): |
| 61 | + """ |
| 62 | + Prepends a given code block to all .sql files in the specified directories, |
| 63 | + taking into account the file's encoding to handle UTF properly. |
| 64 | + """ |
| 65 | + combined_options_pattern = re.compile('|'.join(re.escape(opt) for opt in options.values()), re.DOTALL) |
| 66 | + |
| 67 | + for directory in directories: |
| 68 | + for root, _, files in os.walk(directory): |
| 69 | + for file in files: |
| 70 | + if file.endswith(".sql"): |
| 71 | + file_path = os.path.join(root, file) |
| 72 | + file_content, encoding_used = read_file_with_encoding_detection(file_path, encodings) |
| 73 | + |
| 74 | + # Check if the file content starts with any of the options |
| 75 | + if combined_options_pattern.match(file_content): |
| 76 | + new_content = combined_options_pattern.sub('', file_content).lstrip() |
| 77 | + new_content = chosen_option.rstrip() + "\n\n" + new_content |
| 78 | + else: |
| 79 | + new_content = chosen_option.rstrip() + "\n\n" + file_content |
| 80 | + |
| 81 | + with open(file_path, 'w', encoding=encoding_used) as f: |
| 82 | + f.write(new_content) |
| 83 | + print(f"Processed {file_path} with encoding {encoding_used}") |
| 84 | + |
| 85 | +def read_file_with_encoding_detection(file_path, encodings): |
| 86 | + """ |
| 87 | + Attempts to read a file with multiple encodings until one succeeds. |
| 88 | + Returns the file content and the encoding used. |
| 89 | + """ |
| 90 | + for encoding in encodings: |
| 91 | + try: |
| 92 | + with open(file_path, 'r', encoding=encoding) as file: |
| 93 | + return file.read(), encoding |
| 94 | + except UnicodeDecodeError: |
| 95 | + continue |
| 96 | + raise ValueError(f"Failed to decode {file_path} with given encodings.") |
| 97 | + |
| 98 | +if __name__ == "__main__": |
| 99 | + encodings = ['utf-8-sig', 'utf-8', 'latin-1'] |
| 100 | + |
| 101 | + #Set you option!!! |
| 102 | + #Set you option!!! |
| 103 | + #Set you option!!! |
| 104 | + option_to_use = 'option1' # for example |
| 105 | + code_block = options[option_to_use] |
| 106 | + |
| 107 | + directories = [ |
| 108 | + r'C:\b\bamplus-postgres-research-main\bamplus-postgres-research-main\Bam+ Installer\module-packages\BAMPlus.BAM.Bridge\database\new_install_before\\' |
| 109 | + #,r'C:\b\bamplus-postgres-research-main\bamplus-postgres-research-main\Bam+ Installer\module-packages\BAMPlus.BAM.Bridge\database\bsav2_upgrade\\' |
| 110 | + #,r'C:\b\bamplus-postgres-research-main\bamplus-postgres-research-main\Bam+ Installer\module-packages\BAMPlus.BAM.Bridge\database\before_every_upgrade\\' |
| 111 | + #,r'C:\b\bamplus-postgres-research-main\bamplus-postgres-research-main\Bam+ Installer\module-packages\BAMPlus.BAM.Bridge\database\after_every_upgrade\\' |
| 112 | + ] |
| 113 | + |
| 114 | + prepend_code_block_with_encoding_handling(directories, code_block, encodings, options) |
| 115 | + print("Done processing .sql files.") |
0 commit comments