aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


server/database_migrations.go (3017B)


      1 package main
      2 
      3 import (
      4 	"database/sql"
      5 	"embed"
      6 	"fmt"
      7 	"strings"
      8 )
      9 
     10 // Version zero is the unversioned world schema. Its CREATE IF NOT EXISTS
     11 // statements also initialize a new database before the first migration.
     12 //
     13 //go:embed world.sql
     14 var worldSchema string
     15 
     16 //go:embed migrations/*.sql
     17 var migrations embed.FS
     18 
     19 func migrateWorld(tx *sql.Tx) error {
     20 	var version int
     21 	if err := tx.QueryRow("PRAGMA user_version").Scan(&version); err != nil {
     22 		return err
     23 	}
     24 	files, err := migrations.ReadDir("migrations")
     25 	if err != nil {
     26 		return err
     27 	}
     28 	if version < 0 || version > len(files) {
     29 		return fmt.Errorf("unsupported world database version %d (server supports through %d)", version, len(files))
     30 	}
     31 	if version == 0 {
     32 		if _, err := tx.Exec(worldSchema); err != nil {
     33 			return err
     34 		}
     35 		if err := upgradeUnversionedWorld(tx); err != nil {
     36 			return err
     37 		}
     38 	}
     39 
     40 	// ReadDir sorts filenames. Require consecutive numbers so a missing
     41 	// migration cannot advance the version past an unapplied schema change.
     42 	for index, file := range files {
     43 		next := index + 1
     44 		if !strings.HasPrefix(file.Name(), fmt.Sprintf("%03d_", next)) {
     45 			return fmt.Errorf("expected world migration %03d, found %s", next, file.Name())
     46 		}
     47 		if next <= version {
     48 			continue
     49 		}
     50 		script, err := migrations.ReadFile("migrations/" + file.Name())
     51 		if err != nil {
     52 			return err
     53 		}
     54 		if _, err := tx.Exec(string(script)); err != nil {
     55 			return fmt.Errorf("world migration %s: %w", file.Name(), err)
     56 		}
     57 		if _, err := tx.Exec(fmt.Sprintf("PRAGMA user_version=%d", next)); err != nil {
     58 			return err
     59 		}
     60 	}
     61 	return nil
     62 }
     63 
     64 // Unversioned databases in use can predate these additions to world.sql.
     65 // CREATE TABLE IF NOT EXISTS installs missing tables but cannot add columns.
     66 func upgradeUnversionedWorld(tx *sql.Tx) error {
     67 	for _, field := range []struct{ table, column, definition string }{
     68 		{"characters", "title", "TEXT NOT NULL DEFAULT ''"},
     69 		{"players", "last_ip", "TEXT NOT NULL DEFAULT ''"},
     70 		{"players", "connected_seconds", "INTEGER NOT NULL DEFAULT 0"},
     71 		{"players", "last_connected_at", "DATETIME NOT NULL DEFAULT '0001-01-01 00:00:00+00:00'"},
     72 		{"world", "team_revision", "INTEGER NOT NULL DEFAULT 0"},
     73 		{"teams", "revision", "INTEGER NOT NULL DEFAULT 0"},
     74 	} {
     75 		var exists bool
     76 		if err := tx.QueryRow("SELECT EXISTS(SELECT 1 FROM pragma_table_info(?) WHERE name=?)", field.table, field.column).Scan(&exists); err != nil {
     77 			return err
     78 		}
     79 		if !exists {
     80 			if _, err := tx.Exec("ALTER TABLE " + field.table + " ADD COLUMN " + field.column + " " + field.definition); err != nil {
     81 				return err
     82 			}
     83 		}
     84 	}
     85 	var missionExperience bool
     86 	if err := tx.QueryRow("SELECT EXISTS(SELECT 1 FROM pragma_table_info('missions') WHERE name='experience')").Scan(&missionExperience); err != nil {
     87 		return err
     88 	}
     89 	if missionExperience {
     90 		// The old required column prevents inserts using the current mission schema.
     91 		if _, err := tx.Exec("ALTER TABLE missions DROP COLUMN experience"); err != nil {
     92 			return err
     93 		}
     94 	}
     95 	return nil
     96 }