fix(mysql)!: look for *.sql files in the top-level schema directory (#154)

* mysql service would assume that all the `*.sql` files in the directory, provided by `initialDatabases [{ schema = <directory>; … }]`, exists in a folder named [mysql-databases](291e7be83a/nix/mysql.nix (L239-L241)), after this change it would just look for all the `*.sql` files in the top-level schema directory.
* add tests
This commit is contained in:
Shivaraj B H 2024-03-18 22:32:08 +05:30 committed by GitHub
parent 291e7be83a
commit f158353b59
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 17 additions and 5 deletions

View File

@ -7,7 +7,7 @@ in
./apache-kafka.nix
./clickhouse
./elasticsearch.nix
./mysql.nix
./mysql
./nginx.nix
./postgres
./redis-cluster.nix

View File

@ -216,7 +216,7 @@ in
'';
configureScript = pkgs.writeShellScriptBin "configure-mysql" ''
PATH="${lib.makeBinPath [config.package pkgs.coreutils]}:$PATH"
PATH="${lib.makeBinPath [config.package pkgs.coreutils pkgs.findutils]}:$PATH"
set -euo pipefail
${envs}
${lib.concatMapStrings (database: ''
@ -237,7 +237,7 @@ in
cat ${database.schema}
elif [ -d "${database.schema}" ]
then
cat ${database.schema}/mysql-databases/*.sql
find ${database.schema} -type f -name '*.sql' | xargs cat
fi
''}
) | MYSQL_PWD="" ${config.package}/bin/mysql -u root -N

View File

@ -1,7 +1,7 @@
{ pkgs, config, ... }: {
services.mysql.m1 = {
enable = true;
initialDatabases = [{ name = "test_database"; }];
initialDatabases = [{ name = "test_database"; schema = ./test_schemas; }];
initialScript = ''
CREATE USER foo IDENTIFIED BY 'password@123';
CREATE USER bar;
@ -35,6 +35,15 @@
echo "$isFooPresent" | grep 1
echo "$isBarPresent" | grep 1
echo "Checking if both foo.sql and bar.sql are executed, ignoring baz.md"
echo "SELECT * FROM information_schema.tables WHERE table_schema = 'test_database' AND table_name = 'foo' LIMIT 1;" | MYSQL_PWD="" mysql -h 127.0.0.1 -u root | grep foo
echo "SELECT * FROM information_schema.tables WHERE table_schema = 'test_database' AND table_name = 'bar' LIMIT 1;" | MYSQL_PWD="" mysql -h 127.0.0.1 -u root | grep bar
if [[ -z $(echo "SELECT * FROM information_schema.tables WHERE table_schema = 'test_database' AND table_name = 'baz' LIMIT 1;" | MYSQL_PWD="" mysql -h 127.0.0.1 -u root) ]]; then
echo "success! baz table not found"
else
echo "baz table shoudn't exist"
exit 1
fi
'';
name = "mysql-test";
};

View File

@ -0,0 +1 @@
CREATE TABLE bar (id INT PRIMARY KEY);

View File

@ -0,0 +1 @@
CREATE TABLE baz (id INT PRIMARY KEY);

View File

@ -0,0 +1 @@
CREATE TABLE foo (id INT PRIMARY KEY);

View File

@ -36,7 +36,7 @@
"${inputs.services-flake}/nix/apache-kafka_test.nix"
"${inputs.services-flake}/nix/clickhouse/clickhouse_test.nix"
"${inputs.services-flake}/nix/elasticsearch_test.nix"
"${inputs.services-flake}/nix/mysql_test.nix"
"${inputs.services-flake}/nix/mysql/mysql_test.nix"
"${inputs.services-flake}/nix/nginx_test.nix"
"${inputs.services-flake}/nix/postgres/postgres_test.nix"
"${inputs.services-flake}/nix/redis_test.nix"