mirror of
https://github.com/PrivateCaptcha/PrivateCaptcha.git
synced 2026-02-11 08:18:52 -06:00
- more aggressive caching of API keys and added cache warmup - remove direct DB access in APIkey() middleware immediately - decrease resources usage for test puzzles - decrease cache collision possibility for cached solved puzzles - cosmetic improvement in rate limiter and leaky bucket Updater logic - move user limits check and API key backfill to a separate goroutine
482 lines
13 KiB
Go
482 lines
13 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.29.0
|
|
// source: properties.sql
|
|
|
|
package generated
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
"time"
|
|
)
|
|
|
|
const createProperty = `-- name: CreateProperty :one
|
|
INSERT INTO backend.properties (name, org_id, creator_id, org_owner_id, domain, level, growth)
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
|
RETURNING id, name, external_id, org_id, creator_id, org_owner_id, domain, level, salt, growth, created_at, updated_at, deleted_at, validity_interval, allow_subdomains, allow_localhost, allow_replay
|
|
`
|
|
|
|
type CreatePropertyParams struct {
|
|
Name string `db:"name" json:"name"`
|
|
OrgID pgtype.Int4 `db:"org_id" json:"org_id"`
|
|
CreatorID pgtype.Int4 `db:"creator_id" json:"creator_id"`
|
|
OrgOwnerID pgtype.Int4 `db:"org_owner_id" json:"org_owner_id"`
|
|
Domain string `db:"domain" json:"domain"`
|
|
Level pgtype.Int2 `db:"level" json:"level"`
|
|
Growth DifficultyGrowth `db:"growth" json:"growth"`
|
|
}
|
|
|
|
func (q *Queries) CreateProperty(ctx context.Context, arg *CreatePropertyParams) (*Property, error) {
|
|
row := q.db.QueryRow(ctx, createProperty,
|
|
arg.Name,
|
|
arg.OrgID,
|
|
arg.CreatorID,
|
|
arg.OrgOwnerID,
|
|
arg.Domain,
|
|
arg.Level,
|
|
arg.Growth,
|
|
)
|
|
var i Property
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.ExternalID,
|
|
&i.OrgID,
|
|
&i.CreatorID,
|
|
&i.OrgOwnerID,
|
|
&i.Domain,
|
|
&i.Level,
|
|
&i.Salt,
|
|
&i.Growth,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.DeletedAt,
|
|
&i.ValidityInterval,
|
|
&i.AllowSubdomains,
|
|
&i.AllowLocalhost,
|
|
&i.AllowReplay,
|
|
)
|
|
return &i, err
|
|
}
|
|
|
|
const deleteProperties = `-- name: DeleteProperties :exec
|
|
DELETE FROM backend.properties WHERE id = ANY($1::INT[])
|
|
`
|
|
|
|
func (q *Queries) DeleteProperties(ctx context.Context, dollar_1 []int32) error {
|
|
_, err := q.db.Exec(ctx, deleteProperties, dollar_1)
|
|
return err
|
|
}
|
|
|
|
const getOrgProperties = `-- name: GetOrgProperties :many
|
|
SELECT id, name, external_id, org_id, creator_id, org_owner_id, domain, level, salt, growth, created_at, updated_at, deleted_at, validity_interval, allow_subdomains, allow_localhost, allow_replay from backend.properties WHERE org_id = $1 AND deleted_at IS NULL ORDER BY created_at
|
|
`
|
|
|
|
func (q *Queries) GetOrgProperties(ctx context.Context, orgID pgtype.Int4) ([]*Property, error) {
|
|
rows, err := q.db.Query(ctx, getOrgProperties, orgID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []*Property
|
|
for rows.Next() {
|
|
var i Property
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.ExternalID,
|
|
&i.OrgID,
|
|
&i.CreatorID,
|
|
&i.OrgOwnerID,
|
|
&i.Domain,
|
|
&i.Level,
|
|
&i.Salt,
|
|
&i.Growth,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.DeletedAt,
|
|
&i.ValidityInterval,
|
|
&i.AllowSubdomains,
|
|
&i.AllowLocalhost,
|
|
&i.AllowReplay,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, &i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const getOrgPropertyByName = `-- name: GetOrgPropertyByName :one
|
|
SELECT id, name, external_id, org_id, creator_id, org_owner_id, domain, level, salt, growth, created_at, updated_at, deleted_at, validity_interval, allow_subdomains, allow_localhost, allow_replay from backend.properties WHERE org_id = $1 AND name = $2 AND deleted_at IS NULL
|
|
`
|
|
|
|
type GetOrgPropertyByNameParams struct {
|
|
OrgID pgtype.Int4 `db:"org_id" json:"org_id"`
|
|
Name string `db:"name" json:"name"`
|
|
}
|
|
|
|
func (q *Queries) GetOrgPropertyByName(ctx context.Context, arg *GetOrgPropertyByNameParams) (*Property, error) {
|
|
row := q.db.QueryRow(ctx, getOrgPropertyByName, arg.OrgID, arg.Name)
|
|
var i Property
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.ExternalID,
|
|
&i.OrgID,
|
|
&i.CreatorID,
|
|
&i.OrgOwnerID,
|
|
&i.Domain,
|
|
&i.Level,
|
|
&i.Salt,
|
|
&i.Growth,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.DeletedAt,
|
|
&i.ValidityInterval,
|
|
&i.AllowSubdomains,
|
|
&i.AllowLocalhost,
|
|
&i.AllowReplay,
|
|
)
|
|
return &i, err
|
|
}
|
|
|
|
const getProperties = `-- name: GetProperties :many
|
|
SELECT id, name, external_id, org_id, creator_id, org_owner_id, domain, level, salt, growth, created_at, updated_at, deleted_at, validity_interval, allow_subdomains, allow_localhost, allow_replay FROM backend.properties LIMIT $1
|
|
`
|
|
|
|
func (q *Queries) GetProperties(ctx context.Context, limit int32) ([]*Property, error) {
|
|
rows, err := q.db.Query(ctx, getProperties, limit)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []*Property
|
|
for rows.Next() {
|
|
var i Property
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.ExternalID,
|
|
&i.OrgID,
|
|
&i.CreatorID,
|
|
&i.OrgOwnerID,
|
|
&i.Domain,
|
|
&i.Level,
|
|
&i.Salt,
|
|
&i.Growth,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.DeletedAt,
|
|
&i.ValidityInterval,
|
|
&i.AllowSubdomains,
|
|
&i.AllowLocalhost,
|
|
&i.AllowReplay,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, &i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const getPropertiesByExternalID = `-- name: GetPropertiesByExternalID :many
|
|
SELECT id, name, external_id, org_id, creator_id, org_owner_id, domain, level, salt, growth, created_at, updated_at, deleted_at, validity_interval, allow_subdomains, allow_localhost, allow_replay from backend.properties WHERE external_id = ANY($1::UUID[])
|
|
`
|
|
|
|
func (q *Queries) GetPropertiesByExternalID(ctx context.Context, dollar_1 []pgtype.UUID) ([]*Property, error) {
|
|
rows, err := q.db.Query(ctx, getPropertiesByExternalID, dollar_1)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []*Property
|
|
for rows.Next() {
|
|
var i Property
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.ExternalID,
|
|
&i.OrgID,
|
|
&i.CreatorID,
|
|
&i.OrgOwnerID,
|
|
&i.Domain,
|
|
&i.Level,
|
|
&i.Salt,
|
|
&i.Growth,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.DeletedAt,
|
|
&i.ValidityInterval,
|
|
&i.AllowSubdomains,
|
|
&i.AllowLocalhost,
|
|
&i.AllowReplay,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, &i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const getPropertiesByID = `-- name: GetPropertiesByID :many
|
|
SELECT id, name, external_id, org_id, creator_id, org_owner_id, domain, level, salt, growth, created_at, updated_at, deleted_at, validity_interval, allow_subdomains, allow_localhost, allow_replay from backend.properties WHERE id = ANY($1::INT[])
|
|
`
|
|
|
|
func (q *Queries) GetPropertiesByID(ctx context.Context, dollar_1 []int32) ([]*Property, error) {
|
|
rows, err := q.db.Query(ctx, getPropertiesByID, dollar_1)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []*Property
|
|
for rows.Next() {
|
|
var i Property
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.ExternalID,
|
|
&i.OrgID,
|
|
&i.CreatorID,
|
|
&i.OrgOwnerID,
|
|
&i.Domain,
|
|
&i.Level,
|
|
&i.Salt,
|
|
&i.Growth,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.DeletedAt,
|
|
&i.ValidityInterval,
|
|
&i.AllowSubdomains,
|
|
&i.AllowLocalhost,
|
|
&i.AllowReplay,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, &i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const getPropertyByExternalID = `-- name: GetPropertyByExternalID :one
|
|
SELECT id, name, external_id, org_id, creator_id, org_owner_id, domain, level, salt, growth, created_at, updated_at, deleted_at, validity_interval, allow_subdomains, allow_localhost, allow_replay from backend.properties WHERE external_id = $1
|
|
`
|
|
|
|
func (q *Queries) GetPropertyByExternalID(ctx context.Context, externalID pgtype.UUID) (*Property, error) {
|
|
row := q.db.QueryRow(ctx, getPropertyByExternalID, externalID)
|
|
var i Property
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.ExternalID,
|
|
&i.OrgID,
|
|
&i.CreatorID,
|
|
&i.OrgOwnerID,
|
|
&i.Domain,
|
|
&i.Level,
|
|
&i.Salt,
|
|
&i.Growth,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.DeletedAt,
|
|
&i.ValidityInterval,
|
|
&i.AllowSubdomains,
|
|
&i.AllowLocalhost,
|
|
&i.AllowReplay,
|
|
)
|
|
return &i, err
|
|
}
|
|
|
|
const getPropertyByID = `-- name: GetPropertyByID :one
|
|
SELECT id, name, external_id, org_id, creator_id, org_owner_id, domain, level, salt, growth, created_at, updated_at, deleted_at, validity_interval, allow_subdomains, allow_localhost, allow_replay from backend.properties WHERE id = $1
|
|
`
|
|
|
|
func (q *Queries) GetPropertyByID(ctx context.Context, id int32) (*Property, error) {
|
|
row := q.db.QueryRow(ctx, getPropertyByID, id)
|
|
var i Property
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.ExternalID,
|
|
&i.OrgID,
|
|
&i.CreatorID,
|
|
&i.OrgOwnerID,
|
|
&i.Domain,
|
|
&i.Level,
|
|
&i.Salt,
|
|
&i.Growth,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.DeletedAt,
|
|
&i.ValidityInterval,
|
|
&i.AllowSubdomains,
|
|
&i.AllowLocalhost,
|
|
&i.AllowReplay,
|
|
)
|
|
return &i, err
|
|
}
|
|
|
|
const getSoftDeletedProperties = `-- name: GetSoftDeletedProperties :many
|
|
SELECT p.id, p.name, p.external_id, p.org_id, p.creator_id, p.org_owner_id, p.domain, p.level, p.salt, p.growth, p.created_at, p.updated_at, p.deleted_at, p.validity_interval, p.allow_subdomains, p.allow_localhost, p.allow_replay
|
|
FROM backend.properties p
|
|
JOIN backend.organizations o ON p.org_id = o.id
|
|
JOIN backend.users u ON o.user_id = u.id
|
|
WHERE p.deleted_at IS NOT NULL
|
|
AND p.deleted_at < $1
|
|
AND o.deleted_at IS NULL
|
|
AND u.deleted_at IS NULL
|
|
LIMIT $2
|
|
`
|
|
|
|
type GetSoftDeletedPropertiesParams struct {
|
|
DeletedAt pgtype.Timestamptz `db:"deleted_at" json:"deleted_at"`
|
|
Limit int32 `db:"limit" json:"limit"`
|
|
}
|
|
|
|
type GetSoftDeletedPropertiesRow struct {
|
|
Property Property `db:"property" json:"property"`
|
|
}
|
|
|
|
func (q *Queries) GetSoftDeletedProperties(ctx context.Context, arg *GetSoftDeletedPropertiesParams) ([]*GetSoftDeletedPropertiesRow, error) {
|
|
rows, err := q.db.Query(ctx, getSoftDeletedProperties, arg.DeletedAt, arg.Limit)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []*GetSoftDeletedPropertiesRow
|
|
for rows.Next() {
|
|
var i GetSoftDeletedPropertiesRow
|
|
if err := rows.Scan(
|
|
&i.Property.ID,
|
|
&i.Property.Name,
|
|
&i.Property.ExternalID,
|
|
&i.Property.OrgID,
|
|
&i.Property.CreatorID,
|
|
&i.Property.OrgOwnerID,
|
|
&i.Property.Domain,
|
|
&i.Property.Level,
|
|
&i.Property.Salt,
|
|
&i.Property.Growth,
|
|
&i.Property.CreatedAt,
|
|
&i.Property.UpdatedAt,
|
|
&i.Property.DeletedAt,
|
|
&i.Property.ValidityInterval,
|
|
&i.Property.AllowSubdomains,
|
|
&i.Property.AllowLocalhost,
|
|
&i.Property.AllowReplay,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, &i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const getUserPropertiesCount = `-- name: GetUserPropertiesCount :one
|
|
SELECT COUNT(*) as count FROM backend.properties WHERE org_owner_id = $1 AND deleted_at IS NULL
|
|
`
|
|
|
|
func (q *Queries) GetUserPropertiesCount(ctx context.Context, orgOwnerID pgtype.Int4) (int64, error) {
|
|
row := q.db.QueryRow(ctx, getUserPropertiesCount, orgOwnerID)
|
|
var count int64
|
|
err := row.Scan(&count)
|
|
return count, err
|
|
}
|
|
|
|
const softDeleteProperty = `-- name: SoftDeleteProperty :one
|
|
UPDATE backend.properties SET deleted_at = NOW(), updated_at = NOW(), name = name || ' deleted_' || substr(md5(random()::text), 1, 8) WHERE id = $1 RETURNING id, name, external_id, org_id, creator_id, org_owner_id, domain, level, salt, growth, created_at, updated_at, deleted_at, validity_interval, allow_subdomains, allow_localhost, allow_replay
|
|
`
|
|
|
|
func (q *Queries) SoftDeleteProperty(ctx context.Context, id int32) (*Property, error) {
|
|
row := q.db.QueryRow(ctx, softDeleteProperty, id)
|
|
var i Property
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.ExternalID,
|
|
&i.OrgID,
|
|
&i.CreatorID,
|
|
&i.OrgOwnerID,
|
|
&i.Domain,
|
|
&i.Level,
|
|
&i.Salt,
|
|
&i.Growth,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.DeletedAt,
|
|
&i.ValidityInterval,
|
|
&i.AllowSubdomains,
|
|
&i.AllowLocalhost,
|
|
&i.AllowReplay,
|
|
)
|
|
return &i, err
|
|
}
|
|
|
|
const updateProperty = `-- name: UpdateProperty :one
|
|
UPDATE backend.properties SET name = $2, level = $3, growth = $4, validity_interval = $5, allow_subdomains = $6, allow_localhost = $7, allow_replay = $8, updated_at = NOW()
|
|
WHERE id = $1
|
|
RETURNING id, name, external_id, org_id, creator_id, org_owner_id, domain, level, salt, growth, created_at, updated_at, deleted_at, validity_interval, allow_subdomains, allow_localhost, allow_replay
|
|
`
|
|
|
|
type UpdatePropertyParams struct {
|
|
ID int32 `db:"id" json:"id"`
|
|
Name string `db:"name" json:"name"`
|
|
Level pgtype.Int2 `db:"level" json:"level"`
|
|
Growth DifficultyGrowth `db:"growth" json:"growth"`
|
|
ValidityInterval time.Duration `db:"validity_interval" json:"validity_interval"`
|
|
AllowSubdomains bool `db:"allow_subdomains" json:"allow_subdomains"`
|
|
AllowLocalhost bool `db:"allow_localhost" json:"allow_localhost"`
|
|
AllowReplay bool `db:"allow_replay" json:"allow_replay"`
|
|
}
|
|
|
|
func (q *Queries) UpdateProperty(ctx context.Context, arg *UpdatePropertyParams) (*Property, error) {
|
|
row := q.db.QueryRow(ctx, updateProperty,
|
|
arg.ID,
|
|
arg.Name,
|
|
arg.Level,
|
|
arg.Growth,
|
|
arg.ValidityInterval,
|
|
arg.AllowSubdomains,
|
|
arg.AllowLocalhost,
|
|
arg.AllowReplay,
|
|
)
|
|
var i Property
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.ExternalID,
|
|
&i.OrgID,
|
|
&i.CreatorID,
|
|
&i.OrgOwnerID,
|
|
&i.Domain,
|
|
&i.Level,
|
|
&i.Salt,
|
|
&i.Growth,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.DeletedAt,
|
|
&i.ValidityInterval,
|
|
&i.AllowSubdomains,
|
|
&i.AllowLocalhost,
|
|
&i.AllowReplay,
|
|
)
|
|
return &i, err
|
|
}
|