From cd187815251c8e6caa92cfb60e7f5516ca4f4ca0 Mon Sep 17 00:00:00 2001 From: Pavel Date: Wed, 18 Sep 2024 19:22:34 +0300 Subject: [PATCH] added version to sqlc queryes --- dal/db_query/queries.sql | 9 +++++---- dal/sqlcgen/db.go | 2 +- dal/sqlcgen/models.go | 3 ++- dal/sqlcgen/queries.sql.go | 20 ++++++++++++++------ model/model.go | 4 +++- repository/answer/answer.go | 1 + repository/result/result.go | 9 +++++---- 7 files changed, 31 insertions(+), 17 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index 956c561..5e95203 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -286,7 +286,7 @@ WHERE privilege_name = $2 -- name: GetAllAnswersByQuizID :many SELECT DISTINCT ON (a.question_id) - a.content, a.created_at, a.question_id, a.id, q.questiontype::Text as questiontype, quiz.qid + a.content, a.created_at, a.question_id, a.id,a.version, q.questiontype::Text as questiontype, quiz.qid FROM answer a JOIN @@ -312,12 +312,13 @@ INSERT INTO answer( browser, ip, start, - utm -) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14) + utm, + version +) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15) RETURNING *; -- name: GetResultAnswers :many -SELECT DISTINCT on (question_id) id, content, quiz_id, question_id, fingerprint, session,created_at, result, new,deleted, device_type,device,os,browser,ip FROM answer WHERE session = ( +SELECT DISTINCT on (question_id) id, content, quiz_id, question_id, fingerprint, session,created_at, result, new,deleted, device_type,device,os,browser,ip,version FROM answer WHERE session = ( SELECT session FROM answer WHERE answer.id = $1) ORDER BY question_id, created_at DESC; -- name: GetQuestions :many diff --git a/dal/sqlcgen/db.go b/dal/sqlcgen/db.go index 7090fb4..2d00a87 100644 --- a/dal/sqlcgen/db.go +++ b/dal/sqlcgen/db.go @@ -1,6 +1,6 @@ // Code generated by sqlc. DO NOT EDIT. // versions: -// sqlc v1.26.0 +// sqlc v1.27.0 package sqlcgen diff --git a/dal/sqlcgen/models.go b/dal/sqlcgen/models.go index c9fbf05..e6430ff 100644 --- a/dal/sqlcgen/models.go +++ b/dal/sqlcgen/models.go @@ -1,6 +1,6 @@ // Code generated by sqlc. DO NOT EDIT. // versions: -// sqlc v1.26.0 +// sqlc v1.27.0 package sqlcgen @@ -66,6 +66,7 @@ type Answer struct { Ip string `db:"ip" json:"ip"` Start bool `db:"start" json:"start"` Utm json.RawMessage `db:"utm" json:"utm"` + Version int32 `db:"version" json:"version"` } type Field struct { diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index c55c3f3..8e9435c 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -1,6 +1,6 @@ // Code generated by sqlc. DO NOT EDIT. // versions: -// sqlc v1.26.0 +// sqlc v1.27.0 // source: queries.sql package sqlcgen @@ -1361,7 +1361,7 @@ func (q *Queries) GetAccountWithPrivileges(ctx context.Context, userID string) ( const getAllAnswersByQuizID = `-- name: GetAllAnswersByQuizID :many SELECT DISTINCT ON (a.question_id) - a.content, a.created_at, a.question_id, a.id, q.questiontype::Text as questiontype, quiz.qid + a.content, a.created_at, a.question_id, a.id,a.version, q.questiontype::Text as questiontype, quiz.qid FROM answer a JOIN @@ -1379,6 +1379,7 @@ type GetAllAnswersByQuizIDRow struct { CreatedAt sql.NullTime `db:"created_at" json:"created_at"` QuestionID int64 `db:"question_id" json:"question_id"` ID int64 `db:"id" json:"id"` + Version int32 `db:"version" json:"version"` Questiontype string `db:"questiontype" json:"questiontype"` Qid uuid.NullUUID `db:"qid" json:"qid"` } @@ -1397,6 +1398,7 @@ func (q *Queries) GetAllAnswersByQuizID(ctx context.Context, session sql.NullStr &i.CreatedAt, &i.QuestionID, &i.ID, + &i.Version, &i.Questiontype, &i.Qid, ); err != nil { @@ -2413,7 +2415,7 @@ func (q *Queries) GetQuizRule(ctx context.Context, quizid int32) (Rule, error) { } const getResultAnswers = `-- name: GetResultAnswers :many -SELECT DISTINCT on (question_id) id, content, quiz_id, question_id, fingerprint, session,created_at, result, new,deleted, device_type,device,os,browser,ip FROM answer WHERE session = ( +SELECT DISTINCT on (question_id) id, content, quiz_id, question_id, fingerprint, session,created_at, result, new,deleted, device_type,device,os,browser,ip,version FROM answer WHERE session = ( SELECT session FROM answer WHERE answer.id = $1) ORDER BY question_id, created_at DESC ` @@ -2433,6 +2435,7 @@ type GetResultAnswersRow struct { Os string `db:"os" json:"os"` Browser string `db:"browser" json:"browser"` Ip string `db:"ip" json:"ip"` + Version int32 `db:"version" json:"version"` } func (q *Queries) GetResultAnswers(ctx context.Context, id int64) ([]GetResultAnswersRow, error) { @@ -2460,6 +2463,7 @@ func (q *Queries) GetResultAnswers(ctx context.Context, id int64) ([]GetResultAn &i.Os, &i.Browser, &i.Ip, + &i.Version, ); err != nil { return nil, err } @@ -3000,9 +3004,10 @@ INSERT INTO answer( browser, ip, start, - utm -) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14) -RETURNING id, content, quiz_id, question_id, fingerprint, session, created_at, result, new, deleted, email, device_type, device, os, browser, ip, start, utm + utm, + version +) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15) +RETURNING id, content, quiz_id, question_id, fingerprint, session, created_at, result, new, deleted, email, device_type, device, os, browser, ip, start, utm, version ` type InsertAnswersParams struct { @@ -3020,6 +3025,7 @@ type InsertAnswersParams struct { Ip string `db:"ip" json:"ip"` Start bool `db:"start" json:"start"` Utm json.RawMessage `db:"utm" json:"utm"` + Version int32 `db:"version" json:"version"` } func (q *Queries) InsertAnswers(ctx context.Context, arg InsertAnswersParams) (Answer, error) { @@ -3038,6 +3044,7 @@ func (q *Queries) InsertAnswers(ctx context.Context, arg InsertAnswersParams) (A arg.Ip, arg.Start, arg.Utm, + arg.Version, ) var i Answer err := row.Scan( @@ -3059,6 +3066,7 @@ func (q *Queries) InsertAnswers(ctx context.Context, arg InsertAnswersParams) (A &i.Ip, &i.Start, &i.Utm, + &i.Version, ) return i, err } diff --git a/model/model.go b/model/model.go index 0bfff36..1e5687a 100644 --- a/model/model.go +++ b/model/model.go @@ -131,7 +131,7 @@ type Answer struct { OS string Start bool Utm UTMSavingMap - Version int + Version int32 } type ResultContent struct { @@ -162,6 +162,7 @@ type ResultAnswer struct { CreatedAt time.Time QuestionID uint64 AnswerID uint64 + Version int32 } const skey = "squiz" @@ -305,6 +306,7 @@ type AnswerExport struct { Id uint64 `json:"id"` New bool `json:"new"` CreatedAt time.Time `json:"created_at"` + Version int32 } type UTMSavingMap map[string]string diff --git a/repository/answer/answer.go b/repository/answer/answer.go index 893dbb9..e42ff82 100644 --- a/repository/answer/answer.go +++ b/repository/answer/answer.go @@ -114,6 +114,7 @@ func (r *AnswerRepository) GetAllAnswersByQuizID(ctx context.Context, session st CreatedAt: row.CreatedAt.Time, QuestionID: uint64(row.QuestionID), AnswerID: uint64(row.ID), + Version: row.Version, } results = append(results, resultAnswer) diff --git a/repository/result/result.go b/repository/result/result.go index 52c1010..897c90d 100644 --- a/repository/result/result.go +++ b/repository/result/result.go @@ -57,7 +57,7 @@ func (r *ResultRepository) GetQuizResults(ctx context.Context, quizID uint64, re } offset := reqExport.Page * reqExport.Limit - mainQuery := "SELECT content, id, new, created_at " + queryBase + " ORDER BY created_at DESC LIMIT $" + strconv.Itoa(len(queryParams)+1) + " OFFSET $" + strconv.Itoa(len(queryParams)+2) + mainQuery := "SELECT content, id, new, created_at,version " + queryBase + " ORDER BY created_at DESC LIMIT $" + strconv.Itoa(len(queryParams)+1) + " OFFSET $" + strconv.Itoa(len(queryParams)+2) queryParams = append(queryParams, reqExport.Limit, offset) rows, err := r.pool.QueryContext(ctx, mainQuery, queryParams...) @@ -68,7 +68,7 @@ func (r *ResultRepository) GetQuizResults(ctx context.Context, quizID uint64, re for rows.Next() { var answer model.AnswerExport - if err := rows.Scan(&answer.Content, &answer.Id, &answer.New, &answer.CreatedAt); err != nil { + if err := rows.Scan(&answer.Content, &answer.Id, &answer.New, &answer.CreatedAt, &answer.Version); err != nil { return nil, 0, err } @@ -118,7 +118,7 @@ func (r *ResultRepository) GetQuizResultsCSV(ctx context.Context, quizID uint64, var results []model.Answer mainQuery := `SELECT DISTINCT ON (a.question_id, a.session) - a.id, a.content, a.question_id, a.quiz_id, a.fingerprint, a.session, a.result, a.created_at, a.new, a.deleted + a.id, a.content, a.question_id, a.quiz_id, a.fingerprint, a.session, a.result, a.created_at, a.new, a.deleted,a.version FROM answer a WHERE @@ -151,7 +151,7 @@ func (r *ResultRepository) GetQuizResultsCSV(ctx context.Context, quizID uint64, for rows.Next() { var answer model.Answer - if err := rows.Scan(&answer.Id, &answer.Content, &answer.QuestionId, &answer.QuizId, &answer.Fingerprint, &answer.Session, &answer.Result, &answer.CreatedAt, &answer.New, &answer.Deleted); err != nil { + if err := rows.Scan(&answer.Id, &answer.Content, &answer.QuestionId, &answer.QuizId, &answer.Fingerprint, &answer.Session, &answer.Result, &answer.CreatedAt, &answer.New, &answer.Deleted, &answer.Version); err != nil { return nil, err } results = append(results, answer) @@ -245,6 +245,7 @@ func (r *ResultRepository) GetResultAnswers(ctx context.Context, answerID uint64 OS: row.Os, IP: row.Ip, Browser: row.Browser, + Version: row.Version, } answers = append(answers, answer)