Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions memcache/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,18 @@ type Config struct {
//
// Optional. Default is false
Reset bool

// DisableStartupCheck skips the initial connection validation during New.
//
// Optional. Default is false
DisableStartupCheck bool
Comment on lines +77 to +80
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Replace hard tabs with spaces to comply with markdown linting rules.

The static analysis tool detected hard tabs in the documentation. The content is correct, but formatting should use spaces for consistency.

Apply this fix:

-	// DisableStartupCheck skips the initial connection validation during New.
-	//
-	// Optional. Default is false
-	DisableStartupCheck bool
+	// DisableStartupCheck skips the initial connection validation during New.
+	//
+	// Optional. Default is false
+	DisableStartupCheck bool
-	DisableStartupCheck: false,
+	DisableStartupCheck: false,

Based on static analysis hints.

Also applies to: 88-88

🧰 Tools
🪛 markdownlint-cli2 (0.18.1)

77-77: Hard tabs
Column: 1

(MD010, no-hard-tabs)


78-78: Hard tabs
Column: 1

(MD010, no-hard-tabs)


79-79: Hard tabs
Column: 1

(MD010, no-hard-tabs)


80-80: Hard tabs
Column: 1

(MD010, no-hard-tabs)

🤖 Prompt for AI Agents
In memcache/README.md around lines 77-80 and also line 88, replace any hard tab
characters with spaces to satisfy markdown linting; open the file, locate the
indented lines shown (the comment block for DisableStartupCheck and the other
occurrence at line 88), convert tabs to the project’s preferred number of spaces
(typically 2 or 4) and save the file so the documentation uses only spaces for
indentation.

}
```

### Default Config
```go
var ConfigDefault = Config{
Servers: "127.0.0.1:11211",
DisableStartupCheck: false,
}
```
12 changes: 9 additions & 3 deletions memcache/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ type Config struct {
// Optional. Default is false
Reset bool

// DisableStartupCheck skips the initial connection validation during New.
//
// Optional. Default is false
DisableStartupCheck bool

// The socket read/write timeout.
//
// Optional. Default is 100 * time.Millisecond
Expand All @@ -31,9 +36,10 @@ type Config struct {

// ConfigDefault is the default config
var ConfigDefault = Config{
Servers: "127.0.0.1:11211",
timeout: 100 * time.Millisecond,
maxIdleConns: 2,
Servers: "127.0.0.1:11211",
timeout: 100 * time.Millisecond,
maxIdleConns: 2,
DisableStartupCheck: false,
}

// Helper function to set default values
Expand Down
16 changes: 9 additions & 7 deletions memcache/memcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,17 @@ func New(config ...Config) *Storage {
db.Timeout = cfg.timeout
db.MaxIdleConns = cfg.maxIdleConns

// Ping database to ensure a connection has been made
if err := db.Ping(); err != nil {
panic(err)
}

if cfg.Reset {
if err := db.DeleteAll(); err != nil {
if !cfg.DisableStartupCheck {
// Ping database to ensure a connection has been made
if err := db.Ping(); err != nil {
panic(err)
}

if cfg.Reset {
if err := db.DeleteAll(); err != nil {
panic(err)
}
}
}

// Create storage
Expand Down
10 changes: 10 additions & 0 deletions memcache/memcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,16 @@ func Test_Memcache_Reset(t *testing.T) {
require.Zero(t, len(result))
}

func Test_Memcache_New_DisableStartupCheck(t *testing.T) {
require.NotPanics(t, func() {
store := New(Config{
Servers: "127.0.0.1:11210",
DisableStartupCheck: true,
})
require.NotNil(t, store)
})
}

func Test_Memcache_Close(t *testing.T) {
testStore := newTestStore(t)
require.Nil(t, testStore.Close())
Expand Down
138 changes: 72 additions & 66 deletions postgres/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,85 +56,91 @@ store := postgres.New()

// Initialize custom config
store := postgres.New(postgres.Config{
DB: dbPool,
Table: "fiber_storage",
Reset: false,
GCInterval: 10 * time.Second,
DB: dbPool,
Table: "fiber_storage",
Reset: false,
GCInterval: 10 * time.Second,
})
```

### Config
```go
// Config defines the config for storage.
type Config struct {
// DB pgxpool.Pool object will override connection uri and other connection fields
//
// Optional. Default is nil
DB *pgxpool.Pool

// Connection string to use for DB. Will override all other authentication values if used
//
// Optional. Default is ""
ConnectionURI string

// Host name where the DB is hosted
//
// Optional. Default is "127.0.0.1"
Host string

// Port where the DB is listening on
//
// Optional. Default is 5432
Port int

// Server username
//
// Optional. Default is ""
Username string

// Server password
//
// Optional. Default is ""
Password string

// Database name
//
// Optional. Default is "fiber"
Database string

// Table name
//
// Optional. Default is "fiber_storage"
Table string

// The SSL mode for the connection
//
// Optional. Default is "disable"
SSLMode string

// Reset clears any existing keys in existing Table
//
// Optional. Default is false
Reset bool

// Time before deleting expired keys
//
// Optional. Default is 10 * time.Second
GCInterval time.Duration
// DB pgxpool.Pool object will override connection uri and other connection fields
//
// Optional. Default is nil
DB *pgxpool.Pool

// Connection string to use for DB. Will override all other authentication values if used
//
// Optional. Default is ""
ConnectionURI string

// Host name where the DB is hosted
//
// Optional. Default is "127.0.0.1"
Host string

// Port where the DB is listening on
//
// Optional. Default is 5432
Port int

// Server username
//
// Optional. Default is ""
Username string

// Server password
//
// Optional. Default is ""
Password string

// Database name
//
// Optional. Default is "fiber"
Database string

// Table name
//
// Optional. Default is "fiber_storage"
Table string

// The SSL mode for the connection
//
// Optional. Default is "disable"
SSLMode string

// Reset clears any existing keys in existing Table
//
// Optional. Default is false
Reset bool

// DisableStartupCheck skips the initial connection validation during New.
//
// Optional. Default is false
DisableStartupCheck bool

Comment on lines +120 to +124
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Docs still show New returning Storage.

Code now returns (*Storage, error); update the README signature/comments to match.

🤖 Prompt for AI Agents
In postgres/README.md around lines 120 to 124 the docs still state that New
returns Storage; update the README comments/signature to reflect the current
function signature which returns (*Storage, error). Edit the text to show New
returns a pointer and an error (e.g., "New returns (*Storage, error)") and
adjust any surrounding examples or descriptions to mention the error return
value and that callers must handle the returned error.

// Time before deleting expired keys
//
// Optional. Default is 10 * time.Second
GCInterval time.Duration
}
```

### Default Config
```go
// ConfigDefault is the default config
var ConfigDefault = Config{
ConnectionURI: "",
Host: "127.0.0.1",
Port: 5432,
Database: "fiber",
Table: "fiber_storage",
SSLMode: "disable",
Reset: false,
GCInterval: 10 * time.Second,
ConnectionURI: "",
Host: "127.0.0.1",
Port: 5432,
Database: "fiber",
Table: "fiber_storage",
SSLMode: "disable",
Reset: false,
DisableStartupCheck: false,
GCInterval: 10 * time.Second,
}
```
22 changes: 14 additions & 8 deletions postgres/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ type Config struct {
// Optional. Default is false
Reset bool

// DisableStartupCheck skips the initial connection validation during New.
//
// Optional. Default is false
DisableStartupCheck bool

// Time before deleting expired keys
//
// Optional. Default is 10 * time.Second
Expand All @@ -69,14 +74,15 @@ type Config struct {

// ConfigDefault is the default config
var ConfigDefault = Config{
ConnectionURI: "",
Host: "127.0.0.1",
Port: 5432,
Database: "fiber",
Table: "fiber_storage",
SSLMode: "disable",
Reset: false,
GCInterval: 10 * time.Second,
ConnectionURI: "",
Host: "127.0.0.1",
Port: 5432,
Database: "fiber",
Table: "fiber_storage",
SSLMode: "disable",
Reset: false,
DisableStartupCheck: false,
GCInterval: 10 * time.Second,
}

func (c *Config) getDSN() string {
Expand Down
68 changes: 36 additions & 32 deletions postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,45 +68,47 @@ func New(config ...Config) *Storage {
}
}

// Ping database
if err := db.Ping(context.Background()); err != nil {
panic(err)
}

// Parse out schema in config, if provided
schema := "public"
tableName := cfg.Table
if strings.Contains(cfg.Table, ".") {
schema = strings.Split(cfg.Table, ".")[0]
tableName = strings.Split(cfg.Table, ".")[1]
}

// Drop table if set to true
if cfg.Reset {
if _, err := db.Exec(context.Background(), fmt.Sprintf(dropQuery, cfg.Table)); err != nil {
db.Close()
if !cfg.DisableStartupCheck {
// Ping database
if err := db.Ping(context.Background()); err != nil {
panic(err)
}
}

// Determine if table exists
tableExists := false
row := db.QueryRow(context.Background(), fmt.Sprintf(checkTableExistsQuery, schema, tableName))
var count int
if err := row.Scan(&count); err != nil {
db.Close()
panic(err)
}
tableExists = count > 0
// Parse out schema in config, if provided
schema := "public"
tableName := cfg.Table
if strings.Contains(cfg.Table, ".") {
schema = strings.Split(cfg.Table, ".")[0]
tableName = strings.Split(cfg.Table, ".")[1]
}

// Init database queries
if !tableExists {
for _, query := range initQuery {
if _, err := db.Exec(context.Background(), fmt.Sprintf(query, cfg.Table)); err != nil {
// Drop table if set to true
if cfg.Reset {
if _, err := db.Exec(context.Background(), fmt.Sprintf(dropQuery, cfg.Table)); err != nil {
db.Close()
panic(err)
}
}

// Determine if table exists
tableExists := false
row := db.QueryRow(context.Background(), fmt.Sprintf(checkTableExistsQuery, schema, tableName))
var count int
if err := row.Scan(&count); err != nil {
db.Close()
panic(err)
}
tableExists = count > 0

// Init database queries
if !tableExists {
for _, query := range initQuery {
if _, err := db.Exec(context.Background(), fmt.Sprintf(query, cfg.Table)); err != nil {
db.Close()
panic(err)
}
}
}
}

// Create storage
Expand All @@ -121,7 +123,9 @@ func New(config ...Config) *Storage {
sqlGC: fmt.Sprintf("DELETE FROM %s WHERE e <= $1 AND e != 0", cfg.Table),
}

store.checkSchema(cfg.Table)
if !cfg.DisableStartupCheck {
store.checkSchema(cfg.Table)
}
Comment on lines 121 to +128

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Keep PostgreSQL table setup independent of startup check flag

Similar to the MySQL change, guarding the entire initialization block with if !cfg.DisableStartupCheck now prevents the storage from creating or resetting its table when the flag is true. With startup checks disabled, New returns a store whose underlying table may not exist and whose schema is never verified, causing later operations or the GC ticker to return errors once the application first touches the store. The flag should only suppress the ping, not the schema creation/reset logic.

Useful? React with 👍 / 👎.


// Start garbage collector
go store.gcTicker()
Expand Down
12 changes: 12 additions & 0 deletions postgres/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,18 @@ func TestNoCreateUser(t *testing.T) {
})

}

func Test_Postgres_New_DisableStartupCheck(t *testing.T) {
require.NotPanics(t, func() {
store := New(Config{
Host: "127.0.0.1",
Port: 65432,
DisableStartupCheck: true,
})
require.NotNil(t, store)
defer store.Close()
})
}
func Test_Should_Panic_On_Wrong_Schema(t *testing.T) {
testStore := newTestStore(t)
defer testStore.Close()
Expand Down
Loading