Documentation
¶
Index ¶
- Constants
- func Connect(ctx context.Context, connString string) (*pgxpool.Pool, error)
- func CreateTestDB(t testingT, opts ...testDatabaseOption) (*pgxpool.Pool, error)
- func EmbeddedMigrationsSource() (source.Driver, error)
- func ExecuteMigrations(ctx context.Context, migrations source.Driver, db *pgxpool.Pool) error
- func WithAdditionalMigration(name, content string) testDatabaseOption
- func WithoutMigrations() testDatabaseOption
- type AuditLogEvent
- type AuditLogEventData
- type AuditLogUserDeleted
- type AuditLogUserEmailInserted
- type AuditLogUserInserted
- type AuditLogUserPasswordInserted
- type AuditLogUserRoleInserted
- type AuditLogUserSessionInserted
- type CreateAuditLogEventParams
- type CreateUserEmailParams
- type CreateUserParams
- type CreateUserPasswordParams
- type CreateUserRoleParams
- type CreateUserSessionParams
- type CreateUserSessionRow
- type DBTX
- type Flags
- type GetAuditLogEventsParams
- type GetUserByTokenRow
- type GetUserEmailsParams
- type GetUserEmailsRow
- type GetUserWithOptionalPasswordByUsernameRow
- type Organisation
- type Querier
- type Queries
- func (q *Queries) CreateAuditLogEvent(ctx context.Context, arg CreateAuditLogEventParams) (int64, error)
- func (q *Queries) CreateUser(ctx context.Context, arg CreateUserParams) (*User, error)
- func (q *Queries) CreateUserEmail(ctx context.Context, arg CreateUserEmailParams) error
- func (q *Queries) CreateUserPassword(ctx context.Context, arg CreateUserPasswordParams) error
- func (q *Queries) CreateUserRole(ctx context.Context, arg CreateUserRoleParams) error
- func (q *Queries) CreateUserSession(ctx context.Context, arg CreateUserSessionParams) (*CreateUserSessionRow, error)
- func (q *Queries) DeleteUser(ctx context.Context, id pgtype.UUID) error
- func (q *Queries) GetAuditLogEvents(ctx context.Context, arg GetAuditLogEventsParams) ([]*AuditLogEvent, error)
- func (q *Queries) GetUserByID(ctx context.Context, id pgtype.UUID) (*User, error)
- func (q *Queries) GetUserByToken(ctx context.Context, sessionToken string) (*GetUserByTokenRow, error)
- func (q *Queries) GetUserByUsername(ctx context.Context, username string) (*User, error)
- func (q *Queries) GetUserEmails(ctx context.Context, arg GetUserEmailsParams) ([]*GetUserEmailsRow, error)
- func (q *Queries) GetUserWithOptionalPasswordByUsername(ctx context.Context, username string) (*GetUserWithOptionalPasswordByUsernameRow, error)
- func (q *Queries) WithTx(tx pgx.Tx) *Queries
- type Report
- type ReportFile
- type ReportFileLineRegion
- type ReportFlag
- type Repository
- type User
- type UserEmail
- type UserPassword
- type UserRole
- type UserSession
Constants ¶
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 ¶
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 ¶
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 ¶
EmbeddedMigrationsSource returns a source driver for the embedded migrations. These files are embedded into the binary using the `embed` package.
func ExecuteMigrations ¶
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 AuditLogUserInserted ¶
type AuditLogUserPasswordInserted ¶
type AuditLogUserPasswordInserted struct {
UserID string `json:"user_id"`
}
type CreateAuditLogEventParams ¶
type CreateAuditLogEventParams struct {
EventType string `db:"event_type" json:"event_type"`
EventData AuditLogEventData `db:"event_data" json:"event_data"`
}
type CreateUserEmailParams ¶
type CreateUserParams ¶
type CreateUserRoleParams ¶
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 GetAuditLogEventsParams ¶
type GetUserByTokenRow ¶
type GetUserEmailsParams ¶
type GetUserEmailsRow ¶
type Organisation ¶
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 (*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 ¶
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) 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 ¶
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 ¶
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
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 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 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"`
}