Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Ovid (2709)

Ovid
  (email not shown publicly)
http://publius-ovidius.livejournal.com/
AOL IM: ovidperl (Add Buddy, Send Message)

Stuff with the Perl Foundation. A couple of patches in the Perl core. A few CPAN modules. That about sums it up.

Journal of Ovid (2709)

Thursday December 23, 2004
05:01 PM

Brute forcing the database ordering problem

[ #22436 ]

It's really annoying when trying to add a foreign key constraint to a table that doesn't exist because you haven't created it yet. So you create the database and then you add the constraint.

Then you try to reference a view that doesn't exist. Or a trigger. Or stored procedure. I've been dealing with this quite a bit because we have a ton of dynamically generated SQL. Trying to dynamically determine the order of everything was a pain, so I cheated and took the easy way out. Here's my very embarrasing brute force method of solving the problem:

sub do {
    my $self    = shift;
    my %actions = map { $_ => 1 } @_;
    my $count   = keys %actions;
    my $dbh     = $self->_dbh;

    my ($schema_created, @failures);

    while ( ! $schema_created ) {
        foreach my $action (keys %actions) {
            eval {$dbh->do($action)};
            if ($@) {
                push @failures => [$action => $@];
            } else {
                delete $actions{$action};
            }
        }
        if ( ! @failures ) {
            $schema_created = 1;
        } elsif ( $count == keys %actions ) {
            foreach my $failure (@failures) {
                warn "Action: \n$failure->[0]\n"
                    ."Failure reason: $@\n----------\n";
            }
            die "Database schema creation failed.";
        } else {
            @failures = ();
            $count    = keys %actions;
        }
    }
    return $self;
}

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.