changeset 85:50d6c69836a1

Add test data mode for database creation.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 18 Oct 2013 09:07:48 +0300
parents 1f34037a7cae
children 4684336cec09
files createdb.php
diffstat 1 files changed, 67 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/createdb.php	Fri Oct 18 09:05:21 2013 +0300
+++ b/createdb.php	Fri Oct 18 09:07:48 2013 +0300
@@ -164,11 +164,41 @@
   "compos" => "id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(128), description VARCHAR(4096), visible INT DEFAULT 0, voting INT DEFAULT 0, showAuthors INT DEFAULT 0",
   "entries" => "id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(64), author VARCHAR(64), compo_id INT DEFAULT NULL, filename VARCHAR(256) DEFAULT NULL",
 
-  "attendees" => "id INTEGER PRIMARY KEY AUTOINCREMENT, regtime INT, name VARCHAR(64), groups VARCHAR(64), oneliner VARCHAR(64), email VARCHAR(80), votekey VARCHAR(64), active INT DEFAULT 0",
+  "attendees" => "id INTEGER PRIMARY KEY AUTOINCREMENT, regtime INT, name VARCHAR(64), groups VARCHAR(64), oneliner VARCHAR(64), email VARCHAR(80), key VARCHAR(64), active INT DEFAULT 0",
 
   "votes" => "id INTEGER PRIMARY KEY AUTOINCREMENT, entry_id INT DEFAULT NULL, voter_id INT DEFAULT NULL, value INT DEFAULT 0",
 );
 
+$siteTestData = array(
+  "news" => array(
+    "utime,title,text,author",
+    time().",%s,%s,%s",
+    array("Today's news", "We. Are. Back.", "orgaz"),
+    array("Good news, everybody!", "...", "The Professor"),
+  ),
+
+  "attendees" => array(
+    "regtime,name,groups,oneliner,email,key",
+    "%d,%s,%s,%s,%s,%s",
+    array(time()-0, "man with no alias", "supergroup", "foo-bar", "c@supergroup.com", "12t3454"),
+    array(time()-15, "man with alias", "supergroup", "hi!", "c@supergroup.com", "123e45fd4"),
+    array(time()-30, "alias with a man", "supergroup", "mega super kewl rulets", "x@microsoft.com", "4346zxc54"),
+  ),
+
+  "compos" => array(
+    "name,description,visible,voting",
+    "%s,%s,1,1",
+    array("Graphics", "Anything in standard resolutions."),
+    array("Protracker music", "Standard 4-channel Protracker MOD music."),
+  ),
+  
+  "entries" => array(
+    "name,author,compo_id,filename",
+    "%s,%s,%d,%s",
+    array("my dong", "visualice", 1, "juice.lbm"),
+    array("penis song", "reed/flt", 2, "penis.mod"),
+  ),
+);
 
 //
 // Helper functions
@@ -232,6 +262,33 @@
 }
 
 
+function stAddTestData()
+{
+  global $siteTestData;
+  echo "Adding test data.\n";
+
+  foreach ($siteTestData as $table => $data)
+  {
+    echo " - ".$table."...\n";
+    if (count($data) >= 3)
+    {
+      for ($n = 2; $n < count($data); $n++)
+      {
+        $arr = array_merge(
+          array("INSERT INTO ".$table." (".$data[0].") VALUES (".$data[1].")"),
+          $data[$n]);
+
+        $sql = call_user_func_array('stPrepareSQL', $arr);
+        stExecSQL($sql);
+      }
+    }
+    else
+    {
+      echo "  Invalid table / data definition.\n";
+    }
+  }
+}
+
 //
 // Main program starts
 //
@@ -243,7 +300,7 @@
   "  new [dbspec]      Create a new database with given PDO spec\n".
   "                    or default to the one in mconfig.inc.php\n".
   "\n".
-  "  reset [dbspec]    Reset settings (similar to 'new').\n".
+  "  test [dbspec]     Like new, but add initial test data.\n".
   "\n";
   exit;
 }
@@ -260,12 +317,20 @@
 
 echo "Using database spec '".$spec."'.\n";
 
+
+// Act according to specified command
 switch (stCArgLC(1))
 {
+  case "test":
+    $addData = TRUE;
+
   case "new":
     if (stCreateTables())
       stAddSettings();
     
+    if ($addData)
+      stAddTestData();
+
     if (substr($spec, 0, 7) == "sqlite:")
     {
       $filename = substr($spec, 7);
@@ -281,14 +346,6 @@
     }
     break;
 
-  case "reset":
-    echo "Deleting old settings.\n";
-    stExecSQL("DROP TABLE settings");
-
-    if (stCreateOneTable("settings", $sqlTables["settings"]))
-      stAddSettings();
-    break;
-
   default:
     echo "ERROR! Invalid operation mode '".stCArg(1)."'.\n";
     break;