mirror of
https://github.com/juspay/services-flake.git
synced 2024-09-11 12:26:04 +03:00
Mysql: add initialScript
option (#98)
— Co-authored-by: rsrohitsingh682 <rsrohitsingh682@gmail.com>
This commit is contained in:
parent
63ff90eed9
commit
7593351c71
@ -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 {
|
initialDatabases = lib.mkOption {
|
||||||
type = types.listOf (types.submodule {
|
type = types.listOf (types.submodule {
|
||||||
options = {
|
options = {
|
||||||
@ -199,6 +212,9 @@ in
|
|||||||
exec ${config.package}/bin/mysqld ${mysqldOptions}
|
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" ''
|
configureScript = pkgs.writeShellScriptBin "configure-mysql" ''
|
||||||
PATH="${lib.makeBinPath [config.package pkgs.coreutils]}:$PATH"
|
PATH="${lib.makeBinPath [config.package pkgs.coreutils]}:$PATH"
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
@ -242,6 +258,8 @@ in
|
|||||||
) | MYSQL_PWD="" ${config.package}/bin/mysql -u root -N
|
) | MYSQL_PWD="" ${config.package}/bin/mysql -u root -N
|
||||||
'')
|
'')
|
||||||
config.ensureUsers}
|
config.ensureUsers}
|
||||||
|
|
||||||
|
${runInitialScript}
|
||||||
'';
|
'';
|
||||||
|
|
||||||
in
|
in
|
||||||
|
@ -1,6 +1,12 @@
|
|||||||
{ pkgs, config, ... }: {
|
{ pkgs, config, ... }: {
|
||||||
services.mysql.m1.enable = true;
|
services.mysql.m1 = {
|
||||||
services.mysql.m1.initialDatabases = [{ name = "test_database"; }];
|
enable = true;
|
||||||
|
initialDatabases = [{ name = "test_database"; }];
|
||||||
|
initialScript = ''
|
||||||
|
CREATE USER foo IDENTIFIED BY 'password@123';
|
||||||
|
CREATE USER bar;
|
||||||
|
'';
|
||||||
|
};
|
||||||
services.mysql.m1.ensureUsers = [
|
services.mysql.m1.ensureUsers = [
|
||||||
{
|
{
|
||||||
name = "test_database";
|
name = "test_database";
|
||||||
@ -13,7 +19,7 @@
|
|||||||
settings.processes.test =
|
settings.processes.test =
|
||||||
{
|
{
|
||||||
command = pkgs.writeShellApplication {
|
command = pkgs.writeShellApplication {
|
||||||
runtimeInputs = [ config.services.mysql.m1.package pkgs.gnugrep ];
|
runtimeInputs = [ config.services.mysql.m1.package pkgs.gnugrep pkgs.coreutils ];
|
||||||
text = ''
|
text = ''
|
||||||
rows=$(echo "SHOW DATABASES LIKE 'test_database';" | MYSQL_PWD="" mysql -h 127.0.0.1 | wc -l)
|
rows=$(echo "SHOW DATABASES LIKE 'test_database';" | MYSQL_PWD="" mysql -h 127.0.0.1 | wc -l)
|
||||||
if [ "$rows" -eq 0 ]; then
|
if [ "$rows" -eq 0 ]; then
|
||||||
@ -22,6 +28,13 @@
|
|||||||
fi
|
fi
|
||||||
echo 'SELECT VERSION();' | MYSQL_PWD="" mysql -h 127.0.0.1
|
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 '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";
|
name = "mysql-test";
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user