root/veekun/trunk/script/dump_sql.pl

Revision 406, 2.5 kB (checked in by eevee, 10 months ago)

Database refactoring. Renamed columns and tables to be more consistent and more readable. (#58)

Line 
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5use File::Basename;
6use File::Spec;
7use YAML;
8
9# usage: dump_sql.pl [table_groups...]
10# In general, just running the script with no arguments should do what you want.
11
12my %TABLES = (
13    base    => [qw/ edits error_log forums group_permissions groups messages posts sessions shoutbox threads thread_views user_groups users /],
14    gallery => [qw/ creators gallery gallery_keywords item_keywords /],
15    pokedex => [qw/ abilities berries contest_effects evo_chains flavor_text items locations location_sections location_encounters machines moves move_effects pokemon pokemon_abilities pokemon_breeds pokemon_items pokemon_moves types /],
16);
17
18my (undef, $path) = fileparse( File::Spec->rel2abs($0) );
19chdir $path;
20
21my $config = YAML::LoadFile('../vee.yml');
22my ($db_db, $db_user, $db_pass) = @{ $config->{'Model::DBIC'}{connect_info} };
23$db_db =~ s/.+://g;  # dbi:mysql:database -> database
24
25my @table_groups = @ARGV ? @ARGV : keys %TABLES;
26
27for my $table_group (@table_groups) {
28    next unless exists $TABLES{$table_group};
29
30    system mysqldump => qw/--skip-quote-names/,
31        ( $table_group eq 'pokedex' ? '--skip-extended-insert' : '-d' ),  # cheap cheap hack to make sure Pokedex gets its data
32        '-r' => "$table_group.sql",
33        '-u' => $db_user,
34        ( $db_pass ? "-p$db_pass" : () ),
35        $db_db => @{ $TABLES{$table_group} };
36
37    system sed => '-ri'# -r == +awesome
38        # remove the header at the top, as it contains the server version which can change across users
39        '-e' => '1, /^$/ d',
40
41        # get rid of AUTO_INCREMENT options, which we certainly do not want in fresh tables
42        '-e' => 's/ AUTO_INCREMENT=[0-9]+//',
43
44        # hold every INSERT command and don't print it
45        '-e' => '/^INSERT INTO/ { H ; d }',
46
47        # when we run out of INSERTS:
48        # - hold the line and clear it from the pattern space
49        # - swap hold space with the blank pattern space
50        # - delete the newline from the very first hold
51        # - delete the INSERT command on adjacent inserts
52        # - as is default, print the entire pattern space
53        '-e' => '/ENABLE KEYS/ { H ; s/.*// ; x ; s/^\n// ; s/;\nINSERT INTO \w+ VALUES /,\n/g }',
54
55        # also don't want that 'dump completed' line; svn will take care of timestamps
56        # this leaves two newlines at the end, but fixing that is too complex to be worth it
57        '-e' => '/^-- Dump completed/, $ d',
58       
59        "$table_group.sql";
60}
Note: See TracBrowser for help on using the browser.