Skip to content

Commit

Permalink
postgres: add Init function
Browse files Browse the repository at this point in the history
Signed-off-by: Hank Donnay <hdonnay@redhat.com>
  • Loading branch information
hdonnay committed Feb 22, 2022
1 parent e0db5ac commit 164219e
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion notifier/postgres/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,50 @@ package postgres

import (
"context"
"database/sql"
"fmt"

"github.com/jackc/pgconn"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/jackc/pgx/v4/stdlib"
"github.com/prometheus/client_golang/prometheus"
"github.com/quay/zlog"
"github.com/remind101/migrate"

"github.com/quay/clair/v4/notifier/migrations"
)

// Store implements the notifier.Store interface
// Store implements the notifier.Store interface.
type Store struct {
pool *pgxpool.Pool
}

// NewStore returns a Store using the passed-in Pool.
//
// The caller should close the Pool once the store is no longer needed.
func NewStore(pool *pgxpool.Pool) *Store {
return &Store{pool}
}

// Init initializes the database using the specified config.
func Init(ctx context.Context, cfg *pgx.ConnConfig) error {
ctx = zlog.ContextWithValues(ctx, "component", "notifier/postgres/Init")
db, err := sql.Open("pgx", stdlib.RegisterConnConfig(cfg))
if err != nil {
return fmt.Errorf("failed to open db: %w", err)
}
defer db.Close()

zlog.Info(ctx).Msg("performing notifier migrations")
migrator := migrate.NewPostgresMigrator(db)
migrator.Table = migrations.MigrationTable
if err := migrator.Exec(migrate.Up, migrations.Migrations...); err != nil {
return fmt.Errorf("failed to perform migrations: %w", err)
}
return nil
}

// As a rule of thumb, admin/worker locks should be in the two-part keyspace to
// avoid clashing with the manifest locks. They're engine wide, so it's a
// concern even here in the notifier's bailiwick.
Expand Down

0 comments on commit 164219e

Please sign in to comment.