6745e589250ddad82e43597afe7ef61411128574
[eq/.git] / bin / upgrade_1_1_0_to_1_1_1
1 #!/usr/bin/perl
2
3 use DBI;
4 use Getopt::Std;
5
6 $mydir = `cd \$(dirname $0) 2>/dev/null; pwd`; chomp($mydir);
7 unshift @INC,("$mydir/../setup");
8
9 if( -f "$mydir/../setup/db_config.local") { require "db_config.local"; }
10 else { require "db_config"; }
11
12 ###################################################
13 # Connect to the database
14 $dbh=DBI->connect("dbi:mysql:dbname=$dbname:host=$dbhost:port=$dbport",$dbuser,$dbpass,{
15     AutoCommit=>0,
16     PrintError=>0}) or print "Connect Failure:".$DBI::errstr."\n" and exit 2;
17 ###################################################
18
19 # Change interview_type field in tc_interview table to type, change enum to use 'H','P'
20 $sth = $dbh->prepare("ALTER TABLE `tc_interview` ADD `type` ENUM( 'H', 'P' ) NOT NULL AFTER `interview_type`");
21 $sth->execute or die "-E- DB error: $DBI::errstr\n";
22 $sth = $dbh->prepare("UPDATE tc_interview SET type='H' WHERE interview_type='hti'");
23 $sth->execute or die "-E- DB error: $DBI::errstr\n";
24 $sth = $dbh->prepare("UPDATE tc_interview SET type='P' WHERE interview_type='ppi'");
25 $sth->execute or die "-E- DB error: $DBI::errstr\n";
26 $sth = $dbh->prepare("ALTER TABLE `tc_interview` DROP `interview_type` ");
27 $sth->execute or die "-E- DB error: $DBI::errstr\n";
28
29 # Change visit_type field in tc_visit table to type, change enum to use 'H','P'
30 $sth = $dbh->prepare("ALTER TABLE `tc_visit` ADD `type` ENUM( 'H', 'P' ) NOT NULL AFTER `visit_type`");
31 $sth->execute or die "-E- DB error: $DBI::errstr\n";
32 $sth = $dbh->prepare("UPDATE tc_visit SET type='H' WHERE visit_type='hometeaching'");
33 $sth->execute or die "-E- DB error: $DBI::errstr\n";
34 $sth = $dbh->prepare("UPDATE tc_visit SET type='P' WHERE visit_type='presidency'");
35 $sth->execute or die "-E- DB error: $DBI::errstr\n";
36 $sth = $dbh->prepare("ALTER TABLE `tc_visit` DROP `visit_type` ");
37 $sth->execute or die "-E- DB error: $DBI::errstr\n";
38
39 # add type field to tc_companionship
40 $sth = $dbh->prepare("ALTER TABLE `tc_companionship` ADD `type` ENUM( 'H', 'P' ) NOT NULL DEFAULT 'H' AFTER `district`");
41 $sth->execute or die "-E- DB error: $DBI::errstr\n";
42
43 # rename presidency field in tc_appointment to leader
44 $sth = $dbh->prepare("ALTER TABLE `tc_appointment` CHANGE `presidency` `leader` INT( 16 ) UNSIGNED NOT NULL DEFAULT '0'");
45 $sth->execute or die "-E- DB error: $DBI::errstr\n";
46
47 # rename presidency field in tc_leader to leader
48 $sth = $dbh->prepare("ALTER TABLE `tc_presidency` CHANGE `presidency` `leader` INT( 16 ) UNSIGNED NOT NULL AUTO_INCREMENT");
49 $sth->execute or die "-E- DB error: $DBI::errstr\n";
50
51 # rename tc_presidency to tc_leader
52 $sth = $dbh->prepare("RENAME TABLE `tc_presidency` TO `tc_leader`");
53 $sth->execute or die "-E- DB error: $DBI::errstr\n";
54
55 # add type field to tc_leader in place of president, counselor, secretary fields
56 $sth = $dbh->prepare("ALTER TABLE `tc_leader` ADD `type` ENUM( 'P', 'C', 'S', 'D' ) NOT NULL AFTER `email`");
57 $sth->execute or die "-E- DB error: $DBI::errstr\n";
58 $sth = $dbh->prepare("UPDATE tc_leader SET type='P' WHERE president=1");
59 $sth->execute or die "-E- DB error: $DBI::errstr\n";
60 $sth = $dbh->prepare("UPDATE tc_leader SET type='C' WHERE counselor=1");
61 $sth->execute or die "-E- DB error: $DBI::errstr\n";
62 $sth = $dbh->prepare("UPDATE tc_leader SET type='S' WHERE secretary=1");
63 $sth->execute or die "-E- DB error: $DBI::errstr\n";
64 $sth = $dbh->prepare("UPDATE tc_leader SET type='D' WHERE president=0 AND counselor=0 AND secretary=0");
65 $sth->execute or die "-E- DB error: $DBI::errstr\n";
66 $sth = $dbh->prepare("ALTER TABLE `tc_leader` DROP `president`, DROP `counselor`, DROP `secretary`");
67 $sth->execute or die "-E- DB error: $DBI::errstr\n";
68
69 # rename supervisor field in tc_district to leader
70 $sth = $dbh->prepare("ALTER TABLE `tc_district` CHANGE `supervisor` `leader` INT( 16 ) UNSIGNED NULL DEFAULT NULL");
71 $sth->execute or die "-E- DB error: $DBI::errstr\n";
72 $sth = $dbh->prepare("UPDATE tc_district AS td JOIN tc_leader AS tl SET td.leader=tl.leader WHERE td.leader=tl.individual AND td.district=tl.district AND tl.valid=1 AND td.valid=1");
73 $sth->execute or die "-E- DB error: $DBI::errstr\n";
74
75 # rename supervisor field in tc_district_sandbox to leader
76 $sth = $dbh->prepare("ALTER TABLE `tc_district_sandbox` CHANGE `supervisor` `leader` INT( 16 ) UNSIGNED NULL DEFAULT NULL");
77 $sth->execute or die "-E- DB error: $DBI::errstr\n";
78 $sth = $dbh->prepare("UPDATE tc_district_sandbox AS tdc JOIN tc_leader AS tl SET tdc.leader=tl.leader WHERE tdc.leader=tl.individual AND tdc.district=tl.district AND tl.valid=1");
79 $sth->execute or die "-E- DB error: $DBI::errstr\n";
80
81 # remove district field from tc_leader
82 $sth = $dbh->prepare("ALTER TABLE `tc_leader` DROP `district`");
83 $sth->execute or die "-E- DB error: $DBI::errstr\n";
84
85
86
87 print "-> Done!\n";
88
89 ###################################################
90 # Disconnect from the database
91 $dbh->disconnect();
92 ###################################################