Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion nix/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ in
./apache-kafka.nix
./clickhouse
./elasticsearch.nix
./mysql.nix
./mysql
./nginx.nix
./postgres
./redis-cluster.nix
Expand Down
4 changes: 2 additions & 2 deletions nix/mysql.nix → nix/mysql/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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"
shivaraj-bh marked this conversation as resolved.
Show resolved Hide resolved
set -euo pipefail
${envs}
${lib.concatMapStrings (database: ''
Expand All @@ -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
Expand Down
11 changes: 10 additions & 1 deletion nix/mysql_test.nix → nix/mysql/mysql_test.nix
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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";
};
Expand Down
1 change: 1 addition & 0 deletions nix/mysql/test_schemas/bar.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE TABLE bar (id INT PRIMARY KEY);
1 change: 1 addition & 0 deletions nix/mysql/test_schemas/baz.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE TABLE baz (id INT PRIMARY KEY);
1 change: 1 addition & 0 deletions nix/mysql/test_schemas/foo.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE TABLE foo (id INT PRIMARY KEY);
2 changes: 1 addition & 1 deletion test/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading