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
This commit is contained in:
Sander van der Burg 2009-11-18 16:19:04 +00:00
parent 6d11d63ba3
commit 27d0d2927e

View file

@ -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.