diff --git a/crossfire/protocol.rkt b/crossfire/protocol.rkt index 3c38724..68f7951 100644 --- a/crossfire/protocol.rkt +++ b/crossfire/protocol.rkt @@ -16,7 +16,8 @@ ;; You should have received a copy of the GNU Affero General Public License ;; along with this program. If not, see . -(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") ;; configuration @@ -26,14 +27,29 @@ (define SERVER-DATA-DIR (if PRODUCTION? "/var/lib/crossfire/" "lib/")) (define SERVER-DB-PATH (build-path SERVER-DATA-DIR "crossfire.sqlite")) +;; north migrations +(define-runtime-path migrations-dir "migrations/") ;; database -(define (open-server-db) - (sqlite3-connect #:database SERVER-DB-PATH)) - (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) (parameterize ([current-db (open-server-db)]) (proc)