make server capable of migrating itself

This commit is contained in:
xenia 2020-11-13 21:07:50 -05:00
parent 245c6b7dbb
commit ed503363a4
1 changed files with 20 additions and 4 deletions

View File

@ -16,7 +16,8 @@
;; 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/>.
(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)