Skip to content

Commit

Permalink
Updating the enabled linters
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacobbrewer1 committed Mar 3, 2025
1 parent ff2cbdd commit c216556
Show file tree
Hide file tree
Showing 41 changed files with 7,588 additions and 50 deletions.
4 changes: 4 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ linters:
- thelper
- tparallel
- intrange
- testifylint
- perfsprint
issues:
exclude-rules:
- path: (.+)_test.go
Expand Down Expand Up @@ -142,3 +144,5 @@ linters-settings:
enable:
- identical # Identifies interfaces in the same package that have identical method sets.
- unused # Identifies interfaces that are not used anywhere in the same package where the interface is defined.
testifylint:
enable-all: true
4 changes: 2 additions & 2 deletions cmd_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ func (c *createCmd) Execute(_ context.Context, _ *flag.FlagSet, _ ...any) subcom
name := fmt.Sprintf("%s_%s", now.Format(migrations.FilePrefix), strings.TrimSpace(c.name))
name = strings.ReplaceAll(name, " ", "_")

upName := fmt.Sprintf("%s.up.sql", name)
downName := fmt.Sprintf("%s.down.sql", name)
upName := name + ".up.sql"
downName := name + "%s.down.sql"

upPath := fmt.Sprintf("%s/%s", c.outputLocation, upName)
downPath := fmt.Sprintf("%s/%s", c.outputLocation, downName)
Expand Down
4 changes: 2 additions & 2 deletions cmd_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package main
import (
"context"
"flag"
"fmt"
"log/slog"
"os"
"strconv"

"github.com/google/subcommands"
"github.com/jacobbrewer1/goschema/pkg/logging"
Expand Down Expand Up @@ -55,7 +55,7 @@ func (c *statusCmd) Execute(_ context.Context, _ *flag.FlagSet, _ ...any) subcom
tableDataStr := make([][]string, 0)
tableDataStr = append(tableDataStr, []string{"Version", "Current", "Created At"})
for _, v := range versions {
tableDataStr = append(tableDataStr, []string{v.Version, fmt.Sprintf("%t", v.IsCurrent == 1), v.CreatedAt.String()})
tableDataStr = append(tableDataStr, []string{v.Version, strconv.FormatBool(v.IsCurrent), v.CreatedAt.String()})
}

var tableData pterm.TableData = tableDataStr
Expand Down
2 changes: 1 addition & 1 deletion pkg/generation/template_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ func getType(col *entities.Column) string {
}

func getTags(col *entities.Column) string {
tags := fmt.Sprintf("`db:\"%s", col.Name)
tags := "`db:\"" + col.Name
if col.InPrimaryKey {
tags += ",pk"
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/migrations/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ func getConnectionStr() string {
if strings.Contains(connStr, "?") {
connStr = strings.Split(connStr, "?")[0]
}
return fmt.Sprintf("%s?timeout=90s&multiStatements=true&parseTime=true", connStr)
return connStr + "?timeout=90s&multiStatements=true&parseTime=true"
}
2 changes: 1 addition & 1 deletion pkg/migrations/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func (v *versioning) GetStatus() ([]*models.GoschemaMigrationVersion, error) {
versions, err := models.GetAllGoschemaMigrationVersion(v.db)
versions, err := models.GetAllGoschemaMigrationVersions(v.db)
if err != nil {
return nil, fmt.Errorf("error getting goschema migration versions: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/migrations/versioning.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func (v *versioning) setCurrentVersion(version string) error {

newVersion := &models.GoschemaMigrationVersion{
Version: version,
IsCurrent: 1,
IsCurrent: true,
CreatedAt: time.Now().UTC(),
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/models/db.xo.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ type DB interface {
Exec(string, ...any) (sql.Result, error)
Query(string, ...any) (*sql.Rows, error)
QueryRow(string, ...any) *sql.Row
Get(dest any, query string, args ...any) error
Select(dest any, query string, args ...any) error
Get(dest any, query string, args ...interface{}) error
Select(dest any, query string, args ...interface{}) error
}

// Transactioner is the interface that a database connection that can start
Expand Down
56 changes: 47 additions & 9 deletions pkg/models/goschema_migration_history.xo.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package models
import (
"errors"
"fmt"
"strings"
"time"

"github.com/jacobbrewer1/goschema/usql"
Expand Down Expand Up @@ -199,10 +200,10 @@ func GoschemaMigrationHistoryById(db DB, id int) (*GoschemaMigrationHistory, err
}

type goschemaMigrationHistoryPKWherer struct {
ids []any
ids []interface{}
}

func (m goschemaMigrationHistoryPKWherer) Where() (string, []any) {
func (m goschemaMigrationHistoryPKWherer) Where() (string, []interface{}) {
return "`id` = ?", m.ids
}

Expand All @@ -222,8 +223,11 @@ func (m *GoschemaMigrationHistory) Patch(db DB, newT *GoschemaMigrationHistory)
newT,
patcher.WithTable(GoschemaMigrationHistoryTableName),
patcher.WithWhere(&goschemaMigrationHistoryPKWherer{
ids: []any{m.Id},
ids: []interface{}{m.Id},
}),
patcher.WithIgnoredFields(
"Id",
),
)
if err != nil {
switch {
Expand All @@ -248,19 +252,53 @@ func (m *GoschemaMigrationHistory) Patch(db DB, newT *GoschemaMigrationHistory)
return nil
}

// GetAllGoschemaMigrationHistory retrieves all rows from 'goschema_migration_history' as a slice of GoschemaMigrationHistory.
// GetAllGoschemaMigrationHistorys retrieves all rows from 'goschema_migration_history' as a slice of GoschemaMigrationHistory.
//
// Generated from table 'goschema_migration_history'.
func GetAllGoschemaMigrationHistory(db DB) ([]*GoschemaMigrationHistory, error) {
func GetAllGoschemaMigrationHistorys(db DB, filters ...any) ([]*GoschemaMigrationHistory, error) {
t := prometheus.NewTimer(DatabaseLatency.WithLabelValues("get_all_" + GoschemaMigrationHistoryTableName))
defer t.ObserveDuration()

const sqlstr = "SELECT `id`, `version`, `action`, `created_at` " +
"FROM goschema_migration_history"
args := make([]any, 0)
builder := new(strings.Builder)
builder.WriteString("SELECT `t.id`, `t.version`, `t.action`, `t.created_at`")

if len(filters) > 0 {
for _, filter := range filters {
if joiner := filter.(patcher.Joiner); joiner != nil {
joinSql, joinArgs := joiner.Join()
builder.WriteString(joinSql)
args = append(args, joinArgs...)
}
}
}

builder.WriteString("\nFROM goschema_migration_history t")

if len(filters) > 0 {
builder.WriteString("\nWHERE\n")
for i, filter := range filters {
if where := filter.(patcher.Wherer); where != nil {
if i > 0 {
wtStr := patcher.WhereTypeAnd
if wt, ok := filter.(patcher.WhereTyper); ok {
wtStr = wt.WhereType()
}
builder.WriteString(string(" " + wtStr + " "))
}
whereSql, whereArgs := where.Where()
builder.WriteString(whereSql)
builder.WriteString("\n")
args = append(args, whereArgs...)
}
}
}

sqlstr := builder.String()
DBLog(sqlstr, args...)

DBLog(sqlstr)
m := make([]*GoschemaMigrationHistory, 0)
if err := db.Select(&m, sqlstr); err != nil {
if err := db.Select(&m, sqlstr, args...); err != nil {
return nil, fmt.Errorf("failed to get all GoschemaMigrationHistory: %w", err)
}

Expand Down
58 changes: 48 additions & 10 deletions pkg/models/goschema_migration_version.xo.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package models
import (
"errors"
"fmt"
"strings"
"time"

"github.com/jacobbrewer1/patcher"
Expand All @@ -21,7 +22,7 @@ const (
// GoschemaMigrationVersion represents a row from 'goschema_migration_version'.
type GoschemaMigrationVersion struct {
Version string `db:"version,pk"`
IsCurrent int `db:"is_current"`
IsCurrent bool `db:"is_current"`
CreatedAt time.Time `db:"created_at"`
}

Expand Down Expand Up @@ -168,10 +169,10 @@ func GoschemaMigrationVersionByVersion(db DB, version string) (*GoschemaMigratio
}

type goschemaMigrationVersionPKWherer struct {
ids []any
ids []interface{}
}

func (m goschemaMigrationVersionPKWherer) Where() (string, []any) {
func (m goschemaMigrationVersionPKWherer) Where() (string, []interface{}) {
return "`version` = ?", m.ids
}

Expand All @@ -191,8 +192,11 @@ func (m *GoschemaMigrationVersion) Patch(db DB, newT *GoschemaMigrationVersion)
newT,
patcher.WithTable(GoschemaMigrationVersionTableName),
patcher.WithWhere(&goschemaMigrationVersionPKWherer{
ids: []any{m.Version},
ids: []interface{}{m.Version},
}),
patcher.WithIgnoredFields(
"Version",
),
)
if err != nil {
switch {
Expand All @@ -217,19 +221,53 @@ func (m *GoschemaMigrationVersion) Patch(db DB, newT *GoschemaMigrationVersion)
return nil
}

// GetAllGoschemaMigrationVersion retrieves all rows from 'goschema_migration_version' as a slice of GoschemaMigrationVersion.
// GetAllGoschemaMigrationVersions retrieves all rows from 'goschema_migration_version' as a slice of GoschemaMigrationVersion.
//
// Generated from table 'goschema_migration_version'.
func GetAllGoschemaMigrationVersion(db DB) ([]*GoschemaMigrationVersion, error) {
func GetAllGoschemaMigrationVersions(db DB, filters ...any) ([]*GoschemaMigrationVersion, error) {
t := prometheus.NewTimer(DatabaseLatency.WithLabelValues("get_all_" + GoschemaMigrationVersionTableName))
defer t.ObserveDuration()

const sqlstr = "SELECT `version`, `is_current`, `created_at` " +
"FROM goschema_migration_version"
args := make([]any, 0)
builder := new(strings.Builder)
builder.WriteString("SELECT `t.version`, `t.is_current`, `t.created_at`")

if len(filters) > 0 {
for _, filter := range filters {
if joiner := filter.(patcher.Joiner); joiner != nil {
joinSql, joinArgs := joiner.Join()
builder.WriteString(joinSql)
args = append(args, joinArgs...)
}
}
}

builder.WriteString("\nFROM goschema_migration_version t")

if len(filters) > 0 {
builder.WriteString("\nWHERE\n")
for i, filter := range filters {
if where := filter.(patcher.Wherer); where != nil {
if i > 0 {
wtStr := patcher.WhereTypeAnd
if wt, ok := filter.(patcher.WhereTyper); ok {
wtStr = wt.WhereType()
}
builder.WriteString(string(" " + wtStr + " "))
}
whereSql, whereArgs := where.Where()
builder.WriteString(whereSql)
builder.WriteString("\n")
args = append(args, whereArgs...)
}
}
}

sqlstr := builder.String()
DBLog(sqlstr, args...)

DBLog(sqlstr)
m := make([]*GoschemaMigrationVersion, 0)
if err := db.Select(&m, sqlstr); err != nil {
if err := db.Select(&m, sqlstr, args...); err != nil {
return nil, fmt.Errorf("failed to get all GoschemaMigrationVersion: %w", err)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/models/helpers.xo.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func NewLoggableDBTransactionHandler(db Transactioner, l *slog.Logger) *Loggable
// 1. x is an integer and greater than zero.
// 2. x not an integer and is not the zero value.
// Otherwise, returns false
func IsKeySet(x any) bool {
func IsKeySet(x interface{}) bool {
switch x := x.(type) {
case int:
return x > 0
Expand Down
Loading

0 comments on commit c216556

Please sign in to comment.