Merged in feature/logVersioning (pull request #40)
Can Sync Versioning * started * client * jobcansync * tests
This commit is contained in:
@@ -30,7 +30,7 @@ func TestDBConn(t *testing.T) {
|
||||
assert.NotNil(t, pool)
|
||||
}
|
||||
|
||||
func TestExecuteTransation(t *testing.T) {
|
||||
func TestExecuteTransaction(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
pool, err := pgxmock.NewPool()
|
||||
|
||||
@@ -11,6 +11,23 @@ import (
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
const addClientCanSync = `-- name: AddClientCanSync :exec
|
||||
INSERT INTO clientCanSync (canSync, clientId) VALUES ($1, $2)
|
||||
`
|
||||
|
||||
type AddClientCanSyncParams struct {
|
||||
Cansync bool `db:"cansync"`
|
||||
Clientid pgtype.UUID `db:"clientid"`
|
||||
}
|
||||
|
||||
// AddClientCanSync
|
||||
//
|
||||
// INSERT INTO clientCanSync (canSync, clientId) VALUES ($1, $2)
|
||||
func (q *Queries) AddClientCanSync(ctx context.Context, arg *AddClientCanSyncParams) error {
|
||||
_, err := q.db.Exec(ctx, addClientCanSync, arg.Cansync, arg.Clientid)
|
||||
return err
|
||||
}
|
||||
|
||||
const createClient = `-- name: CreateClient :one
|
||||
INSERT INTO clients (name) VALUES ($1) RETURNING id
|
||||
`
|
||||
@@ -26,33 +43,56 @@ func (q *Queries) CreateClient(ctx context.Context, name string) (pgtype.UUID, e
|
||||
}
|
||||
|
||||
const getClient = `-- name: GetClient :one
|
||||
SELECT id, name, canSync FROM clients WHERE id = $1
|
||||
SELECT c.id, c.name, coalesce(cs.canSync, false) as canSync
|
||||
FROM clients as c
|
||||
LEFT JOIN (
|
||||
SELECT clientId, canSync
|
||||
FROM clientCanSync
|
||||
WHERE clientId = $1
|
||||
ORDER BY addedAt DESC
|
||||
LIMIT 1
|
||||
) as cs on cs.clientId = c.id
|
||||
WHERE c.id = $1
|
||||
`
|
||||
|
||||
type GetClientRow struct {
|
||||
ID pgtype.UUID `db:"id"`
|
||||
Name string `db:"name"`
|
||||
Cansync bool `db:"cansync"`
|
||||
}
|
||||
|
||||
// GetClient
|
||||
//
|
||||
// SELECT id, name, canSync FROM clients WHERE id = $1
|
||||
func (q *Queries) GetClient(ctx context.Context, id pgtype.UUID) (*Client, error) {
|
||||
row := q.db.QueryRow(ctx, getClient, id)
|
||||
var i Client
|
||||
// SELECT c.id, c.name, coalesce(cs.canSync, false) as canSync
|
||||
// FROM clients as c
|
||||
// LEFT JOIN (
|
||||
// SELECT clientId, canSync
|
||||
// FROM clientCanSync
|
||||
// WHERE clientId = $1
|
||||
// ORDER BY addedAt DESC
|
||||
// LIMIT 1
|
||||
// ) as cs on cs.clientId = c.id
|
||||
// WHERE c.id = $1
|
||||
func (q *Queries) GetClient(ctx context.Context, clientid pgtype.UUID) (*GetClientRow, error) {
|
||||
row := q.db.QueryRow(ctx, getClient, clientid)
|
||||
var i GetClientRow
|
||||
err := row.Scan(&i.ID, &i.Name, &i.Cansync)
|
||||
return &i, err
|
||||
}
|
||||
|
||||
const updateClient = `-- name: UpdateClient :exec
|
||||
UPDATE clients SET name = $1, canSync = $2 WHERE id = $3
|
||||
UPDATE clients SET name = $1 WHERE id = $2
|
||||
`
|
||||
|
||||
type UpdateClientParams struct {
|
||||
Name string `db:"name"`
|
||||
Cansync bool `db:"cansync"`
|
||||
ID pgtype.UUID `db:"id"`
|
||||
Name string `db:"name"`
|
||||
ID pgtype.UUID `db:"id"`
|
||||
}
|
||||
|
||||
// UpdateClient
|
||||
//
|
||||
// UPDATE clients SET name = $1, canSync = $2 WHERE id = $3
|
||||
// UPDATE clients SET name = $1 WHERE id = $2
|
||||
func (q *Queries) UpdateClient(ctx context.Context, arg *UpdateClientParams) error {
|
||||
_, err := q.db.Exec(ctx, updateClient, arg.Name, arg.Cansync, arg.ID)
|
||||
_, err := q.db.Exec(ctx, updateClient, arg.Name, arg.ID)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -28,22 +28,35 @@ func TestClient(t *testing.T) {
|
||||
|
||||
client, err := queries.GetClient(ctx, id)
|
||||
assert.Nil(t, err)
|
||||
assert.EqualExportedValues(t, &repository.Client{
|
||||
assert.EqualExportedValues(t, &repository.GetClientRow{
|
||||
ID: id,
|
||||
Name: "example_client",
|
||||
Cansync: false,
|
||||
}, client)
|
||||
|
||||
err = queries.UpdateClient(ctx, &repository.UpdateClientParams{
|
||||
ID: id,
|
||||
Name: "updated_client",
|
||||
Cansync: true,
|
||||
ID: id,
|
||||
Name: "updated_client",
|
||||
})
|
||||
assert.Nil(t, err)
|
||||
|
||||
client, err = queries.GetClient(ctx, id)
|
||||
assert.Nil(t, err)
|
||||
assert.EqualExportedValues(t, &repository.Client{
|
||||
assert.EqualExportedValues(t, &repository.GetClientRow{
|
||||
ID: id,
|
||||
Name: "updated_client",
|
||||
Cansync: false,
|
||||
}, client)
|
||||
|
||||
err = queries.AddClientCanSync(ctx, &repository.AddClientCanSyncParams{
|
||||
Clientid: id,
|
||||
Cansync: true,
|
||||
})
|
||||
assert.Nil(t, err)
|
||||
|
||||
client, err = queries.GetClient(ctx, id)
|
||||
assert.Nil(t, err)
|
||||
assert.EqualExportedValues(t, &repository.GetClientRow{
|
||||
ID: id,
|
||||
Name: "updated_client",
|
||||
Cansync: true,
|
||||
|
||||
@@ -11,6 +11,23 @@ import (
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
const addJobCanSync = `-- name: AddJobCanSync :exec
|
||||
INSERT INTO jobCanSync (canSync, jobId) VALUES ($1, $2)
|
||||
`
|
||||
|
||||
type AddJobCanSyncParams struct {
|
||||
Cansync bool `db:"cansync"`
|
||||
Jobid pgtype.UUID `db:"jobid"`
|
||||
}
|
||||
|
||||
// AddJobCanSync
|
||||
//
|
||||
// INSERT INTO jobCanSync (canSync, jobId) VALUES ($1, $2)
|
||||
func (q *Queries) AddJobCanSync(ctx context.Context, arg *AddJobCanSyncParams) error {
|
||||
_, err := q.db.Exec(ctx, addJobCanSync, arg.Cansync, arg.Jobid)
|
||||
return err
|
||||
}
|
||||
|
||||
const createJob = `-- name: CreateJob :one
|
||||
INSERT INTO jobs (clientId) VALUES ($1) RETURNING id
|
||||
`
|
||||
@@ -26,32 +43,39 @@ func (q *Queries) CreateJob(ctx context.Context, clientid pgtype.UUID) (pgtype.U
|
||||
}
|
||||
|
||||
const getJob = `-- name: GetJob :one
|
||||
SELECT id, clientId, canSync FROM jobs WHERE id = $1 LIMIT 1
|
||||
SELECT j.id, j.clientId, coalesce(cs.canSync, false) as canSync
|
||||
FROM jobs as j
|
||||
LEFT JOIN (
|
||||
SELECT jobId, canSync
|
||||
FROM jobCanSync
|
||||
WHERE jobId = $1
|
||||
ORDER BY addedAt DESC
|
||||
LIMIT 1
|
||||
) as cs on cs.jobId = j.id
|
||||
WHERE j.id = $1
|
||||
`
|
||||
|
||||
type GetJobRow struct {
|
||||
ID pgtype.UUID `db:"id"`
|
||||
Clientid pgtype.UUID `db:"clientid"`
|
||||
Cansync bool `db:"cansync"`
|
||||
}
|
||||
|
||||
// GetJob
|
||||
//
|
||||
// SELECT id, clientId, canSync FROM jobs WHERE id = $1 LIMIT 1
|
||||
func (q *Queries) GetJob(ctx context.Context, id pgtype.UUID) (*Job, error) {
|
||||
row := q.db.QueryRow(ctx, getJob, id)
|
||||
var i Job
|
||||
// SELECT j.id, j.clientId, coalesce(cs.canSync, false) as canSync
|
||||
// FROM jobs as j
|
||||
// LEFT JOIN (
|
||||
// SELECT jobId, canSync
|
||||
// FROM jobCanSync
|
||||
// WHERE jobId = $1
|
||||
// ORDER BY addedAt DESC
|
||||
// LIMIT 1
|
||||
// ) as cs on cs.jobId = j.id
|
||||
// WHERE j.id = $1
|
||||
func (q *Queries) GetJob(ctx context.Context, jobid pgtype.UUID) (*GetJobRow, error) {
|
||||
row := q.db.QueryRow(ctx, getJob, jobid)
|
||||
var i GetJobRow
|
||||
err := row.Scan(&i.ID, &i.Clientid, &i.Cansync)
|
||||
return &i, err
|
||||
}
|
||||
|
||||
const updateJob = `-- name: UpdateJob :exec
|
||||
UPDATE jobs SET canSync = $1 WHERE id = $2
|
||||
`
|
||||
|
||||
type UpdateJobParams struct {
|
||||
Cansync bool `db:"cansync"`
|
||||
ID pgtype.UUID `db:"id"`
|
||||
}
|
||||
|
||||
// UpdateJob
|
||||
//
|
||||
// UPDATE jobs SET canSync = $1 WHERE id = $2
|
||||
func (q *Queries) UpdateJob(ctx context.Context, arg *UpdateJobParams) error {
|
||||
_, err := q.db.Exec(ctx, updateJob, arg.Cansync, arg.ID)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -31,21 +31,21 @@ func TestJob(t *testing.T) {
|
||||
|
||||
job, err := queries.GetJob(ctx, id)
|
||||
assert.Nil(t, err)
|
||||
assert.EqualExportedValues(t, &repository.Job{
|
||||
assert.EqualExportedValues(t, &repository.GetJobRow{
|
||||
ID: id,
|
||||
Clientid: clientId,
|
||||
Cansync: false,
|
||||
}, job)
|
||||
|
||||
err = queries.UpdateJob(ctx, &repository.UpdateJobParams{
|
||||
err = queries.AddJobCanSync(ctx, &repository.AddJobCanSyncParams{
|
||||
Cansync: true,
|
||||
ID: id,
|
||||
Jobid: id,
|
||||
})
|
||||
assert.Nil(t, err)
|
||||
|
||||
job, err = queries.GetJob(ctx, id)
|
||||
assert.Nil(t, err)
|
||||
assert.EqualExportedValues(t, &repository.Job{
|
||||
assert.EqualExportedValues(t, &repository.GetJobRow{
|
||||
ID: id,
|
||||
Clientid: clientId,
|
||||
Cansync: true,
|
||||
|
||||
@@ -63,9 +63,15 @@ func (e Querytype) Valid() bool {
|
||||
}
|
||||
|
||||
type Client struct {
|
||||
ID pgtype.UUID `db:"id"`
|
||||
Name string `db:"name"`
|
||||
Cansync bool `db:"cansync"`
|
||||
ID pgtype.UUID `db:"id"`
|
||||
Name string `db:"name"`
|
||||
}
|
||||
|
||||
type Clientcansync struct {
|
||||
ID pgtype.UUID `db:"id"`
|
||||
Clientid pgtype.UUID `db:"clientid"`
|
||||
Cansync bool `db:"cansync"`
|
||||
Addedat pgtype.Timestamp `db:"addedat"`
|
||||
}
|
||||
|
||||
type Collector struct {
|
||||
@@ -130,7 +136,13 @@ type Fullactivequery struct {
|
||||
type Job struct {
|
||||
ID pgtype.UUID `db:"id"`
|
||||
Clientid pgtype.UUID `db:"clientid"`
|
||||
Cansync bool `db:"cansync"`
|
||||
}
|
||||
|
||||
type Jobcansync struct {
|
||||
ID pgtype.UUID `db:"id"`
|
||||
Jobid pgtype.UUID `db:"jobid"`
|
||||
Cansync bool `db:"cansync"`
|
||||
Addedat pgtype.Timestamp `db:"addedat"`
|
||||
}
|
||||
|
||||
type Query struct {
|
||||
|
||||
Reference in New Issue
Block a user