From 2fddcae20eef14029b79452074619c5de3b4a06e Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Tue, 1 Dec 2026 16:58:17 -0800 Subject: [PATCH] filer/postgres_flat: name a missing filemeta table instead of a foreign layout With createTable = false and no filemeta table, the layout check said the table had no path column and blamed a postgres2 layout. Report the missing table and the key-value table that also has to be created. Co-Authored-By: Claude Opus 5.5 --- weed/filer/postgres_flat/postgres_flat_store.go | 14 +++++++++----- .../postgres_flat/postgres_flat_store_test.go | 9 +++++++++ 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/weed/filer/postgres_flat/postgres_flat_store.go b/weed/filer/postgres_flat/postgres_flat_store.go index 5d12070fb8..2717e1c04f 100644 --- a/weed/filer/postgres_flat/postgres_flat_store.go +++ b/weed/filer/postgres_flat/postgres_flat_store.go @@ -85,14 +85,18 @@ func (store *PostgresFlatStore) initialize(createTable, upsertQuery string, enab return nil } -// checkFlatLayout refuses a filemeta table of the postgres or postgres2 layout. +// checkFlatLayout refuses a missing filemeta table, which only a createTable +// of false leaves, and one of the postgres or postgres2 layout. func checkFlatLayout(ctx context.Context, db *sql.DB) error { - var found bool - if err := db.QueryRowContext(ctx, `SELECT EXISTS (SELECT 1 FROM information_schema.columns - WHERE table_schema = current_schema() AND table_name = $1 AND column_name = 'path')`, abstract_sql.DEFAULT_TABLE).Scan(&found); err != nil { + var columns, pathColumns int + if err := db.QueryRowContext(ctx, `SELECT count(*), count(*) FILTER (WHERE column_name = 'path') FROM information_schema.columns + WHERE table_schema = current_schema() AND table_name = $1`, abstract_sql.DEFAULT_TABLE).Scan(&columns, &pathColumns); err != nil { return fmt.Errorf("check table %s: %v", abstract_sql.DEFAULT_TABLE, err) } - if !found { + if columns == 0 { + return fmt.Errorf("table %s does not exist: with createTable = false, create it and %s beforehand", abstract_sql.DEFAULT_TABLE, kvTable) + } + if pathColumns == 0 { return fmt.Errorf("table %s has no path column: postgres_flat needs a database or schema of its own, apart from the postgres and postgres2 stores", abstract_sql.DEFAULT_TABLE) } return nil diff --git a/weed/filer/postgres_flat/postgres_flat_store_test.go b/weed/filer/postgres_flat/postgres_flat_store_test.go index 6308f8693d..8a095fdca9 100644 --- a/weed/filer/postgres_flat/postgres_flat_store_test.go +++ b/weed/filer/postgres_flat/postgres_flat_store_test.go @@ -72,3 +72,12 @@ func TestRefusesAnotherLayout(t *testing.T) { } store.Shutdown() } + +func TestRefusesAMissingTableWithoutDDL(t *testing.T) { + store := &PostgresFlatStore{} + err := store.initialize("false", "", true, postgres.Connection{DSNOptions: store_test.NewPostgresTestDatabase(t)}) + if err == nil || !strings.Contains(err.Error(), "does not exist") { + t.Fatalf("initialize without DDL on an empty schema: %v", err) + } + store.Shutdown() +}