I have created a simple C# console application a split SQL script tool to split big scripts. For example, I had a problem with data seed scripts. Those scripts are huge because the database has more than a million records. These records need to be added to a new database. Each script is roughly 400Mb and I can’t upload it in the repository in Azure DevOps for example.
Also, I created a repository for the Split SQL script tool. It contains the code in C#. This repository is available on GitHub.
Implementation explained
For the implementation, I added 2 libraries:
- Spectre.Console for displaying nicely the progress of splitting and the result files in a table
- System.CommandLine.DragonFruit helps in the creation of the help page and manages the parameters.
I have to explore the possibility of using only Spectre.Console that has similar functionality to System.CommandLine.DragonFruit.
System.CommandLine.DragonFruit
So, this library extends the Main
function of the console application. The documentation is available on the Microsoft documentation but it is old and the library is not maintained.
The magic occurs in the Main
function and its attribute. For example, look at the code:
/// <summary>
/// Split SQL script in multiple files based on the required size.
/// </summary>
/// <param name="file">The SQL script file to split.</param>
/// <param name="destination">The destination folder. If this is empty or null,
/// the new files will be created in the same directory as the original file.</param>
/// <param name="limit">The maximum bytes limit for the new files.</param>
/// <param name="addGo">if set to <c>true</c> the procedure will add the command GO
/// at the end of each file.</param>
public static void Main(string file, string? destination,
int limit = 10240000, bool addGo = true)
Using this package, the application has the --help
option and it is displayed in a professional way the details. Here the screenshot
Options
Option | Description | Default |
---|---|---|
file | The SQL script file to split. | |
destination | The destination folder. If this is empty or null, the new files will be created in the same directory as the original file. | |
limit | The maximum bytes limit for the new files. [default: 10240000] | 10240000 |
add-go | If set to true the procedure will add the command GO at the end of each file. | True |
version | Show version information | |
?, h, help | Show help and usage information |
Wrap up
In conclusion, I hope that the code for a split SQL script tool will be useful for you. Please, use the code and send your PR to improve the project.
Happy coding!