Add insert timestamp to tag, jobtag

Fixes #43
This commit is contained in:
2023-06-27 09:41:06 +02:00
parent 60a3b653af
commit d1e3e06b10
8 changed files with 41 additions and 9 deletions

View File

@@ -16,7 +16,7 @@ import (
"github.com/golang-migrate/migrate/v4/source/iofs"
)
const Version uint = 4
const Version uint = 5
//go:embed migrations/*
var migrationFiles embed.FS

View File

@@ -0,0 +1,2 @@
ALTER TABLE tag DROP COLUMN insert_time;
ALTER TABLE jobtag DROP COLUMN insert_time;

View File

@@ -0,0 +1,2 @@
ALTER TABLE tag ADD COLUMN insert_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP;
ALTER TABLE jobtag ADD COLUMN insert_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP;

View File

@@ -0,0 +1,2 @@
ALTER TABLE tag DROP COLUMN insert_time;
ALTER TABLE jobtag DROP COLUMN insert_time;

View File

@@ -0,0 +1,18 @@
ALTER TABLE tag ADD COLUMN insert_ts TEXT DEFAULT NULL /* replace me */;
ALTER TABLE jobtag ADD COLUMN insert_ts TEXT DEFAULT NULL /* replace me */;
UPDATE tag SET insert_ts = CURRENT_TIMESTAMP;
UPDATE jobtag SET insert_ts = CURRENT_TIMESTAMP;
PRAGMA writable_schema = on;
UPDATE sqlite_master
SET sql = replace(sql, 'DEFAULT NULL /* replace me */',
'DEFAULT CURRENT_TIMESTAMP')
WHERE type = 'table'
AND name = 'tag';
UPDATE sqlite_master
SET sql = replace(sql, 'DEFAULT NULL /* replace me */',
'DEFAULT CURRENT_TIMESTAMP')
WHERE type = 'table'
AND name = 'jobtag';
PRAGMA writable_schema = off;

View File

@@ -70,14 +70,14 @@ func (r *JobRepository) CreateTag(tagType string, tagName string) (tagId int64,
func (r *JobRepository) CountTags(user *auth.User) (tags []schema.Tag, counts map[string]int, err error) {
tags = make([]schema.Tag, 0, 100)
xrows, err := r.DB.Queryx("SELECT * FROM tag")
xrows, err := r.DB.Queryx("SELECT id, tag_type, tag_name FROM tag")
if err != nil {
return nil, nil, err
}
for xrows.Next() {
var t schema.Tag
if err := xrows.StructScan(&t); err != nil {
if err = xrows.StructScan(&t); err != nil {
return nil, nil, err
}
tags = append(tags, t)
@@ -89,7 +89,7 @@ func (r *JobRepository) CountTags(user *auth.User) (tags []schema.Tag, counts ma
GroupBy("t.tag_name")
if user != nil && user.HasAnyRole([]auth.Role{auth.RoleAdmin, auth.RoleSupport}) { // ADMIN || SUPPORT: Count all jobs
log.Info("CountTags: User Admin or Support -> Count all Jobs for Tags")
log.Debug("CountTags: User Admin or Support -> Count all Jobs for Tags")
// Unchanged: Needs to be own case still, due to UserRole/NoRole compatibility handling in else case
} else if user != nil && user.HasRole(auth.RoleManager) { // MANAGER: Count own jobs plus project's jobs
// Build ("project1", "project2", ...) list of variable length directly in SQL string
@@ -107,7 +107,7 @@ func (r *JobRepository) CountTags(user *auth.User) (tags []schema.Tag, counts ma
for rows.Next() {
var tagName string
var count int
if err := rows.Scan(&tagName, &count); err != nil {
if err = rows.Scan(&tagName, &count); err != nil {
return nil, nil, err
}
counts[tagName] = count