Last active
May 10, 2022 10:50
-
-
Save ElectricCoffee/06be1982671d8160f5ba2017124df1da to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env perl | |
| use v5.30; | |
| use warnings; | |
| use Getopt::Long; | |
| use autodie; | |
| # Define command-line arguments | |
| GetOptions ( | |
| 'output=s' => \my $out_file, | |
| ); | |
| # If no migration name is provided, fail. | |
| unless (@ARGV) { | |
| say STDERR "Please provide the name of the migration you wish to run"; | |
| exit; | |
| } | |
| my $name = shift; | |
| # run the dotnet migration command, and route its output to $in_handle | |
| open(my $in_handle, '-|', "dotnet ef migrations script '$name' --context DatabaseContext"); | |
| my $copying_sql = 0; | |
| my $sql = ''; | |
| # As long as $in_handle has data sent to it from the command, save it to $sql | |
| while (<$in_handle>) { | |
| # only start copying sql once you see START TRANSACTION in the file stream | |
| $copying_sql = 1 if m/START TRANSACTION;/; | |
| print unless $copying_sql; # print all the non-sql bits to the screen. This is build and debug info. | |
| $sql .= $_ if $copying_sql; | |
| # stop copying sql when you see COMMIT in the file stream | |
| $copying_sql = 0 if m/COMMIT;/; | |
| } | |
| close $in_handle; | |
| # If there's any data (sometimes it fails and doesn't generate any sql)... | |
| if ($sql) { | |
| my $out_handle; | |
| # write it to $out_file if you wanted a file | |
| open($out_handle, '>', $out_file) if $out_file; | |
| # otherwise write it to the clipboard | |
| open($out_handle, '|-', 'pbcopy') unless $out_file; | |
| print $out_handle $sql; | |
| say $out_file | |
| ? "Content written to $out_file." | |
| : 'Content copied to clipboard.'; | |
| close $out_handle; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment