Project

General

Profile

« Previous | Next » 

Revision 4312

inputs/CTFS/: Moved _scripts_to_drop_extra_tables to _archive because they are for a different version of the CTFS database than the extract we received (bci.sql)

View differences:

inputs/CTFS/_scripts_to_drop_extra_tables/create.sql
1
/* ***************** 2011 STRI DB 'CREATE' MySQL Script *****************
2
 *									*
3
 *		Most of the 2011 db is based off of the 2007 db.	*
4
 *		This script creates the new tables of the 2011 db	*
5
 *		and creates the new attributes of existing tables	*
6
 *		Nothing is deleted in this script. The drop.sql		*
7
 *		script will remove excess data and schema.		*
8
 *									*
9
 ***********************************************************************/
10
 
11
/* ***************************** Developers *****************************
12
 *									*
13
 *		Bradley University, Computer Science Dept.		*
14
 *		Smithsonian Tropical Research Institute			*
15
 *									*
16
 *			2011 Capstone Project				*
17
 *		Mario Campos, Michael Koeber, Zach Caschetta 		*
18
 *									*	
19
 ***********************************************************************/
20

  
21
-- Quadrat TABLE
22
ALTER TABLE Tree DROP FOREIGN KEY `Tree_ibfk_1`;
23
ALTER TABLE CensusQuadrat DROP FOREIGN KEY `CensusQuadrat_ibfk_2`;
24
ALTER TABLE Quadrat DROP PRIMARY KEY;
25
ALTER TABLE Quadrat CHANGE COLUMN QuadratID QuadratID2 INT UNSIGNED;
26
ALTER TABLE Quadrat ADD COLUMN (Area FLOAT UNSIGNED, IsStandardShape ENUM('Y','N') NOT NULL);
27
ALTER TABLE Quadrat ADD COLUMN QuadratID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY;
28

  
29
-- CensusQuadrat TABLE
30
ALTER TABLE DataEntry DROP FOREIGN KEY `DataEntry_ibfk_1`;
31
ALTER TABLE DataCollection DROP FOREIGN KEY `DataCollection_ibfk_1`;
32
ALTER TABLE CensusQuadrat CHANGE COLUMN QuadratID QuadratID2 INT UNSIGNED;
33
ALTER TABLE CensusQuadrat ADD COLUMN QuadratID INT UNSIGNED NOT NULL;
34
UPDATE CensusQuadrat CQ, Quadrat Q SET CQ.QuadratID=Q.QuadratID WHERE CQ.QuadratID2=Q.QuadratID2 AND CQ.PlotID=Q.PlotID;
35
ALTER TABLE CensusQuadrat DROP PRIMARY KEY;
36
ALTER TABLE CensusQuadrat ADD COLUMN CensusQuadratID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY;
37
ALTER TABLE CensusQuadrat ADD FOREIGN KEY (`QuadratID`) REFERENCES `Quadrat` (`QuadratID`);
38

  
39
-- DataCollection TABLE
40
ALTER TABLE DataCollection DROP PRIMARY KEY;
41
ALTER TABLE DataCollection ADD COLUMN DataCollectionID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY;
42
ALTER TABLE DataCollection ADD COLUMN PersonnelRoleID INT UNSIGNED NOT NULL;
43
ALTER TABLE DataCollection CHANGE COLUMN QuadratID QuadratID2 INT UNSIGNED;
44
ALTER TABLE DataCollection ADD COLUMN QuadratID INT UNSIGNED NOT NULL;
45
UPDATE DataCollection D, Quadrat Q SET D.QuadratID=Q.QuadratID WHERE D.QuadratID2=Q.QuadratID2;
46
ALTER TABLE DataCollection ADD FOREIGN KEY (`QuadratID`) REFERENCES `Quadrat` (`QuadratID`);
47
ALTER TABLE DataCollection ADD FOREIGN KEY (`CensusID`)  REFERENCES `Census` (`CensusID`);
48

  
49
-- PersonnelRole TABLE
50
CREATE TABLE PersonnelRole (PersonnelRoleID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
51
       	     		    PersonnelID SMALLINT UNSIGNED NOT NULL,
52
			    RoleID SMALLINT UNSIGNED NOT NULL
53
			   )ENGINE=INNODB;
54
INSERT INTO PersonnelRole (PersonnelID,RoleID) SELECT PersonnelID, RoleID FROM DataCollection;
55
ALTER TABLE PersonnelRole ADD FOREIGN KEY (RoleID) REFERENCES RoleReference(RoleID);
56
ALTER TABLE PersonnelRole ADD FOREIGN KEY (PersonnelID) REFERENCES Personnel(PersonnelID);
57
UPDATE DataCollection D, PersonnelRole P SET D.PersonnelRoleID=P.PersonnelRoleID WHERE D.PersonnelID=P.PersonnelID AND D.RoleID=P.RoleID;
58
ALTER TABLE DataCollection DROP FOREIGN KEY `DataCollection_ibfk_2`;
59
ALTER TABLE DataCollection DROP FOREIGN KEY `DataCollection_ibfk_3`;
60
ALTER TABLE DataCollection ADD FOREIGN KEY (`PersonnelRoleID`) REFERENCES `PersonnelRole` (`PersonnelRoleID`);
61

  
62
-- Tree TABLE
63
ALTER TABLE Tree CHANGE COLUMN QuadratID QuadratID2 INT UNSIGNED;
64
ALTER TABLE Tree ADD COLUMN QuadratID INT UNSIGNED;
65
UPDATE Tree T, Quadrat Q SET T.QuadratID=Q.QuadratID WHERE T.QuadratID2=Q.QuadratID2;
66

  
67
-- Stem TABLE
68
ALTER TABLE Measurement DROP FOREIGN KEY `Measurement_ibfk_2`;
69
-- ALTER TABLE Remeasurement DROP FOREIGN KEY to stem --FINISH THIS LINE
70
ALTER TABLE Stem DROP PRIMARY KEY;
71
ALTER TABLE Stem ADD COLUMN (QuadratID INT UNSIGNED NOT NULL,
72
      	    	     	     StemNumber INT UNSIGNED NOT NULL,
73
			     Moved ENUM('Y','N') NOT NULL,
74
			     GX FLOAT,
75
			     GY FLOAT,
76
			     GZ FLOAT,
77
			     PX FLOAT,
78
			     PY FLOAT,
79
			     PZ FLOAT,
80
			     QX FLOAT,
81
			     QY FLOAT,
82
			     QZ FLOAT
83
			    );
84
UPDATE Stem S, Tree T SET S.QuadratID=T.QuadratID WHERE S.TreeID=T.TreeID;
85
UPDATE Stem S1, Stem S2 SET S1.StemNumber=S2.StemID WHERE S1.StemID=S2.StemID AND S1.TreeID=S2.TreeID;
86
UPDATE Stem S, Tree T SET S.QX=T.x, S.QY=T.y WHERE S.TreeID=T.TreeID;
87
ALTER TABLE Stem DROP COLUMN StemID;
88
ALTER TABLE Stem ADD COLUMN StemID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST;
89

  
90
-- Measurement(DBH) TABLE
91
ALTER TABLE LogMeasurementHistory DROP FOREIGN KEY `LogMeasurementHistory_ibfk_1`;
92
ALTER TABLE MeasurementAttributes DROP FOREIGN KEY `MeasurementAttributes_ibfk_1`;
93
ALTER TABLE Measurement DROP PRIMARY KEY;
94
ALTER TABLE Measurement ADD COLUMN DBHID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY;
95
ALTER TABLE Measurement ADD COLUMN Comments VARCHAR(128);
96
ALTER TABLE Measurement MODIFY COLUMN HOM CHAR(16);
97
UPDATE Measurement M, Stem S SET M.StemID=S.StemID WHERE M.StemID=S.StemNumber AND M.TreeID=S.TreeID;
98
ALTER TABLE Measurement ADD FOREIGN KEY (`StemID`) REFERENCES `Stem` (`StemID`);
99

  
100
-- MeasurementAttributes TABLE
101
ALTER TABLE MeasurementAttributes ADD COLUMN DBHID INT UNSIGNED;
102
UPDATE MeasurementAttributes MA, Measurement M SET MA.DBHID=M.DBHID WHERE MA.MeasureID=M.MeasureID;
103
ALTER TABLE MeasurementAttributes ADD FOREIGN KEY (`DBHID`) REFERENCES `Measurement` (`DBHID`);
104
ALTER TABLE LogMAttrHistory DROP FOREIGN KEY `LogMAttrHistory_ibfk_1`;
105
ALTER TABLE MeasurementAttributes DROP PRIMARY KEY;
106
ALTER TABLE MeasurementAttributes ADD COLUMN DBHAttID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY;
107

  
108
-- Remeasurement TABLE
109
ALTER TABLE RemeasAttribs DROP FOREIGN KEY `RemeasAttribs_ibfk_1`;
110
ALTER TABLE Remeasurement DROP PRIMARY KEY;
111
ALTER TABLE Remeasurement CHANGE COLUMN RemeasureID RemeasureID2 INT UNSIGNED;
112
ALTER TABLE Remeasurement ADD COLUMN RemeasureID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY;
113
UPDATE Remeasurement R, Stem S SET R.StemID=S.StemID WHERE R.StemID=S.StemNumber AND R.TreeID=S.TreeID;
114
ALTER TABLE Remeasurement ADD FOREIGN KEY (`StemID`) REFERENCES `Stem` (`StemID`);
115

  
116
-- RemeasAttribs TABLE
117
ALTER TABLE RemeasAttribs CHANGE COLUMN RemeasureID RemeasureID2 INT UNSIGNED;
118
ALTER TABLE RemeasAttribs ADD COLUMN RemeasureID INT UNSIGNED NOT NULL;
119
UPDATE RemeasAttribs RA, Remeasurement R SET RA.RemeasureID=R.RemeasureID WHERE RA.RemeasureID2=R.RemeasureID2;
120
ALTER TABLE RemeasAttribs DROP PRIMARY KEY;
121
ALTER TABLE RemeasAttribs ADD COLUMN RmAttID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY;
122
ALTER TABLE RemeasAttribs ADD FOREIGN KEY (`RemeasureID`) REFERENCES `Remeasurement` (`RemeasureID`);
123

  
124
-- TreeAttributes TABLE
125
ALTER TABLE LogTreeAttrHistory DROP FOREIGN KEY `LogTreeAttrHistory_ibfk_1`;
126
ALTER TABLE TreeAttributes DROP PRIMARY KEY;
127
ALTER TABLE TreeAttributes ADD COLUMN TAttID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY;
128

  
129
-- rename tables
130
ALTER TABLE Measurement RENAME TO DBH;
131
ALTER TABLE MeasurementAttributes RENAME TO DBHAttributes;
132

  
133
-- add new tables
134
CREATE TABLE IF NOT EXISTS FeatureTypes (
135
					   FeatureTypeID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
136
					   Type VARCHAR(32) NOT NULL
137
					)  ENGINE=INNODB;
138

  
139
CREATE TABLE IF NOT EXISTS Features (
140
					FeatureID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
141
					FeatureTypeID INT UNSIGNED NOT NULL,
142
					Name VARCHAR(32) NOT NULL,
143
					ShortDescrip VARCHAR(32),
144
					LongDescrip VARCHAR(128),
145
					FOREIGN KEY (`FeatureTypeID`) REFERENCES `FeatureTypes` (`FeatureTypeID`)
146
				    )	ENGINE=INNODB;
147

  
148
CREATE TABLE IF NOT EXISTS Coordinates (
149
					  CoorID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
150
					  FeatureID INT UNSIGNED,
151
					  PlotID INT UNSIGNED,
152
					  QuadratID INT UNSIGNED,
153
					  GX FLOAT(8),
154
					  GY FLOAT(8),
155
					  GZ FLOAT(8),
156
					  PX FLOAT(8),
157
					  PY FLOAT(8),
158
					  PZ FLOAT(8),
159
					  QX FLOAT(8),
160
					  QY FLOAT(8),
161
					  QZ FLOAT(8),
162
					  CoordinateNo INT UNSIGNED,
163
					  FOREIGN KEY (`FeatureID`) REFERENCES `Features` (`FeatureID`),
164
					  FOREIGN KEY (`PlotID`) REFERENCES `Site` (`PlotID`),
165
					  FOREIGN KEY (`QuadratID`) REFERENCES `Quadrat` (`QuadratID`)
166
				       )  ENGINE=INNODB;
167

  
168
CREATE TABLE IF NOT EXISTS MeasurementType (
169
						MeasurementTypeID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
170
						UOM VARCHAR(32) NOT NULL,
171
						Type VARCHAR(256)
172
					   )	ENGINE=INNODB;
173

  
174
CREATE TABLE IF NOT EXISTS Measurement (
175
					MeasureID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
176
					CensusID INT UNSIGNED NOT NULL,
177
					TreeID INT UNSIGNED NOT NULL,
178
					StemID INT UNSIGNED NOT NULL,
179
					MeasurementTypeID INT UNSIGNED NOT NULL,
180
					Measure VARCHAR(256) NOT NULL,
181
					ExactDate DATE NOT NULL,
182
					Comments VARCHAR(128),
183
					FOREIGN KEY (`CensusID`) REFERENCES `Census` (`CensusID`),
184
					FOREIGN KEY (`TreeID`) REFERENCES `Tree` (`TreeID`),
185
					FOREIGN KEY (`StemID`) REFERENCES `Stem` (`StemID`),
186
					FOREIGN KEY (`MeasurementTypeID`) REFERENCES `MeasurementType` (`MeasurementTypeID`)
187
				       )ENGINE=INNODB;
188

  
189
CREATE TABLE IF NOT EXISTS MeasurementAttributes (
190
       	     	    	   			  MAttID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
191
						  MeasureID INT UNSIGNED NOT NULL,
192
						  CensusID INT UNSIGNED NOT NULL,
193
						  TSMID INT UNSIGNED NOT NULL,
194
						  FOREIGN KEY (`MeasureID`) REFERENCES `Measurement` (`MeasureID`),
195
						  FOREIGN KEY (`CensusID`) REFERENCES `Census` (`CensusID`),
196
						  FOREIGN KEY (`TSMID`) REFERENCES `TSMAttributes` (`TSMID`)
197
						 )ENGINE=INNODB;
198

  
199
CREATE TABLE IF NOT EXISTS Log (
200
				LogID BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
201
				PersonnelID SMALLINT UNSIGNED,
202
				ChangedTable VARCHAR(32) NOT NULL,
203
				PrimaryKey VARCHAR(32) NOT NULL,
204
				ChangedColumn VARCHAR(32) NOT NULL,
205
				ChangeDate DATE,
206
				ChangeTime TIMESTAMP,
207
				Description VARCHAR(256),
208
				Action ENUM('I','D','U') NOT NULL,
209
				Old VARCHAR(512) NOT NULL,
210
				New VARCHAR(512) NOT NULL,
211
				FOREIGN KEY (`PersonnelID`) REFERENCES `Personnel` (`PersonnelID`)
212
			       )ENGINE=INNODB;
213

  
214
-- modify existing tables to meet new schema's specs
215
ALTER TABLE Site ADD COLUMN (  Area INT UNSIGNED NOT NULL,
216
      	    	     	       QDimX INT UNSIGNED NOT NULL,
217
			       QDimY INT UNSIGNED NOT NULL,
218
			       GUOM VARCHAR(32) NOT NULL,
219
			       GZUOM VARCHAR(32) NOT NULL,
220
			       PUOM VARCHAR(32) NOT NULL,
221
			       QUOM VARCHAR(32) NOT NULL,
222
			       GCoorCollected VARCHAR(32),
223
			       PCoorCollected VARCHAR(32),
224
			       QCoorCollected VARCHAR(32)
225
			    ); 
226

  
227
ALTER TABLE Stem ADD COLUMN (	GX FLOAT(8),
228
				GY FLOAT(8),
229
				GZ FLOAT(8),
230
				PX FLOAT(8),
231
				PY FLOAT(8),
232
				PZ FLOAT(8),
233
				QX FLOAT(8),
234
				QY FLOAT(8),
235
				QZ FLOAT(8),
236
				Moved ENUM('Y','N') NOT NULL
237
			    );
238

  
239
ALTER TABLE Census MODIFY COLUMN PlotCensusNumber CHAR(16);
inputs/CTFS/_scripts_to_drop_extra_tables/drop.sql
1
/* ************ MySQL Database and Schema Converting Script *************
2
 *									*
3
 *		This script first replicates the old databases		*
4
 *		schema into a new database. The new database's		*
5
 *		schema is then modified to meet the new schema		*
6
 *		specifications. Finally, the the data of the 		*
7
 *		old database is copied into the new database		*
8
 *		and converted, if necessary.				*
9
 *									*
10
 ***********************************************************************/
11
 
12
/* ***************************** Developers *****************************
13
 *									*
14
 *		Bradley University, Computer Science Dept.		*
15
 *		Smithsonian Tropical Research Institute			*
16
 *									*
17
 *			2011 Capstone Project				*
18
 *		Mario Campos, Michael Koeber, Zach Caschetta 		*
19
 *									*	
20
 ***********************************************************************/
21

  
22
-- drop unnecessary columns
23
ALTER TABLE Site DROP COLUMN Latitude;
24
ALTER TABLE Site DROP COLUMN Longitude;
25
ALTER TABLE Site DROP COLUMN Elevation;
26
ALTER TABLE Site DROP COLUMN ReferenceX;
27
ALTER TABLE Site DROP COLUMN ReferenceY;
28
ALTER TABLE Quadrat DROP COLUMN DimX;
29
ALTER TABLE Quadrat DROP COLUMN DimY;
30
ALTER TABLE Quadrat DROP COLUMN StartX;
31
ALTER TABLE Quadrat DROP COLUMN StartY;
32
ALTER TABLE Quadrat DROP COLUMN QuadratID2;
33
ALTER TABLE CensusQuadrat DROP COLUMN QuadratID2;
34
ALTER TABLE CensusQuadrat DROP COLUMN PlotID;
35
ALTER TABLE DataCollection DROP COLUMN PlotID;
36
ALTER TABLE DataCollection DROP COLUMN PersonnelID;
37
ALTER TABLE DataCollection DROP COLUMN RoleID;
38
ALTER TABLE DataCollection DROP COLUMN QuadratID2;
39
ALTER TABLE Tree DROP COLUMN QuadratID2;
40
ALTER TABLE Tree DROP COLUMN QuadratID;
41
ALTER TABLE Tree DROP COLUMN PlotID;
42
ALTER TABLE Tree DROP COLUMN x;
43
ALTER TABLE Tree DROP COLUMN y;
44
ALTER TABLE DBH DROP COLUMN TreeID;
45
ALTER TABLE DBH DROP COLUMN MeasureID;
46
ALTER TABLE DBHAttributes DROP COLUMN MeasureID;
47
ALTER TABLE Remeasurement DROP FOREIGN KEY `Remeasurement_ibfk_2`;
48
ALTER TABLE Remeasurement DROP COLUMN TreeID;
49
ALTER TABLE Remeasurement DROP COLUMN RemeasureID2;
50
ALTER TABLE RemeasAttribs DROP COLUMN RemeasureID2;
51

  
52
-- drop unnecessary tables
53
DROP TABLE IF EXISTS DFTemp,
54
     	      	     LogMAttrHistory,
55
		     LogTreeHistory,
56
		     DataEntry,
57
		     LogStemHistory,
58
		     LogMeasurementHistory,
59
		     StageTargetNew,
60
		     StageTarget,
61
		     StageSource,
62
		     StageTaxonomicChanges,
63
		     LogTreeAttrHistory;
inputs/CTFS/_scripts_to_drop_extra_tables/migrate.pl
1
#!/usr/bin/perl -w -s
2

  
3
############################# migrate.pl ###############################
4
#								       #
5
#	This perl script creates the new 2011 db and migrates          #
6
#       from the old db by...                                          #
7
#	1. creates the new database using create.sql                   #
8
#	2. maps the data from the old db to the new db                 #
9
#	3. drops the excess data and schema                            #
10
#								       #
11
#       Usage: migrate.pl [options]                                    #
12
#       options:                                                       #
13
#         -olddb='<DATABASENAME>'    Name of the old STRI Database     #
14
#         -newdb='<DATABASENAME>'    Name of the new STRI Database     #
15
#         -user='<USERNAME>'         MySQL username                    #
16
#         -passwd='<PASSWORD>'       Password of the MySQL user        #
17
#         -cfile='<FILEPATH>'        Absolute path to create.sql       #
18
#         -dfile='<FILEPATH>'        Absolute path to drop.sql         #
19
#         -mysql='<FILEPATH>'        Absolute path to mysql            #
20
#         -mysqldump='<FILEPATH>'    Absolute path to mysqldump        #
21
#         -quiet                     Ignores errors                    #
22
#                                                                      #
23
########################################################################
24
 
25
############################## Developers ##############################
26
#								       #
27
#		Bradley University, Computer Science Dept.	       #
28
#		Smithsonian Tropical Research Institute		       #
29
#								       #
30
#			2011 Capstone Project			       #
31
#		Mario Campos, Michael Koeber, Zach Caschetta 	       #
32
#								       #
33
########################################################################
34

  
35
use strict;
36
use DBI;
37
use Fcntl;
38

  
39
our($olddb,$newdb,$user,$passwd,$cfile,$dfile,$mysql,$mysqldump,$quiet);
40
BEGIN {
41
    $olddb or print 'Enter the old database name:' and chomp($olddb=<>);
42
    $newdb or print 'Enter the new database name:' and chomp($newdb=<>);
43
    $user or print 'Enter the username:' and chomp($user=<>);
44
    $passwd or print 'Enter the password:' and chomp($passwd=<>);
45
    $cfile or print 'Enter the path of the sql create file:' and chomp($cfile=<>);
46
    $dfile or print 'Enter the path of the sql drop file:' and chomp($dfile=<>);
47
    $mysql or $mysql='mysql';
48
    $mysqldump or $mysqldump='mysqldump';
49
    sysopen FILE,'migrate.log',O_CREAT|O_EXCL|O_WRONLY,0755 or die $!;
50
}
51

  
52
# create new db
53
print "Creating `$newdb`...";
54
system "$mysql -u $user -p$passwd -e 'create database $newdb;'";
55
print "done!\n";
56

  
57
# dump old db
58
print "Dumping `$olddb`...";
59
system "$mysqldump --opt -u $user -p$passwd $olddb > $olddb";
60
print "done!\n";
61

  
62
# inserting data into new db
63
print "Undumping into `$newdb`...";
64
system "$mysql -u $user -p$passwd $newdb < $olddb";
65
print "done!\n";
66

  
67
# running create.sql
68
system "$mysql -v -u $user -p$passwd $newdb < $cfile";
69

  
70

  
71
# connect to MySQL
72
my $dbh = DBI->connect("DBI:mysql:database=$newdb;host=localhost",$user,$passwd);
73

  
74
#   create coordinates of plot points
75
my $sql = 'SELECT PlotID, Latitude, Longitude, Elevation FROM Site';
76
my $sth = $dbh->prepare("$sql");
77
$sth->execute;
78
my $tbl_ref = $sth->fetchall_hashref('PlotID');
79
foreach my $key (keys %$tbl_ref) {
80
    $sql = 'INSERT INTO Coordinates (PlotID,GX,GY';
81
    defined $tbl_ref->{$key}->{'Elevation'} and $sql.=',GZ';
82
    $sql.=") VALUES ($key,$tbl_ref->{$key}->{Longitude},$tbl_ref->{$key}->{Latitude}";
83
    defined $tbl_ref->{$key}->{'Elevation'} and $sql.=",$tbl_ref->{$key}->{'Elevation'}";
84
    $sql.=')';
85
    if($dbh->do($sql)) {
86
	print "$sql\n";
87
    } else {
88
	die "Error executing: $sql\n" unless defined $quiet;
89
	print FILE "Error executing: $sql\n";
90
    }
91
}
92

  
93
#   create coordinates of quadrat points
94
$sql = 'SELECT QuadratID, PlotID, StartX, StartY FROM Quadrat';
95
$sth = $dbh->prepare("$sql");
96
$sth->execute;
97
$tbl_ref = $sth->fetchall_hashref('QuadratID');
98
foreach my $key (keys %$tbl_ref) {
99
    $sql = "INSERT INTO Coordinates (QuadratID,PlotID,PX,PY) VALUES ($key,$tbl_ref->{$key}->{PlotID},$tbl_ref->{$key}->{StartX},$tbl_ref->{$key}->{StartY})";
100
    if($dbh->do($sql)) {
101
        print "$sql\n";
102
    } else {
103
        die "Error executing: $sql\n" unless defined $quiet;
104
        print FILE "Error executing: $sql\n";
105
    }
106
}
107

  
108
$sql = 'SELECT DISTINCT DimX, DimY, PlotID FROM Quadrat';
109
$sth = $dbh->prepare("$sql");
110
$sth->execute;
111
$tbl_ref = $sth->fetchall_hashref('PlotID');
112
foreach my $key (keys %$tbl_ref) {
113
    if(defined $tbl_ref->{$key}->{'DimX'} && defined $tbl_ref->{$key}->{'DimY'}) {
114
	$sql = "UPDATE Site SET QDimX=$tbl_ref->{$key}->{DimX}, QDimY=$tbl_ref->{$key}->{DimY} WHERE PlotID=$key";
115
	if($dbh->do($sql)) {
116
	    print "$sql\n";
117
	} else {
118
	    die "Error executing: $sql\n" unless defined $quiet;
119
	    print FILE "Error executing: $sql\n";
120
	}
121
    }
122
}
123

  
124
# LogTreeHistory
125
$sql = 'SELECT TreeID,ChangeDate,ChangeDescription,ChangeCodeID,QuadratID,PlotID,Tag,X,Y,SpeciesID,SubSpeciesID FROM LogTreeHistory ORDER BY ChangeDate';
126
$sth = $dbh->prepare("$sql");
127
$sth->execute;
128
my @row = ();
129
while( @row = $sth->fetchrow_array ) {
130
    $sql = "INSERT INTO Log (ChangedTable,PrimaryKey,";
131
    defined $row[1] and $sql .= 'ChangeDate,';
132
    defined $row[2] and $sql .= 'Description,';
133
    $sql .= "New) VALUES ('Tree',$row[0],";
134
    defined $row[1] and $sql .= "'$row[1]',";
135
    defined $row[2] and $sql .= "'$row[2]',";
136
    $sql .= "'ChangeCodeID=$row[3],QuadratID=$row[4],PlotID=$row[5],Tag=$row[6],X=$row[7],Y=$row[8],SpeciesID=$row[9],SubspeciesID=$row[10]')";
137
    if($dbh->do($sql)) {
138
	print "$sql\n";
139
    } else {
140
	die "Error executing: $sql\n" unless defined $quiet;
141
	print FILE "Error executing: $sql\n";
142
    }
143
}
144

  
145
# LogMeasurementHistory
146
$sql = 'SELECT MeasureID,CensusID,DateOfChange,DescriptionOfChange,StemID,TreeID,DBH,HOM,ExactDate FROM LogMeasurementHistory ORDER BY DateOfChange';
147
$sth = $dbh->prepare("$sql");
148
$sth->execute;
149
while( @row = $sth->fetchrow_array ) {
150
    $sql = "INSERT INTO Log (ChangedTable,PrimaryKey,";
151
    defined $row[2] and $sql .= 'ChangeDate,';
152
    defined $row[3] and $sql .= 'Description,';
153
    $sql .= "New) VALUES ('Measurement','MeasureID=$row[0],CensusID=$row[1]',";
154
    defined $row[2] and $sql .= "'$row[2]',";
155
    defined $row[3] and $sql .= "'$row[3]',";
156
    $sql .= "'StemID=$row[4],TreeID=$row[5],DBH=$row[6],HOM=$row[7],ExactDate=$row[8]')";
157

  
158
    if($dbh->do($sql)) {
159
	print "$sql\n";
160
    } else {
161
        die "Error executing: $sql\n" unless defined $quiet;
162
        print FILE "Error executing: $sql\n";
163
    }
164
}
165

  
166
# LogMAttrHistory
167
$sql = 'SELECT MeasureID,CensusID,TSMID,DateOfChange,DescriptionOfChange FROM LogMAttrHistory ORDER BY DateOfChange';
168
$sth = $dbh->prepare($sql);
169
$sth->execute;
170
while( @row = $sth->fetchrow_array ) {
171
    $sql = "INSERT INTO Log (ChangedTable,PrimaryKey,";
172
    defined $row[3] and $sql .= 'ChangeDate,';
173
    defined $row[4] and $sql .= 'Description,';
174
    $sql .= "New) VALUES ('MeasurementAttributes','MeasureID=$row[0],CensusID=$row[1],TSMID=$row[2]',";
175
    defined $row[3] and $sql .= "'$row[3]',";
176
    defined $row[4] and $sql .= "'$row[4]',";
177
    $sql .= "'MeasureID=$row[0],CensusID=$row[1],TSMID=$row[2]')";
178

  
179
    if($dbh->do($sql)) {
180
        print "$sql\n";
181
    } else {
182
        die "Error executing: $sql\n" unless defined $quiet;
183
        print FILE "Error executing: $sql\n";
184
    }
185
}
186

  
187
# LogTreeAttrHistory
188
$sql = 'SELECT CensusID,TreeID,TSMID,DateOfChange,DescriptionOfChange FROM LogTreeAttrHistory ORDER BY DateOfChange';
189
$sth = $dbh->prepare("$sql");
190
$sth->execute;
191
while( @row = $sth->fetchrow_array ) {
192
    $sql = "INSERT INTO Log (ChangedTable,PrimaryKey,";
193
    defined $row[3] and $sql .= 'ChangeDate,';
194
    defined $row[4] and $sql .= 'Description,';
195
    $sql .= "New) VALUES ('TreeAttributes','CensusID=$row[0],TreeID=$row[1],TSMID=$row[2]',";
196
    defined $row[3] and $sql .= "'$row[3]',";
197
    defined $row[4] and $sql .= "'$row[4]',";
198
    $sql .= "'CensusID=$row[0],TreeID=$row[1],TSMID=$row[2]')";
199

  
200
    if($dbh->do($sql)) {
201
        print "$sql\n";
202
    } else {
203
        die "Error executing: $sql\n" unless defined $quiet;
204
        print FILE "Error executing: $sql\n";
205
    }
206
}
207

  
208
# execute drop.sql
209
system "$mysql -v -u $user -p$passwd $newdb < $dfile" or die $!;
210

  
211
close FILE or die $!;
inputs/CTFS/_archive/scripts_to_drop_extra_tables/create.sql
1
/* ***************** 2011 STRI DB 'CREATE' MySQL Script *****************
2
 *									*
3
 *		Most of the 2011 db is based off of the 2007 db.	*
4
 *		This script creates the new tables of the 2011 db	*
5
 *		and creates the new attributes of existing tables	*
6
 *		Nothing is deleted in this script. The drop.sql		*
7
 *		script will remove excess data and schema.		*
8
 *									*
9
 ***********************************************************************/
10
 
11
/* ***************************** Developers *****************************
12
 *									*
13
 *		Bradley University, Computer Science Dept.		*
14
 *		Smithsonian Tropical Research Institute			*
15
 *									*
16
 *			2011 Capstone Project				*
17
 *		Mario Campos, Michael Koeber, Zach Caschetta 		*
18
 *									*	
19
 ***********************************************************************/
20

  
21
-- Quadrat TABLE
22
ALTER TABLE Tree DROP FOREIGN KEY `Tree_ibfk_1`;
23
ALTER TABLE CensusQuadrat DROP FOREIGN KEY `CensusQuadrat_ibfk_2`;
24
ALTER TABLE Quadrat DROP PRIMARY KEY;
25
ALTER TABLE Quadrat CHANGE COLUMN QuadratID QuadratID2 INT UNSIGNED;
26
ALTER TABLE Quadrat ADD COLUMN (Area FLOAT UNSIGNED, IsStandardShape ENUM('Y','N') NOT NULL);
27
ALTER TABLE Quadrat ADD COLUMN QuadratID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY;
28

  
29
-- CensusQuadrat TABLE
30
ALTER TABLE DataEntry DROP FOREIGN KEY `DataEntry_ibfk_1`;
31
ALTER TABLE DataCollection DROP FOREIGN KEY `DataCollection_ibfk_1`;
32
ALTER TABLE CensusQuadrat CHANGE COLUMN QuadratID QuadratID2 INT UNSIGNED;
33
ALTER TABLE CensusQuadrat ADD COLUMN QuadratID INT UNSIGNED NOT NULL;
34
UPDATE CensusQuadrat CQ, Quadrat Q SET CQ.QuadratID=Q.QuadratID WHERE CQ.QuadratID2=Q.QuadratID2 AND CQ.PlotID=Q.PlotID;
35
ALTER TABLE CensusQuadrat DROP PRIMARY KEY;
36
ALTER TABLE CensusQuadrat ADD COLUMN CensusQuadratID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY;
37
ALTER TABLE CensusQuadrat ADD FOREIGN KEY (`QuadratID`) REFERENCES `Quadrat` (`QuadratID`);
38

  
39
-- DataCollection TABLE
40
ALTER TABLE DataCollection DROP PRIMARY KEY;
41
ALTER TABLE DataCollection ADD COLUMN DataCollectionID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY;
42
ALTER TABLE DataCollection ADD COLUMN PersonnelRoleID INT UNSIGNED NOT NULL;
43
ALTER TABLE DataCollection CHANGE COLUMN QuadratID QuadratID2 INT UNSIGNED;
44
ALTER TABLE DataCollection ADD COLUMN QuadratID INT UNSIGNED NOT NULL;
45
UPDATE DataCollection D, Quadrat Q SET D.QuadratID=Q.QuadratID WHERE D.QuadratID2=Q.QuadratID2;
46
ALTER TABLE DataCollection ADD FOREIGN KEY (`QuadratID`) REFERENCES `Quadrat` (`QuadratID`);
47
ALTER TABLE DataCollection ADD FOREIGN KEY (`CensusID`)  REFERENCES `Census` (`CensusID`);
48

  
49
-- PersonnelRole TABLE
50
CREATE TABLE PersonnelRole (PersonnelRoleID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
51
       	     		    PersonnelID SMALLINT UNSIGNED NOT NULL,
52
			    RoleID SMALLINT UNSIGNED NOT NULL
53
			   )ENGINE=INNODB;
54
INSERT INTO PersonnelRole (PersonnelID,RoleID) SELECT PersonnelID, RoleID FROM DataCollection;
55
ALTER TABLE PersonnelRole ADD FOREIGN KEY (RoleID) REFERENCES RoleReference(RoleID);
56
ALTER TABLE PersonnelRole ADD FOREIGN KEY (PersonnelID) REFERENCES Personnel(PersonnelID);
57
UPDATE DataCollection D, PersonnelRole P SET D.PersonnelRoleID=P.PersonnelRoleID WHERE D.PersonnelID=P.PersonnelID AND D.RoleID=P.RoleID;
58
ALTER TABLE DataCollection DROP FOREIGN KEY `DataCollection_ibfk_2`;
59
ALTER TABLE DataCollection DROP FOREIGN KEY `DataCollection_ibfk_3`;
60
ALTER TABLE DataCollection ADD FOREIGN KEY (`PersonnelRoleID`) REFERENCES `PersonnelRole` (`PersonnelRoleID`);
61

  
62
-- Tree TABLE
63
ALTER TABLE Tree CHANGE COLUMN QuadratID QuadratID2 INT UNSIGNED;
64
ALTER TABLE Tree ADD COLUMN QuadratID INT UNSIGNED;
65
UPDATE Tree T, Quadrat Q SET T.QuadratID=Q.QuadratID WHERE T.QuadratID2=Q.QuadratID2;
66

  
67
-- Stem TABLE
68
ALTER TABLE Measurement DROP FOREIGN KEY `Measurement_ibfk_2`;
69
-- ALTER TABLE Remeasurement DROP FOREIGN KEY to stem --FINISH THIS LINE
70
ALTER TABLE Stem DROP PRIMARY KEY;
71
ALTER TABLE Stem ADD COLUMN (QuadratID INT UNSIGNED NOT NULL,
72
      	    	     	     StemNumber INT UNSIGNED NOT NULL,
73
			     Moved ENUM('Y','N') NOT NULL,
74
			     GX FLOAT,
75
			     GY FLOAT,
76
			     GZ FLOAT,
77
			     PX FLOAT,
78
			     PY FLOAT,
79
			     PZ FLOAT,
80
			     QX FLOAT,
81
			     QY FLOAT,
82
			     QZ FLOAT
83
			    );
84
UPDATE Stem S, Tree T SET S.QuadratID=T.QuadratID WHERE S.TreeID=T.TreeID;
85
UPDATE Stem S1, Stem S2 SET S1.StemNumber=S2.StemID WHERE S1.StemID=S2.StemID AND S1.TreeID=S2.TreeID;
86
UPDATE Stem S, Tree T SET S.QX=T.x, S.QY=T.y WHERE S.TreeID=T.TreeID;
87
ALTER TABLE Stem DROP COLUMN StemID;
88
ALTER TABLE Stem ADD COLUMN StemID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST;
89

  
90
-- Measurement(DBH) TABLE
91
ALTER TABLE LogMeasurementHistory DROP FOREIGN KEY `LogMeasurementHistory_ibfk_1`;
92
ALTER TABLE MeasurementAttributes DROP FOREIGN KEY `MeasurementAttributes_ibfk_1`;
93
ALTER TABLE Measurement DROP PRIMARY KEY;
94
ALTER TABLE Measurement ADD COLUMN DBHID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY;
95
ALTER TABLE Measurement ADD COLUMN Comments VARCHAR(128);
96
ALTER TABLE Measurement MODIFY COLUMN HOM CHAR(16);
97
UPDATE Measurement M, Stem S SET M.StemID=S.StemID WHERE M.StemID=S.StemNumber AND M.TreeID=S.TreeID;
98
ALTER TABLE Measurement ADD FOREIGN KEY (`StemID`) REFERENCES `Stem` (`StemID`);
99

  
100
-- MeasurementAttributes TABLE
101
ALTER TABLE MeasurementAttributes ADD COLUMN DBHID INT UNSIGNED;
102
UPDATE MeasurementAttributes MA, Measurement M SET MA.DBHID=M.DBHID WHERE MA.MeasureID=M.MeasureID;
103
ALTER TABLE MeasurementAttributes ADD FOREIGN KEY (`DBHID`) REFERENCES `Measurement` (`DBHID`);
104
ALTER TABLE LogMAttrHistory DROP FOREIGN KEY `LogMAttrHistory_ibfk_1`;
105
ALTER TABLE MeasurementAttributes DROP PRIMARY KEY;
106
ALTER TABLE MeasurementAttributes ADD COLUMN DBHAttID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY;
107

  
108
-- Remeasurement TABLE
109
ALTER TABLE RemeasAttribs DROP FOREIGN KEY `RemeasAttribs_ibfk_1`;
110
ALTER TABLE Remeasurement DROP PRIMARY KEY;
111
ALTER TABLE Remeasurement CHANGE COLUMN RemeasureID RemeasureID2 INT UNSIGNED;
112
ALTER TABLE Remeasurement ADD COLUMN RemeasureID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY;
113
UPDATE Remeasurement R, Stem S SET R.StemID=S.StemID WHERE R.StemID=S.StemNumber AND R.TreeID=S.TreeID;
114
ALTER TABLE Remeasurement ADD FOREIGN KEY (`StemID`) REFERENCES `Stem` (`StemID`);
115

  
116
-- RemeasAttribs TABLE
117
ALTER TABLE RemeasAttribs CHANGE COLUMN RemeasureID RemeasureID2 INT UNSIGNED;
118
ALTER TABLE RemeasAttribs ADD COLUMN RemeasureID INT UNSIGNED NOT NULL;
119
UPDATE RemeasAttribs RA, Remeasurement R SET RA.RemeasureID=R.RemeasureID WHERE RA.RemeasureID2=R.RemeasureID2;
120
ALTER TABLE RemeasAttribs DROP PRIMARY KEY;
121
ALTER TABLE RemeasAttribs ADD COLUMN RmAttID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY;
122
ALTER TABLE RemeasAttribs ADD FOREIGN KEY (`RemeasureID`) REFERENCES `Remeasurement` (`RemeasureID`);
123

  
124
-- TreeAttributes TABLE
125
ALTER TABLE LogTreeAttrHistory DROP FOREIGN KEY `LogTreeAttrHistory_ibfk_1`;
126
ALTER TABLE TreeAttributes DROP PRIMARY KEY;
127
ALTER TABLE TreeAttributes ADD COLUMN TAttID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY;
128

  
129
-- rename tables
130
ALTER TABLE Measurement RENAME TO DBH;
131
ALTER TABLE MeasurementAttributes RENAME TO DBHAttributes;
132

  
133
-- add new tables
134
CREATE TABLE IF NOT EXISTS FeatureTypes (
135
					   FeatureTypeID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
136
					   Type VARCHAR(32) NOT NULL
137
					)  ENGINE=INNODB;
138

  
139
CREATE TABLE IF NOT EXISTS Features (
140
					FeatureID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
141
					FeatureTypeID INT UNSIGNED NOT NULL,
142
					Name VARCHAR(32) NOT NULL,
143
					ShortDescrip VARCHAR(32),
144
					LongDescrip VARCHAR(128),
145
					FOREIGN KEY (`FeatureTypeID`) REFERENCES `FeatureTypes` (`FeatureTypeID`)
146
				    )	ENGINE=INNODB;
147

  
148
CREATE TABLE IF NOT EXISTS Coordinates (
149
					  CoorID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
150
					  FeatureID INT UNSIGNED,
151
					  PlotID INT UNSIGNED,
152
					  QuadratID INT UNSIGNED,
153
					  GX FLOAT(8),
154
					  GY FLOAT(8),
155
					  GZ FLOAT(8),
156
					  PX FLOAT(8),
157
					  PY FLOAT(8),
158
					  PZ FLOAT(8),
159
					  QX FLOAT(8),
160
					  QY FLOAT(8),
161
					  QZ FLOAT(8),
162
					  CoordinateNo INT UNSIGNED,
163
					  FOREIGN KEY (`FeatureID`) REFERENCES `Features` (`FeatureID`),
164
					  FOREIGN KEY (`PlotID`) REFERENCES `Site` (`PlotID`),
165
					  FOREIGN KEY (`QuadratID`) REFERENCES `Quadrat` (`QuadratID`)
166
				       )  ENGINE=INNODB;
167

  
168
CREATE TABLE IF NOT EXISTS MeasurementType (
169
						MeasurementTypeID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
170
						UOM VARCHAR(32) NOT NULL,
171
						Type VARCHAR(256)
172
					   )	ENGINE=INNODB;
173

  
174
CREATE TABLE IF NOT EXISTS Measurement (
175
					MeasureID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
176
					CensusID INT UNSIGNED NOT NULL,
177
					TreeID INT UNSIGNED NOT NULL,
178
					StemID INT UNSIGNED NOT NULL,
179
					MeasurementTypeID INT UNSIGNED NOT NULL,
180
					Measure VARCHAR(256) NOT NULL,
181
					ExactDate DATE NOT NULL,
182
					Comments VARCHAR(128),
183
					FOREIGN KEY (`CensusID`) REFERENCES `Census` (`CensusID`),
184
					FOREIGN KEY (`TreeID`) REFERENCES `Tree` (`TreeID`),
185
					FOREIGN KEY (`StemID`) REFERENCES `Stem` (`StemID`),
186
					FOREIGN KEY (`MeasurementTypeID`) REFERENCES `MeasurementType` (`MeasurementTypeID`)
187
				       )ENGINE=INNODB;
188

  
189
CREATE TABLE IF NOT EXISTS MeasurementAttributes (
190
       	     	    	   			  MAttID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
191
						  MeasureID INT UNSIGNED NOT NULL,
192
						  CensusID INT UNSIGNED NOT NULL,
193
						  TSMID INT UNSIGNED NOT NULL,
194
						  FOREIGN KEY (`MeasureID`) REFERENCES `Measurement` (`MeasureID`),
195
						  FOREIGN KEY (`CensusID`) REFERENCES `Census` (`CensusID`),
196
						  FOREIGN KEY (`TSMID`) REFERENCES `TSMAttributes` (`TSMID`)
197
						 )ENGINE=INNODB;
198

  
199
CREATE TABLE IF NOT EXISTS Log (
200
				LogID BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
201
				PersonnelID SMALLINT UNSIGNED,
202
				ChangedTable VARCHAR(32) NOT NULL,
203
				PrimaryKey VARCHAR(32) NOT NULL,
204
				ChangedColumn VARCHAR(32) NOT NULL,
205
				ChangeDate DATE,
206
				ChangeTime TIMESTAMP,
207
				Description VARCHAR(256),
208
				Action ENUM('I','D','U') NOT NULL,
209
				Old VARCHAR(512) NOT NULL,
210
				New VARCHAR(512) NOT NULL,
211
				FOREIGN KEY (`PersonnelID`) REFERENCES `Personnel` (`PersonnelID`)
212
			       )ENGINE=INNODB;
213

  
214
-- modify existing tables to meet new schema's specs
215
ALTER TABLE Site ADD COLUMN (  Area INT UNSIGNED NOT NULL,
216
      	    	     	       QDimX INT UNSIGNED NOT NULL,
217
			       QDimY INT UNSIGNED NOT NULL,
218
			       GUOM VARCHAR(32) NOT NULL,
219
			       GZUOM VARCHAR(32) NOT NULL,
220
			       PUOM VARCHAR(32) NOT NULL,
221
			       QUOM VARCHAR(32) NOT NULL,
222
			       GCoorCollected VARCHAR(32),
223
			       PCoorCollected VARCHAR(32),
224
			       QCoorCollected VARCHAR(32)
225
			    ); 
226

  
227
ALTER TABLE Stem ADD COLUMN (	GX FLOAT(8),
228
				GY FLOAT(8),
229
				GZ FLOAT(8),
230
				PX FLOAT(8),
231
				PY FLOAT(8),
232
				PZ FLOAT(8),
233
				QX FLOAT(8),
234
				QY FLOAT(8),
235
				QZ FLOAT(8),
236
				Moved ENUM('Y','N') NOT NULL
237
			    );
238

  
239
ALTER TABLE Census MODIFY COLUMN PlotCensusNumber CHAR(16);
inputs/CTFS/_archive/scripts_to_drop_extra_tables/drop.sql
1
/* ************ MySQL Database and Schema Converting Script *************
2
 *									*
3
 *		This script first replicates the old databases		*
4
 *		schema into a new database. The new database's		*
5
 *		schema is then modified to meet the new schema		*
6
 *		specifications. Finally, the the data of the 		*
7
 *		old database is copied into the new database		*
8
 *		and converted, if necessary.				*
9
 *									*
10
 ***********************************************************************/
11
 
12
/* ***************************** Developers *****************************
13
 *									*
14
 *		Bradley University, Computer Science Dept.		*
15
 *		Smithsonian Tropical Research Institute			*
16
 *									*
17
 *			2011 Capstone Project				*
18
 *		Mario Campos, Michael Koeber, Zach Caschetta 		*
19
 *									*	
20
 ***********************************************************************/
21

  
22
-- drop unnecessary columns
23
ALTER TABLE Site DROP COLUMN Latitude;
24
ALTER TABLE Site DROP COLUMN Longitude;
25
ALTER TABLE Site DROP COLUMN Elevation;
26
ALTER TABLE Site DROP COLUMN ReferenceX;
27
ALTER TABLE Site DROP COLUMN ReferenceY;
28
ALTER TABLE Quadrat DROP COLUMN DimX;
29
ALTER TABLE Quadrat DROP COLUMN DimY;
30
ALTER TABLE Quadrat DROP COLUMN StartX;
31
ALTER TABLE Quadrat DROP COLUMN StartY;
32
ALTER TABLE Quadrat DROP COLUMN QuadratID2;
33
ALTER TABLE CensusQuadrat DROP COLUMN QuadratID2;
34
ALTER TABLE CensusQuadrat DROP COLUMN PlotID;
35
ALTER TABLE DataCollection DROP COLUMN PlotID;
36
ALTER TABLE DataCollection DROP COLUMN PersonnelID;
37
ALTER TABLE DataCollection DROP COLUMN RoleID;
38
ALTER TABLE DataCollection DROP COLUMN QuadratID2;
39
ALTER TABLE Tree DROP COLUMN QuadratID2;
40
ALTER TABLE Tree DROP COLUMN QuadratID;
41
ALTER TABLE Tree DROP COLUMN PlotID;
42
ALTER TABLE Tree DROP COLUMN x;
43
ALTER TABLE Tree DROP COLUMN y;
44
ALTER TABLE DBH DROP COLUMN TreeID;
45
ALTER TABLE DBH DROP COLUMN MeasureID;
46
ALTER TABLE DBHAttributes DROP COLUMN MeasureID;
47
ALTER TABLE Remeasurement DROP FOREIGN KEY `Remeasurement_ibfk_2`;
48
ALTER TABLE Remeasurement DROP COLUMN TreeID;
49
ALTER TABLE Remeasurement DROP COLUMN RemeasureID2;
50
ALTER TABLE RemeasAttribs DROP COLUMN RemeasureID2;
51

  
52
-- drop unnecessary tables
53
DROP TABLE IF EXISTS DFTemp,
54
     	      	     LogMAttrHistory,
55
		     LogTreeHistory,
56
		     DataEntry,
57
		     LogStemHistory,
58
		     LogMeasurementHistory,
59
		     StageTargetNew,
60
		     StageTarget,
61
		     StageSource,
62
		     StageTaxonomicChanges,
63
		     LogTreeAttrHistory;
inputs/CTFS/_archive/scripts_to_drop_extra_tables/migrate.pl
1
#!/usr/bin/perl -w -s
2

  
3
############################# migrate.pl ###############################
4
#								       #
5
#	This perl script creates the new 2011 db and migrates          #
6
#       from the old db by...                                          #
7
#	1. creates the new database using create.sql                   #
8
#	2. maps the data from the old db to the new db                 #
9
#	3. drops the excess data and schema                            #
10
#								       #
11
#       Usage: migrate.pl [options]                                    #
12
#       options:                                                       #
13
#         -olddb='<DATABASENAME>'    Name of the old STRI Database     #
14
#         -newdb='<DATABASENAME>'    Name of the new STRI Database     #
15
#         -user='<USERNAME>'         MySQL username                    #
16
#         -passwd='<PASSWORD>'       Password of the MySQL user        #
17
#         -cfile='<FILEPATH>'        Absolute path to create.sql       #
18
#         -dfile='<FILEPATH>'        Absolute path to drop.sql         #
19
#         -mysql='<FILEPATH>'        Absolute path to mysql            #
20
#         -mysqldump='<FILEPATH>'    Absolute path to mysqldump        #
21
#         -quiet                     Ignores errors                    #
22
#                                                                      #
23
########################################################################
24
 
25
############################## Developers ##############################
26
#								       #
27
#		Bradley University, Computer Science Dept.	       #
28
#		Smithsonian Tropical Research Institute		       #
29
#								       #
30
#			2011 Capstone Project			       #
31
#		Mario Campos, Michael Koeber, Zach Caschetta 	       #
32
#								       #
33
########################################################################
34

  
35
use strict;
36
use DBI;
37
use Fcntl;
38

  
39
our($olddb,$newdb,$user,$passwd,$cfile,$dfile,$mysql,$mysqldump,$quiet);
40
BEGIN {
41
    $olddb or print 'Enter the old database name:' and chomp($olddb=<>);
42
    $newdb or print 'Enter the new database name:' and chomp($newdb=<>);
43
    $user or print 'Enter the username:' and chomp($user=<>);
44
    $passwd or print 'Enter the password:' and chomp($passwd=<>);
45
    $cfile or print 'Enter the path of the sql create file:' and chomp($cfile=<>);
46
    $dfile or print 'Enter the path of the sql drop file:' and chomp($dfile=<>);
47
    $mysql or $mysql='mysql';
48
    $mysqldump or $mysqldump='mysqldump';
49
    sysopen FILE,'migrate.log',O_CREAT|O_EXCL|O_WRONLY,0755 or die $!;
50
}
51

  
52
# create new db
53
print "Creating `$newdb`...";
54
system "$mysql -u $user -p$passwd -e 'create database $newdb;'";
55
print "done!\n";
56

  
57
# dump old db
58
print "Dumping `$olddb`...";
59
system "$mysqldump --opt -u $user -p$passwd $olddb > $olddb";
60
print "done!\n";
61

  
62
# inserting data into new db
63
print "Undumping into `$newdb`...";
64
system "$mysql -u $user -p$passwd $newdb < $olddb";
65
print "done!\n";
66

  
67
# running create.sql
68
system "$mysql -v -u $user -p$passwd $newdb < $cfile";
69

  
70

  
71
# connect to MySQL
72
my $dbh = DBI->connect("DBI:mysql:database=$newdb;host=localhost",$user,$passwd);
73

  
74
#   create coordinates of plot points
75
my $sql = 'SELECT PlotID, Latitude, Longitude, Elevation FROM Site';
76
my $sth = $dbh->prepare("$sql");
77
$sth->execute;
78
my $tbl_ref = $sth->fetchall_hashref('PlotID');
79
foreach my $key (keys %$tbl_ref) {
80
    $sql = 'INSERT INTO Coordinates (PlotID,GX,GY';
81
    defined $tbl_ref->{$key}->{'Elevation'} and $sql.=',GZ';
82
    $sql.=") VALUES ($key,$tbl_ref->{$key}->{Longitude},$tbl_ref->{$key}->{Latitude}";
83
    defined $tbl_ref->{$key}->{'Elevation'} and $sql.=",$tbl_ref->{$key}->{'Elevation'}";
84
    $sql.=')';
85
    if($dbh->do($sql)) {
86
	print "$sql\n";
87
    } else {
88
	die "Error executing: $sql\n" unless defined $quiet;
89
	print FILE "Error executing: $sql\n";
90
    }
91
}
92

  
93
#   create coordinates of quadrat points
94
$sql = 'SELECT QuadratID, PlotID, StartX, StartY FROM Quadrat';
95
$sth = $dbh->prepare("$sql");
96
$sth->execute;
97
$tbl_ref = $sth->fetchall_hashref('QuadratID');
98
foreach my $key (keys %$tbl_ref) {
99
    $sql = "INSERT INTO Coordinates (QuadratID,PlotID,PX,PY) VALUES ($key,$tbl_ref->{$key}->{PlotID},$tbl_ref->{$key}->{StartX},$tbl_ref->{$key}->{StartY})";
100
    if($dbh->do($sql)) {
101
        print "$sql\n";
102
    } else {
103
        die "Error executing: $sql\n" unless defined $quiet;
104
        print FILE "Error executing: $sql\n";
105
    }
106
}
107

  
108
$sql = 'SELECT DISTINCT DimX, DimY, PlotID FROM Quadrat';
109
$sth = $dbh->prepare("$sql");
110
$sth->execute;
111
$tbl_ref = $sth->fetchall_hashref('PlotID');
112
foreach my $key (keys %$tbl_ref) {
113
    if(defined $tbl_ref->{$key}->{'DimX'} && defined $tbl_ref->{$key}->{'DimY'}) {
114
	$sql = "UPDATE Site SET QDimX=$tbl_ref->{$key}->{DimX}, QDimY=$tbl_ref->{$key}->{DimY} WHERE PlotID=$key";
115
	if($dbh->do($sql)) {
116
	    print "$sql\n";
117
	} else {
118
	    die "Error executing: $sql\n" unless defined $quiet;
119
	    print FILE "Error executing: $sql\n";
120
	}
121
    }
122
}
123

  
124
# LogTreeHistory
125
$sql = 'SELECT TreeID,ChangeDate,ChangeDescription,ChangeCodeID,QuadratID,PlotID,Tag,X,Y,SpeciesID,SubSpeciesID FROM LogTreeHistory ORDER BY ChangeDate';
126
$sth = $dbh->prepare("$sql");
127
$sth->execute;
128
my @row = ();
129
while( @row = $sth->fetchrow_array ) {
130
    $sql = "INSERT INTO Log (ChangedTable,PrimaryKey,";
131
    defined $row[1] and $sql .= 'ChangeDate,';
132
    defined $row[2] and $sql .= 'Description,';
133
    $sql .= "New) VALUES ('Tree',$row[0],";
134
    defined $row[1] and $sql .= "'$row[1]',";
135
    defined $row[2] and $sql .= "'$row[2]',";
136
    $sql .= "'ChangeCodeID=$row[3],QuadratID=$row[4],PlotID=$row[5],Tag=$row[6],X=$row[7],Y=$row[8],SpeciesID=$row[9],SubspeciesID=$row[10]')";
137
    if($dbh->do($sql)) {
138
	print "$sql\n";
139
    } else {
140
	die "Error executing: $sql\n" unless defined $quiet;
141
	print FILE "Error executing: $sql\n";
142
    }
143
}
144

  
145
# LogMeasurementHistory
146
$sql = 'SELECT MeasureID,CensusID,DateOfChange,DescriptionOfChange,StemID,TreeID,DBH,HOM,ExactDate FROM LogMeasurementHistory ORDER BY DateOfChange';
147
$sth = $dbh->prepare("$sql");
148
$sth->execute;
149
while( @row = $sth->fetchrow_array ) {
150
    $sql = "INSERT INTO Log (ChangedTable,PrimaryKey,";
151
    defined $row[2] and $sql .= 'ChangeDate,';
152
    defined $row[3] and $sql .= 'Description,';
153
    $sql .= "New) VALUES ('Measurement','MeasureID=$row[0],CensusID=$row[1]',";
154
    defined $row[2] and $sql .= "'$row[2]',";
155
    defined $row[3] and $sql .= "'$row[3]',";
156
    $sql .= "'StemID=$row[4],TreeID=$row[5],DBH=$row[6],HOM=$row[7],ExactDate=$row[8]')";
157

  
158
    if($dbh->do($sql)) {
159
	print "$sql\n";
160
    } else {
161
        die "Error executing: $sql\n" unless defined $quiet;
162
        print FILE "Error executing: $sql\n";
163
    }
164
}
165

  
166
# LogMAttrHistory
167
$sql = 'SELECT MeasureID,CensusID,TSMID,DateOfChange,DescriptionOfChange FROM LogMAttrHistory ORDER BY DateOfChange';
168
$sth = $dbh->prepare($sql);
169
$sth->execute;
170
while( @row = $sth->fetchrow_array ) {
171
    $sql = "INSERT INTO Log (ChangedTable,PrimaryKey,";
172
    defined $row[3] and $sql .= 'ChangeDate,';
173
    defined $row[4] and $sql .= 'Description,';
174
    $sql .= "New) VALUES ('MeasurementAttributes','MeasureID=$row[0],CensusID=$row[1],TSMID=$row[2]',";
175
    defined $row[3] and $sql .= "'$row[3]',";
176
    defined $row[4] and $sql .= "'$row[4]',";
177
    $sql .= "'MeasureID=$row[0],CensusID=$row[1],TSMID=$row[2]')";
178

  
179
    if($dbh->do($sql)) {
180
        print "$sql\n";
181
    } else {
182
        die "Error executing: $sql\n" unless defined $quiet;
183
        print FILE "Error executing: $sql\n";
184
    }
185
}
186

  
187
# LogTreeAttrHistory
188
$sql = 'SELECT CensusID,TreeID,TSMID,DateOfChange,DescriptionOfChange FROM LogTreeAttrHistory ORDER BY DateOfChange';
189
$sth = $dbh->prepare("$sql");
190
$sth->execute;
191
while( @row = $sth->fetchrow_array ) {
192
    $sql = "INSERT INTO Log (ChangedTable,PrimaryKey,";
193
    defined $row[3] and $sql .= 'ChangeDate,';
194
    defined $row[4] and $sql .= 'Description,';
195
    $sql .= "New) VALUES ('TreeAttributes','CensusID=$row[0],TreeID=$row[1],TSMID=$row[2]',";
196
    defined $row[3] and $sql .= "'$row[3]',";
197
    defined $row[4] and $sql .= "'$row[4]',";
198
    $sql .= "'CensusID=$row[0],TreeID=$row[1],TSMID=$row[2]')";
199

  
200
    if($dbh->do($sql)) {
201
        print "$sql\n";
202
    } else {
203
        die "Error executing: $sql\n" unless defined $quiet;
204
        print FILE "Error executing: $sql\n";
205
    }
206
}
207

  
208
# execute drop.sql
209
system "$mysql -v -u $user -p$passwd $newdb < $dfile" or die $!;
210

  
211
close FILE or die $!;

Also available in: Unified diff