Skip to content

Instantly share code, notes, and snippets.

@larrynung
Forked from temmings/seperate.pl
Created November 18, 2019 07:30
Show Gist options
  • Save larrynung/537e88e4f3ed6638a45fb6a75b4b7766 to your computer and use it in GitHub Desktop.
Save larrynung/537e88e4f3ed6638a45fb6a75b4b7766 to your computer and use it in GitHub Desktop.
Do "mysqldump --routines" to separate files. (fuzzy)
#!/usr/bin/perl
#
# mysqldump -routines --no-create-info --no-data --no-create-db --compact | perl $0
use strict;
use warnings;
use utf8;
my $output_dir = "out";
sub getProcName() {
(my $name = $_) =~ s/^CREATE\s+([^\s]+)\s+`([^\s`(]+)`\s*\(?.*/$2/eg;
return $name;
}
## main
my %procs = ();
my $proc_name = "";
my $delimiter = ';';
while (<>) {
if (/^DELIMITER\s+/) {
$delimiter = $_;
$delimiter =~ s/^DELIMITER\s+(.+)$/$1/eg;
}
if (/^CREATE\s+/) {
if (/DEFINER=/) {
# remove DEFINER
s/^(.+\s+)DEFINER=`[^\s]+`\s+(.+)$/$1$2/;
}
if (/^CREATE\s+(FUNCTION|PROCEDURE)/) {
$proc_name = "$1." . &getProcName($_);
# append DELIMITER to first line
$procs{$proc_name} .= "DELIMITER $delimiter\n";
}
}
if ($proc_name) {
$procs{$proc_name} .= $_;
}
if (/END\s+$delimiter/) {
$proc_name = "";
}
}
# store files
mkdir($output_dir);
for (keys %procs) {
my $key = $_;
chomp;
my $filename = "$output_dir/$_.sql";
print "Dump $_ -> $filename\n";
open(my $fh, '>', $filename) or die "Could not open file '$filename' $!";
print $fh $procs{$key};
close $fh;
}
print "done\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment