mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 10:37:23 +01:00
ReadConnection reads the settings and their defaults, Connection.Open opens the store's pools. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK
80 lines
2.3 KiB
Go
80 lines
2.3 KiB
Go
// Package postgres2 provides PostgreSQL filer store implementation with bucket support
|
|
// Migrated from github.com/lib/pq to github.com/jackc/pgx for:
|
|
// - Active development and support
|
|
// - Better performance and PostgreSQL-specific features
|
|
// - Improved error handling (no more panics)
|
|
// - Built-in logging capabilities
|
|
// - Superior SSL certificate support
|
|
package postgres2
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/filer"
|
|
"github.com/seaweedfs/seaweedfs/weed/filer/abstract_sql"
|
|
"github.com/seaweedfs/seaweedfs/weed/filer/postgres"
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
|
)
|
|
|
|
var _ filer.BucketAware = (*PostgresStore2)(nil)
|
|
|
|
func init() {
|
|
filer.Stores = append(filer.Stores, &PostgresStore2{})
|
|
}
|
|
|
|
type PostgresStore2 struct {
|
|
abstract_sql.AbstractSqlStore
|
|
}
|
|
|
|
func (store *PostgresStore2) GetName() string {
|
|
return "postgres2"
|
|
}
|
|
|
|
func (store *PostgresStore2) Initialize(configuration util.Configuration, prefix string) (err error) {
|
|
// Default on so minimal configs are not exposed to duplicate-key tx
|
|
// poisoning on Postgres; an explicit false still disables it.
|
|
configuration.SetDefault(prefix+"enableUpsert", true)
|
|
return store.initialize(
|
|
configuration.GetString(prefix+"createTable"),
|
|
configuration.GetString(prefix+"upsertQuery"),
|
|
configuration.GetBool(prefix+"enableUpsert"),
|
|
postgres.ReadConnection(configuration, prefix),
|
|
)
|
|
}
|
|
|
|
func (store *PostgresStore2) initialize(createTable, upsertQuery string, enableUpsert bool, conn postgres.Connection) (err error) {
|
|
|
|
store.SupportBucketTable = true
|
|
store.SkipDDL = createTable == "false"
|
|
createTable = postgres.ResolveCreateTableQuery(createTable)
|
|
if createTable == "" && !store.SkipDDL {
|
|
createTable = postgres.DefaultCreateTableQuery
|
|
}
|
|
if !enableUpsert {
|
|
upsertQuery = ""
|
|
} else if upsertQuery == "" {
|
|
upsertQuery = postgres.DefaultUpsertQuery
|
|
}
|
|
gen := &postgres.SqlGenPostgres{
|
|
CreateTableSqlTemplate: createTable,
|
|
DropTableSqlTemplate: `drop table if exists "%s"`,
|
|
UpsertQueryTemplate: upsertQuery,
|
|
}
|
|
store.SqlGenerator = gen
|
|
|
|
if err = conn.Open(&store.AbstractSqlStore); err != nil {
|
|
return err
|
|
}
|
|
|
|
if !store.SkipDDL {
|
|
if err = store.CreateTable(context.Background(), abstract_sql.DEFAULT_TABLE); err != nil {
|
|
return fmt.Errorf("init table %s: %v", abstract_sql.DEFAULT_TABLE, err)
|
|
}
|
|
}
|
|
|
|
postgres.ConfigureListOrdering(store.DB, gen)
|
|
|
|
return nil
|
|
}
|