Skip to content

Instantly share code, notes, and snippets.

@jh3
Created July 20, 2010 15:44
Show Gist options
  • Save jh3/483135 to your computer and use it in GitHub Desktop.
Save jh3/483135 to your computer and use it in GitHub Desktop.
#!/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