summary refs log tree commit diff
path: root/modules/services/databases/mysql.nix
diff options
context:
space:
mode:
authorSander van der Burg <s.vanderburg@tudelft.nl>2009-11-18 16:19:04 +0000
committerSander van der Burg <s.vanderburg@tudelft.nl>2009-11-18 16:19:04 +0000
commit27d0d2927eca16ca02acdf78d3c505278845b6d7 (patch)
treeaa217f9986fec8be91d0fc7616104af6605a5159 /modules/services/databases/mysql.nix
parent6d11d63ba313cf2ca839df20edd98d816cecd527 (diff)
downloadnixpkgs-27d0d2927eca16ca02acdf78d3c505278845b6d7.tar
nixpkgs-27d0d2927eca16ca02acdf78d3c505278845b6d7.tar.gz
nixpkgs-27d0d2927eca16ca02acdf78d3c505278845b6d7.tar.bz2
nixpkgs-27d0d2927eca16ca02acdf78d3c505278845b6d7.tar.lz
nixpkgs-27d0d2927eca16ca02acdf78d3c505278845b6d7.tar.xz
nixpkgs-27d0d2927eca16ca02acdf78d3c505278845b6d7.tar.zst
nixpkgs-27d0d2927eca16ca02acdf78d3c505278845b6d7.zip
Added initialDatabases option to the MySQL service. This is useful for e.g. automatically intialing databases in a test VM
svn path=/nixos/branches/upstart-0.6/; revision=18437
Diffstat (limited to 'modules/services/databases/mysql.nix')
-rw-r--r--modules/services/databases/mysql.nix40
1 files changed, 39 insertions, 1 deletions
diff --git a/modules/services/databases/mysql.nix b/modules/services/databases/mysql.nix
index 230d36dfe91..50d00c0e5ba 100644
--- a/modules/services/databases/mysql.nix
+++ b/modules/services/databases/mysql.nix
@@ -55,7 +55,15 @@ in
         default = "/var/run/mysql";
         description = "Location of the file which stores the PID of the MySQL server";
       };
-
+      
+      initialDatabases = mkOption {
+        default = [];
+        description = "List of database names and their initial schemas that should be used to create databases on the first startup of MySQL";
+	example = [
+	  { name = "foodatabase"; schema = ./foodatabase.sql; }
+	  { name = "bardatabase"; schema = ./bardatabase.sql; }
+	];
+      };
     };
     
   };
@@ -90,6 +98,36 @@ in
           '';
 
         exec = "${mysql}/libexec/mysqld ${mysqldOptions}";
+	
+	postStart =
+	  ''
+            # Wait until the MySQL server is available for use
+            count=0
+            while [ ! -e /tmp/mysql.sock ]
+            do
+                if [ $count -eq 30 ]
+                then
+                  echo "Tried 30 times, giving up..."
+	          exit 1
+                 fi
+
+                 echo "MySQL daemon not yet started. Waiting for 1 second..."
+                 count=$((count++))
+                 sleep 1
+            done
+
+            # Create initial databases
+
+            ${concatMapStrings (database: 
+              ''
+                if ! test -e "${cfg.dataDir}/${database.name}"; then
+                    echo "Creating initial database: ${database.name}"
+                    ( echo "create database ${database.name};"
+                      echo "use ${database.name};"
+                      cat ${database.schema} ) | ${mysql}/bin/mysql -u root -N
+                fi
+              '') cfg.initialDatabases}            
+	  '';
 
         # !!! Need a postStart script to wait until mysqld is ready to
         # accept connections.