Skip to content

Instantly share code, notes, and snippets.

@doctorallen
Created May 7, 2013 17:16
Show Gist options
  • Save doctorallen/5534365 to your computer and use it in GitHub Desktop.
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.
#!/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);
}
@Rican7
Copy link

Rican7 commented May 7, 2013

Why, oh why are you writing Perl?
:P

@doctorallen
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment