db

package
v0.0.0-...-fdc595f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 10, 2025 License: BSD-3-Clause Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	AuditLogEventTypeUserInserted         = "user.inserted"
	AuditLogEventTypeUserDeleted          = "user.deleted"
	AuditLogEventTypeUserPasswordInserted = "user.password.inserted"
	AuditLogEventTypeUserEmailInserted    = "user.email.inserted"
	AuditLogEventTypeUserRoleInserted     = "user.role.inserted"
	AuditLogEventTypeUserSessionInserted  = "user.session.inserted"
)

Variables

This section is empty.

Functions

func Connect

func Connect(ctx context.Context, connString string) (*pgxpool.Pool, error)

Connect establishes a connection to the PostgreSQL database using the provided connection string. It returns a connection pool and an optional error. If the error is not nil, the connection pool is nil and does not need closing.

The connection string must either be a DSN or a URL. The DSN format is:

host=localhost port=5432 user=postgres password=secret dbname=mydb sslmode=disable pool_max_conns=10

The URL format is:

postgres://user:password@localhost:5432/mydb?sslmode=disable&pool_max_conns=10

func CreateTestDB

func CreateTestDB(t testingT, opts ...testDatabaseOption) (*pgxpool.Pool, error)

CreateTestDB connects to the running test database and returns a connection pool. It will create a new ephemeral database for the test run, and clean up after itself afterwards.

func EmbeddedMigrationsSource

func EmbeddedMigrationsSource() (source.Driver, error)

EmbeddedMigrationsSource returns a source driver for the embedded migrations. These files are embedded into the binary using the `embed` package.

func ExecuteMigrations

func ExecuteMigrations(ctx context.Context, migrations source.Driver, db *pgxpool.Pool) error

ExecuteMigrations executes the migrations against the provided database connection pool. It leaves the database pool open for further use. It will return an error if any migration fails, or if the source driver cannot be created. It will not return an error if there are no changes to be made (i.e. the database is already up to date).

func WithAdditionalMigration

func WithAdditionalMigration(name, content string) testDatabaseOption

func WithoutMigrations

func WithoutMigrations() testDatabaseOption

Types

type AuditLogEvent

type AuditLogEvent struct {
	ID        int64              `db:"id" json:"id"`
	EventType string             `db:"event_type" json:"event_type"`
	EventData AuditLogEventData  `db:"event_data" json:"event_data"`
	CreatedAt pgtype.Timestamptz `db:"created_at" json:"created_at"`
}

type AuditLogEventData

type AuditLogEventData struct {
	// By is the user who performed the action.
	// This is not necessarily a record in the users table, but could be a reference to a CLI or similar.
	By string `json:"by"`

	UserInserted         *AuditLogUserInserted         `json:"user_inserted,omitempty"`
	UserDeleted          *AuditLogUserDeleted          `json:"user_deleted,omitempty"`
	UserPasswordInserted *AuditLogUserPasswordInserted `json:"user_password_inserted,omitempty"`
	UserEmailInserted    *AuditLogUserEmailInserted    `json:"user_email_inserted,omitempty"`
	UserRoleInserted     *AuditLogUserRoleInserted     `json:"user_role_inserted,omitempty"`
	UserSessionInserted  *AuditLogUserSessionInserted  `json:"user_session_inserted,omitempty"`
}

type AuditLogUserDeleted

type AuditLogUserDeleted struct {
	UserID string `json:"user_id"`
}

type AuditLogUserEmailInserted

type AuditLogUserEmailInserted struct {
	UserID    string `json:"user_id"`
	Email     string `json:"email"`
	Verified  bool   `json:"verified"`
	IsPrimary bool   `json:"is_primary"`
}

type AuditLogUserInserted

type AuditLogUserInserted struct {
	UserID   string `json:"user_id"`
	Username string `json:"username"`
}

type AuditLogUserPasswordInserted

type AuditLogUserPasswordInserted struct {
	UserID string `json:"user_id"`
}

type AuditLogUserRoleInserted

type AuditLogUserRoleInserted struct {
	UserID string `json:"user_id"`
	Role   string `json:"role"`
}

type AuditLogUserSessionInserted

type AuditLogUserSessionInserted struct {
	UserID string    `json:"user_id"`
	Token  string    `json:"token"`
	Expiry time.Time `json:"expiry"`
	Method string    `json:"method"`
}

type CreateAuditLogEventParams

type CreateAuditLogEventParams struct {
	EventType string            `db:"event_type" json:"event_type"`
	EventData AuditLogEventData `db:"event_data" json:"event_data"`
}

type CreateUserEmailParams

type CreateUserEmailParams struct {
	ID        pgtype.UUID `db:"id" json:"id"`
	Email     string      `db:"email" json:"email"`
	Verified  bool        `db:"verified" json:"verified"`
	IsPrimary bool        `db:"is_primary" json:"is_primary"`
}

type CreateUserParams

type CreateUserParams struct {
	ID       pgtype.UUID `db:"id" json:"id"`
	Username string      `db:"username" json:"username"`
}

type CreateUserPasswordParams

type CreateUserPasswordParams struct {
	ID       pgtype.UUID `db:"id" json:"id"`
	Password string      `db:"password" json:"password"`
}

type CreateUserRoleParams

type CreateUserRoleParams struct {
	ID   pgtype.UUID `db:"id" json:"id"`
	Role string      `db:"role" json:"role"`
}

type CreateUserSessionParams

type CreateUserSessionParams struct {
	ID           pgtype.UUID        `db:"id" json:"id"`
	SessionToken string             `db:"session_token" json:"session_token"`
	Expiry       pgtype.Timestamptz `db:"expiry" json:"expiry"`
}

type CreateUserSessionRow

type CreateUserSessionRow struct {
	ID           pgtype.UUID        `db:"id" json:"id"`
	SessionToken string             `db:"session_token" json:"session_token"`
	Expiry       pgtype.Timestamptz `db:"expiry" json:"expiry"`
}

type DBTX

type DBTX interface {
	Exec(context.Context, string, ...interface{}) (pgconn.CommandTag, error)
	Query(context.Context, string, ...interface{}) (pgx.Rows, error)
	QueryRow(context.Context, string, ...interface{}) pgx.Row
}

type Flags

type Flags struct {
	Database string `` /* 136-byte string literal not displayed */
}

func (*Flags) Connect

func (f *Flags) Connect(ctx context.Context) (*pgxpool.Pool, error)

type GetAuditLogEventsParams

type GetAuditLogEventsParams struct {
	Limit  int32 `db:"limit" json:"limit"`
	Offset int32 `db:"offset" json:"offset"`
}

type GetUserByTokenRow

type GetUserByTokenRow struct {
	ID       pgtype.UUID `db:"id" json:"id"`
	Username string      `db:"username" json:"username"`
	Roles    []string    `db:"roles" json:"roles"`
}

type GetUserEmailsParams

type GetUserEmailsParams struct {
	ID     pgtype.UUID `db:"id" json:"id"`
	Limit  int32       `db:"limit" json:"limit"`
	Offset int32       `db:"offset" json:"offset"`
}

type GetUserEmailsRow

type GetUserEmailsRow struct {
	ID        pgtype.UUID `db:"id" json:"id"`
	Email     string      `db:"email" json:"email"`
	Verified  bool        `db:"verified" json:"verified"`
	IsPrimary bool        `db:"is_primary" json:"is_primary"`
}

type GetUserWithOptionalPasswordByUsernameRow

type GetUserWithOptionalPasswordByUsernameRow struct {
	ID       pgtype.UUID `db:"id" json:"id"`
	Password *string     `db:"password" json:"password"`
}

type Organisation

type Organisation struct {
	ID        pgtype.UUID        `db:"id" json:"id"`
	Service   string             `db:"service" json:"service"`
	Name      string             `db:"name" json:"name"`
	CreatedAt pgtype.Timestamptz `db:"created_at" json:"created_at"`
}

type Querier

type Querier interface {
	// Creates a new audit log event with the given type and data.
	//
	//  INSERT INTO audit_log_events (event_type, event_data)
	//  VALUES ($1, $2)
	//  RETURNING id
	CreateAuditLogEvent(ctx context.Context, arg CreateAuditLogEventParams) (int64, error)
	// Creates a new user with the given ID and username.
	//
	//  INSERT INTO users (id, username)
	//  VALUES ($1, $2)
	//  RETURNING id, username, created_at, updated_at
	CreateUser(ctx context.Context, arg CreateUserParams) (*User, error)
	// Creates a new user email with the given ID and email address.
	//
	//  INSERT INTO user_emails (id, email, verified, is_primary)
	//  VALUES ($1, $2, $3, $4)
	//  ON CONFLICT (id, email) DO UPDATE
	//      SET
	//          verified = excluded.verified,
	//          is_primary = excluded.is_primary
	CreateUserEmail(ctx context.Context, arg CreateUserEmailParams) error
	// Creates a new user password with the given ID and password hash.
	//
	//  INSERT INTO user_passwords (id, password)
	//  VALUES ($1, $2)
	//  ON CONFLICT (id) DO UPDATE
	//      SET password = excluded.password
	CreateUserPassword(ctx context.Context, arg CreateUserPasswordParams) error
	// Creates a new user role with the given ID and role name.
	//
	//  INSERT INTO user_roles (id, role)
	//  VALUES ($1, $2)
	//  ON CONFLICT (id, role) DO NOTHING
	CreateUserRole(ctx context.Context, arg CreateUserRoleParams) error
	// Creates a new user session with the given ID and session token.
	//
	//  INSERT INTO user_sessions (id, session_token, expiry)
	//  VALUES ($1, $2, $3)
	//  RETURNING id, session_token, expiry
	CreateUserSession(ctx context.Context, arg CreateUserSessionParams) (*CreateUserSessionRow, error)
	// Deletes a user with the given ID.
	//
	//  DELETE FROM users
	//  WHERE id = $1
	DeleteUser(ctx context.Context, id pgtype.UUID) error
	// Gets all audit log events in a page.
	//
	//  SELECT
	//      id,
	//      event_type,
	//      event_data,
	//      created_at
	//  FROM audit_log_events
	//  ORDER BY id ASC
	//  LIMIT $1 OFFSET $2
	GetAuditLogEvents(ctx context.Context, arg GetAuditLogEventsParams) ([]*AuditLogEvent, error)
	// Gets a user by their ID.
	//
	//  SELECT
	//      users.id,
	//      users.username,
	//      users.created_at,
	//      users.updated_at
	//  FROM users
	//  WHERE users.id = $1
	GetUserByID(ctx context.Context, id pgtype.UUID) (*User, error)
	// Gets a user by one of their session tokens.
	//
	//  SELECT
	//      users.id,
	//      users.username,
	//      ARRAY_AGG(user_roles.role)::TEXT [] AS roles
	//  FROM user_sessions
	//  INNER JOIN users ON user_sessions.id = users.id
	//  INNER JOIN user_roles ON users.id = user_roles.id
	//  WHERE user_sessions.session_token = $1
	//  GROUP BY users.id, users.username
	GetUserByToken(ctx context.Context, sessionToken string) (*GetUserByTokenRow, error)
	// Gets a user by their username.
	//
	//  SELECT
	//      users.id,
	//      users.username,
	//      users.created_at,
	//      users.updated_at
	//  FROM users
	//  WHERE users.username = $1
	GetUserByUsername(ctx context.Context, username string) (*User, error)
	// Gets all emails for a user with the given ID in a page.
	//
	//  SELECT
	//      user_emails.id,
	//      user_emails.email,
	//      user_emails.verified,
	//      user_emails.is_primary
	//  FROM user_emails
	//  WHERE user_emails.id = $1
	//  ORDER BY user_emails.is_primary DESC, user_emails.email ASC
	//  LIMIT $2 OFFSET $3
	GetUserEmails(ctx context.Context, arg GetUserEmailsParams) ([]*GetUserEmailsRow, error)
	// Gets a user by their username, optionally including their password hash.
	//
	//  SELECT
	//      users.id,
	//      user_passwords.password
	//  FROM users
	//  LEFT JOIN user_passwords ON users.id = user_passwords.id
	//  WHERE users.username = $1
	GetUserWithOptionalPasswordByUsername(ctx context.Context, username string) (*GetUserWithOptionalPasswordByUsernameRow, error)
}

type Queries

type Queries struct {
	// contains filtered or unexported fields
}

func New

func New(db DBTX) *Queries

func (*Queries) CreateAuditLogEvent

func (q *Queries) CreateAuditLogEvent(ctx context.Context, arg CreateAuditLogEventParams) (int64, error)

Creates a new audit log event with the given type and data.

INSERT INTO audit_log_events (event_type, event_data)
VALUES ($1, $2)
RETURNING id

func (*Queries) CreateUser

func (q *Queries) CreateUser(ctx context.Context, arg CreateUserParams) (*User, error)

Creates a new user with the given ID and username.

INSERT INTO users (id, username)
VALUES ($1, $2)
RETURNING id, username, created_at, updated_at

func (*Queries) CreateUserEmail

func (q *Queries) CreateUserEmail(ctx context.Context, arg CreateUserEmailParams) error

Creates a new user email with the given ID and email address.

INSERT INTO user_emails (id, email, verified, is_primary)
VALUES ($1, $2, $3, $4)
ON CONFLICT (id, email) DO UPDATE
    SET
        verified = excluded.verified,
        is_primary = excluded.is_primary

func (*Queries) CreateUserPassword

func (q *Queries) CreateUserPassword(ctx context.Context, arg CreateUserPasswordParams) error

Creates a new user password with the given ID and password hash.

INSERT INTO user_passwords (id, password)
VALUES ($1, $2)
ON CONFLICT (id) DO UPDATE
    SET password = excluded.password

func (*Queries) CreateUserRole

func (q *Queries) CreateUserRole(ctx context.Context, arg CreateUserRoleParams) error

Creates a new user role with the given ID and role name.

INSERT INTO user_roles (id, role)
VALUES ($1, $2)
ON CONFLICT (id, role) DO NOTHING

func (*Queries) CreateUserSession

func (q *Queries) CreateUserSession(ctx context.Context, arg CreateUserSessionParams) (*CreateUserSessionRow, error)

Creates a new user session with the given ID and session token.

INSERT INTO user_sessions (id, session_token, expiry)
VALUES ($1, $2, $3)
RETURNING id, session_token, expiry

func (*Queries) DeleteUser

func (q *Queries) DeleteUser(ctx context.Context, id pgtype.UUID) error

Deletes a user with the given ID.

DELETE FROM users
WHERE id = $1

func (*Queries) GetAuditLogEvents

func (q *Queries) GetAuditLogEvents(ctx context.Context, arg GetAuditLogEventsParams) ([]*AuditLogEvent, error)

Gets all audit log events in a page.

SELECT
    id,
    event_type,
    event_data,
    created_at
FROM audit_log_events
ORDER BY id ASC
LIMIT $1 OFFSET $2

func (*Queries) GetUserByID

func (q *Queries) GetUserByID(ctx context.Context, id pgtype.UUID) (*User, error)

Gets a user by their ID.

SELECT
    users.id,
    users.username,
    users.created_at,
    users.updated_at
FROM users
WHERE users.id = $1

func (*Queries) GetUserByToken

func (q *Queries) GetUserByToken(ctx context.Context, sessionToken string) (*GetUserByTokenRow, error)

Gets a user by one of their session tokens.

SELECT
    users.id,
    users.username,
    ARRAY_AGG(user_roles.role)::TEXT [] AS roles
FROM user_sessions
INNER JOIN users ON user_sessions.id = users.id
INNER JOIN user_roles ON users.id = user_roles.id
WHERE user_sessions.session_token = $1
GROUP BY users.id, users.username

func (*Queries) GetUserByUsername

func (q *Queries) GetUserByUsername(ctx context.Context, username string) (*User, error)

Gets a user by their username.

SELECT
    users.id,
    users.username,
    users.created_at,
    users.updated_at
FROM users
WHERE users.username = $1

func (*Queries) GetUserEmails

func (q *Queries) GetUserEmails(ctx context.Context, arg GetUserEmailsParams) ([]*GetUserEmailsRow, error)

Gets all emails for a user with the given ID in a page.

SELECT
    user_emails.id,
    user_emails.email,
    user_emails.verified,
    user_emails.is_primary
FROM user_emails
WHERE user_emails.id = $1
ORDER BY user_emails.is_primary DESC, user_emails.email ASC
LIMIT $2 OFFSET $3

func (*Queries) GetUserWithOptionalPasswordByUsername

func (q *Queries) GetUserWithOptionalPasswordByUsername(ctx context.Context, username string) (*GetUserWithOptionalPasswordByUsernameRow, error)

Gets a user by their username, optionally including their password hash.

SELECT
    users.id,
    user_passwords.password
FROM users
LEFT JOIN user_passwords ON users.id = user_passwords.id
WHERE users.username = $1

func (*Queries) WithTx

func (q *Queries) WithTx(tx pgx.Tx) *Queries

type Report

type Report struct {
	ID           pgtype.UUID        `db:"id" json:"id"`
	RepositoryID pgtype.UUID        `db:"repository_id" json:"repository_id"`
	Commit       string             `db:"commit" json:"commit"`
	CreatedAt    pgtype.Timestamptz `db:"created_at" json:"created_at"`
}

type ReportFile

type ReportFile struct {
	ID           pgtype.UUID        `db:"id" json:"id"`
	RepositoryID pgtype.UUID        `db:"repository_id" json:"repository_id"`
	ReportID     pgtype.UUID        `db:"report_id" json:"report_id"`
	FilePath     string             `db:"file_path" json:"file_path"`
	Coverage     pgtype.Numeric     `db:"coverage" json:"coverage"`
	CreatedAt    pgtype.Timestamptz `db:"created_at" json:"created_at"`
}

type ReportFileLineRegion

type ReportFileLineRegion struct {
	ID           pgtype.UUID        `db:"id" json:"id"`
	RepositoryID pgtype.UUID        `db:"repository_id" json:"repository_id"`
	ReportFileID pgtype.UUID        `db:"report_file_id" json:"report_file_id"`
	StartLine    int32              `db:"start_line" json:"start_line"`
	EndLine      int32              `db:"end_line" json:"end_line"`
	StartColumn  int32              `db:"start_column" json:"start_column"`
	EndColumn    int32              `db:"end_column" json:"end_column"`
	Statements   int32              `db:"statements" json:"statements"`
	Executed     int32              `db:"executed" json:"executed"`
	CreatedAt    pgtype.Timestamptz `db:"created_at" json:"created_at"`
}

type ReportFlag

type ReportFlag struct {
	ReportID  pgtype.UUID        `db:"report_id" json:"report_id"`
	Name      string             `db:"name" json:"name"`
	CreatedAt pgtype.Timestamptz `db:"created_at" json:"created_at"`
}

type Repository

type Repository struct {
	ID             pgtype.UUID        `db:"id" json:"id"`
	OrganisationID pgtype.UUID        `db:"organisation_id" json:"organisation_id"`
	Name           string             `db:"name" json:"name"`
	CreatedAt      pgtype.Timestamptz `db:"created_at" json:"created_at"`
}

type User

type User struct {
	ID        pgtype.UUID        `db:"id" json:"id"`
	Username  string             `db:"username" json:"username"`
	CreatedAt pgtype.Timestamptz `db:"created_at" json:"created_at"`
	UpdatedAt pgtype.Timestamptz `db:"updated_at" json:"updated_at"`
}

type UserEmail

type UserEmail struct {
	ID        pgtype.UUID        `db:"id" json:"id"`
	Email     string             `db:"email" json:"email"`
	Verified  bool               `db:"verified" json:"verified"`
	IsPrimary bool               `db:"is_primary" json:"is_primary"`
	CreatedAt pgtype.Timestamptz `db:"created_at" json:"created_at"`
}

type UserPassword

type UserPassword struct {
	ID        pgtype.UUID        `db:"id" json:"id"`
	Password  string             `db:"password" json:"password"`
	CreatedAt pgtype.Timestamptz `db:"created_at" json:"created_at"`
}

type UserRole

type UserRole struct {
	ID        pgtype.UUID        `db:"id" json:"id"`
	Role      string             `db:"role" json:"role"`
	CreatedAt pgtype.Timestamptz `db:"created_at" json:"created_at"`
}

type UserSession

type UserSession struct {
	ID           pgtype.UUID        `db:"id" json:"id"`
	SessionToken string             `db:"session_token" json:"session_token"`
	Expiry       pgtype.Timestamptz `db:"expiry" json:"expiry"`
	CreatedAt    pgtype.Timestamptz `db:"created_at" json:"created_at"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL