filer: default the filemeta CREATE TABLE for postgres2/mysql2 when createTable is unset (#10232)

* postgres2: default the filemeta CREATE TABLE when createTable is unset

An empty createTable rendered through fmt.Sprintf produced
%!(EXTRA string=filemeta), which Postgres rejected with a syntax error at
init. Fall back to a working template so a minimal config bootstraps.

* mysql2: default the filemeta CREATE TABLE when createTable is unset

Same empty-template failure the postgres2 path had: an unset createTable
rendered to %!(EXTRA string=filemeta) and MySQL rejected it at init.
Fall back to a working template.
This commit is contained in:
Chris Lu
2026-07-05 10:17:40 -07:00
committed by GitHub
parent c332323b01
commit 873705baf9
6 changed files with 36 additions and 0 deletions
+4
View File
@@ -22,6 +22,10 @@ type SqlGenMysql struct {
// is left for users to opt into via an explicit upsertQuery.
const DefaultUpsertQuery = "INSERT INTO `%s` (`dirhash`,`name`,`directory`,`meta`) VALUES(?,?,?,?) ON DUPLICATE KEY UPDATE `meta` = VALUES(`meta`)"
// DefaultCreateTableQuery is used when createTable is left unset; an empty
// template would otherwise render as %!(EXTRA ...) garbage SQL.
const DefaultCreateTableQuery = "CREATE TABLE IF NOT EXISTS `%s` (`dirhash` BIGINT NOT NULL, `name` VARCHAR(766) NOT NULL, `directory` TEXT NOT NULL, `meta` LONGBLOB, PRIMARY KEY (`dirhash`, `name`)) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"
var (
_ = abstract_sql.SqlGenerator(&SqlGenMysql{})
)
+11
View File
@@ -19,6 +19,17 @@ func TestDefaultUpsertQueryUsesOnDuplicateKey(t *testing.T) {
}
}
func TestDefaultCreateTableQueryRendersValidSql(t *testing.T) {
gen := &SqlGenMysql{CreateTableSqlTemplate: DefaultCreateTableQuery}
got := gen.GetSqlCreateTable("filemeta")
if strings.Contains(got, "%!") {
t.Fatalf("template rendered to malformed SQL, got: %s", got)
}
if !strings.Contains(got, "`filemeta`") {
t.Fatalf("expected backticked table name, got: %s", got)
}
}
func TestEmptyUpsertTemplateFallsBackToPlainInsert(t *testing.T) {
gen := &SqlGenMysql{}
got := gen.GetSqlInsert("filemeta")
+3
View File
@@ -58,6 +58,9 @@ func (store *MysqlStore2) initialize(createTable, upsertQuery string, enableUpse
maxLifetimeSeconds int, interpolateParams bool) (err error) {
store.SupportBucketTable = true
if createTable == "" {
createTable = mysql.DefaultCreateTableQuery
}
if !enableUpsert {
upsertQuery = ""
} else if upsertQuery == "" {
+4
View File
@@ -20,6 +20,10 @@ type SqlGenPostgres struct {
// user enables upsert but does not provide their own template.
const DefaultUpsertQuery = `INSERT INTO "%s" (dirhash,name,directory,meta) VALUES($1,$2,$3,$4) ON CONFLICT (dirhash, name) DO UPDATE SET directory=EXCLUDED.directory, meta=EXCLUDED.meta`
// DefaultCreateTableQuery is used when createTable is left unset; an empty
// template would otherwise render as %!(EXTRA ...) garbage SQL.
const DefaultCreateTableQuery = `CREATE TABLE IF NOT EXISTS "%s" (dirhash BIGINT, name VARCHAR(65535), directory VARCHAR(65535), meta bytea, PRIMARY KEY (dirhash, name))`
var (
_ = abstract_sql.SqlGenerator(&SqlGenPostgres{})
)
@@ -16,6 +16,17 @@ func TestDefaultUpsertQueryIsConflictSafe(t *testing.T) {
}
}
func TestDefaultCreateTableQueryRendersValidSql(t *testing.T) {
gen := &SqlGenPostgres{CreateTableSqlTemplate: DefaultCreateTableQuery}
got := gen.GetSqlCreateTable("filemeta")
if strings.Contains(got, "%!") {
t.Fatalf("template rendered to malformed SQL, got: %s", got)
}
if !strings.Contains(got, `"filemeta"`) {
t.Fatalf("expected quoted table name, got: %s", got)
}
}
func TestEmptyUpsertTemplateFallsBackToPlainInsert(t *testing.T) {
gen := &SqlGenPostgres{}
got := gen.GetSqlInsert("filemeta")
+3
View File
@@ -63,6 +63,9 @@ func (store *PostgresStore2) Initialize(configuration util.Configuration, prefix
func (store *PostgresStore2) initialize(createTable, upsertQuery string, enableUpsert bool, user, password, hostname string, port int, database, schema, sslmode, sslcert, sslkey, sslrootcert, sslcrl string, pgbouncerCompatible bool, maxIdle, maxOpen, maxLifetimeSeconds int) (err error) {
store.SupportBucketTable = true
if createTable == "" {
createTable = postgres.DefaultCreateTableQuery
}
if !enableUpsert {
upsertQuery = ""
} else if upsertQuery == "" {