c57460e73607453a402425fbfbcff5b80d704fcc
[dbsrgits/DBIx-Class.git] / examples / Schema / insertdb.pl
1 #!/usr/bin/env perl
2
3 use strict;
4 use warnings;
5
6 use MyApp::Schema;
7
8 use Path::Class 'file';
9 my $db_fn = file($INC{'MyApp/Schema.pm'})->dir->parent->file('db/example.db');
10
11 my $schema = MyApp::Schema->connect("dbi:SQLite:$db_fn");
12
13 my @artists = (['Michael Jackson'], ['Eminem']);
14 $schema->populate('Artist', [
15     [qw/name/],
16     @artists,
17 ]);
18
19 my %albums = (
20     'Thriller' => 'Michael Jackson',
21     'Bad' => 'Michael Jackson',
22     'The Marshall Mathers LP' => 'Eminem',
23 );
24
25 my @cds;
26 foreach my $lp (keys %albums) {
27     my $artist = $schema->resultset('Artist')->find({
28         name => $albums{$lp}
29     });
30     push @cds, [$lp, $artist->id];
31 }
32
33 $schema->populate('Cd', [
34     [qw/title artist/],
35     @cds,
36 ]);
37
38
39 my %tracks = (
40     'Beat It'         => 'Thriller',
41     'Billie Jean'     => 'Thriller',
42     'Dirty Diana'     => 'Bad',
43     'Smooth Criminal' => 'Bad',
44     'Leave Me Alone'  => 'Bad',
45     'Stan'            => 'The Marshall Mathers LP',
46     'The Way I Am'    => 'The Marshall Mathers LP',
47 );
48
49 my @tracks;
50 foreach my $track (keys %tracks) {
51     my $cd = $schema->resultset('Cd')->find({
52         title => $tracks{$track},
53     });
54     push @tracks, [$cd->id, $track];
55 }
56
57 $schema->populate('Track',[
58     [qw/cd title/],
59     @tracks,
60 ]);