Skip to content

Commit

Permalink
Mysql: add initialScript option (#98)
Browse files Browse the repository at this point in the history
—

Co-authored-by: rsrohitsingh682 <[email protected]>
  • Loading branch information
shivaraj-bh and rsrohitsingh682 authored Feb 15, 2024
1 parent 63ff90e commit 7593351
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
18 changes: 18 additions & 0 deletions nix/mysql.nix
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,19 @@ in
'';
};

initialScript = lib.mkOption {
type = types.nullOr types.str;
default = null;
description = ''
Initial SQL commands to run after `initialDatabases` and `ensureUsers`. This can be multiple
SQL expressions separated by a semi-colon.
'';
example = ''
CREATE USER foo IDENTIFIED BY 'password@123';
CREATE USER bar;
'';
};

initialDatabases = lib.mkOption {
type = types.listOf (types.submodule {
options = {
Expand Down Expand Up @@ -199,6 +212,9 @@ in
exec ${config.package}/bin/mysqld ${mysqldOptions}
'';

runInitialScript = lib.optionalString (config.initialScript != null) ''echo ${lib.escapeShellArg config.initialScript} | MYSQL_PWD="" ${config.package}/bin/mysql -u root -N
'';

configureScript = pkgs.writeShellScriptBin "configure-mysql" ''
PATH="${lib.makeBinPath [config.package pkgs.coreutils]}:$PATH"
set -euo pipefail
Expand Down Expand Up @@ -242,6 +258,8 @@ in
) | MYSQL_PWD="" ${config.package}/bin/mysql -u root -N
'')
config.ensureUsers}
${runInitialScript}
'';

in
Expand Down
19 changes: 16 additions & 3 deletions nix/mysql_test.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
{ pkgs, config, ... }: {
services.mysql.m1.enable = true;
services.mysql.m1.initialDatabases = [{ name = "test_database"; }];
services.mysql.m1 = {
enable = true;
initialDatabases = [{ name = "test_database"; }];
initialScript = ''
CREATE USER foo IDENTIFIED BY 'password@123';
CREATE USER bar;
'';
};
services.mysql.m1.ensureUsers = [
{
name = "test_database";
Expand All @@ -13,7 +19,7 @@
settings.processes.test =
{
command = pkgs.writeShellApplication {
runtimeInputs = [ config.services.mysql.m1.package pkgs.gnugrep ];
runtimeInputs = [ config.services.mysql.m1.package pkgs.gnugrep pkgs.coreutils ];
text = ''
rows=$(echo "SHOW DATABASES LIKE 'test_database';" | MYSQL_PWD="" mysql -h 127.0.0.1 | wc -l)
if [ "$rows" -eq 0 ]; then
Expand All @@ -22,6 +28,13 @@
fi
echo 'SELECT VERSION();' | MYSQL_PWD="" mysql -h 127.0.0.1
echo 'SELECT VERSION();' | MYSQL_PWD="" mysql -h 127.0.0.1 -P 3308
echo "Checking if users foo and bar are present: "
isFooPresent=$(echo "SELECT EXISTS(SELECT 1 FROM mysql.user WHERE user = 'foo');" | MYSQL_PWD="" mysql -h 127.0.0.1 -u root | tail -n1)
isBarPresent=$(echo "SELECT EXISTS(SELECT 1 FROM mysql.user WHERE user = 'bar');" | MYSQL_PWD="" mysql -h 127.0.0.1 -u root | tail -n1)
echo "$isFooPresent" | grep 1
echo "$isBarPresent" | grep 1
'';
name = "mysql-test";
};
Expand Down

0 comments on commit 7593351

Please sign in to comment.