From: Matt S Trout Date: Fri, 27 Jan 2006 19:17:17 +0000 (+0000) Subject: Added $schema->populate X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=a37a4697942b373405e80f3f74a92f3abbcf823f;p=dbsrgits%2FDBIx-Class-Historic.git Added $schema->populate --- diff --git a/Changes b/Changes index c95b8df..13bcfa4 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,6 @@ Revision history for DBIx::Class + - added $schema->populate to load test data (similar to AR fixtures) - removed cdbi-t dependencies, only run tests if installed - Removed DBIx::Class::Exception - unified throw_exception stuff, using Carp::Clan diff --git a/lib/DBIx/Class/Schema.pm b/lib/DBIx/Class/Schema.pm index 69b48d3..cc6c949 100644 --- a/lib/DBIx/Class/Schema.pm +++ b/lib/DBIx/Class/Schema.pm @@ -344,6 +344,32 @@ sub clone { return $clone; } +=item populate($moniker, \@data); + +Populates the source registered with the given moniker with the supplied data. +@data should be a list of listrefs, the first containing column names, the +second matching values - i.e. + +$schema->populate('Foo', [ + [ qw/foo_id foo_string/ ], + [ 1, 'One' ], + [ 2, 'Two' ], + ... +]); + +=cut + +sub populate { + my ($self, $name, $data) = @_; + my $rs = $self->resultset($name); + my @names = @{shift(@$data)}; + foreach my $item (@$data) { + my %create; + @create{@names} = @$item; + $rs->create(\%create); + } +} + =item throw_exception Defaults to using Carp::Clan to report errors from user perspective. diff --git a/t/lib/DBICTest/Setup.pm b/t/lib/DBICTest/Setup.pm index de113db..9b64890 100755 --- a/t/lib/DBICTest/Setup.pm +++ b/t/lib/DBICTest/Setup.pm @@ -12,91 +12,15 @@ my $dsn = "dbi:SQLite:${db_file}"; my $schema = DBICTest::Schema->compose_connection('DBICTest' => $dsn); -my $dbh = DBI->connect($dsn); +$schema->storage->on_connect_do([ "PRAGMA synchronous = OFF" ]); -my $sql = <storage->dbh; open IN, "t/lib/sqlite.sql"; -{ local $/ = undef; $sql = .$sql; } +my $sql; + +{ local $/ = undef; $sql = ; } close IN; @@ -104,42 +28,86 @@ $dbh->do($_) for split(/\n\n/, $sql); $schema->storage->dbh->do("PRAGMA synchronous = OFF"); -1; - -__DATA__ - -CREATE TABLE artist (artistid INTEGER NOT NULL PRIMARY KEY, name VARCHAR); - -CREATE TABLE cd (cdid INTEGER NOT NULL PRIMARY KEY, artist INTEGER NOT NULL, - title VARCHAR, year VARCHAR); - -CREATE TABLE liner_notes (liner_id INTEGER NOT NULL PRIMARY KEY, notes VARCHAR); - -CREATE TABLE track (trackid INTEGER NOT NULL PRIMARY KEY, cd INTEGER NOT NULL, - position INTEGER NOT NULL, title VARCHAR); - -CREATE TABLE tags (tagid INTEGER NOT NULL PRIMARY KEY, cd INTEGER NOT NULL, - tag VARCHAR); - -CREATE TABLE twokeys (artist INTEGER NOT NULL, cd INTEGER NOT NULL, - PRIMARY KEY (artist, cd) ); - -CREATE TABLE fourkeys (foo INTEGER NOT NULL, bar INTEGER NOT NULL, - hello INTEGER NOT NULL, goodbye INTEGER NOT NULL, - PRIMARY KEY (foo, bar, hello, goodbye) ); - -CREATE TABLE onekey (id INTEGER NOT NULL PRIMARY KEY, - artist INTEGER NOT NULL, cd INTEGER NOT NULL ); - -CREATE TABLE self_ref (id INTEGER NOT NULL PRIMARY KEY, - name VARCHAR ); - -CREATE TABLE self_ref_alias (self_ref INTEGER NOT NULL, alias INTEGER NOT NULL, - PRIMARY KEY( self_ref, alias ) ); - -CREATE TABLE artist_undirected_map (id1 INTEGER NOT NULL, id2 INTEGER NOT NULL, PRIMARY KEY(id1, id2)); - -CREATE TABLE producer (producerid INTEGER NOT NULL PRIMARY KEY, name VARCHAR); - -CREATE TABLE cd_to_producer (cd INTEGER NOT NULL, producer INTEGER NOT NULL); +$schema->populate('Artist', [ + [ qw/artistid name/ ], + [ 1, 'Caterwauler McCrae' ], + [ 2, 'Random Boy Band' ], + [ 3, 'We Are Goth' ], +]); + +$schema->populate('CD', [ + [ qw/cdid artist title year/ ], + [ 1, 1, "Spoonful of bees", 1999 ], + [ 2, 1, "Forkful of bees", 2001 ], + [ 3, 1, "Caterwaulin' Blues", 1997 ], + [ 4, 2, "Generic Manufactured Singles", 2001 ], + [ 5, 3, "Come Be Depressed With Us", 1998 ], +]); + +$schema->populate('LinerNotes', [ + [ qw/liner_id notes/ ], + [ 2, "Buy Whiskey!" ], + [ 4, "Buy Merch!" ], + [ 5, "Kill Yourself!" ], +]); + +$schema->populate('Tag', [ + [ qw/tagid cd tag/ ], + [ 1, 1, "Blue" ], + [ 2, 2, "Blue" ], + [ 3, 3, "Blue" ], + [ 4, 5, "Blue" ], + [ 5, 2, "Cheesy" ], + [ 6, 4, "Cheesy" ], + [ 7, 5, "Cheesy" ], + [ 8, 2, "Shiny" ], + [ 9, 4, "Shiny" ], +]); + +$schema->populate('TwoKeys', [ + [ qw/artist cd/ ], + [ 1, 1 ], + [ 1, 2 ], + [ 2, 2 ], +]); + +$schema->populate('FourKeys', [ + [ qw/foo bar hello goodbye/ ], + [ 1, 2, 3, 4 ], + [ 5, 4, 3, 6 ], +]); + +$schema->populate('OneKey', [ + [ qw/id artist cd/ ], + [ 1, 1, 1 ], + [ 2, 1, 2 ], + [ 3, 2, 2 ], +]); + +$schema->populate('SelfRef', [ + [ qw/id name/ ], + [ 1, 'First' ], + [ 2, 'Second' ], +]); + +$schema->populate('SelfRefAlias', [ + [ qw/self_ref alias/ ], + [ 1, 2 ] +]); + +$schema->populate('ArtistUndirectedMap', [ + [ qw/id1 id2/ ], + [ 1, 2 ] +]); + +$schema->populate('Producer', [ + [ qw/producerid name/ ], + [ 1, 'Matt S Trout' ], +]); + +$schema->populate('CD_to_Producer', [ + [ qw/cd producer/ ], + [ 1, 1 ], +]); +1;