mirror of
https://github.com/NginxProxyManager/nginx-proxy-manager.git
synced 2025-08-28 19:40:28 +00:00
Ditch dbmate in favour of internal migration
such that migration files can be embedded
This commit is contained in:
@@ -0,0 +1,172 @@
|
||||
-- migrate:up
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `user`
|
||||
(
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
created_on INTEGER NOT NULL DEFAULT 0,
|
||||
modified_on INTEGER NOT NULL DEFAULT 0,
|
||||
name TEXT NOT NULL,
|
||||
nickname TEXT NOT NULL,
|
||||
email TEXT NOT NULL,
|
||||
roles TEXT NOT NULL,
|
||||
is_disabled INTEGER NOT NULL DEFAULT 0,
|
||||
is_deleted INTEGER NOT NULL DEFAULT 0
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `auth`
|
||||
(
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
created_on INTEGER NOT NULL DEFAULT 0,
|
||||
modified_on INTEGER NOT NULL DEFAULT 0,
|
||||
user_id INTEGER NOT NULL,
|
||||
type TEXT NOT NULL,
|
||||
secret TEXT NOT NULL,
|
||||
is_deleted INTEGER NOT NULL DEFAULT 0,
|
||||
FOREIGN KEY (user_id) REFERENCES user (id),
|
||||
UNIQUE (user_id, type)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `setting`
|
||||
(
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
created_on INTEGER NOT NULL DEFAULT 0,
|
||||
modified_on INTEGER NOT NULL DEFAULT 0,
|
||||
name TEXT NOT NULL,
|
||||
value TEXT NOT NULL,
|
||||
UNIQUE (name)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `audit_log`
|
||||
(
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
created_on INTEGER NOT NULL DEFAULT 0,
|
||||
modified_on INTEGER NOT NULL DEFAULT 0,
|
||||
user_id INTEGER NOT NULL,
|
||||
object_type TEXT NOT NULL,
|
||||
object_id INTEGER NOT NULL,
|
||||
action TEXT NOT NULL,
|
||||
meta TEXT NOT NULL,
|
||||
FOREIGN KEY (user_id) REFERENCES user (id)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `certificate_authority`
|
||||
(
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
created_on INTEGER NOT NULL DEFAULT 0,
|
||||
modified_on INTEGER NOT NULL DEFAULT 0,
|
||||
name TEXT NOT NULL,
|
||||
acme2_url TEXT NOT NULL,
|
||||
is_deleted INTEGER NOT NULL DEFAULT 0
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `dns_provider`
|
||||
(
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
created_on INTEGER NOT NULL DEFAULT 0,
|
||||
modified_on INTEGER NOT NULL DEFAULT 0,
|
||||
user_id INTEGER NOT NULL,
|
||||
provider_key TEXT NOT NULL,
|
||||
name TEXT NOT NULL,
|
||||
meta TEXT NOT NULL,
|
||||
is_deleted INTEGER NOT NULL DEFAULT 0,
|
||||
FOREIGN KEY (user_id) REFERENCES user (id)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `certificate`
|
||||
(
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
created_on INTEGER NOT NULL DEFAULT 0,
|
||||
modified_on INTEGER NOT NULL DEFAULT 0,
|
||||
type TEXT NOT NULL, -- custom,dns,http
|
||||
user_id INTEGER NOT NULL,
|
||||
certificate_authority_id INTEGER, -- 0 for a custom cert
|
||||
dns_provider_id INTEGER, -- 0, for a http or custom cert
|
||||
name TEXT NOT NULL,
|
||||
domain_names TEXT NOT NULL,
|
||||
expires_on INTEGER DEFAULT 0,
|
||||
status TEXT NOT NULL, -- ready,requesting,failed,provided
|
||||
error_message text NOT NULL DEFAULT "",
|
||||
meta TEXT NOT NULL,
|
||||
is_deleted INTEGER NOT NULL DEFAULT 0,
|
||||
FOREIGN KEY (user_id) REFERENCES user (id),
|
||||
FOREIGN KEY (certificate_authority_id) REFERENCES certificate_authority (id),
|
||||
FOREIGN KEY (dns_provider_id) REFERENCES dns_provider (id)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `stream`
|
||||
(
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
created_on INTEGER NOT NULL DEFAULT 0,
|
||||
modified_on INTEGER NOT NULL DEFAULT 0,
|
||||
user_id INTEGER NOT NULL,
|
||||
listen_interface TEXT NOT NULL,
|
||||
incoming_port INTEGER NOT NULL,
|
||||
upstream_options TEXT NOT NULL,
|
||||
tcp_forwarding INTEGER NOT NULL DEFAULT 0,
|
||||
udp_forwarding INTEGER NOT NULL DEFAULT 0,
|
||||
advanced_config TEXT NOT NULL,
|
||||
is_disabled INTEGER NOT NULL DEFAULT 0,
|
||||
is_deleted INTEGER NOT NULL DEFAULT 0,
|
||||
FOREIGN KEY (user_id) REFERENCES user (id)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `upstream`
|
||||
(
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
created_on INTEGER NOT NULL DEFAULT 0,
|
||||
modified_on INTEGER NOT NULL DEFAULT 0,
|
||||
user_id INTEGER NOT NULL,
|
||||
hosts TEXT NOT NULL,
|
||||
balance_method TEXT NOT NULL,
|
||||
max_fails INTEGER NOT NULL DEFAULT 1,
|
||||
fail_timeout INTEGER NOT NULL DEFAULT 10,
|
||||
advanced_config TEXT NOT NULL,
|
||||
is_deleted INTEGER NOT NULL DEFAULT 0,
|
||||
FOREIGN KEY (user_id) REFERENCES user (id)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `access_list`
|
||||
(
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
created_on INTEGER NOT NULL DEFAULT 0,
|
||||
modified_on INTEGER NOT NULL DEFAULT 0,
|
||||
user_id INTEGER NOT NULL,
|
||||
name TEXT NOT NULL,
|
||||
meta TEXT NOT NULL,
|
||||
is_deleted INTEGER NOT NULL DEFAULT 0,
|
||||
FOREIGN KEY (user_id) REFERENCES user (id)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `host`
|
||||
(
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
created_on INTEGER NOT NULL DEFAULT 0,
|
||||
modified_on INTEGER NOT NULL DEFAULT 0,
|
||||
user_id INTEGER NOT NULL,
|
||||
type TEXT NOT NULL,
|
||||
listen_interface TEXT NOT NULL,
|
||||
domain_names TEXT NOT NULL,
|
||||
upstream_id INTEGER NOT NULL,
|
||||
certificate_id INTEGER,
|
||||
access_list_id INTEGER,
|
||||
ssl_forced INTEGER NOT NULL DEFAULT 0,
|
||||
caching_enabled INTEGER NOT NULL DEFAULT 0,
|
||||
block_exploits INTEGER NOT NULL DEFAULT 0,
|
||||
allow_websocket_upgrade INTEGER NOT NULL DEFAULT 0,
|
||||
http2_support INTEGER NOT NULL DEFAULT 0,
|
||||
hsts_enabled INTEGER NOT NULL DEFAULT 0,
|
||||
hsts_subdomains INTEGER NOT NULL DEFAULT 0,
|
||||
paths TEXT NOT NULL,
|
||||
upstream_options TEXT NOT NULL DEFAULT "",
|
||||
advanced_config TEXT NOT NULL DEFAULT "",
|
||||
is_disabled INTEGER NOT NULL DEFAULT 0,
|
||||
is_deleted INTEGER NOT NULL DEFAULT 0,
|
||||
FOREIGN KEY (user_id) REFERENCES user (id),
|
||||
FOREIGN KEY (upstream_id) REFERENCES upstream (id),
|
||||
FOREIGN KEY (certificate_id) REFERENCES certificate (id),
|
||||
FOREIGN KEY (access_list_id) REFERENCES access_list (id)
|
||||
);
|
||||
|
||||
-- migrate:down
|
||||
|
||||
-- Not allowed to go down from initial
|
@@ -0,0 +1,38 @@
|
||||
-- migrate:up
|
||||
|
||||
-- Default error reporting setting
|
||||
INSERT INTO `setting` (
|
||||
created_on,
|
||||
modified_on,
|
||||
name,
|
||||
value
|
||||
) VALUES (
|
||||
strftime('%s', 'now'),
|
||||
strftime('%s', 'now'),
|
||||
"error-reporting",
|
||||
"true"
|
||||
);
|
||||
|
||||
-- Default Certificate Authorities
|
||||
|
||||
INSERT INTO `certificate_authority` (
|
||||
created_on,
|
||||
modified_on,
|
||||
name,
|
||||
acme2_url
|
||||
) VALUES (
|
||||
strftime('%s', 'now'),
|
||||
strftime('%s', 'now'),
|
||||
"Let's Encrypt",
|
||||
"https://acme-v02.api.letsencrypt.org/directory"
|
||||
), (
|
||||
strftime('%s', 'now'),
|
||||
strftime('%s', 'now'),
|
||||
"Let's Encrypt (Staging)",
|
||||
"https://acme-staging-v02.api.letsencrypt.org/directory"
|
||||
);
|
||||
|
||||
|
||||
-- migrate:down
|
||||
|
||||
-- Not allowed to go down from initial
|
205
backend/internal/database/migrator.go
Normal file
205
backend/internal/database/migrator.go
Normal file
@@ -0,0 +1,205 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"embed"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"npm/internal/logger"
|
||||
"npm/internal/util"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
)
|
||||
|
||||
//go:embed migrations/*.sql
|
||||
var migrationFiles embed.FS
|
||||
|
||||
// MigrationConfiguration options for the migrator.
|
||||
type MigrationConfiguration struct {
|
||||
Table string `json:"table"`
|
||||
mux sync.Mutex
|
||||
}
|
||||
|
||||
// Default migrator configuration
|
||||
var mConfiguration = MigrationConfiguration{
|
||||
Table: "migration",
|
||||
}
|
||||
|
||||
// ConfigureMigrator and will return error if missing required fields.
|
||||
func ConfigureMigrator(c *MigrationConfiguration) error {
|
||||
// ensure updates to the config are atomic
|
||||
mConfiguration.mux.Lock()
|
||||
defer mConfiguration.mux.Unlock()
|
||||
if c == nil {
|
||||
return fmt.Errorf("a non nil Configuration is mandatory")
|
||||
}
|
||||
if strings.TrimSpace(c.Table) != "" {
|
||||
mConfiguration.Table = c.Table
|
||||
}
|
||||
mConfiguration.Table = c.Table
|
||||
return nil
|
||||
}
|
||||
|
||||
type afterMigrationComplete func()
|
||||
|
||||
// Migrate will perform the migration from start to finish
|
||||
func Migrate(followup afterMigrationComplete) bool {
|
||||
logger.Info("Migration: Started")
|
||||
|
||||
// Try to connect to the database sleeping for 15 seconds in between
|
||||
var db *sqlx.DB
|
||||
for {
|
||||
db = GetInstance()
|
||||
if db == nil {
|
||||
logger.Warn("Database is unavailable for migration, retrying in 15 seconds")
|
||||
time.Sleep(15 * time.Second)
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// Check for migration table existence
|
||||
if !tableExists(db, mConfiguration.Table) {
|
||||
err := createMigrationTable(db)
|
||||
if err != nil {
|
||||
logger.Error("MigratorError", err)
|
||||
return false
|
||||
}
|
||||
logger.Info("Migration: Migration Table created")
|
||||
}
|
||||
|
||||
// DO MIGRATION
|
||||
migrationCount, migrateErr := performFileMigrations(db)
|
||||
if migrateErr != nil {
|
||||
logger.Error("MigratorError", migrateErr)
|
||||
}
|
||||
|
||||
if migrateErr == nil {
|
||||
logger.Info("Migration: Completed %v migration files", migrationCount)
|
||||
followup()
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// createMigrationTable performs a query to create the migration table
|
||||
// with the name specified in the configuration
|
||||
func createMigrationTable(db *sqlx.DB) error {
|
||||
logger.Info("Migration: Creating Migration Table: %v", mConfiguration.Table)
|
||||
// nolint:lll
|
||||
query := fmt.Sprintf("CREATE TABLE IF NOT EXISTS `%v` (filename TEXT PRIMARY KEY, migrated_on INTEGER NOT NULL DEFAULT 0)", mConfiguration.Table)
|
||||
_, err := db.Exec(query)
|
||||
return err
|
||||
}
|
||||
|
||||
// tableExists will check the database for the existence of the specified table.
|
||||
func tableExists(db *sqlx.DB, tableName string) bool {
|
||||
query := `SELECT name FROM sqlite_master WHERE type='table' AND name = $1`
|
||||
|
||||
row := db.QueryRowx(query, tableName)
|
||||
if row == nil {
|
||||
logger.Error("MigratorError", fmt.Errorf("Cannot check if table exists, no row returned: %v", tableName))
|
||||
return false
|
||||
}
|
||||
|
||||
var exists *bool
|
||||
if err := row.Scan(&exists); err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return false
|
||||
}
|
||||
logger.Error("MigratorError", err)
|
||||
return false
|
||||
}
|
||||
return *exists
|
||||
}
|
||||
|
||||
// performFileMigrations will perform the actual migration,
|
||||
// importing files and updating the database with the rows imported.
|
||||
func performFileMigrations(db *sqlx.DB) (int, error) {
|
||||
var importedCount = 0
|
||||
|
||||
// Grab a list of previously ran migrations from the database:
|
||||
previousMigrations, prevErr := getPreviousMigrations(db)
|
||||
if prevErr != nil {
|
||||
return importedCount, prevErr
|
||||
}
|
||||
|
||||
// List up the ".sql" files on disk
|
||||
err := fs.WalkDir(migrationFiles, ".", func(file string, d fs.DirEntry, err error) error {
|
||||
if !d.IsDir() {
|
||||
shortFile := filepath.Base(file)
|
||||
|
||||
// Check if this file already exists in the previous migrations
|
||||
// and if so, ignore it
|
||||
if util.SliceContainsItem(previousMigrations, shortFile) {
|
||||
return nil
|
||||
}
|
||||
|
||||
logger.Info("Migration: Importing %v", shortFile)
|
||||
|
||||
sqlContents, ioErr := migrationFiles.ReadFile(path.Clean(file))
|
||||
if ioErr != nil {
|
||||
return ioErr
|
||||
}
|
||||
|
||||
sqlString := string(sqlContents)
|
||||
|
||||
tx := db.MustBegin()
|
||||
if _, execErr := tx.Exec(sqlString); execErr != nil {
|
||||
return execErr
|
||||
}
|
||||
if commitErr := tx.Commit(); commitErr != nil {
|
||||
return commitErr
|
||||
}
|
||||
if markErr := markMigrationSuccessful(db, shortFile); markErr != nil {
|
||||
return markErr
|
||||
}
|
||||
|
||||
importedCount++
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
return importedCount, err
|
||||
}
|
||||
|
||||
// getPreviousMigrations will query the migration table for names
|
||||
// of migrations we can ignore because they should have already
|
||||
// been imported
|
||||
func getPreviousMigrations(db *sqlx.DB) ([]string, error) {
|
||||
var existingMigrations []string
|
||||
// nolint:gosec
|
||||
query := fmt.Sprintf("SELECT filename FROM `%v` ORDER BY filename", mConfiguration.Table)
|
||||
rows, err := db.Queryx(query)
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return existingMigrations, nil
|
||||
}
|
||||
return existingMigrations, err
|
||||
}
|
||||
|
||||
for rows.Next() {
|
||||
var filename *string
|
||||
err := rows.Scan(&filename)
|
||||
if err != nil {
|
||||
return existingMigrations, err
|
||||
}
|
||||
existingMigrations = append(existingMigrations, *filename)
|
||||
}
|
||||
|
||||
return existingMigrations, nil
|
||||
}
|
||||
|
||||
// markMigrationSuccessful will add a row to the migration table
|
||||
func markMigrationSuccessful(db *sqlx.DB, filename string) error {
|
||||
// nolint:gosec
|
||||
query := fmt.Sprintf("INSERT INTO `%v` (filename) VALUES ($1)", mConfiguration.Table)
|
||||
_, err := db.Exec(query, filename)
|
||||
return err
|
||||
}
|
Reference in New Issue
Block a user