Skip to content

Commit

Permalink
Added scripts to load and dump candidate data.
Browse files Browse the repository at this point in the history
  • Loading branch information
davorg committed Aug 16, 2014
1 parent 4fa49c8 commit 43752a1
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
22 changes: 22 additions & 0 deletions bin/dump_cand
@@ -0,0 +1,22 @@
#!/usr/bin/perl

use strict;
use warnings;
use 5.010;

use lib 'lib';
use TwittElection::Schema;

my $file = shift || 'db/candidate.csv';

my $sch = TwittElection::Schema->connect(
"dbi:mysql:database=$ENV{TE_DB}", $ENV{TE_USER}, $ENV{TE_PASS},
) or die;

my $cand_rs = $sch->resultset('Candidate');

open my $fh, '>', $file or die "$!: $file";

while (my $cand = $cand_rs->next) {
say $fh $cand->dump;
}
46 changes: 46 additions & 0 deletions bin/load_cand
@@ -0,0 +1,46 @@
#!/usr/bin/perl

use strict;
use warnings;
use 5.010;

use lib 'lib';
use TwittElection::Schema;

my $file = shift || 'db/candidate.csv';

my $sch = TwittElection::Schema->connect(
"dbi:mysql:database=$ENV{TE_DB}", $ENV{TE_USER}, $ENV{TE_PASS},
) or die;

my $con_rs = $sch->resultset('Constituency');
my $par_rs = $sch->resultset('Party');

open my $fh, '<', $file or die "$!: $file";

#delete existing candidates
$sch->resultset('Candidate')->delete;

while (<$fh>) {
chomp;

my ($name, $twitter, $party, $constituency) = split /\|/;
my $con = $con_rs->find({
name => $constituency,
});
warn "Can't find constituency: $constituency", next unless $con;
my $par = $par_rs->find({
name => $party,
});
warn "Can't find party: $party", next unless $par;
$con->add_to_candidates({
name => $name,
twitter => $twitter,
party_id => $par->id,
});
}

0 comments on commit 43752a1

Please sign in to comment.