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() +}