[LON-CAPA-cvs] cvs: loncom /metadata_database lonmetadata_test.pl /metadata_database/LONCAPA lonmetadata.pm

matthew lon-capa-cvs@mail.lon-capa.org
Mon, 12 Jan 2004 21:48:39 -0000


matthew		Mon Jan 12 16:48:39 2004 EDT

  Modified files:              
    /loncom/metadata_database	lonmetadata_test.pl 
    /loncom/metadata_database/LONCAPA	lonmetadata.pm 
  Log:
  Modified lonmetadata::create_metadata_storage to take an optional parameter
  for the table name to make life a little easier in searchcat.pl.
  Added test for it to lonmetadata_test.pl
  
  
Index: loncom/metadata_database/lonmetadata_test.pl
diff -u loncom/metadata_database/lonmetadata_test.pl:1.1 loncom/metadata_database/lonmetadata_test.pl:1.2
--- loncom/metadata_database/lonmetadata_test.pl:1.1	Mon Jan 12 16:33:19 2004
+++ loncom/metadata_database/lonmetadata_test.pl	Mon Jan 12 16:48:38 2004
@@ -1,7 +1,7 @@
 #!/usr/bin/perl -w
 # The LearningOnline Network with CAPA
 #
-# $Id: lonmetadata_test.pl,v 1.1 2004/01/12 21:33:19 matthew Exp $
+# $Id: lonmetadata_test.pl,v 1.2 2004/01/12 21:48:38 matthew Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -30,11 +30,12 @@
 
 use DBI;
 use LONCAPA::lonmetadata();
-use Test::Simple tests => 3;
+use Test::Simple tests => 4;
 
 
 ok(&create_test_db(),'database creation');
 ok(&test_creation(),'table creation');
+ok(&test_named_creation(),'named table creation');
 ok(&test_inserts(),'insert test');
 
 exit;
@@ -46,8 +47,14 @@
 ##
 #####################################################################
 #####################################################################
+##
+##  Note: The root password to my MySQL server is shown below.
+##  Access is only allowed from localhost so it should be okay.
+##  Now if you will excuse me I have to change the password on my luggage.
+##
+my $supersecretpassword = '123';  # shhhh
 sub create_test_db {
-    my $dbh = DBI->connect("DBI:mysql:test","root","123",
+    my $dbh = DBI->connect("DBI:mysql:test","root",$supersecretpassword,
                            { RaiseError =>0,PrintError=>0});
     if (! defined($dbh)) {
         return 0;
@@ -65,7 +72,7 @@
 }
 
 sub test_creation {
-    my $dbh = DBI->connect("DBI:mysql:lonmetatest","root","123",
+    my $dbh = DBI->connect("DBI:mysql:lonmetatest","root",$supersecretpassword,
                            { RaiseError =>0,PrintError=>0});
     my $request = &LONCAPA::lonmetadata::create_metadata_storage();
     $dbh->do($request);
@@ -78,8 +85,29 @@
     }
 }
 
+sub test_named_creation {
+    my $request = 
+             &LONCAPA::lonmetadata::create_metadata_storage('nonmetadata');
+    my $dbh = DBI->connect("DBI:mysql:lonmetatest","root",$supersecretpassword,
+                           { RaiseError =>0,PrintError=>0});
+    $dbh->do($request); # Create the table, only return 0 if we cannot.
+    if ($dbh->err) {
+        $dbh->disconnect();
+        return 0;
+    }
+    $dbh->do('DROP TABLE nonmetadata'); # This will generate an error if the
+                                        # table does not exist
+    if ($dbh->err) {
+        $dbh->disconnect();
+        return 0;
+    } else {
+        $dbh->disconnect();
+        return 1;
+    }
+}
+
 sub test_inserts {
-    my $dbh = DBI->connect("DBI:mysql:lonmetatest","root","123",
+    my $dbh = DBI->connect("DBI:mysql:lonmetatest","root",$supersecretpassword,
                            { RaiseError =>0,PrintError=>0});
     my @TestRecords = (
                       { url => 'm/b/h/test1' },
Index: loncom/metadata_database/LONCAPA/lonmetadata.pm
diff -u loncom/metadata_database/LONCAPA/lonmetadata.pm:1.2 loncom/metadata_database/LONCAPA/lonmetadata.pm:1.3
--- loncom/metadata_database/LONCAPA/lonmetadata.pm:1.2	Mon Jan 12 16:32:20 2004
+++ loncom/metadata_database/LONCAPA/lonmetadata.pm	Mon Jan 12 16:48:38 2004
@@ -1,6 +1,6 @@
 # The LearningOnline Network with CAPA
 #
-# $Id: lonmetadata.pm,v 1.2 2004/01/12 21:32:20 matthew Exp $
+# $Id: lonmetadata.pm,v 1.3 2004/01/12 21:48:38 matthew Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -183,7 +183,7 @@
 
 =item create_metadata_storage()
 
-Inputs: None
+Inputs: table name (optional): the name of the table.  Default is 'metadata'.
 
 Returns: A perl string which, when executed by MySQL, will cause the
 metadata storage to be initialized.
@@ -193,7 +193,8 @@
 ######################################################################
 ######################################################################
 sub create_metadata_storage { 
-    my $tablename = 'metadata';
+    my ($tablename) = @_;
+    $tablename = 'metadata' if (! defined($tablename));
     my $request = "CREATE TABLE IF NOT EXISTS ".$tablename." ";
     #
     # Process the columns  (this code is stolen from lonmysql.pm)
@@ -230,7 +231,7 @@
         my $text = 'FULLTEXT idx_'.$colname.' ('.$colname.')';
         push (@Columns,$text);
     }
-    $request .= "(".join(", ",@Columns).") ";
+    $request .= "(".join(", ",@Columns).") TYPE=MyISAM";
     return $request;
 }