make server capable of migrating itself
This commit is contained in:
parent
245c6b7dbb
commit
ed503363a4
|
@ -16,7 +16,8 @@
|
||||||
;; You should have received a copy of the GNU Affero General Public License
|
;; You should have received a copy of the GNU Affero General Public License
|
||||||
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
|
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
(require db/base db/sqlite3 racket/path
|
(require db/base db/sqlite3 racket/path racket/runtime-path
|
||||||
|
north/base north/adapter/base north/adapter/sqlite
|
||||||
"comms.rkt" "not-crypto.rkt")
|
"comms.rkt" "not-crypto.rkt")
|
||||||
|
|
||||||
;; configuration
|
;; configuration
|
||||||
|
@ -26,14 +27,29 @@
|
||||||
(define SERVER-DATA-DIR (if PRODUCTION? "/var/lib/crossfire/" "lib/"))
|
(define SERVER-DATA-DIR (if PRODUCTION? "/var/lib/crossfire/" "lib/"))
|
||||||
(define SERVER-DB-PATH (build-path SERVER-DATA-DIR "crossfire.sqlite"))
|
(define SERVER-DB-PATH (build-path SERVER-DATA-DIR "crossfire.sqlite"))
|
||||||
|
|
||||||
|
;; north migrations
|
||||||
|
(define-runtime-path migrations-dir "migrations/")
|
||||||
|
|
||||||
;; database
|
;; database
|
||||||
|
|
||||||
(define (open-server-db)
|
|
||||||
(sqlite3-connect #:database SERVER-DB-PATH))
|
|
||||||
|
|
||||||
(define current-db (make-parameter #f))
|
(define current-db (make-parameter #f))
|
||||||
|
|
||||||
|
(define (open-server-db [mode 'read/write])
|
||||||
|
(sqlite3-connect #:database SERVER-DB-PATH #:mode mode))
|
||||||
|
|
||||||
|
;; this allows the server to be capable of migrating itself
|
||||||
|
(define (migrate-server-db [db (current-db)])
|
||||||
|
(define base (path->migration migrations-dir))
|
||||||
|
(define adapter (sqlite-adapter db))
|
||||||
|
(adapter-init adapter)
|
||||||
|
(define current-revision (adapter-current-revision adapter))
|
||||||
|
(define target-revision (migration-revision (migration-most-recent base)))
|
||||||
|
(define plan (migration-plan base current-revision target-revision))
|
||||||
|
(for ([migration (in-list plan)])
|
||||||
|
(displayln (format "applying migration: ~a" (migration-revision migration)))
|
||||||
|
(adapter-apply! adapter (migration-revision migration) (migration-up migration)))
|
||||||
|
(void))
|
||||||
|
|
||||||
(define (with-server-db proc)
|
(define (with-server-db proc)
|
||||||
(parameterize ([current-db (open-server-db)])
|
(parameterize ([current-db (open-server-db)])
|
||||||
(proc)
|
(proc)
|
||||||
|
|
Loading…
Reference in New Issue