7836351a9f890b1998f0334d55e15e4e1e1d3728
[eq/.git] / bin / upgrade_4_0_to_5_0
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 if( -f "$mydir/../setup/db_config.local") { require "db_config.local"; }
9 else { require "db_config"; }
10
11 ###################################################
12 # Connect to the database
13 $dbh=DBI->connect("dbi:mysql:dbname=$dbname:host=$dbhost:port=$dbport",$dbuser,$dbpass,{
14     AutoCommit=>0,
15     PrintError=>0}) or print "Connect Failure:".$DBI::errstr."\n" and exit 2;
16 ###################################################
17
18 # Rename eq_aaronic table to 3rd_aaronic
19 $sth = $dbh->prepare("RENAME TABLE `eq_aaronic` TO `3rd_aaronic`");
20 $sth->execute or die "-E- DB error: $DBI::errstr\n";
21
22 # Rename eq_activity table to 3rd_activity
23 $sth = $dbh->prepare("RENAME TABLE `eq_activity` TO `3rd_activity`");
24 $sth->execute or die "-E- DB error: $DBI::errstr\n";
25
26 # Rename eq_appointment table to 3rd_appointment
27 $sth = $dbh->prepare("RENAME TABLE `eq_appointment` TO `3rd_appointment`");
28 $sth->execute or die "-E- DB error: $DBI::errstr\n";
29
30 # Change elder field in 3rd_appointment table to indiv
31 $sth = $dbh->prepare("ALTER TABLE `3rd_appointment` CHANGE `elder` `indiv` INT(16)");
32 $sth->execute or die "-E- DB error: $DBI::errstr\n";
33
34 # Rename eq_assignment table to 3rd_assignment
35 $sth = $dbh->prepare("RENAME TABLE `eq_assignment` TO `3rd_assignment`");
36 $sth->execute or die "-E- DB error: $DBI::errstr\n";
37
38 # Rename eq_attendance table to 3rd_attendance
39 $sth = $dbh->prepare("RENAME TABLE `eq_attendance` TO `3rd_attendance`");
40 $sth->execute or die "-E- DB error: $DBI::errstr\n";
41
42 # Change elder field in 3rd_attendance table to indiv
43 $sth = $dbh->prepare("ALTER TABLE `3rd_attendance` CHANGE `elder` `indiv` INT(16)");
44 $sth->execute or die "-E- DB error: $DBI::errstr\n";
45
46 # Rename eq_calling table to 3rd_calling
47 $sth = $dbh->prepare("RENAME TABLE `eq_calling` TO `3rd_calling`");
48 $sth->execute or die "-E- DB error: $DBI::errstr\n";
49
50 # Rename eq_child table to 3rd_child
51 $sth = $dbh->prepare("RENAME TABLE `eq_child` TO `3rd_child`");
52 $sth->execute or die "-E- DB error: $DBI::errstr\n";
53
54 # Rename eq_companionship table to 3rd_companionship
55 $sth = $dbh->prepare("RENAME TABLE `eq_companionship` TO `3rd_companionship`");
56 $sth->execute or die "-E- DB error: $DBI::errstr\n";
57
58 # Change elder field in 3rd_companionship table to indiv
59 $sth = $dbh->prepare("ALTER TABLE `3rd_companionship` CHANGE `elder` `indiv` INT(16)");
60 $sth->execute or die "-E- DB error: $DBI::errstr\n";
61
62 # Rename eq_district table to 3rd_district
63 $sth = $dbh->prepare("RENAME TABLE `eq_district` TO `3rd_district`");
64 $sth->execute or die "-E- DB error: $DBI::errstr\n";
65
66 # Rename eq_elder table to 3rd_indiv
67 $sth = $dbh->prepare("RENAME TABLE `eq_elder` TO `3rd_indiv`");
68 $sth->execute or die "-E- DB error: $DBI::errstr\n";
69
70 # Add a new 'propective' field to the 3rd_indiv table
71 $sth = $dbh->prepare("ALTER TABLE `3rd_indiv` ADD `prospective` enum('y','n') NULL AFTER `priesthood`");
72 $sth->execute or die "-E- DB error: $DBI::errstr\n";
73
74 # Change elder field in 3rd_indiv table to indiv
75 $sth = $dbh->prepare("ALTER TABLE `3rd_indiv` CHANGE `elder` `indiv` INT(16)");
76 $sth->execute or die "-E- DB error: $DBI::errstr\n";
77
78 # Rename eq_family table to 3rd_family
79 $sth = $dbh->prepare("RENAME TABLE `eq_family` TO `3rd_family`");
80 $sth->execute or die "-E- DB error: $DBI::errstr\n";
81
82 # Change elder_id field in 3rd_family table to indiv_id
83 $sth = $dbh->prepare("ALTER TABLE `3rd_family` CHANGE `elder_id` `indiv_id` INT(16)");
84 $sth->execute or die "-E- DB error: $DBI::errstr\n";
85
86 # Rename eq_parent table to 3rd_parent
87 $sth = $dbh->prepare("RENAME TABLE `eq_parent` TO `3rd_parent`");
88 $sth->execute or die "-E- DB error: $DBI::errstr\n";
89
90 # Rename eq_participation table to 3rd_participation
91 $sth = $dbh->prepare("RENAME TABLE `eq_participation` TO `3rd_participation`");
92 $sth->execute or die "-E- DB error: $DBI::errstr\n";
93
94 # Change elder field in 3rd_participation table to indiv
95 $sth = $dbh->prepare("ALTER TABLE `3rd_participation` CHANGE `elder` `indiv` INT(16)");
96 $sth->execute or die "-E- DB error: $DBI::errstr\n";
97
98 # Rename eq_ppi table to 3rd_interview
99 $sth = $dbh->prepare("RENAME TABLE `eq_ppi` TO `3rd_interview`");
100 $sth->execute or die "-E- DB error: $DBI::errstr\n";
101
102 # Change elder field in 3rd_interview table to indiv
103 $sth = $dbh->prepare("ALTER TABLE `3rd_interview` CHANGE `elder` `indiv` INT(16)");
104 $sth->execute or die "-E- DB error: $DBI::errstr\n";
105
106 # Change ppi field in 3rd_interview table to interview
107 $sth = $dbh->prepare("ALTER TABLE `3rd_interview` CHANGE `ppi` `interview` INT(16)");
108 $sth->execute or die "-E- DB error: $DBI::errstr\n";
109
110 # Add a new 'interview_type' column to the 3rd_interview table
111 $sth = $dbh->prepare("ALTER TABLE `3rd_interview` ADD `interview_type` enum('hti','ppi') NOT NULL DEFAULT 'hti' AFTER `eqpresppi`");
112 $sth->execute or die "-E- DB error: $DBI::errstr\n";
113
114 # set all interviews to be ppi's if eqpresppi is 1
115 $sth = $dbh->prepare("UPDATE 3rd_interview SET interview_type='ppi' WHERE eqpresppi='1'");
116 $sth->execute or die "-E- DB error: $DBI::errstr\n";
117
118 # remove 'eqpresppi' from 3rd_interview
119 $sth = $dbh->prepare("ALTER TABLE `3rd_interview` DROP `eqpresppi`");
120 $sth->execute or die "-E- DB error: $DBI::errstr\n";
121
122 # Rename eq_presidency table to 3rd_presidency
123 $sth = $dbh->prepare("RENAME TABLE `eq_presidency` TO `3rd_presidency`");
124 $sth->execute or die "-E- DB error: $DBI::errstr\n";
125
126 # Change elder field in 3rd_presidency table to indiv
127 $sth = $dbh->prepare("ALTER TABLE `3rd_presidency` CHANGE `elder` `indiv` INT(16)");
128 $sth->execute or die "-E- DB error: $DBI::errstr\n";
129
130 # Rename eq_visit table to 3rd_visit
131 $sth = $dbh->prepare("RENAME TABLE `eq_visit` TO `3rd_visit`");
132 $sth->execute or die "-E- DB error: $DBI::errstr\n";
133
134 # Add a new 'companion1' column to the 3rd_visit table
135 $sth = $dbh->prepare("ALTER TABLE `3rd_visit` ADD `companion1` INT( 16 ) NULL AFTER `companionship`");
136 $sth->execute or die "-E- DB error: $DBI::errstr\n";
137
138 # Add a new 'companion2' column to the 3rd_visit table
139 $sth = $dbh->prepare("ALTER TABLE `3rd_visit` ADD `companion2` INT( 16 ) NULL AFTER `companion1`");
140 $sth->execute or die "-E- DB error: $DBI::errstr\n";
141
142 # Add a new 'visit_type' column to the 3rd_visit table
143 $sth = $dbh->prepare("ALTER TABLE `3rd_visit` ADD `visit_type` enum('presidency','hometeaching') NOT NULL DEFAULT 'hometeaching' AFTER `visited`");
144 $sth->execute or die "-E- DB error: $DBI::errstr\n";
145
146 # set all visits currently marked as companionship=0 with "type" "presidency" and all other visits should set to "hometeaching".
147 $sth = $dbh->prepare("update 3rd_visit set visit_type='presidency' where companionship='0'");
148 $sth->execute or die "-E- DB error: $DBI::errstr\n";
149
150 # Rename eq_willingness table to 3rd_willingness
151 $sth = $dbh->prepare("RENAME TABLE `eq_willingness` TO `3rd_willingness`");
152 $sth->execute or die "-E- DB error: $DBI::errstr\n";
153
154 # Change elder field in 3rd_willingness table to indiv
155 $sth = $dbh->prepare("ALTER TABLE `3rd_willingness` CHANGE `elder` `indiv` INT(16)");
156 $sth->execute or die "-E- DB error: $DBI::errstr\n";
157
158 print "-> Done!\n";
159
160 ###################################################
161 # Disconnect from the database
162 $dbh->disconnect();
163 ###################################################
164
165