Created
May 7, 2013 17:16
-
-
Save doctorallen/5534365 to your computer and use it in GitHub Desktop.
Parses MySQL Dump files and creates individual stored procedure files for each procedure.
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/perl | |
#Script written by David Allen on April 15th 2013 | |
use warnings; | |
use Data::Dumper; | |
my @databases = ("TEST"); | |
foreach( @databases ) | |
{ | |
$db = $_; | |
open(FD, $db . "_stored_procs.sql"); | |
my @procedure; | |
my $start; | |
my $name; | |
while(<FD>){ | |
my $line = $_;#this reads from the buffer and saves the line data | |
if( $line eq "DELIMITER ;;\n"){ | |
$start = 1; | |
} | |
if( $start == 1 ){ | |
push(@procedure, $line); | |
if($line =~ m/PROCEDURE\s+\`(\w+)\`/){ | |
$name = $1; | |
} | |
} | |
#grab stored procedure name | |
if( $start == 1 ){ | |
if( $line eq "DELIMITER ;\n" && defined($name) ){ | |
my $procedure = join("\n", @procedure); | |
@procedure = (); | |
my $filename = "> $db" . "/" . "$db.$name.sql"; | |
print "database:" . $db . "\n"; | |
print $filename . "\n"; | |
open FILE, $filename; | |
print FILE $procedure; | |
close FILE; | |
$start = 0; | |
$name = undef; | |
$filename = undef; | |
} | |
} | |
} | |
close(FD); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm creating a CI/deployment procedure for one of our clients that uses stored procedures in their development. So I need to write scripts that we can use on many different servers and Perl fits the bill. I've actually enjoyed working on some of the scripts. There is another gist I created called gittug which will automatically source any sql (stored procedures in this case) that is changed/added in the repository.