Changeset 257 for trunk

Show
Ignore:
Timestamp:
07/04/11 19:07:39 (3 years ago)
Author:
depesz
Message:

First part of code beautifying. Removal of redundant calls to File::Spec->catfile, making run_command a bit smarter

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/tools/fast.dump.and.restore/fast.dump

    r255 r257  
    1414use Getopt::Long qw( :config no_ignore_case ); 
    1515use Data::Dumper; 
     16use File::Basename; 
    1617use Cwd qw( abs_path ); 
    1718use Pod::Usage; 
     
    6970    } 
    7071 
    71     open my $fh, ">", File::Spec->catfile( $self->{ 'output' }, 'index.lst' ) or croak( "Cannot create index file: $OS_ERROR\n" ); 
     72    open my $fh, ">", 'index.lst' or croak( "Cannot create index file: $OS_ERROR\n" ); 
    7273    printf $fh '%-5s | %-7s | %-32s | %-32s | %-10s | %s%s', '#', qw( type schema table size condition ), "\n"; 
    7374    for my $i ( @{ $self->{ 'blobs' } } ) { 
     
    133134    $PROGRAM_NAME .= ' ... ' . $file_name; 
    134135 
    135     my $output_path = File::Spec->catfile( $self->{ 'output' }, $file_name ); 
    136     my $sql_filename = $output_path . '.sql'; 
     136    my $sql_filename = $file_name . '.sql'; 
    137137    open my $sql_fh, '>', $sql_filename or croak( "Cannot write to $sql_filename: $OS_ERROR\n" ); 
    138138 
     
    161161    my ( $stderr_fh, $stderr_filename ) = tempfile( 'fast.dump.XXXXXXXX', 'TMPDIR' => 1, ); 
    162162 
    163     $psql_call .= sprintf ' 2>%s >%s', quotemeta $stderr_filename, quotemeta $output_path
     163    $psql_call .= sprintf ' 2>%s >%s', quotemeta $stderr_filename, quotemeta $file_name
    164164 
    165165    system $psql_call; 
     
    352352    my $self = shift; 
    353353 
    354     my $restored = $self->run_command( $self->{ 'pg_restore' }, '-l', File::Spec->catfile( $self->{ 'output' }, 'schema.dump' ) ); 
     354    my $restored = $self->run_command( qw( pg_restore -l schema.dump ) ); 
    355355 
    356356    my @lines = split /\r?\n/, $restored; 
     
    409409sub dump_schema { 
    410410    my $self = shift; 
    411     $self->run_command( $self->{ 'pg_dump' }, '-Fc', '-f', File::Spec->catfile( $self->{ 'output' }, 'schema.dump' ), '-s', '-v' ); 
     411    $self->run_command( qw( pg_dump -Fc -f schema.dump -s -v ) ); 
    412412 
    413413    my ( $sql_fh, $sql_filename ) = tempfile( 'fast.dump.XXXXXXXX', 'TMPDIR' => 1, ); 
    414     print $sql_fh "\\o " . File::Spec->catfile( $self->{ 'output' }, 'index.sizes' ) . "\n"; 
     414    print $sql_fh "\\o index.sizes\n"; 
    415415    print $sql_fh 
    416416        "select n.nspname, c.relname, pg_relation_size(c.oid) from pg_class c join pg_namespace n on c.relnamespace = n.oid where c.relkind = 'i' and n.nspname !~ '^pg_' order by 3 desc;\n"; 
     
    419419 
    420420    open $sql_fh, '>', $sql_filename or croak( "Cannot re-write to $sql_filename: $OS_ERROR\n" ); 
    421     print $sql_fh "\\o " . File::Spec->catfile( $self->{ 'output' }, 'fkeys.ordering' ) . "\n"; 
     421    print $sql_fh "\\o fkeys.ordering\n"; 
    422422    print $sql_fh 
    423423"SELECT n.nspname, c.conname, c.conrelid::regclass, c.confrelid::regclass, pg_relation_size( c.conrelid ) + pg_relation_size( c.confrelid ) as tables_size FROM pg_constraint c JOIN pg_namespace n on c.connamespace = n.oid WHERE c.contype = 'f';\n"; 
     
    433433    my $self = shift; 
    434434    printf "\n\nAre you sure you want to continue?\n"; 
    435     printf "Please remember that any other ( aside from $PROGRAM_NAME ) connections to database can cause dump corruption!\n"
     435    printf "Please remember that any other ( aside from %s ) connections to database can cause dump corruption!\n", basename( $PROGRAM_NAME )
    436436    printf "Enter YES to continue: "; 
    437437    my $input = <STDIN>; 
     
    494494 
    495495    $opts->{ 'output' } = abs_path( $opts->{ 'output' } ); 
     496 
    496497    @{ $self }{ keys %{ $opts } } = values %{ $opts }; 
     498 
     499    # Thanks to this we will just create files in ".", so creation of output filenames will be easier. 
     500    chdir $self->{ 'output' }; 
     501 
    497502    return; 
    498503} 
     
    501506    my $self   = shift; 
    502507    my $query  = shift; 
    503     my $output = $self->run_command( $self->{ 'psql' }, '-qAtX', '-F', "\t", '-c', $query, ); 
     508    my $output = $self->run_command( qw( psql -qAtX -F ), "\t", '-c', $query ); 
    504509    my @rows   = grep { '' ne $_ } split /\r?\n/, $output; 
    505510    my @data   = map { [ split /\t/, $_ ] } @rows; 
     
    510515    my $self = shift; 
    511516    my ( @cmd ) = @_; 
     517 
     518    # Use paths provided by user as command line options 
     519    $cmd[ 0 ] = $self->{ $cmd[ 0 ] } if $self->{ $cmd[ 0 ] }; 
    512520 
    513521    my $real_command = join( ' ', map { quotemeta } @cmd );