Skip to content

Commit

Permalink
fix for #860: hardcoded DB name + crash when DB is not initialized
Browse files Browse the repository at this point in the history
  • Loading branch information
or-else committed May 2, 2023
1 parent 1ec2559 commit 52f33fe
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
19 changes: 9 additions & 10 deletions server/db/postgres/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,26 +150,25 @@ func (a *adapter) Open(jsonconfig json.RawMessage) error {
return errors.New("adapter postgres failed to parse config: " + err.Error())
}

// This just initializes the driver but does not open the network connection.
// ConnectConfig creates a new Pool and immediately establishes one connection.
a.db, err = pgxpool.ConnectConfig(ctx, a.poolConfig)
if isMissingDb(err) {
// Ignore missing database here. If we are initializing the database
// missing DB is OK.
if a.poolConfig.ConnConfig.Database == "tinode" {
a.poolConfig.ConnConfig.Database = "postgres"
a.db, err = pgxpool.ConnectConfig(ctx, a.poolConfig)
}
// Missing DB is OK if we are initializing the database.
// Since tinode DB does not exist, connect without specifying the DB name.
a.poolConfig.ConnConfig.Database = ""
a.db, err = pgxpool.ConnectConfig(ctx, a.poolConfig)
}
if err != nil {
return err
}

// Actually opening the network connection.
err = a.db.Ping(ctx)
// Actually opening the network connection if one was not opened earlier.
if a.poolConfig.LazyConnect {
err = a.db.Ping(ctx)
}

if err == nil {
if config.MaxOpenConns > 0 {

a.poolConfig.MaxConns = int32(config.MaxOpenConns)
}
if config.MaxIdleConns > 0 {
Expand Down
5 changes: 4 additions & 1 deletion tinode-db/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,10 @@ func main() {
defer store.Store.Close()

adapterVersion := store.Store.GetAdapterVersion()
databaseVersion := store.Store.GetDbVersion()
databaseVersion := 0
if store.Store.IsOpen() {
databaseVersion = store.Store.GetDbVersion()
}
log.Printf("Database adapter: '%s'; version: %d", store.Store.GetAdapterName(), adapterVersion)

var created bool
Expand Down

0 comments on commit 52f33fe

Please sign in to comment.