git.iko.soy/typeable/nixpkgs
on master help sign in
Merge pull request #58702 from florianjacob/fix-mysql

nixos/mysql: fix typing-induced bugs

commitf631167557e812860f0c4079df85215703a461c8 authorSilvan Mosberger <infinisil@icloud.com> dateFri 24 May 2019, 19:51 (8 years ago) parentb596bec14571f5
2 files changed+21−6
nixos/modules/services/databases/mysql.nix+7−3 nixos/tests/mysql.nix+14−3
nixos/modules/services/databases/mysql.nix+7−3
@@ -133,7 +133,7 @@ in
133133 };
134134
135135 initialScript = mkOption {
136− type = types.nullOr types.lines;
136+ type = types.nullOr types.path;
137137 default = null;
138138 description = "A file containing SQL statements to be executed on the first startup. Can be used for granting certain permissions on the database";
139139 };
@@ -360,9 +360,11 @@ in
360360 echo "Creating initial database: ${database.name}"
361361 ( echo 'create database `${database.name}`;'
362362
363− ${optionalString (database ? "schema") ''
363+ ${optionalString (database.schema != null) ''
364364 echo 'use `${database.name}`;'
365365
366+ # TODO: this silently falls through if database.schema does not exist,
367+ # we should catch this somehow and exit, but can't do it here because we're in a subshell.
366368 if [ -f "${database.schema}" ]
367369 then
368370 cat ${database.schema}
@@ -399,7 +401,9 @@ in
399401 ${optionalString (cfg.initialScript != null)
400402 ''
401403 # Execute initial script
402− cat ${cfg.initialScript} | ${mysql}/bin/mysql -u root -N
404+ # using toString to avoid copying the file to nix store if given as path instead of string,
405+ # as it might contain credentials
406+ cat ${toString cfg.initialScript} | ${mysql}/bin/mysql -u root -N
403407 ''}
404408
405409 ${optionalString (cfg.rootPassword != null)
nixos/tests/mysql.nix+14−3
@@ -10,7 +10,15 @@ import ./make-test.nix ({ pkgs, ...} : {
1010
1111 {
1212 services.mysql.enable = true;
13− services.mysql.initialDatabases = [ { name = "testdb"; schema = ./testdb.sql; } ];
13+ services.mysql.initialDatabases = [
14+ { name = "testdb"; schema = ./testdb.sql; }
15+ { name = "empty_testdb"; }
16+ ];
17+ # note that using pkgs.writeText here is generally not a good idea,
18+ # as it will store the password in world-readable /nix/store ;)
19+ services.mysql.initialScript = pkgs.writeText "mysql-init.sql" ''
20+ CREATE USER 'passworduser'@'localhost' IDENTIFIED BY 'password123';
21+ '';
1422 services.mysql.package = pkgs.mysql;
1523 };
1624
@@ -36,11 +44,14 @@ import ./make-test.nix ({ pkgs, ...} : {
3644 startAll;
3745
3846 $mysql->waitForUnit("mysql");
39− $mysql->succeed("echo 'use testdb; select * from tests' | mysql -u root -N | grep 4");
47+ $mysql->succeed("echo 'use empty_testdb;' | mysql -u root");
48+ $mysql->succeed("echo 'use testdb; select * from tests;' | mysql -u root -N | grep 4");
49+ # ';' acts as no-op, just check whether login succeeds with the user created from the initialScript
50+ $mysql->succeed("echo ';' | mysql -u passworduser --password=password123");
4051
4152 $mariadb->waitForUnit("mysql");
4253 $mariadb->succeed("echo 'use testdb; create table tests (test_id INT, PRIMARY KEY (test_id));' | sudo -u testuser mysql -u testuser");
4354 $mariadb->succeed("echo 'use testdb; insert into tests values (42);' | sudo -u testuser mysql -u testuser");
44− $mariadb->succeed("echo 'use testdb; select test_id from tests' | sudo -u testuser mysql -u testuser -N | grep 42");
55+ $mariadb->succeed("echo 'use testdb; select test_id from tests;' | sudo -u testuser mysql -u testuser -N | grep 42");
4556 '';
4657})