From ff0a6f5f1a13b5817ec63d3f4419f4043aa03068 Mon Sep 17 00:00:00 2001 From: ccoVeille <3875889+ccoVeille@users.noreply.github.com> Date: Wed, 17 Jul 2024 13:43:09 +0200 Subject: [PATCH 01/15] chore: use Go canonical way to return errors this way the code is clearer. The changes were made on a limited set of files, that are about to be refactored or fixed. --- drivers/mysql.go | 29 ++++++++++++++--------------- drivers/postgres.go | 32 ++++++++++++++++---------------- drivers/sqlite.go | 33 ++++++++++++++------------------- 3 files changed, 44 insertions(+), 50 deletions(-) diff --git a/drivers/mysql.go b/drivers/mysql.go index 43b4b6c..062771f 100644 --- a/drivers/mysql.go +++ b/drivers/mysql.go @@ -42,14 +42,14 @@ func (db *MySQL) GetDatabases() ([]string, error) { rows, err := db.Connection.Query("SHOW DATABASES") if err != nil { - return databases, err + return nil, err } for rows.Next() { var database string err := rows.Scan(&database) if err != nil { - return databases, err + return nil, err } if database != "information_schema" && database != "mysql" && database != "performance_schema" && database != "sys" { databases = append(databases, database) @@ -65,7 +65,7 @@ func (db *MySQL) GetTables(database string) (map[string][]string, error) { tables := make(map[string][]string) if err != nil { - return tables, err + return nil, err } for rows.Next() { @@ -83,13 +83,13 @@ func (db *MySQL) GetTableColumns(database, table string) (results [][]string, er rows, err := db.Connection.Query("DESCRIBE " + table) if err != nil { - return results, err + return nil, err } defer rows.Close() columns, err := rows.Columns() if err != nil { - return results, err + return nil, err } results = append(results, columns) @@ -122,14 +122,14 @@ func (db *MySQL) GetConstraints(table string) (results [][]string, err error) { rows, err := db.Connection.Query(fmt.Sprintf("SELECT CONSTRAINT_NAME, COLUMN_NAME, REFERENCED_TABLE_NAME, REFERENCED_COLUMN_NAME FROM information_schema.KEY_COLUMN_USAGE where TABLE_SCHEMA = '%s' AND TABLE_NAME = '%s'", database, tableName)) if err != nil { - return results, err + return nil, err } defer rows.Close() columns, err := rows.Columns() if err != nil { - return results, err + return nil, err } results = append(results, columns) @@ -161,14 +161,14 @@ func (db *MySQL) GetForeignKeys(table string) (results [][]string, err error) { rows, err := db.Connection.Query(fmt.Sprintf("SELECT TABLE_NAME, COLUMN_NAME, CONSTRAINT_NAME, REFERENCED_COLUMN_NAME, REFERENCED_TABLE_NAME FROM information_schema.KEY_COLUMN_USAGE where REFERENCED_TABLE_SCHEMA = '%s' AND REFERENCED_TABLE_NAME = '%s'", database, tableName)) if err != nil { - return results, err + return nil, err } defer rows.Close() columns, err := rows.Columns() if err != nil { - return results, err + return nil, err } results = append(results, columns) @@ -196,7 +196,7 @@ func (db *MySQL) GetIndexes(table string) (results [][]string, err error) { table = db.formatTableName(table) rows, err := db.Connection.Query("SHOW INDEX FROM " + table) if err != nil { - return results, err + return nil, err } defer rows.Close() @@ -245,7 +245,7 @@ func (db *MySQL) GetRecords(table, where, sort string, offset, limit int) (pagin paginatedRows, err := db.Connection.Query(query) if err != nil { - return paginatedResults, totalRecords, err + return nil, 0, err } if isPaginationEnabled { @@ -254,7 +254,7 @@ func (db *MySQL) GetRecords(table, where, sort string, offset, limit int) (pagin rows := db.Connection.QueryRow(queryWithoutLimit) if err != nil { - return paginatedResults, totalRecords, err + return nil, 0, err } rows.Scan(&totalRecords) @@ -289,7 +289,7 @@ func (db *MySQL) GetRecords(table, where, sort string, offset, limit int) (pagin func (db *MySQL) ExecuteQuery(query string) (results [][]string, err error) { rows, err := db.Connection.Query(query) if err != nil { - return results, err + return nil, err } defer rows.Close() @@ -438,8 +438,7 @@ func (db *MySQL) ExecutePendingChanges(changes []models.DbDmlChange, inserts []m if err != nil { return err } - - return err + return nil } func (db *MySQL) SetProvider(provider string) { diff --git a/drivers/postgres.go b/drivers/postgres.go index a586c6e..c2036aa 100644 --- a/drivers/postgres.go +++ b/drivers/postgres.go @@ -63,14 +63,14 @@ func (db *Postgres) TestConnection(urlstr string) error { func (db *Postgres) GetDatabases() (databases []string, err error) { rows, err := db.Connection.Query("SELECT datname FROM pg_database;") if err != nil { - return databases, err + return nil, err } for rows.Next() { var database string err := rows.Scan(&database) if err != nil { - return databases, err + return nil, err } databases = append(databases, database) } @@ -86,7 +86,7 @@ func (db *Postgres) GetTables(database string) (tables map[string][]string, err if database != db.CurrentDatabase { err = db.SwitchDatabase(database) if err != nil { - return tables, err + return nil, err } switchDatabase = true } @@ -96,10 +96,10 @@ func (db *Postgres) GetTables(database string) (tables map[string][]string, err if switchDatabase { err = db.SwitchDatabase(db.PreviousDatabase) if err != nil { - return tables, err + return nil, err } } - return tables, err + return tables, nil } for rows.Next() { @@ -120,13 +120,13 @@ func (db *Postgres) GetTableColumns(database, table string) (results [][]string, tableName := strings.Split(table, ".")[1] rows, err := db.Connection.Query(fmt.Sprintf("SELECT column_name, data_type, is_nullable, column_default FROM information_schema.columns WHERE table_catalog = '%s' AND table_schema = '%s' AND table_name = '%s' ORDER by ordinal_position", database, tableSchema, tableName)) if err != nil { - return results, err + return nil, err } defer rows.Close() columns, err := rows.Columns() if err != nil { - return results, err + return nil, err } results = append(results, columns) @@ -172,14 +172,14 @@ func (db *Postgres) GetConstraints(table string) (constraints [][]string, error AND tc.table_name = '%s' `, tableSchema, tableName)) if err != nil { - return constraints, err + return nil, err } defer rows.Close() columns, err := rows.Columns() if err != nil { - return constraints, err + return nil, err } constraints = append(constraints, columns) @@ -226,14 +226,14 @@ func (db *Postgres) GetForeignKeys(table string) (foreignKeys [][]string, error AND tc.table_name = '%s' `, tableSchema, tableName)) if err != nil { - return foreignKeys, err + return nil, err } defer rows.Close() columns, err := rows.Columns() if err != nil { - return foreignKeys, err + return nil, err } foreignKeys = append(foreignKeys, columns) @@ -289,7 +289,7 @@ func (db *Postgres) GetIndexes(table string) (indexes [][]string, error error) { i.relname `, tableSchema, tableName)) if err != nil { - return indexes, err + return nil, err } defer rows.Close() @@ -337,7 +337,7 @@ func (db *Postgres) GetRecords(table, where, sort string, offset, limit int) (re paginatedRows, err := db.Connection.Query(query) if err != nil { - return records, totalRecords, err + return nil, 0, err } if isPaginationEnabled { @@ -346,7 +346,7 @@ func (db *Postgres) GetRecords(table, where, sort string, offset, limit int) (re rows := db.Connection.QueryRow(queryWithoutLimit) if err != nil { - return records, totalRecords, err + return nil, 0, err } err = rows.Scan(&totalRecords) @@ -412,7 +412,7 @@ func (db *Postgres) ExecuteDMLStatement(query string) (result string, err error) func (db *Postgres) ExecuteQuery(query string) (results [][]string, err error) { rows, err := db.Connection.Query(query) if err != nil { - return results, err + return nil, err } defer rows.Close() @@ -531,7 +531,7 @@ func (db *Postgres) ExecutePendingChanges(changes []models.DbDmlChange, inserts return err } - return err + return nil } func (db *Postgres) SetProvider(provider string) { diff --git a/drivers/sqlite.go b/drivers/sqlite.go index f3a0330..53fa3e6 100644 --- a/drivers/sqlite.go +++ b/drivers/sqlite.go @@ -24,7 +24,6 @@ func (db *SQLite) Connect(urlstr string) (err error) { db.SetProvider("sqlite3") db.Connection, err = dburl.Open(urlstr) - if err != nil { return err } @@ -42,14 +41,14 @@ func (db *SQLite) GetDatabases() ([]string, error) { rows, err := db.Connection.Query("SELECT file FROM pragma_database_list WHERE name='main'") if err != nil { - return databases, err + return nil, err } for rows.Next() { var database string err := rows.Scan(&database) if err != nil { - return databases, err + return nil, err } split := strings.Split(database, "/") @@ -67,7 +66,7 @@ func (db *SQLite) GetTables(database string) (map[string][]string, error) { tables := make(map[string][]string) if err != nil { - return tables, err + return nil, err } for rows.Next() { @@ -83,13 +82,13 @@ func (db *SQLite) GetTables(database string) (map[string][]string, error) { func (db *SQLite) GetTableColumns(database, table string) (results [][]string, err error) { rows, err := db.Connection.Query("PRAGMA table_info(" + table + ")") if err != nil { - return results, err + return nil, err } defer rows.Close() columns, err := rows.Columns() if err != nil { - return results, err + return nil, err } results = append(results, columns[1:]) @@ -121,14 +120,14 @@ func (db *SQLite) GetTableColumns(database, table string) (results [][]string, e func (db *SQLite) GetConstraints(table string) (results [][]string, err error) { rows, err := db.Connection.Query("SELECT sql FROM sqlite_master WHERE type='table' AND name = '" + table + "'") if err != nil { - return results, err + return nil, err } defer rows.Close() columns, err := rows.Columns() if err != nil { - return results, err + return nil, err } results = append(results, columns) @@ -159,14 +158,14 @@ func (db *SQLite) GetConstraints(table string) (results [][]string, err error) { func (db *SQLite) GetForeignKeys(table string) (results [][]string, err error) { rows, err := db.Connection.Query("PRAGMA foreign_key_list(" + table + ")") if err != nil { - return results, err + return nil, err } defer rows.Close() columns, err := rows.Columns() if err != nil { - return results, err + return nil, err } results = append(results, columns) @@ -197,7 +196,7 @@ func (db *SQLite) GetForeignKeys(table string) (results [][]string, err error) { func (db *SQLite) GetIndexes(table string) (results [][]string, err error) { rows, err := db.Connection.Query("PRAGMA index_list(" + table + ")") if err != nil { - return results, err + return nil, err } defer rows.Close() @@ -249,16 +248,15 @@ func (db *SQLite) GetRecords(table, where, sort string, offset, limit int) (pagi paginatedRows, err := db.Connection.Query(query) if err != nil { - return paginatedResults, totalRecords, err + return nil, 0, err } if isPaginationEnabled { queryWithoutLimit := fmt.Sprintf("SELECT COUNT(*) FROM %s %s", table, where) rows := db.Connection.QueryRow(queryWithoutLimit) - if err != nil { - return paginatedResults, totalRecords, err + return nil, 0, err } rows.Scan(&totalRecords) @@ -297,7 +295,7 @@ func (db *SQLite) GetRecords(table, where, sort string, offset, limit int) (pagi func (db *SQLite) ExecuteQuery(query string) (results [][]string, err error) { rows, err := db.Connection.Query(query) if err != nil { - return results, err + return nil, err } defer rows.Close() @@ -436,7 +434,6 @@ func (db *SQLite) ExecutePendingChanges(changes []models.DbDmlChange, inserts [] for _, query := range queries { fmt.Printf("LS -> drivers/sqlite.go:440 -> query: %+v\n", query) _, err = tx.Exec(query) - if err != nil { tx.Rollback() @@ -445,12 +442,10 @@ func (db *SQLite) ExecutePendingChanges(changes []models.DbDmlChange, inserts [] } err = tx.Commit() - if err != nil { return err } - - return err + return nil } func (db *SQLite) SetProvider(provider string) { From 9fa9e274275c8ae2a19c0cfa2a05cee17c64c0f2 Mon Sep 17 00:00:00 2001 From: ccoVeille <3875889+ccoVeille@users.noreply.github.com> Date: Wed, 17 Jul 2024 13:50:51 +0200 Subject: [PATCH 02/15] chore: use the canonical name for error variable here there was an import shadowing of the error interface nothing should be ever named "error" --- drivers/mysql.go | 18 +++++++++--------- drivers/postgres.go | 8 ++++---- drivers/sqlite.go | 18 +++++++++--------- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/drivers/mysql.go b/drivers/mysql.go index 062771f..401dec6 100644 --- a/drivers/mysql.go +++ b/drivers/mysql.go @@ -337,14 +337,14 @@ func (db *MySQL) DeleteRecord(table, primaryKeyColumnName, primaryKeyValue strin } func (db *MySQL) ExecuteDMLStatement(query string) (result string, err error) { - res, error := db.Connection.Exec(query) + res, err := db.Connection.Exec(query) - if error != nil { - return result, error + if err != nil { + return result, err } else { rowsAffected, _ := res.RowsAffected() - return fmt.Sprintf("%d rows affected", rowsAffected), error + return fmt.Sprintf("%d rows affected", rowsAffected), err } } @@ -405,9 +405,9 @@ func (db *MySQL) ExecutePendingChanges(changes []models.DbDmlChange, inserts []m values := make([]string, 0, len(insert.Values)) for _, value := range insert.Values { - _, error := strconv.ParseFloat(value, 64) + _, err := strconv.ParseFloat(value, 64) - if strings.ToLower(value) != "default" && error != nil { + if strings.ToLower(value) != "default" && err != nil { values = append(values, fmt.Sprintf("\"%s\"", value)) } else { values = append(values, value) @@ -419,9 +419,9 @@ func (db *MySQL) ExecutePendingChanges(changes []models.DbDmlChange, inserts []m queries = append(queries, query) } - tx, error := db.Connection.Begin() - if error != nil { - return error + tx, err := db.Connection.Begin() + if err != nil { + return err } for _, query := range queries { diff --git a/drivers/postgres.go b/drivers/postgres.go index c2036aa..57b132f 100644 --- a/drivers/postgres.go +++ b/drivers/postgres.go @@ -115,7 +115,7 @@ func (db *Postgres) GetTables(database string) (tables map[string][]string, err return tables, nil } -func (db *Postgres) GetTableColumns(database, table string) (results [][]string, error error) { +func (db *Postgres) GetTableColumns(database, table string) (results [][]string, err error) { tableSchema := strings.Split(table, ".")[0] tableName := strings.Split(table, ".")[1] rows, err := db.Connection.Query(fmt.Sprintf("SELECT column_name, data_type, is_nullable, column_default FROM information_schema.columns WHERE table_catalog = '%s' AND table_schema = '%s' AND table_name = '%s' ORDER by ordinal_position", database, tableSchema, tableName)) @@ -150,7 +150,7 @@ func (db *Postgres) GetTableColumns(database, table string) (results [][]string, return } -func (db *Postgres) GetConstraints(table string) (constraints [][]string, error error) { +func (db *Postgres) GetConstraints(table string) (constraints [][]string, err error) { splitTableString := strings.Split(table, ".") tableSchema := splitTableString[0] tableName := splitTableString[1] @@ -203,7 +203,7 @@ func (db *Postgres) GetConstraints(table string) (constraints [][]string, error return } -func (db *Postgres) GetForeignKeys(table string) (foreignKeys [][]string, error error) { +func (db *Postgres) GetForeignKeys(table string) (foreignKeys [][]string, err error) { splitTableString := strings.Split(table, ".") tableSchema := splitTableString[0] tableName := splitTableString[1] @@ -257,7 +257,7 @@ func (db *Postgres) GetForeignKeys(table string) (foreignKeys [][]string, error return } -func (db *Postgres) GetIndexes(table string) (indexes [][]string, error error) { +func (db *Postgres) GetIndexes(table string) (indexes [][]string, err error) { splitTableString := strings.Split(table, ".") tableSchema := splitTableString[0] tableName := splitTableString[1] diff --git a/drivers/sqlite.go b/drivers/sqlite.go index 53fa3e6..456cc07 100644 --- a/drivers/sqlite.go +++ b/drivers/sqlite.go @@ -343,14 +343,14 @@ func (db *SQLite) DeleteRecord(table, primaryKeyColumnName, primaryKeyValue stri } func (db *SQLite) ExecuteDMLStatement(query string) (result string, err error) { - res, error := db.Connection.Exec(query) + res, err := db.Connection.Exec(query) - if error != nil { - return result, error + if err != nil { + return result, err } else { rowsAffected, _ := res.RowsAffected() - return fmt.Sprintf("%d rows affected", rowsAffected), error + return fmt.Sprintf("%d rows affected", rowsAffected), err } } @@ -412,9 +412,9 @@ func (db *SQLite) ExecutePendingChanges(changes []models.DbDmlChange, inserts [] columnsToBeInserted := insert.Columns for _, value := range insert.Values { - _, error := strconv.ParseFloat(value, 64) + _, err := strconv.ParseFloat(value, 64) - if error != nil { + if err != nil { values = append(values, fmt.Sprintf("\"%s\"", value)) } else { values = append(values, value) @@ -426,9 +426,9 @@ func (db *SQLite) ExecutePendingChanges(changes []models.DbDmlChange, inserts [] queries = append(queries, query) } - tx, error := db.Connection.Begin() - if error != nil { - return error + tx, err := db.Connection.Begin() + if err != nil { + return err } for _, query := range queries { From 32801a9e4c895152828c3176ff8a08f7a7c0c51e Mon Sep 17 00:00:00 2001 From: ccoVeille <3875889+ccoVeille@users.noreply.github.com> Date: Wed, 17 Jul 2024 13:24:03 +0200 Subject: [PATCH 03/15] ci: add golangci-lint configuration file --- .golangci.yml | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 .golangci.yml diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000..4ac56e3 --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,28 @@ +--- +# golangci-lint configuration file made by @ccoVeille +# Source: https://github.com/ccoVeille/golangci-lint-config-examples/ +# Author: @ccoVeille +# License: MIT +# Variant: 01-defaults +# Version: v1.0.0 +# +linters: + # some linters are enabled by default + # https://golangci-lint.run/usage/linters/ + # + # enable some extra linters + enable: + # Errcheck is a program for checking for unchecked errors in Go code. + - errcheck + + # Linter for Go source code that specializes in simplifying code. + - gosimple + + # Vet examines Go source code and reports suspicious constructs. + - govet + + # Detects when assignments to existing variables are not used. + - ineffassign + + # It's a set of rules from staticcheck. See https://staticcheck.io/ + - staticcheck From 5a457119f606c96e3085318a55e65fa6bdbf6b81 Mon Sep 17 00:00:00 2001 From: ccoVeille <3875889+ccoVeille@users.noreply.github.com> Date: Wed, 17 Jul 2024 13:44:25 +0200 Subject: [PATCH 04/15] fix: catch errors reported by errcheck linter --- drivers/mysql.go | 45 ++++++++++++++++++++++++++++++++++----------- drivers/postgres.go | 35 ++++++++++++++++++++++++++--------- drivers/sqlite.go | 45 ++++++++++++++++++++++++++++++++++----------- main.go | 5 ++++- 4 files changed, 98 insertions(+), 32 deletions(-) diff --git a/drivers/mysql.go b/drivers/mysql.go index 401dec6..5cdb71a 100644 --- a/drivers/mysql.go +++ b/drivers/mysql.go @@ -2,6 +2,7 @@ package drivers import ( "database/sql" + "errors" "fmt" "strconv" "strings" @@ -70,7 +71,10 @@ func (db *MySQL) GetTables(database string) (map[string][]string, error) { for rows.Next() { var table string - rows.Scan(&table) + err = rows.Scan(&table) + if err != nil { + return nil, err + } tables[database] = append(tables[database], table) } @@ -100,7 +104,10 @@ func (db *MySQL) GetTableColumns(database, table string) (results [][]string, er rowValues[i] = new(sql.RawBytes) } - rows.Scan(rowValues...) + err = rows.Scan(rowValues...) + if err != nil { + return nil, err + } var row []string for _, col := range rowValues { @@ -140,7 +147,10 @@ func (db *MySQL) GetConstraints(table string) (results [][]string, err error) { rowValues[i] = new(sql.RawBytes) } - rows.Scan(rowValues...) + err = rows.Scan(rowValues...) + if err != nil { + return nil, err + } var row []string for _, col := range rowValues { @@ -179,7 +189,10 @@ func (db *MySQL) GetForeignKeys(table string) (results [][]string, err error) { rowValues[i] = new(sql.RawBytes) } - rows.Scan(rowValues...) + err = rows.Scan(rowValues...) + if err != nil { + return nil, err + } var row []string for _, col := range rowValues { @@ -210,7 +223,10 @@ func (db *MySQL) GetIndexes(table string) (results [][]string, err error) { rowValues[i] = new(sql.RawBytes) } - rows.Scan(rowValues...) + err = rows.Scan(rowValues...) + if err != nil { + return nil, err + } var row []string for _, col := range rowValues { @@ -257,7 +273,10 @@ func (db *MySQL) GetRecords(table, where, sort string, offset, limit int) (pagin return nil, 0, err } - rows.Scan(&totalRecords) + err = rows.Scan(&totalRecords) + if err != nil { + return nil, 0, err + } defer paginatedRows.Close() } @@ -272,7 +291,10 @@ func (db *MySQL) GetRecords(table, where, sort string, offset, limit int) (pagin rowValues[i] = new(sql.RawBytes) } - paginatedRows.Scan(rowValues...) + err = paginatedRows.Scan(rowValues...) + if err != nil { + return nil, 0, err + } var row []string for _, col := range rowValues { @@ -304,7 +326,10 @@ func (db *MySQL) ExecuteQuery(query string) (results [][]string, err error) { rowValues[i] = new(sql.RawBytes) } - rows.Scan(rowValues...) + err = rows.Scan(rowValues...) + if err != nil { + return nil, err + } var row []string for _, col := range rowValues { @@ -428,9 +453,7 @@ func (db *MySQL) ExecutePendingChanges(changes []models.DbDmlChange, inserts []m _, err = tx.Exec(query) if err != nil { - tx.Rollback() - - return err + return errors.Join(err, tx.Rollback()) } } diff --git a/drivers/postgres.go b/drivers/postgres.go index 57b132f..4b460be 100644 --- a/drivers/postgres.go +++ b/drivers/postgres.go @@ -2,6 +2,7 @@ package drivers import ( "database/sql" + "errors" "fmt" "strconv" "strings" @@ -106,7 +107,10 @@ func (db *Postgres) GetTables(database string) (tables map[string][]string, err var tableName string var tableSchema string - rows.Scan(&tableName, &tableSchema) + err = rows.Scan(&tableName, &tableSchema) + if err != nil { + return nil, err + } tables[tableSchema] = append(tables[tableSchema], tableName) @@ -137,7 +141,10 @@ func (db *Postgres) GetTableColumns(database, table string) (results [][]string, rowValues[i] = new(sql.RawBytes) } - rows.Scan(rowValues...) + err = rows.Scan(rowValues...) + if err != nil { + return nil, err + } var row []string for _, col := range rowValues { @@ -190,7 +197,10 @@ func (db *Postgres) GetConstraints(table string) (constraints [][]string, err er rowValues[i] = new(sql.RawBytes) } - rows.Scan(rowValues...) + err = rows.Scan(rowValues...) + if err != nil { + return nil, err + } var row []string for _, col := range rowValues { @@ -244,7 +254,10 @@ func (db *Postgres) GetForeignKeys(table string) (foreignKeys [][]string, err er rowValues[i] = new(sql.RawBytes) } - rows.Scan(rowValues...) + err = rows.Scan(rowValues...) + if err != nil { + return nil, err + } var row []string for _, col := range rowValues { @@ -303,7 +316,10 @@ func (db *Postgres) GetIndexes(table string) (indexes [][]string, err error) { rowValues[i] = new(sql.RawBytes) } - rows.Scan(rowValues...) + err = rows.Scan(rowValues...) + if err != nil { + return nil, err + } var row []string for _, col := range rowValues { @@ -367,7 +383,10 @@ func (db *Postgres) GetRecords(table, where, sort string, offset, limit int) (re rowValues[i] = new(sql.RawBytes) } - paginatedRows.Scan(rowValues...) + err = paginatedRows.Scan(rowValues...) + if err != nil { + return nil, 0, err + } var row []string for _, col := range rowValues { @@ -520,9 +539,7 @@ func (db *Postgres) ExecutePendingChanges(changes []models.DbDmlChange, inserts for _, query := range queries { _, err = tx.Exec(query) if err != nil { - tx.Rollback() - - return err + return errors.Join(err, tx.Rollback()) } } diff --git a/drivers/sqlite.go b/drivers/sqlite.go index 456cc07..80a13a8 100644 --- a/drivers/sqlite.go +++ b/drivers/sqlite.go @@ -2,6 +2,7 @@ package drivers import ( "database/sql" + "errors" "fmt" "strconv" "strings" @@ -71,7 +72,10 @@ func (db *SQLite) GetTables(database string) (map[string][]string, error) { for rows.Next() { var table string - rows.Scan(&table) + err = rows.Scan(&table) + if err != nil { + return nil, err + } tables[database] = append(tables[database], table) } @@ -99,7 +103,10 @@ func (db *SQLite) GetTableColumns(database, table string) (results [][]string, e rowValues[i] = new(sql.RawBytes) } - rows.Scan(rowValues...) + err = rows.Scan(rowValues...) + if err != nil { + return nil, err + } var row []string @@ -138,7 +145,10 @@ func (db *SQLite) GetConstraints(table string) (results [][]string, err error) { rowValues[i] = new(sql.RawBytes) } - rows.Scan(rowValues...) + err = rows.Scan(rowValues...) + if err != nil { + return nil, err + } var row []string for _, col := range rowValues { @@ -176,7 +186,10 @@ func (db *SQLite) GetForeignKeys(table string) (results [][]string, err error) { rowValues[i] = new(sql.RawBytes) } - rows.Scan(rowValues...) + err = rows.Scan(rowValues...) + if err != nil { + return nil, err + } var row []string for _, col := range rowValues { @@ -210,7 +223,10 @@ func (db *SQLite) GetIndexes(table string) (results [][]string, err error) { rowValues[i] = new(sql.RawBytes) } - rows.Scan(rowValues...) + err = rows.Scan(rowValues...) + if err != nil { + return nil, err + } var row []string for _, col := range rowValues { @@ -259,7 +275,10 @@ func (db *SQLite) GetRecords(table, where, sort string, offset, limit int) (pagi return nil, 0, err } - rows.Scan(&totalRecords) + err = rows.Scan(&totalRecords) + if err != nil { + return nil, 0, err + } defer paginatedRows.Close() } @@ -274,7 +293,10 @@ func (db *SQLite) GetRecords(table, where, sort string, offset, limit int) (pagi rowValues[i] = new(sql.RawBytes) } - paginatedRows.Scan(rowValues...) + err = paginatedRows.Scan(rowValues...) + if err != nil { + return nil, 0, err + } var row []string for _, col := range rowValues { @@ -310,7 +332,10 @@ func (db *SQLite) ExecuteQuery(query string) (results [][]string, err error) { rowValues[i] = new(sql.RawBytes) } - rows.Scan(rowValues...) + err = rows.Scan(rowValues...) + if err != nil { + return nil, err + } var row []string for _, col := range rowValues { @@ -435,9 +460,7 @@ func (db *SQLite) ExecutePendingChanges(changes []models.DbDmlChange, inserts [] fmt.Printf("LS -> drivers/sqlite.go:440 -> query: %+v\n", query) _, err = tx.Exec(query) if err != nil { - tx.Rollback() - - return err + return errors.Join(err, tx.Rollback()) } } diff --git a/main.go b/main.go index ae1c7cc..17f3609 100644 --- a/main.go +++ b/main.go @@ -14,7 +14,10 @@ import ( var version = "dev" func main() { - mysql.SetLogger(log.New(io.Discard, "", 0)) + err := mysql.SetLogger(log.New(io.Discard, "", 0)) + if err != nil { + panic(err) + } // check if "version" arg is passed argsWithProg := os.Args From b5c64eb7f66ff11aa7babd232f3e8e199b30ebde Mon Sep 17 00:00:00 2001 From: ccoVeille <3875889+ccoVeille@users.noreply.github.com> Date: Wed, 17 Jul 2024 13:58:18 +0200 Subject: [PATCH 05/15] fix: make sure to catch all errors in drivers errors cannot be ignored --- drivers/mysql.go | 27 +++++++++++++++++++-------- drivers/postgres.go | 26 ++++++++++++++++++-------- drivers/sqlite.go | 27 +++++++++++++++++++-------- 3 files changed, 56 insertions(+), 24 deletions(-) diff --git a/drivers/mysql.go b/drivers/mysql.go index 5cdb71a..05cd790 100644 --- a/drivers/mysql.go +++ b/drivers/mysql.go @@ -213,7 +213,10 @@ func (db *MySQL) GetIndexes(table string) (results [][]string, err error) { } defer rows.Close() - columns, _ := rows.Columns() + columns, err := rows.Columns() + if err != nil { + return nil, err + } results = append(results, columns) @@ -281,7 +284,10 @@ func (db *MySQL) GetRecords(table, where, sort string, offset, limit int) (pagin defer paginatedRows.Close() } - columns, _ := paginatedRows.Columns() + columns, err := paginatedRows.Columns() + if err != nil { + return nil, 0, err + } paginatedResults = append(paginatedResults, columns) @@ -316,7 +322,10 @@ func (db *MySQL) ExecuteQuery(query string) (results [][]string, err error) { defer rows.Close() - columns, _ := rows.Columns() + columns, err := rows.Columns() + if err != nil { + return nil, err + } results = append(results, columns) @@ -363,14 +372,16 @@ func (db *MySQL) DeleteRecord(table, primaryKeyColumnName, primaryKeyValue strin func (db *MySQL) ExecuteDMLStatement(query string) (result string, err error) { res, err := db.Connection.Exec(query) - if err != nil { - return result, err - } else { - rowsAffected, _ := res.RowsAffected() + return "", err + } - return fmt.Sprintf("%d rows affected", rowsAffected), err + rowsAffected, err := res.RowsAffected() + if err != nil { + return "", err } + + return fmt.Sprintf("%d rows affected", rowsAffected), nil } func (db *MySQL) ExecutePendingChanges(changes []models.DbDmlChange, inserts []models.DbInsert) (err error) { diff --git a/drivers/postgres.go b/drivers/postgres.go index 4b460be..40c3324 100644 --- a/drivers/postgres.go +++ b/drivers/postgres.go @@ -306,7 +306,10 @@ func (db *Postgres) GetIndexes(table string) (indexes [][]string, err error) { } defer rows.Close() - columns, _ := rows.Columns() + columns, err := rows.Columns() + if err != nil { + return nil, err + } indexes = append(indexes, columns) @@ -373,7 +376,10 @@ func (db *Postgres) GetRecords(table, where, sort string, offset, limit int) (re defer paginatedRows.Close() } - columns, _ := paginatedRows.Columns() + columns, err := paginatedRows.Columns() + if err != nil { + return nil, 0, err + } records = append(records, columns) @@ -418,14 +424,15 @@ func (db *Postgres) DeleteRecord(table, primaryKeyColumnName, primaryKeyValue st func (db *Postgres) ExecuteDMLStatement(query string) (result string, err error) { res, err := db.Connection.Exec(query) - if err != nil { return result, err - } else { - rowsAffected, _ := res.RowsAffected() - - return fmt.Sprintf("%d rows affected", rowsAffected), err } + rowsAffected, err := res.RowsAffected() + if err != nil { + return result, err + } + + return fmt.Sprintf("%d rows affected", rowsAffected), nil } func (db *Postgres) ExecuteQuery(query string) (results [][]string, err error) { @@ -436,7 +443,10 @@ func (db *Postgres) ExecuteQuery(query string) (results [][]string, err error) { defer rows.Close() - columns, _ := rows.Columns() + columns, err := rows.Columns() + if err != nil { + return nil, err + } results = append(results, columns) diff --git a/drivers/sqlite.go b/drivers/sqlite.go index 80a13a8..dadfc52 100644 --- a/drivers/sqlite.go +++ b/drivers/sqlite.go @@ -213,7 +213,10 @@ func (db *SQLite) GetIndexes(table string) (results [][]string, err error) { } defer rows.Close() - columns, _ := rows.Columns() + columns, err := rows.Columns() + if err != nil { + return nil, err + } results = append(results, columns) @@ -283,7 +286,10 @@ func (db *SQLite) GetRecords(table, where, sort string, offset, limit int) (pagi defer paginatedRows.Close() } - columns, _ := paginatedRows.Columns() + columns, err := paginatedRows.Columns() + if err != nil { + return nil, 0, err + } paginatedResults = append(paginatedResults, columns) @@ -322,7 +328,10 @@ func (db *SQLite) ExecuteQuery(query string) (results [][]string, err error) { defer rows.Close() - columns, _ := rows.Columns() + columns, err := rows.Columns() + if err != nil { + return nil, err + } results = append(results, columns) @@ -369,14 +378,16 @@ func (db *SQLite) DeleteRecord(table, primaryKeyColumnName, primaryKeyValue stri func (db *SQLite) ExecuteDMLStatement(query string) (result string, err error) { res, err := db.Connection.Exec(query) - if err != nil { - return result, err - } else { - rowsAffected, _ := res.RowsAffected() + return "", err + } - return fmt.Sprintf("%d rows affected", rowsAffected), err + rowsAffected, err := res.RowsAffected() + if err != nil { + return "", err } + + return fmt.Sprintf("%d rows affected", rowsAffected), nil } func (db *SQLite) ExecutePendingChanges(changes []models.DbDmlChange, inserts []models.DbInsert) (err error) { From eb50649f1b5a439cddcdae558408989f6698f5cc Mon Sep 17 00:00:00 2001 From: ccoVeille <3875889+ccoVeille@users.noreply.github.com> Date: Wed, 17 Jul 2024 13:59:43 +0200 Subject: [PATCH 06/15] ci: enable more linters --- .golangci.yml | 130 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 129 insertions(+), 1 deletion(-) diff --git a/.golangci.yml b/.golangci.yml index 4ac56e3..55c9e8c 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -3,7 +3,7 @@ # Source: https://github.com/ccoVeille/golangci-lint-config-examples/ # Author: @ccoVeille # License: MIT -# Variant: 01-defaults +# Variant: 03-safe # Version: v1.0.0 # linters: @@ -26,3 +26,131 @@ linters: # It's a set of rules from staticcheck. See https://staticcheck.io/ - staticcheck + + # Fast, configurable, extensible, flexible, and beautiful linter for Go. + # Drop-in replacement of golint. + - revive + + # check imports order and makes it always deterministic. + - gci + + # make sure to use t.Helper() when needed + - thelper + + # mirror suggests rewrites to avoid unnecessary []byte/string conversion + - mirror + + # detect the possibility to use variables/constants from the Go standard library. + - usestdlibvars + + # Finds commonly misspelled English words. + - misspell + + # Checks for duplicate words in the source code. + - dupword + +linters-settings: + gci: # define the section orders for imports + sections: + # Standard section: captures all standard packages. + - standard + # Default section: catchall that is not standard or custom + - default + # linters that related to local tool, so they should be separated + - localmodule + + revive: + rules: + # these are the default revive rules + # you can remove the whole "rules" node if you want + # BUT + # ! /!\ they all need to be present when you want to add more rules than the default ones + # otherwise, you won't have the default rules, but only the ones you define in the "rules" node + + # Blank import should be only in a main or test package, or have a comment justifying it. + - name: blank-imports + + # context.Context() should be the first parameter of a function when provided as argument. + - name: context-as-argument + arguments: + - allowTypesBefore: "*testing.T" + + # Basic types should not be used as a key in `context.WithValue` + - name: context-keys-type + + # Importing with `.` makes the programs much harder to understand + - name: dot-imports + + # Empty blocks make code less readable and could be a symptom of a bug or unfinished refactoring. + - name: empty-block + + # for better readability, variables of type `error` must be named with the prefix `err`. + - name: error-naming + + # for better readability, the errors should be last in the list of returned values by a function. + - name: error-return + + # for better readability, error messages should not be capitalized or end with punctuation or a newline. + - name: error-strings + + # report when replacing `errors.New(fmt.Sprintf())` with `fmt.Errorf()` is possible + - name: errorf + + # incrementing an integer variable by 1 is recommended to be done using the `++` operator + - name: increment-decrement + + # highlights redundant else-blocks that can be eliminated from the code + - name: indent-error-flow + + # This rule suggests a shorter way of writing ranges that do not use the second value. + - name: range + + # receiver names in a method should reflect the struct name (p for Person, for example) + - name: receiver-naming + + # redefining built in names (true, false, append, make) can lead to bugs very difficult to detect. + - name: redefines-builtin-id + + # redundant else-blocks that can be eliminated from the code. + - name: superfluous-else + + # prevent confusing name for variables when using `time` package + - name: time-naming + + # warns when an exported function or method returns a value of an un-exported type. + - name: unexported-return + + # spots and proposes to remove unreachable code. also helps to spot errors + - name: unreachable-code + + # Functions or methods with unused parameters can be a symptom of an unfinished refactoring or a bug. + - name: unused-parameter + + # report when a variable declaration can be simplified + - name: var-declaration + + # warns when initialism, variable or package naming conventions are not followed. + - name: var-naming + + dupword: + # Keywords used to ignore detection. + # Default: [] + ignore: + # - "blah" # this will accept "blah blah …" as a valid duplicate word + + misspell: + # Correct spellings using locale preferences for US or UK. + # Setting locale to US will correct the British spelling of 'colour' to 'color'. + # Default ("") is to use a neutral variety of English. + locale: US + + # List of words to ignore + # among the one defined in https://github.com/golangci/misspell/blob/master/words.go + ignore-words: + # - valor + # - and + + # Extra word corrections. + extra-words: + # - typo: "whattever" + # correction: "whatever" From 280d93eb20e033b0422393ddf1ebc4bb0baeb8b1 Mon Sep 17 00:00:00 2001 From: ccoVeille <3875889+ccoVeille@users.noreply.github.com> Date: Wed, 17 Jul 2024 14:00:31 +0200 Subject: [PATCH 07/15] chore: fix import orders use the following command gci write -s standard -s default -s localmodule --- app/Keymap.go | 1 + components/ConnectionForm.go | 6 +++--- components/ConnectionPage.go | 4 ++-- components/ConnectionSelection.go | 6 +++--- components/ConnectionsTable.go | 6 +++--- components/Home.go | 9 ++++----- components/ResultTableFilter.go | 4 ++-- components/ResultsTable.go | 11 +++++------ components/Tree.go | 6 +++--- drivers/mysql.go | 4 ++-- drivers/postgres.go | 4 ++-- drivers/sqlite.go | 3 ++- helpers/config.go | 4 ++-- keymap/map.go | 1 + main.go | 4 ++-- 15 files changed, 37 insertions(+), 36 deletions(-) diff --git a/app/Keymap.go b/app/Keymap.go index ad58ef8..4fd1338 100644 --- a/app/Keymap.go +++ b/app/Keymap.go @@ -2,6 +2,7 @@ package app import ( "github.com/gdamore/tcell/v2" + . "github.com/jorgerojas26/lazysql/commands" . "github.com/jorgerojas26/lazysql/keymap" ) diff --git a/components/ConnectionForm.go b/components/ConnectionForm.go index 81699f7..6720333 100644 --- a/components/ConnectionForm.go +++ b/components/ConnectionForm.go @@ -3,12 +3,12 @@ package components import ( "strings" + "github.com/gdamore/tcell/v2" + "github.com/rivo/tview" + "github.com/jorgerojas26/lazysql/drivers" "github.com/jorgerojas26/lazysql/helpers" "github.com/jorgerojas26/lazysql/models" - - "github.com/gdamore/tcell/v2" - "github.com/rivo/tview" ) type ConnectionForm struct { diff --git a/components/ConnectionPage.go b/components/ConnectionPage.go index 0549df6..1e13431 100644 --- a/components/ConnectionPage.go +++ b/components/ConnectionPage.go @@ -1,9 +1,9 @@ package components import ( - "github.com/jorgerojas26/lazysql/models" - "github.com/rivo/tview" + + "github.com/jorgerojas26/lazysql/models" ) func NewConnectionPages() *models.ConnectionPages { diff --git a/components/ConnectionSelection.go b/components/ConnectionSelection.go index 30105f4..d99f339 100644 --- a/components/ConnectionSelection.go +++ b/components/ConnectionSelection.go @@ -4,13 +4,13 @@ import ( "fmt" "strings" + "github.com/gdamore/tcell/v2" + "github.com/rivo/tview" + "github.com/jorgerojas26/lazysql/app" "github.com/jorgerojas26/lazysql/drivers" "github.com/jorgerojas26/lazysql/helpers" "github.com/jorgerojas26/lazysql/models" - - "github.com/gdamore/tcell/v2" - "github.com/rivo/tview" ) type ConnectionSelection struct { diff --git a/components/ConnectionsTable.go b/components/ConnectionsTable.go index d74b17a..bdeb440 100644 --- a/components/ConnectionsTable.go +++ b/components/ConnectionsTable.go @@ -1,11 +1,11 @@ package components import ( - "github.com/jorgerojas26/lazysql/helpers" - "github.com/jorgerojas26/lazysql/models" - "github.com/gdamore/tcell/v2" "github.com/rivo/tview" + + "github.com/jorgerojas26/lazysql/helpers" + "github.com/jorgerojas26/lazysql/models" ) type ConnectionsTable struct { diff --git a/components/Home.go b/components/Home.go index 3363353..428d6e3 100644 --- a/components/Home.go +++ b/components/Home.go @@ -1,14 +1,13 @@ package components import ( - "github.com/jorgerojas26/lazysql/commands" - "github.com/jorgerojas26/lazysql/models" + "github.com/gdamore/tcell/v2" + "github.com/rivo/tview" "github.com/jorgerojas26/lazysql/app" + "github.com/jorgerojas26/lazysql/commands" "github.com/jorgerojas26/lazysql/drivers" - - "github.com/gdamore/tcell/v2" - "github.com/rivo/tview" + "github.com/jorgerojas26/lazysql/models" ) type Home struct { diff --git a/components/ResultTableFilter.go b/components/ResultTableFilter.go index 630d550..2a91b68 100644 --- a/components/ResultTableFilter.go +++ b/components/ResultTableFilter.go @@ -1,10 +1,10 @@ package components import ( - "github.com/jorgerojas26/lazysql/models" - "github.com/gdamore/tcell/v2" "github.com/rivo/tview" + + "github.com/jorgerojas26/lazysql/models" ) type ResultsTableFilter struct { diff --git a/components/ResultsTable.go b/components/ResultsTable.go index 1e9457f..bae9516 100644 --- a/components/ResultsTable.go +++ b/components/ResultsTable.go @@ -4,16 +4,15 @@ import ( "fmt" "strings" - "github.com/jorgerojas26/lazysql/app" - "github.com/jorgerojas26/lazysql/commands" - "github.com/jorgerojas26/lazysql/models" - - "github.com/jorgerojas26/lazysql/drivers" - "github.com/gdamore/tcell/v2" "github.com/google/uuid" "github.com/rivo/tview" "golang.design/x/clipboard" + + "github.com/jorgerojas26/lazysql/app" + "github.com/jorgerojas26/lazysql/commands" + "github.com/jorgerojas26/lazysql/drivers" + "github.com/jorgerojas26/lazysql/models" ) type ResultsTableState struct { diff --git a/components/Tree.go b/components/Tree.go index 3c078b2..3091eb3 100644 --- a/components/Tree.go +++ b/components/Tree.go @@ -3,13 +3,13 @@ package components import ( "fmt" + "github.com/gdamore/tcell/v2" + "github.com/rivo/tview" + "github.com/jorgerojas26/lazysql/app" "github.com/jorgerojas26/lazysql/commands" "github.com/jorgerojas26/lazysql/drivers" "github.com/jorgerojas26/lazysql/models" - - "github.com/gdamore/tcell/v2" - "github.com/rivo/tview" ) type TreeState struct { diff --git a/drivers/mysql.go b/drivers/mysql.go index 05cd790..dbcfba8 100644 --- a/drivers/mysql.go +++ b/drivers/mysql.go @@ -7,10 +7,10 @@ import ( "strconv" "strings" - "github.com/jorgerojas26/lazysql/models" - _ "github.com/go-sql-driver/mysql" "github.com/xo/dburl" + + "github.com/jorgerojas26/lazysql/models" ) type MySQL struct { diff --git a/drivers/postgres.go b/drivers/postgres.go index 40c3324..ebaafad 100644 --- a/drivers/postgres.go +++ b/drivers/postgres.go @@ -7,10 +7,10 @@ import ( "strconv" "strings" - "github.com/jorgerojas26/lazysql/models" + _ "github.com/lib/pq" "github.com/xo/dburl" - _ "github.com/lib/pq" + "github.com/jorgerojas26/lazysql/models" ) type Postgres struct { diff --git a/drivers/sqlite.go b/drivers/sqlite.go index dadfc52..c684738 100644 --- a/drivers/sqlite.go +++ b/drivers/sqlite.go @@ -7,9 +7,10 @@ import ( "strconv" "strings" - "github.com/jorgerojas26/lazysql/models" _ "github.com/mattn/go-sqlite3" "github.com/xo/dburl" + + "github.com/jorgerojas26/lazysql/models" ) type SQLite struct { diff --git a/helpers/config.go b/helpers/config.go index 2c7036d..2583d65 100644 --- a/helpers/config.go +++ b/helpers/config.go @@ -4,9 +4,9 @@ import ( "os" "path/filepath" - "github.com/jorgerojas26/lazysql/models" - "github.com/pelletier/go-toml/v2" + + "github.com/jorgerojas26/lazysql/models" ) type Config struct { diff --git a/keymap/map.go b/keymap/map.go index d1cfec0..c0af7bc 100644 --- a/keymap/map.go +++ b/keymap/map.go @@ -2,6 +2,7 @@ package keymap import ( "github.com/gdamore/tcell/v2" + "github.com/jorgerojas26/lazysql/commands" ) diff --git a/main.go b/main.go index 17f3609..2e01b09 100644 --- a/main.go +++ b/main.go @@ -5,10 +5,10 @@ import ( "log" "os" + "github.com/go-sql-driver/mysql" + "github.com/jorgerojas26/lazysql/app" "github.com/jorgerojas26/lazysql/components" - - "github.com/go-sql-driver/mysql" ) var version = "dev" From f0e8b75d5e4c96c446b1671da6f21288ae4fa9cd Mon Sep 17 00:00:00 2001 From: ccoVeille <3875889+ccoVeille@users.noreply.github.com> Date: Wed, 17 Jul 2024 14:17:21 +0200 Subject: [PATCH 08/15] chore: remove dot import and use type alias --- app/Keymap.go | 79 ++++++++++++++++++++++++++++----------------------- 1 file changed, 43 insertions(+), 36 deletions(-) diff --git a/app/Keymap.go b/app/Keymap.go index 4fd1338..7dfe714 100644 --- a/app/Keymap.go +++ b/app/Keymap.go @@ -3,8 +3,15 @@ package app import ( "github.com/gdamore/tcell/v2" - . "github.com/jorgerojas26/lazysql/commands" - . "github.com/jorgerojas26/lazysql/keymap" + cmd "github.com/jorgerojas26/lazysql/commands" + "github.com/jorgerojas26/lazysql/keymap" +) + +// local alias added for clarity purpose +type ( + Bind = keymap.Bind + Key = keymap.Key + Map = keymap.Map ) // KeymapSystem is the actual key mapping system. @@ -32,54 +39,54 @@ func (c KeymapSystem) Group(name string) Map { // Resolve translates a tcell.EventKey into a command based on the mappings in // the global group -func (c KeymapSystem) Resolve(event *tcell.EventKey) Command { +func (c KeymapSystem) Resolve(event *tcell.EventKey) cmd.Command { return c.Global.Resolve(event) } // Define a global KeymapSystem object with default keybinds -var Keymaps KeymapSystem = KeymapSystem{ +var Keymaps = KeymapSystem{ Global: Map{ - Bind{Key: Key{Char: 'L'}, Cmd: MoveRight}, - Bind{Key: Key{Char: 'H'}, Cmd: MoveLeft}, - Bind{Key: Key{Code: tcell.KeyCtrlE}, Cmd: SwitchToEditorView}, - Bind{Key: Key{Code: tcell.KeyCtrlS}, Cmd: Save}, - Bind{Key: Key{Char: 'q'}, Cmd: Quit}, - Bind{Key: Key{Code: tcell.KeyBackspace2}, Cmd: SwitchToConnectionsView}, + Bind{Key: Key{Char: 'L'}, Cmd: cmd.MoveRight}, + Bind{Key: Key{Char: 'H'}, Cmd: cmd.MoveLeft}, + Bind{Key: Key{Code: tcell.KeyCtrlE}, Cmd: cmd.SwitchToEditorView}, + Bind{Key: Key{Code: tcell.KeyCtrlS}, Cmd: cmd.Save}, + Bind{Key: Key{Char: 'q'}, Cmd: cmd.Quit}, + Bind{Key: Key{Code: tcell.KeyBackspace2}, Cmd: cmd.SwitchToConnectionsView}, }, Groups: map[string]Map{ "tree": { - Bind{Key: Key{Char: 'g'}, Cmd: GotoTop}, - Bind{Key: Key{Char: 'G'}, Cmd: GotoBottom}, - Bind{Key: Key{Code: tcell.KeyEnter}, Cmd: Execute}, - Bind{Key: Key{Char: 'j'}, Cmd: MoveDown}, - Bind{Key: Key{Code: tcell.KeyDown}, Cmd: MoveDown}, - Bind{Key: Key{Char: 'k'}, Cmd: MoveUp}, - Bind{Key: Key{Code: tcell.KeyUp}, Cmd: MoveUp}, + Bind{Key: Key{Char: 'g'}, Cmd: cmd.GotoTop}, + Bind{Key: Key{Char: 'G'}, Cmd: cmd.GotoBottom}, + Bind{Key: Key{Code: tcell.KeyEnter}, Cmd: cmd.Execute}, + Bind{Key: Key{Char: 'j'}, Cmd: cmd.MoveDown}, + Bind{Key: Key{Code: tcell.KeyDown}, Cmd: cmd.MoveDown}, + Bind{Key: Key{Char: 'k'}, Cmd: cmd.MoveUp}, + Bind{Key: Key{Code: tcell.KeyUp}, Cmd: cmd.MoveUp}, }, "table": { - Bind{Key: Key{Char: '/'}, Cmd: Search}, - Bind{Key: Key{Char: 'c'}, Cmd: Edit}, - Bind{Key: Key{Char: 'd'}, Cmd: Delete}, - Bind{Key: Key{Char: 'w'}, Cmd: GotoNext}, - Bind{Key: Key{Char: 'b'}, Cmd: GotoPrev}, - Bind{Key: Key{Char: '$'}, Cmd: GotoEnd}, - Bind{Key: Key{Char: '0'}, Cmd: GotoStart}, - Bind{Key: Key{Char: 'y'}, Cmd: Copy}, - Bind{Key: Key{Char: 'o'}, Cmd: AppendNewRow}, + Bind{Key: Key{Char: '/'}, Cmd: cmd.Search}, + Bind{Key: Key{Char: 'c'}, Cmd: cmd.Edit}, + Bind{Key: Key{Char: 'd'}, Cmd: cmd.Delete}, + Bind{Key: Key{Char: 'w'}, Cmd: cmd.GotoNext}, + Bind{Key: Key{Char: 'b'}, Cmd: cmd.GotoPrev}, + Bind{Key: Key{Char: '$'}, Cmd: cmd.GotoEnd}, + Bind{Key: Key{Char: '0'}, Cmd: cmd.GotoStart}, + Bind{Key: Key{Char: 'y'}, Cmd: cmd.Copy}, + Bind{Key: Key{Char: 'o'}, Cmd: cmd.AppendNewRow}, // Tabs - Bind{Key: Key{Char: '['}, Cmd: TabPrev}, - Bind{Key: Key{Char: ']'}, Cmd: TabNext}, - Bind{Key: Key{Char: '{'}, Cmd: TabFirst}, - Bind{Key: Key{Char: '}'}, Cmd: TabLast}, - Bind{Key: Key{Char: 'X'}, Cmd: TabClose}, + Bind{Key: Key{Char: '['}, Cmd: cmd.TabPrev}, + Bind{Key: Key{Char: ']'}, Cmd: cmd.TabNext}, + Bind{Key: Key{Char: '{'}, Cmd: cmd.TabFirst}, + Bind{Key: Key{Char: '}'}, Cmd: cmd.TabLast}, + Bind{Key: Key{Char: 'X'}, Cmd: cmd.TabClose}, // Pages - Bind{Key: Key{Char: '>'}, Cmd: PageNext}, - Bind{Key: Key{Char: '<'}, Cmd: PagePrev}, + Bind{Key: Key{Char: '>'}, Cmd: cmd.PageNext}, + Bind{Key: Key{Char: '<'}, Cmd: cmd.PagePrev}, }, "editor": { - Bind{Key: Key{Code: tcell.KeyCtrlR}, Cmd: Execute}, - Bind{Key: Key{Code: tcell.KeyEscape}, Cmd: Quit}, - Bind{Key: Key{Code: tcell.KeyCtrlSpace}, Cmd: OpenInExternalEditor}, + Bind{Key: Key{Code: tcell.KeyCtrlR}, Cmd: cmd.Execute}, + Bind{Key: Key{Code: tcell.KeyEscape}, Cmd: cmd.Quit}, + Bind{Key: Key{Code: tcell.KeyCtrlSpace}, Cmd: cmd.OpenInExternalEditor}, }, }, } From 2fb9a4f7d40b3e88e790574c86050d7e6367a276 Mon Sep 17 00:00:00 2001 From: ccoVeille <3875889+ccoVeille@users.noreply.github.com> Date: Wed, 17 Jul 2024 14:18:17 +0200 Subject: [PATCH 09/15] chore: remove else when previous if ends with a return improve code readability --- components/ConnectionForm.go | 98 ++++++++++++------------- components/ResultsTable.go | 136 +++++++++++++++++------------------ 2 files changed, 117 insertions(+), 117 deletions(-) diff --git a/components/ConnectionForm.go b/components/ConnectionForm.go index 6720333..da12296 100644 --- a/components/ConnectionForm.go +++ b/components/ConnectionForm.go @@ -85,67 +85,67 @@ func (form *ConnectionForm) inputCapture(connectionPages *models.ConnectionPages if err != nil { form.StatusText.SetText(err.Error()).SetTextStyle(tcell.StyleDefault.Foreground(tcell.ColorRed)) return event - } else { + } - databases, _ := helpers.LoadConnections() - newDatabases := make([]models.Connection, len(databases)) + databases, _ := helpers.LoadConnections() + newDatabases := make([]models.Connection, len(databases)) - DBName := strings.Split(parsed.Normalize(",", "NULL", 0), ",")[3] + DBName := strings.Split(parsed.Normalize(",", "NULL", 0), ",")[3] - if DBName == "NULL" { - DBName = "" - } + if DBName == "NULL" { + DBName = "" + } - parsedDatabaseData := models.Connection{ - Name: connectionName, - Provider: parsed.Driver, - DBName: DBName, - URL: connectionString, - } + parsedDatabaseData := models.Connection{ + Name: connectionName, + Provider: parsed.Driver, + DBName: DBName, + URL: connectionString, + } - switch form.Action { - case "create": + switch form.Action { + case "create": - newDatabases = append(databases, parsedDatabaseData) - err := helpers.SaveConnectionConfig(newDatabases) - if err != nil { - form.StatusText.SetText(err.Error()).SetTextStyle(tcell.StyleDefault.Foreground(tcell.ColorRed)) - return event - } + newDatabases = append(databases, parsedDatabaseData) + err := helpers.SaveConnectionConfig(newDatabases) + if err != nil { + form.StatusText.SetText(err.Error()).SetTextStyle(tcell.StyleDefault.Foreground(tcell.ColorRed)) + return event + } - case "edit": - newDatabases = make([]models.Connection, len(databases)) - row, _ := ConnectionListTable.GetSelection() - - for i, database := range databases { - if i == row { - newDatabases[i] = parsedDatabaseData - - // newDatabases[i].Name = connectionName - // newDatabases[i].Provider = database.Provider - // newDatabases[i].User = parsed.User.Username() - // newDatabases[i].Password, _ = parsed.User.Password() - // newDatabases[i].Host = parsed.Hostname() - // newDatabases[i].Port = parsed.Port() - // newDatabases[i].Query = parsed.Query().Encode() - // newDatabases[i].DBName = helpers.ParsedDBName(parsed.Path) - // newDatabases[i].DSN = parsed.DSN - } else { - newDatabases[i] = database - } + case "edit": + newDatabases = make([]models.Connection, len(databases)) + row, _ := ConnectionListTable.GetSelection() + + for i, database := range databases { + if i == row { + newDatabases[i] = parsedDatabaseData + + // newDatabases[i].Name = connectionName + // newDatabases[i].Provider = database.Provider + // newDatabases[i].User = parsed.User.Username() + // newDatabases[i].Password, _ = parsed.User.Password() + // newDatabases[i].Host = parsed.Hostname() + // newDatabases[i].Port = parsed.Port() + // newDatabases[i].Query = parsed.Query().Encode() + // newDatabases[i].DBName = helpers.ParsedDBName(parsed.Path) + // newDatabases[i].DSN = parsed.DSN + } else { + newDatabases[i] = database } + } - err := helpers.SaveConnectionConfig(newDatabases) - if err != nil { - form.StatusText.SetText(err.Error()).SetTextStyle(tcell.StyleDefault.Foreground(tcell.ColorRed)) - return event + err := helpers.SaveConnectionConfig(newDatabases) + if err != nil { + form.StatusText.SetText(err.Error()).SetTextStyle(tcell.StyleDefault.Foreground(tcell.ColorRed)) + return event - } } - - ConnectionListTable.SetConnections(newDatabases) - connectionPages.SwitchToPage("Connections") } + + ConnectionListTable.SetConnections(newDatabases) + connectionPages.SwitchToPage("Connections") + } else if event.Key() == tcell.KeyF2 { connectionString := form.GetFormItem(1).(*tview.InputField).GetText() go form.testConnection(connectionString) diff --git a/components/ResultsTable.go b/components/ResultsTable.go index bae9516..ad46d76 100644 --- a/components/ResultsTable.go +++ b/components/ResultsTable.go @@ -268,96 +268,96 @@ func (table *ResultsTable) tableInputCapture(event *tcell.EventKey) *tcell.Event table.RemoveHighlightTable() table.SetIsFiltering(true) return nil - } else { - App.SetFocus(table.Filter.Input) - table.RemoveHighlightTable() - table.Filter.HighlightLocal() - table.SetIsFiltering(true) - - if table.Filter.Input.GetText() == "/" { - go table.Filter.Input.SetText("") - } + } - table.Filter.Input.SetAutocompleteFunc(func(currentText string) []string { - split := strings.Split(currentText, " ") - comparators := []string{"=", "!=", ">", "<", ">=", "<=", "LIKE", "NOT LIKE", "IN", "NOT IN", "IS", "IS NOT", "BETWEEN", "NOT BETWEEN"} + App.SetFocus(table.Filter.Input) + table.RemoveHighlightTable() + table.Filter.HighlightLocal() + table.SetIsFiltering(true) - if len(split) == 1 { - columns := table.GetColumns() - columnNames := []string{} + if table.Filter.Input.GetText() == "/" { + go table.Filter.Input.SetText("") + } - for i, col := range columns { - if i > 0 { - columnNames = append(columnNames, col[0]) - } - } + table.Filter.Input.SetAutocompleteFunc(func(currentText string) []string { + split := strings.Split(currentText, " ") + comparators := []string{"=", "!=", ">", "<", ">=", "<=", "LIKE", "NOT LIKE", "IN", "NOT IN", "IS", "IS NOT", "BETWEEN", "NOT BETWEEN"} - return columnNames - } else if len(split) == 2 { + if len(split) == 1 { + columns := table.GetColumns() + columnNames := []string{} - for i, comparator := range comparators { - comparators[i] = fmt.Sprintf("%s %s", split[0], strings.ToLower(comparator)) + for i, col := range columns { + if i > 0 { + columnNames = append(columnNames, col[0]) } + } - return comparators - } else if len(split) == 3 { + return columnNames + } else if len(split) == 2 { - ret := true + for i, comparator := range comparators { + comparators[i] = fmt.Sprintf("%s %s", split[0], strings.ToLower(comparator)) + } - if split[1] == "not" { - comparators = []string{"between", "in", "like"} - } else if split[1] == "is" { - comparators = []string{"not", "null"} - } else { - ret = false - } + return comparators + } else if len(split) == 3 { - if ret { - for i, comparator := range comparators { - comparators[i] = fmt.Sprintf("%s %s %s", split[0], split[1], strings.ToLower(comparator)) - } - return comparators - } + ret := true - } else if len(split) == 4 { - ret := true + if split[1] == "not" { + comparators = []string{"between", "in", "like"} + } else if split[1] == "is" { + comparators = []string{"not", "null"} + } else { + ret = false + } - if split[2] == "not" { - comparators = []string{"null"} - } else if split[2] == "is" { - comparators = []string{"not", "null"} - } else { - ret = false + if ret { + for i, comparator := range comparators { + comparators[i] = fmt.Sprintf("%s %s %s", split[0], split[1], strings.ToLower(comparator)) } + return comparators + } - if ret { - for i, comparator := range comparators { - comparators[i] = fmt.Sprintf("%s %s %s %s", split[0], split[1], split[2], strings.ToLower(comparator)) - } + } else if len(split) == 4 { + ret := true - return comparators - } + if split[2] == "not" { + comparators = []string{"null"} + } else if split[2] == "is" { + comparators = []string{"not", "null"} + } else { + ret = false } - return []string{} - }) + if ret { + for i, comparator := range comparators { + comparators[i] = fmt.Sprintf("%s %s %s %s", split[0], split[1], split[2], strings.ToLower(comparator)) + } + + return comparators + } + } - table.Filter.Input.SetAutocompletedFunc(func(text string, _ int, source int) bool { - if source != tview.AutocompletedNavigate { - inputText := strings.Split(table.Filter.Input.GetText(), " ") + return []string{} + }) - if len(inputText) == 1 { - table.Filter.Input.SetText(fmt.Sprintf("%s =", text)) - } else if len(inputText) == 2 { - table.Filter.Input.SetText(fmt.Sprintf("%s %s", inputText[0], text)) - } + table.Filter.Input.SetAutocompletedFunc(func(text string, _ int, source int) bool { + if source != tview.AutocompletedNavigate { + inputText := strings.Split(table.Filter.Input.GetText(), " ") - table.Filter.Input.SetText(text) + if len(inputText) == 1 { + table.Filter.Input.SetText(fmt.Sprintf("%s =", text)) + } else if len(inputText) == 2 { + table.Filter.Input.SetText(fmt.Sprintf("%s %s", inputText[0], text)) } - return source == tview.AutocompletedEnter || source == tview.AutocompletedClick - }) - } + table.Filter.Input.SetText(text) + } + return source == tview.AutocompletedEnter || source == tview.AutocompletedClick + }) + table.SetInputCapture(nil) } else if command == commands.Edit { table.StartEditingCell(selectedRowIndex, selectedColumnIndex, func(newValue string, row, col int) { From 76f23b26fa55cf25797a4a031a1e9b82e410a683 Mon Sep 17 00:00:00 2001 From: ccoVeille <3875889+ccoVeille@users.noreply.github.com> Date: Wed, 17 Jul 2024 14:19:26 +0200 Subject: [PATCH 10/15] chore: remove type or default value when obvious --- components/Home.go | 4 ++-- components/SQLEditor.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/components/Home.go b/components/Home.go index 428d6e3..c279a3f 100644 --- a/components/Home.go +++ b/components/Home.go @@ -77,7 +77,7 @@ func (home *Home) subscribeToTreeChanges() { tableName := stateChange.Value.(string) tab := home.TabbedPane.GetTabByName(tableName) - var table *ResultsTable = nil + var table *ResultsTable if tab != nil { table = tab.Content @@ -230,7 +230,7 @@ func (home *Home) rightWrapperInputCapture(event *tcell.EventKey) *tcell.EventKe func (home *Home) homeInputCapture(event *tcell.EventKey) *tcell.EventKey { tab := home.TabbedPane.GetCurrentTab() - var table *ResultsTable = nil + var table *ResultsTable if tab != nil { table = tab.Content diff --git a/components/SQLEditor.go b/components/SQLEditor.go index 9274c3c..e41b9ad 100644 --- a/components/SQLEditor.go +++ b/components/SQLEditor.go @@ -146,7 +146,7 @@ func openExternalEditor(s *SQLEditor) string { // Function to select editor func getEditor() string { - var editor string = os.Getenv("SQL_EDITOR") + editor := os.Getenv("SQL_EDITOR") if editor == "" { editor = os.Getenv("EDITOR") } @@ -164,7 +164,7 @@ func getEditor() string { // Function to select terminal func getTerminal() string { - var terminal string = os.Getenv("SQL_TERMINAL") + terminal := os.Getenv("SQL_TERMINAL") if terminal == "" { terminal = os.Getenv("TERMINAL") From 0110f988f4ea6dcc0857bac98535751ff315c1ad Mon Sep 17 00:00:00 2001 From: ccoVeille <3875889+ccoVeille@users.noreply.github.com> Date: Wed, 17 Jul 2024 14:20:03 +0200 Subject: [PATCH 11/15] chore: use go initialism for variable name --- components/ResultsTable.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/components/ResultsTable.go b/components/ResultsTable.go index ad46d76..39d81cd 100644 --- a/components/ResultsTable.go +++ b/components/ResultsTable.go @@ -437,13 +437,13 @@ func (table *ResultsTable) tableInputCapture(event *tcell.EventKey) *tcell.Event newRow := make([]string, table.GetColumnCount()) newRowIndex := table.GetRowCount() - newRowUuid := uuid.New() + newRowUUID := uuid.New() for i := 0; i < table.GetColumnCount(); i++ { newRow[i] = "Default" } - table.InsertRow(newRow, newRowIndex, newRowUuid) + table.InsertRow(newRow, newRowIndex, newRowUUID) for i := 0; i < table.GetColumnCount(); i++ { table.GetCell(newRowIndex, i).SetBackgroundColor(tcell.ColorDarkGreen) @@ -453,7 +453,7 @@ func (table *ResultsTable) tableInputCapture(event *tcell.EventKey) *tcell.Event Table: table.GetDBReference(), Columns: table.GetRecords()[0], Values: newRow, - PrimaryKeyValue: newRowUuid, + PrimaryKeyValue: newRowUUID, Option: 1, } @@ -970,9 +970,9 @@ func (table *ResultsTable) StartEditingCell(row int, col int, callback func(newV App.SetFocus(inputField) } -func (table *ResultsTable) CheckIfRowIsInserted(rowId uuid.UUID) bool { +func (table *ResultsTable) CheckIfRowIsInserted(rowID uuid.UUID) bool { for _, insertedRow := range *table.state.listOfDbInserts { - if insertedRow.PrimaryKeyValue == rowId { + if insertedRow.PrimaryKeyValue == rowID { return true } } @@ -980,9 +980,9 @@ func (table *ResultsTable) CheckIfRowIsInserted(rowId uuid.UUID) bool { return false } -func (table *ResultsTable) MutateInsertedRowCell(rowId uuid.UUID, colIndex int, newValue string) { +func (table *ResultsTable) MutateInsertedRowCell(rowID uuid.UUID, colIndex int, newValue string) { for i, insertedRow := range *table.state.listOfDbInserts { - if insertedRow.PrimaryKeyValue == rowId { + if insertedRow.PrimaryKeyValue == rowID { (*table.state.listOfDbInserts)[i].Values[colIndex] = newValue } } From 6f22fa1c6436400967c56e67bd8c989634a94e82 Mon Sep 17 00:00:00 2001 From: ccoVeille <3875889+ccoVeille@users.noreply.github.com> Date: Wed, 17 Jul 2024 14:20:35 +0200 Subject: [PATCH 12/15] chore: no uppercase constant in Go --- drivers/postgres.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/postgres.go b/drivers/postgres.go index ebaafad..0d2fa4a 100644 --- a/drivers/postgres.go +++ b/drivers/postgres.go @@ -22,7 +22,7 @@ type Postgres struct { } const ( - DEFAULT_PORT = "5432" + defaultPort = "5432" ) func (db *Postgres) Connect(urlstr string) (err error) { @@ -582,7 +582,7 @@ func (db *Postgres) SwitchDatabase(database string) error { dbname := parsedConn.Path if port == "" { - port = DEFAULT_PORT + port = defaultPort } if dbname == "" { From e6ff2b384bb4502d8fe550c2d7c98c91b6fcdd48 Mon Sep 17 00:00:00 2001 From: ccoVeille <3875889+ccoVeille@users.noreply.github.com> Date: Wed, 17 Jul 2024 14:21:10 +0200 Subject: [PATCH 13/15] chore: remove suspicious blank imports --- drivers/mysql.go | 1 - drivers/postgres.go | 1 - drivers/sqlite.go | 1 - go.mod | 2 -- go.sum | 4 ---- 5 files changed, 9 deletions(-) diff --git a/drivers/mysql.go b/drivers/mysql.go index dbcfba8..03f00b6 100644 --- a/drivers/mysql.go +++ b/drivers/mysql.go @@ -7,7 +7,6 @@ import ( "strconv" "strings" - _ "github.com/go-sql-driver/mysql" "github.com/xo/dburl" "github.com/jorgerojas26/lazysql/models" diff --git a/drivers/postgres.go b/drivers/postgres.go index 0d2fa4a..a0b2e48 100644 --- a/drivers/postgres.go +++ b/drivers/postgres.go @@ -7,7 +7,6 @@ import ( "strconv" "strings" - _ "github.com/lib/pq" "github.com/xo/dburl" "github.com/jorgerojas26/lazysql/models" diff --git a/drivers/sqlite.go b/drivers/sqlite.go index c684738..c373164 100644 --- a/drivers/sqlite.go +++ b/drivers/sqlite.go @@ -7,7 +7,6 @@ import ( "strconv" "strings" - _ "github.com/mattn/go-sqlite3" "github.com/xo/dburl" "github.com/jorgerojas26/lazysql/models" diff --git a/go.mod b/go.mod index 57c0112..74574e4 100644 --- a/go.mod +++ b/go.mod @@ -6,8 +6,6 @@ require ( github.com/gdamore/tcell/v2 v2.7.0 github.com/go-sql-driver/mysql v1.7.1 github.com/google/uuid v1.5.0 - github.com/lib/pq v1.10.9 - github.com/mattn/go-sqlite3 v1.14.19 github.com/pelletier/go-toml/v2 v2.1.1 github.com/rivo/tview v0.0.0-20240101144852-b3bd1aa5e9f2 github.com/xo/dburl v0.20.2 diff --git a/go.sum b/go.sum index 1dcb137..d643142 100644 --- a/go.sum +++ b/go.sum @@ -10,14 +10,10 @@ github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrt github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU= github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= -github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U= github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= -github.com/mattn/go-sqlite3 v1.14.19 h1:fhGleo2h1p8tVChob4I9HpmVFIAkKGpiukdrgQbWfGI= -github.com/mattn/go-sqlite3 v1.14.19/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= github.com/pelletier/go-toml/v2 v2.1.1 h1:LWAJwfNvjQZCFIDKWYQaM62NcYeYViCmWIwmOStowAI= github.com/pelletier/go-toml/v2 v2.1.1/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= From 2b0ffc412bb921094db028241a0a93755798043c Mon Sep 17 00:00:00 2001 From: ccoVeille <3875889+ccoVeille@users.noreply.github.com> Date: Wed, 17 Jul 2024 14:21:55 +0200 Subject: [PATCH 14/15] fix: make sure to use the database name when getting table columns database variable was not used --- drivers/mysql.go | 2 +- drivers/sqlite.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/mysql.go b/drivers/mysql.go index 03f00b6..6227700 100644 --- a/drivers/mysql.go +++ b/drivers/mysql.go @@ -84,7 +84,7 @@ func (db *MySQL) GetTables(database string) (map[string][]string, error) { func (db *MySQL) GetTableColumns(database, table string) (results [][]string, err error) { table = db.formatTableName(table) - rows, err := db.Connection.Query("DESCRIBE " + table) + rows, err := db.Connection.Query(fmt.Sprintf("DESCRIBE %s.%s", database, table)) if err != nil { return nil, err } diff --git a/drivers/sqlite.go b/drivers/sqlite.go index c373164..700ef81 100644 --- a/drivers/sqlite.go +++ b/drivers/sqlite.go @@ -84,7 +84,7 @@ func (db *SQLite) GetTables(database string) (map[string][]string, error) { } func (db *SQLite) GetTableColumns(database, table string) (results [][]string, err error) { - rows, err := db.Connection.Query("PRAGMA table_info(" + table + ")") + rows, err := db.Connection.Query(fmt.Sprintf("PRAGMA %s.table_info(%s)", database, table)) if err != nil { return nil, err } From 451cce7e785d153d39140abcd314ed1c92971b36 Mon Sep 17 00:00:00 2001 From: ccoVeille <3875889+ccoVeille@users.noreply.github.com> Date: Wed, 17 Jul 2024 14:23:43 +0200 Subject: [PATCH 15/15] ci: enable golang-lint in GitHub actions --- .github/workflows/release.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 81bd942..97d608f 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -20,6 +20,11 @@ jobs: - name: Set up Go uses: actions/setup-go@v5 + + - + name: Golangci-lint + uses: golangci/golangci-lint-action@v6.0.1 + - name: Run GoReleaser uses: goreleaser/goreleaser-action@v6