From 4dc9c34bccf50ad2b4cddc2bc71d476c145cffc6 Mon Sep 17 00:00:00 2001 From: logikonline Date: Sun, 8 Feb 2026 11:16:07 -0500 Subject: [PATCH] fix(plugin): use DriverName instead of Dialect for DB detection Replace x.Dialect().URI().DBType with x.DriverName() for more reliable database driver detection. Add support for 'pgx' and 'sqlite' driver variants alongside existing 'postgres' and 'sqlite3'. Improve logging with driver information and error messages for better migration debugging. --- plugin.go | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/plugin.go b/plugin.go index 93fb4d0..f906507 100644 --- a/plugin.go +++ b/plugin.go @@ -140,15 +140,16 @@ func runColumnMigrations(ctx context.Context, x *xorm.Engine) error { // columnExists checks if a column exists in a table func columnExists(x *xorm.Engine, table, column string) (bool, error) { - dialect := x.Dialect().URI().DBType + driverName := x.DriverName() + log.Info("Vault migration: checking column %s.%s (driver: %s)", table, column, driverName) var sql string - switch dialect { - case "postgres": + switch driverName { + case "postgres", "pgx": sql = fmt.Sprintf(`SELECT 1 FROM information_schema.columns WHERE table_name = '%s' AND column_name = '%s'`, table, column) case "mysql": sql = fmt.Sprintf(`SELECT 1 FROM information_schema.columns WHERE table_name = '%s' AND column_name = '%s'`, table, column) - case "sqlite3": + case "sqlite3", "sqlite": // SQLite uses PRAGMA results, err := x.Query(fmt.Sprintf("PRAGMA table_info(%s)", table)) if err != nil { @@ -163,34 +164,40 @@ func columnExists(x *xorm.Engine, table, column string) (bool, error) { case "mssql": sql = fmt.Sprintf(`SELECT 1 FROM sys.columns WHERE object_id = OBJECT_ID('%s') AND name = '%s'`, table, column) default: - // Fallback: try postgres-style + // Fallback: try information_schema (works for postgres/mysql) + log.Warn("Vault migration: unknown driver '%s', using information_schema fallback", driverName) sql = fmt.Sprintf(`SELECT 1 FROM information_schema.columns WHERE table_name = '%s' AND column_name = '%s'`, table, column) } results, err := x.Query(sql) if err != nil { + log.Error("Vault migration: column check failed: %v", err) return false, err } - return len(results) > 0, nil + exists := len(results) > 0 + log.Info("Vault migration: column %s.%s exists: %v", table, column, exists) + return exists, nil } // buildAddColumnSQL builds the appropriate ALTER TABLE statement for the database type func buildAddColumnSQL(x *xorm.Engine, table, column, columnType, defaultVal string) string { - dialect := x.Dialect().URI().DBType + driverName := x.DriverName() + log.Info("Vault migration: building ALTER TABLE for driver: %s", driverName) - switch dialect { - case "postgres": + switch driverName { + case "postgres", "pgx": return fmt.Sprintf(`ALTER TABLE "%s" ADD COLUMN IF NOT EXISTS "%s" %s NOT NULL DEFAULT %s`, table, column, columnType, defaultVal) case "mysql": // MySQL doesn't have IF NOT EXISTS for columns, but we already checked return fmt.Sprintf("ALTER TABLE `%s` ADD COLUMN `%s` %s NOT NULL DEFAULT %s", table, column, columnType, defaultVal) - case "sqlite3": + case "sqlite3", "sqlite": return fmt.Sprintf(`ALTER TABLE "%s" ADD COLUMN "%s" %s NOT NULL DEFAULT %s`, table, column, columnType, defaultVal) case "mssql": return fmt.Sprintf(`ALTER TABLE [%s] ADD [%s] %s NOT NULL DEFAULT %s`, table, column, columnType, defaultVal) default: - // Fallback to standard SQL - return fmt.Sprintf(`ALTER TABLE "%s" ADD COLUMN "%s" %s NOT NULL DEFAULT %s`, table, column, columnType, defaultVal) + // Fallback to postgres-style (most compatible) + log.Warn("Vault migration: unknown driver '%s', using postgres-style ALTER TABLE", driverName) + return fmt.Sprintf(`ALTER TABLE "%s" ADD COLUMN IF NOT EXISTS "%s" %s NOT NULL DEFAULT %s`, table, column, columnType, defaultVal) } }