2024-04-26 15:05:21 +00:00
|
|
|
package postgres
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2024-05-30 09:35:30 +00:00
|
|
|
"github.com/zitadel/logging"
|
|
|
|
|
2024-04-26 15:05:21 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/database"
|
|
|
|
"github.com/zitadel/zitadel/internal/v2/eventstore"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
_ eventstore.Pusher = (*Storage)(nil)
|
|
|
|
_ eventstore.Querier = (*Storage)(nil)
|
|
|
|
)
|
|
|
|
|
|
|
|
type Storage struct {
|
2024-08-12 10:33:45 +00:00
|
|
|
client *database.DB
|
|
|
|
config *Config
|
|
|
|
pushPositionStmt string
|
2024-05-30 09:35:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
MaxRetries uint32
|
2024-04-26 15:05:21 +00:00
|
|
|
}
|
|
|
|
|
2024-05-30 09:35:30 +00:00
|
|
|
func New(client *database.DB, config *Config) *Storage {
|
|
|
|
initPushStmt(client.Type())
|
2024-04-26 15:05:21 +00:00
|
|
|
return &Storage{
|
2024-08-12 10:33:45 +00:00
|
|
|
client: client,
|
|
|
|
config: config,
|
|
|
|
pushPositionStmt: initPushStmt(client.Type()),
|
2024-05-30 09:35:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-12 10:33:45 +00:00
|
|
|
func initPushStmt(typ string) string {
|
2024-05-30 09:35:30 +00:00
|
|
|
switch typ {
|
|
|
|
case "cockroach":
|
2024-08-12 10:33:45 +00:00
|
|
|
return ", hlc_to_timestamp(cluster_logical_timestamp()), cluster_logical_timestamp()"
|
2024-05-30 09:35:30 +00:00
|
|
|
case "postgres":
|
2024-08-12 10:33:45 +00:00
|
|
|
return ", statement_timestamp(), EXTRACT(EPOCH FROM clock_timestamp())"
|
2024-05-30 09:35:30 +00:00
|
|
|
default:
|
|
|
|
logging.WithFields("database_type", typ).Panic("position statement for type not implemented")
|
2024-08-12 10:33:45 +00:00
|
|
|
return ""
|
2024-04-26 15:05:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Health implements eventstore.Pusher.
|
|
|
|
func (s *Storage) Health(ctx context.Context) error {
|
|
|
|
return s.client.PingContext(ctx)
|
|
|
|
}
|