Created
July 20, 2010 15:44
-
-
Save jh3/483135 to your computer and use it in GitHub Desktop.
This file contains 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 | |
# Quick and dirty way of adding a prefix to all tables | |
# in a sql file | |
# | |
# Takes a sql file, duplicates it, and adds whatever | |
# prefix you pass to all TABLE and INSERT statements | |
# in the copied file. | |
# | |
# Usage: ./dbp.pl example.sql prefix | |
use strict; | |
use warnings; | |
use Tie::File; | |
use File::Copy; | |
use File::Basename; | |
my $file = $ARGV[0]; | |
my $prefix = $ARGV[1]; | |
my ($base, $dir, $ext) = fileparse($file, qr/\.[^.]+$/); | |
my $new = "$base-prefixed$ext"; | |
copy($file, $new); | |
tie my @array, 'Tie::File', $new or die(); | |
for (@array) { | |
if (/TABLE/i || /INSERT/) { | |
s/(`{1})(.*?[^`])(`{1})/`$prefix\_$2`/g; | |
} | |
} | |
untie @array; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment