From 95def61636f66a64eef6c4512960b46025222274 Mon Sep 17 00:00:00 2001 From: shivaraj-bh Date: Mon, 18 Mar 2024 17:42:33 +0530 Subject: [PATCH] fix(mysql)!: look for `*.sql` files in the top-level schema directory * mysql service would assume that all the `*.sql` files in the directory, provided by `initialDatabases [{ schema = ; ... }]`, exists in a folder named [mysql-databases](https://github.com/juspay/services-flake/blob/291e7be83aa0f24b8e40fc61df4a895827788707/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 --- nix/default.nix | 2 +- nix/{mysql.nix => mysql/default.nix} | 4 ++-- nix/{ => mysql}/mysql_test.nix | 11 ++++++++++- nix/mysql/test_schemas/bar.sql | 1 + nix/mysql/test_schemas/baz.md | 1 + nix/mysql/test_schemas/foo.sql | 1 + test/flake.nix | 2 +- 7 files changed, 17 insertions(+), 5 deletions(-) rename nix/{mysql.nix => mysql/default.nix} (99%) rename nix/{ => mysql}/mysql_test.nix (65%) create mode 100644 nix/mysql/test_schemas/bar.sql create mode 100644 nix/mysql/test_schemas/baz.md create mode 100644 nix/mysql/test_schemas/foo.sql diff --git a/nix/default.nix b/nix/default.nix index f3350f86..bb86f813 100644 --- a/nix/default.nix +++ b/nix/default.nix @@ -7,7 +7,7 @@ in ./apache-kafka.nix ./clickhouse ./elasticsearch.nix - ./mysql.nix + ./mysql ./nginx.nix ./postgres ./redis-cluster.nix diff --git a/nix/mysql.nix b/nix/mysql/default.nix similarity index 99% rename from nix/mysql.nix rename to nix/mysql/default.nix index 92d3f389..bc92e1ad 100644 --- a/nix/mysql.nix +++ b/nix/mysql/default.nix @@ -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 diff --git a/nix/mysql_test.nix b/nix/mysql/mysql_test.nix similarity index 65% rename from nix/mysql_test.nix rename to nix/mysql/mysql_test.nix index 4c2d5077..c59b3bf0 100644 --- a/nix/mysql_test.nix +++ b/nix/mysql/mysql_test.nix @@ -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"; }; diff --git a/nix/mysql/test_schemas/bar.sql b/nix/mysql/test_schemas/bar.sql new file mode 100644 index 00000000..992f855d --- /dev/null +++ b/nix/mysql/test_schemas/bar.sql @@ -0,0 +1 @@ +CREATE TABLE bar (id INT PRIMARY KEY); diff --git a/nix/mysql/test_schemas/baz.md b/nix/mysql/test_schemas/baz.md new file mode 100644 index 00000000..db1a61a7 --- /dev/null +++ b/nix/mysql/test_schemas/baz.md @@ -0,0 +1 @@ +CREATE TABLE baz (id INT PRIMARY KEY); diff --git a/nix/mysql/test_schemas/foo.sql b/nix/mysql/test_schemas/foo.sql new file mode 100644 index 00000000..e606b485 --- /dev/null +++ b/nix/mysql/test_schemas/foo.sql @@ -0,0 +1 @@ +CREATE TABLE foo (id INT PRIMARY KEY); diff --git a/test/flake.nix b/test/flake.nix index cef189b6..0c5b2d87 100644 --- a/test/flake.nix +++ b/test/flake.nix @@ -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"