diff options
| author | Marius Bakke <marius@gnu.org> | 2020-11-28 17:42:22 +0100 |
|---|---|---|
| committer | Marius Bakke <marius@gnu.org> | 2020-11-28 19:51:40 +0100 |
| commit | e20388ad7f94e72a7a71272a742031fb5c1fbb4b (patch) | |
| tree | 83d1a8f5082ab9f51f80bf058c71f307bc395c11 /gnu | |
| parent | 927bf98e0e35cbd6d3c8416742f695def8faf90b (diff) | |
services: MySQL: Upgrade database schemas automatically.
* gnu/services/databases.scm (<mysql-configuration>): Add AUTO-UPGRADE? field.
(mysql-upgrade-wrapper, mysql-upgrade-shepherd-service,
mysql-shepherd-services): New variables.
(mysql-service-type): Use MYSQL-SHEPHERD-SERVICES instead of
MYSQL-SHEPHERD-SERVICE.
* doc/guix.texi (Database Services): Document the AUTO-UPGRADE? field of
MYSQL-SERVICE-TYPE.
* gnu/tests/databases.scm (run-mysql-test): Test that mysql_upgrade has run.
Diffstat (limited to 'gnu')
| -rw-r--r-- | gnu/services/databases.scm | 52 | ||||
| -rw-r--r-- | gnu/tests/databases.scm | 4 |
2 files changed, 54 insertions, 2 deletions
diff --git a/gnu/services/databases.scm b/gnu/services/databases.scm index 5a88b70d748..60b31e0373c 100644 --- a/gnu/services/databases.scm +++ b/gnu/services/databases.scm | |||
| @@ -6,6 +6,7 @@ | |||
| 6 | ;;; Copyright © 2018 Clément Lassieur <clement@lassieur.org> | 6 | ;;; Copyright © 2018 Clément Lassieur <clement@lassieur.org> |
| 7 | ;;; Copyright © 2018 Julien Lepiller <julien@lepiller.eu> | 7 | ;;; Copyright © 2018 Julien Lepiller <julien@lepiller.eu> |
| 8 | ;;; Copyright © 2019 Robert Vollmert <rob@vllmrt.net> | 8 | ;;; Copyright © 2019 Robert Vollmert <rob@vllmrt.net> |
| 9 | ;;; Copyright © 2020 Marius Bakke <marius@gnu.org> | ||
| 9 | ;;; | 10 | ;;; |
| 10 | ;;; This file is part of GNU Guix. | 11 | ;;; This file is part of GNU Guix. |
| 11 | ;;; | 12 | ;;; |
| @@ -468,7 +469,8 @@ storage: | |||
| 468 | (bind-address mysql-configuration-bind-address (default "127.0.0.1")) | 469 | (bind-address mysql-configuration-bind-address (default "127.0.0.1")) |
| 469 | (port mysql-configuration-port (default 3306)) | 470 | (port mysql-configuration-port (default 3306)) |
| 470 | (socket mysql-configuration-socket (default "/run/mysqld/mysqld.sock")) | 471 | (socket mysql-configuration-socket (default "/run/mysqld/mysqld.sock")) |
| 471 | (extra-content mysql-configuration-extra-content (default ""))) | 472 | (extra-content mysql-configuration-extra-content (default "")) |
| 473 | (auto-upgrade? mysql-configuration-auto-upgrade? (default #t))) | ||
| 472 | 474 | ||
| 473 | (define %mysql-accounts | 475 | (define %mysql-accounts |
| 474 | (list (user-group | 476 | (list (user-group |
| @@ -559,6 +561,52 @@ FLUSH PRIVILEGES; | |||
| 559 | #:user "mysql" #:group "mysql"))) | 561 | #:user "mysql" #:group "mysql"))) |
| 560 | (stop #~(make-kill-destructor))))) | 562 | (stop #~(make-kill-destructor))))) |
| 561 | 563 | ||
| 564 | (define (mysql-upgrade-wrapper mysql socket-file) | ||
| 565 | ;; The MySQL socket and PID file may appear before the server is ready to | ||
| 566 | ;; accept connections. Ensure the socket is responsive before attempting | ||
| 567 | ;; to run the upgrade script. | ||
| 568 | (program-file | ||
| 569 | "mysql-upgrade-wrapper" | ||
| 570 | #~(begin | ||
| 571 | (let ((mysql-upgrade #$(file-append mysql "/bin/mysql_upgrade")) | ||
| 572 | (timeout 10)) | ||
| 573 | (begin | ||
| 574 | (let loop ((i 0)) | ||
| 575 | (catch 'system-error | ||
| 576 | (lambda () | ||
| 577 | (let ((sock (socket PF_UNIX SOCK_STREAM 0))) | ||
| 578 | (connect sock AF_UNIX #$socket-file) | ||
| 579 | (close-port sock) | ||
| 580 | ;; The socket is ready! | ||
| 581 | (execl mysql-upgrade mysql-upgrade | ||
| 582 | (string-append "--socket=" #$socket-file)))) | ||
| 583 | (lambda args | ||
| 584 | (if (< i timeout) | ||
| 585 | (begin | ||
| 586 | (sleep 1) | ||
| 587 | (loop (+ 1 i))) | ||
| 588 | ;; No luck, give up. | ||
| 589 | (throw 'timeout-error | ||
| 590 | "MySQL server did not appear in time!")))))))))) | ||
| 591 | |||
| 592 | (define (mysql-upgrade-shepherd-service config) | ||
| 593 | (list (shepherd-service | ||
| 594 | (provision '(mysql-upgrade)) | ||
| 595 | (requirement '(mysql)) | ||
| 596 | (one-shot? #t) | ||
| 597 | (documentation "Upgrade MySQL database schemas.") | ||
| 598 | (start (let ((mysql (mysql-configuration-mysql config)) | ||
| 599 | (socket (mysql-configuration-socket config))) | ||
| 600 | #~(make-forkexec-constructor | ||
| 601 | (list #$(mysql-upgrade-wrapper mysql socket)) | ||
| 602 | #:user "mysql" #:group "mysql")))))) | ||
| 603 | |||
| 604 | (define (mysql-shepherd-services config) | ||
| 605 | (if (mysql-configuration-auto-upgrade? config) | ||
| 606 | (append (mysql-shepherd-service config) | ||
| 607 | (mysql-upgrade-shepherd-service config)) | ||
| 608 | (mysql-shepherd-service config))) | ||
| 609 | |||
| 562 | (define mysql-service-type | 610 | (define mysql-service-type |
| 563 | (service-type | 611 | (service-type |
| 564 | (name 'mysql) | 612 | (name 'mysql) |
| @@ -568,7 +616,7 @@ FLUSH PRIVILEGES; | |||
| 568 | (service-extension activation-service-type | 616 | (service-extension activation-service-type |
| 569 | %mysql-activation) | 617 | %mysql-activation) |
| 570 | (service-extension shepherd-root-service-type | 618 | (service-extension shepherd-root-service-type |
| 571 | mysql-shepherd-service))) | 619 | mysql-shepherd-services))) |
| 572 | (default-value (mysql-configuration)))) | 620 | (default-value (mysql-configuration)))) |
| 573 | 621 | ||
| 574 | (define-deprecated (mysql-service #:key (config (mysql-configuration))) | 622 | (define-deprecated (mysql-service #:key (config (mysql-configuration))) |
diff --git a/gnu/tests/databases.scm b/gnu/tests/databases.scm index 1d7f53ec3eb..dd1af1dbccc 100644 --- a/gnu/tests/databases.scm +++ b/gnu/tests/databases.scm | |||
| @@ -1,5 +1,6 @@ | |||
| 1 | ;;; GNU Guix --- Functional package management for GNU | 1 | ;;; GNU Guix --- Functional package management for GNU |
| 2 | ;;; Copyright © 2017 Christopher Baines <mail@cbaines.net> | 2 | ;;; Copyright © 2017 Christopher Baines <mail@cbaines.net> |
| 3 | ;;; Copyright © 2020 Marius Bakke <marius@gnu.org> | ||
| 3 | ;;; | 4 | ;;; |
| 4 | ;;; This file is part of GNU Guix. | 5 | ;;; This file is part of GNU Guix. |
| 5 | ;;; | 6 | ;;; |
| @@ -311,6 +312,9 @@ | |||
| 311 | ((pid) (number? pid)))))) | 312 | ((pid) (number? pid)))))) |
| 312 | marionette)) | 313 | marionette)) |
| 313 | 314 | ||
| 315 | (test-assert "mysql_upgrade completed" | ||
| 316 | (wait-for-file "/var/lib/mysql/mysql_upgrade_info" marionette)) | ||
| 317 | |||
| 314 | (test-end) | 318 | (test-end) |
| 315 | (exit (= (test-runner-fail-count (test-runner-current)) 0))))) | 319 | (exit (= (test-runner-fail-count (test-runner-current)) 0))))) |
| 316 | 320 | ||
