2015-12-06 23:42:50 +00:00
|
|
|
#!/usr/bin/perl
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
|
|
|
use DBI;
|
|
|
|
use Encode qw(encode decode);
|
2015-12-15 00:31:50 +00:00
|
|
|
use Supporters;
|
2015-12-06 23:42:50 +00:00
|
|
|
|
|
|
|
my($OLD_SUPPORTERS_SQLITE_DB_FILE, $NEW_SUPPORTERS_SQLITE_DB_FILE) = @ARGV;
|
|
|
|
|
|
|
|
my $dbhOld = DBI->connect("dbi:SQLite:dbname=$OLD_SUPPORTERS_SQLITE_DB_FILE", "", "",
|
|
|
|
{ RaiseError => 1, sqlite_unicode => 1 })
|
|
|
|
or die $DBI::errstr;
|
|
|
|
|
|
|
|
my $dbhNew = DBI->connect("dbi:SQLite:dbname=$NEW_SUPPORTERS_SQLITE_DB_FILE", "", "",
|
|
|
|
{ RaiseError => 1, sqlite_unicode => 1 })
|
|
|
|
or die $DBI::errstr;
|
|
|
|
|
2015-12-15 00:31:50 +00:00
|
|
|
my $sp = new Supporter($dbhNew, "/usr/bin/ledger");
|
|
|
|
|
2015-12-06 23:42:50 +00:00
|
|
|
# Insert t-shirt types and sizes
|
|
|
|
|
|
|
|
|
2015-12-15 00:31:50 +00:00
|
|
|
my @sizes = qw/LadiesS LadiesM LadiesL LadiesXL MenS MenM MenL MenXL Men2XL/;
|
|
|
|
my $tShirt0 = $sp->addRequestConfigurations("t-shirt-0", \@sizes);
|
|
|
|
my $thShirt1 = $sp->addRequestConfigurations("t-shirt-1", \@sizes);
|
2015-12-06 23:42:50 +00:00
|
|
|
|
2015-12-15 00:31:50 +00:00
|
|
|
my $tShirt0RequestTypeId = (keys %{$tShirt0})[0];
|
|
|
|
my $tShirt1RequestTypeId = (keys %{$tShirt0})[0];
|
2015-12-06 23:43:03 +00:00
|
|
|
|
2015-12-15 00:31:50 +00:00
|
|
|
my %tShirt0SizeRequestConfigurationIds = %{$tShirt0->{$tShirt0RequestTypeId}};
|
|
|
|
|
|
|
|
my $tShirt1RequestTypeId = $sp->addRequestType("t-shirt-1");
|
|
|
|
|
|
|
|
my $announceEmailListRequestTypeId = $sp->addRequestType("join-announce-email-list");
|
2015-12-06 23:42:50 +00:00
|
|
|
|
|
|
|
# Only one email Adress type so far
|
2015-12-06 23:43:03 +00:00
|
|
|
my $sthNew = $dbhNew->prepare("INSERT INTO address_type(name) values('paypal_payer')");
|
2015-12-07 00:33:01 +00:00
|
|
|
$sthNew->execute();
|
2015-12-06 23:43:07 +00:00
|
|
|
my $paypalPayerTypeId = $dbhNew->last_insert_id("","","","");
|
2015-12-06 23:42:50 +00:00
|
|
|
$sthNew->finish();
|
|
|
|
|
|
|
|
# Legacy fulfillment confirmation
|
|
|
|
$sthNew = $dbhNew->prepare("INSERT INTO fulfillment(date, who, how)" .
|
|
|
|
"values(date('now'), 'bkuhn', 'legacy import of old database; exact details of this fulfillment are unknown')");
|
2015-12-07 00:33:01 +00:00
|
|
|
$sthNew->execute();
|
2015-12-06 23:42:50 +00:00
|
|
|
my $fulfillmentId = $dbhNew->last_insert_id("","","","");
|
|
|
|
$sthNew->finish();
|
|
|
|
|
|
|
|
my $sthNewInsertSupporter = $dbhNew->prepare('INSERT INTO supporter(' .
|
|
|
|
'ledger_entity_id, display_name, public_ack) values (?, ?, ?)');
|
2015-12-06 23:43:07 +00:00
|
|
|
my $sthInsertEmailAddress = $dbhNew->prepare('INSERT INTO email_address(email_address, type_id, date_encountered)' .
|
2015-12-06 23:43:10 +00:00
|
|
|
"values(?, $paypalPayerTypeId, date('now'))");
|
2015-12-06 23:42:50 +00:00
|
|
|
|
|
|
|
my $sthLinkSupporterToEmail = $dbhNew->prepare('INSERT INTO supporter_email_address_mapping(supporter_id, email_address_id, preferred)' .
|
2015-12-06 23:43:10 +00:00
|
|
|
"values(?, ?, 1)");
|
2015-12-06 23:42:50 +00:00
|
|
|
|
|
|
|
my $sthLinkSupporterToPostal = $dbhNew->prepare('INSERT INTO supporter_postal_address_mapping(supporter_id, postal_address_id, preferred)' .
|
2015-12-06 23:43:10 +00:00
|
|
|
"values(?, ?, 1)");
|
2015-12-06 23:42:50 +00:00
|
|
|
|
|
|
|
my $sthInsertRequest = $dbhNew->prepare('INSERT INTO request' .
|
|
|
|
'(supporter_id, request_type_id, request_configuration_id, date_requested, fulfillment_id, notes) ' .
|
|
|
|
"values(?, ?, ?, date('now'), ?," .
|
|
|
|
'"import of old database; exact date of this request is unknown")');
|
|
|
|
|
2015-12-06 23:45:22 +00:00
|
|
|
my $sthPostalAddress = $dbhNew->prepare('INSERT INTO postal_address(formatted_address, type_id, date_encountered)' .
|
|
|
|
"VALUES(?, $paypalPayerTypeId, date('now'))");
|
2015-12-06 23:42:50 +00:00
|
|
|
|
|
|
|
my $sthOld = $dbhOld->prepare('SELECT * from supporters order by id;');
|
|
|
|
$sthOld->execute();
|
2015-12-13 20:16:42 +00:00
|
|
|
|
2015-12-06 23:42:50 +00:00
|
|
|
while (my $row = $sthOld->fetchrow_hashref) {
|
2015-12-13 20:16:42 +00:00
|
|
|
$row->{email_address_type} = 'paypal';
|
|
|
|
$row->{email_address} = $row->{paypal_payer};
|
|
|
|
my $supporterId = $sp->addSupporter($row);
|
|
|
|
|
2015-12-06 23:42:50 +00:00
|
|
|
die("Database conversion failed on id matching: $row->{ledger_entity_id} had ID $row->{id} now has $supporterId")
|
|
|
|
unless ($row->{id} == $supporterId);
|
|
|
|
if ($row->{want_gift}) {
|
|
|
|
die "DB Convert Fail: Unknown shirt size of $row->{shirt_size} when someone wanted a shirt"
|
|
|
|
unless defined $tShirt0SizeRequestConfigurationIds{$row->{shirt_size}};
|
|
|
|
$sthInsertRequest->execute($supporterId, $tShirt0RequestTypeId,
|
|
|
|
$tShirt0SizeRequestConfigurationIds{$row->{shirt_size}},
|
|
|
|
($row->{gift_sent} ? $fulfillmentId : undef));
|
|
|
|
}
|
|
|
|
if ($row->{join_list}) {
|
|
|
|
$sthInsertRequest->execute($supporterId, $announceEmailListRequestTypeId, undef,
|
|
|
|
($row->{on_announce_mailman_list} ? $fulfillmentId : undef));
|
|
|
|
}
|
2015-12-06 23:45:18 +00:00
|
|
|
$sthInsertEmailAddress->execute($row->{paypal_payer});
|
2015-12-06 23:42:50 +00:00
|
|
|
my $emailId = $dbhNew->last_insert_id("","","","");
|
|
|
|
$sthLinkSupporterToEmail->execute($supporterId, $emailId);
|
2015-12-06 23:45:25 +00:00
|
|
|
$sthPostalAddress->execute($row->{formatted_address});
|
2015-12-06 23:42:50 +00:00
|
|
|
my $postalId = $dbhNew->last_insert_id("","","","");
|
2015-12-06 23:46:27 +00:00
|
|
|
$sthLinkSupporterToPostal->execute($supporterId, $postalId);
|
2015-12-06 23:42:50 +00:00
|
|
|
}
|
2015-12-07 00:33:01 +00:00
|
|
|
foreach my $sth (($sthOld, $sthOld, $sthNewInsertSupporter, $sthInsertEmailAddress,
|
|
|
|
$sthLinkSupporterToEmail, $sthInsertRequest, $sthPostalAddress,
|
|
|
|
$sthLinkSupporterToPostal,)) {
|
2015-12-06 23:42:50 +00:00
|
|
|
$sth->finish();
|
|
|
|
}
|
2015-12-07 00:33:01 +00:00
|
|
|
foreach my $dbh ($dbhNew, $dbhOld) {
|
2015-12-06 23:42:50 +00:00
|
|
|
$dbhNew->disconnect();
|
|
|
|
}
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
#
|
|
|
|
# Local variables:
|
|
|
|
# compile-command: "perl -c db-convert-0.1-to-0.2.plx"
|
|
|
|
# End:
|