Skip to content
Merged

Gorm #110

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 3 additions & 12 deletions cli/cmd/db.go
Original file line number Diff line number Diff line change
@@ -1,36 +1,27 @@
package cmd

import (
"database/sql"
"fmt"
"os"

"github.com/corecollectives/mist/models"
_ "github.com/mattn/go-sqlite3"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)

const dbPath = "/var/lib/mist/mist.db"

// initDB initializes the database connection for CLI
// It expects the database file to already exist
func initDB() error {
// Check if database file exists
if _, err := os.Stat(dbPath); os.IsNotExist(err) {
return fmt.Errorf("database file not found at %s. Please ensure Mist is installed and running", dbPath)
}

// Open database connection
db, err := sql.Open("sqlite3", dbPath)
db, err := gorm.Open(sqlite.Open(dbPath), &gorm.Config{})
if err != nil {
return fmt.Errorf("failed to open database: %v", err)
}

// Test connection
if err := db.Ping(); err != nil {
return fmt.Errorf("failed to connect to database: %v", err)
}

// Set the database instance for models
models.SetDB(db)
return nil
}
1 change: 0 additions & 1 deletion cli/cmd/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ func changePassword(args []string) {
}

func listUsers(args []string) {
// Initialize database
if err := initDB(); err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
Expand Down
9 changes: 8 additions & 1 deletion cli/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,23 @@ go 1.25.1

require (
github.com/corecollectives/mist v0.0.0
github.com/mattn/go-sqlite3 v1.14.33
golang.org/x/crypto v0.43.0
golang.org/x/term v0.36.0
gorm.io/driver/sqlite v1.6.0
gorm.io/gorm v1.31.1
)

require (
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mattn/go-sqlite3 v1.14.32 // indirect
github.com/rs/zerolog v1.34.0 // indirect
golang.org/x/sys v0.37.0 // indirect
golang.org/x/text v0.33.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace github.com/corecollectives/mist => ../server
20 changes: 18 additions & 2 deletions cli/go.sum
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY=
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-sqlite3 v1.14.32 h1:JD12Ag3oLy1zQA+BNn74xRgaBbdhbNIDYvQUEuuErjs=
github.com/mattn/go-sqlite3 v1.14.32/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/mattn/go-sqlite3 v1.14.33 h1:A5blZ5ulQo2AtayQ9/limgHEkFreKj1Dv226a1K73s0=
github.com/mattn/go-sqlite3 v1.14.33/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0=
github.com/rs/zerolog v1.34.0 h1:k43nTLIwcTVQAncfCw4KZ2VY6ukYoZaBPNOE8txlOeY=
Expand All @@ -20,3 +26,13 @@ golang.org/x/sys v0.37.0 h1:fdNQudmxPjkdUTPnLn5mdQv7Zwvbvpaxqs831goi9kQ=
golang.org/x/sys v0.37.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
golang.org/x/term v0.36.0 h1:zMPR+aF8gfksFprF/Nc/rd1wRS1EI6nDBGyWAvDzx2Q=
golang.org/x/term v0.36.0/go.mod h1:Qu394IJq6V6dCBRgwqshf3mPF85AqzYEzofzRdZkWss=
golang.org/x/text v0.33.0 h1:B3njUFyqtHDUI5jMn1YIr5B0IE2U0qck04r6d4KPAxE=
golang.org/x/text v0.33.0/go.mod h1:LuMebE6+rBincTi9+xWTY8TztLzKHc/9C1uBCG27+q8=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/driver/sqlite v1.6.0 h1:WHRRrIiulaPiPFmDcod6prc4l2VGVWHz80KspNsxSfQ=
gorm.io/driver/sqlite v1.6.0/go.mod h1:AO9V1qIQddBESngQUKWL9yoH93HIeA1X6V633rBwyT8=
gorm.io/gorm v1.31.1 h1:7CA8FTFz/gRfgqgpeKIBcervUn3xSyPUmr6B2WXJ7kg=
gorm.io/gorm v1.31.1/go.mod h1:XyQVbO2k6YkOis7C2437jSit3SsDK72s7n7rsSHd+Gs=
5 changes: 3 additions & 2 deletions server/api/handlers/applications/preview.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package applications

import (
"database/sql"
"encoding/json"
"errors"
"fmt"
"net"
"net/http"

"github.com/corecollectives/mist/api/handlers"
"github.com/corecollectives/mist/api/middleware"
"github.com/corecollectives/mist/models"
"gorm.io/gorm"
)

func getOutboundIP() string {
Expand Down Expand Up @@ -56,7 +57,7 @@ func GetPreviewURL(w http.ResponseWriter, r *http.Request) {

domain, err := models.GetPrimaryDomainByAppID(req.AppID)
if err != nil {
if err == sql.ErrNoRows || err.Error() == "sql: no rows in result set" {
if errors.Is(err, gorm.ErrRecordNotFound) {
app, appErr := models.GetApplicationByID(req.AppID)
if appErr != nil {
handlers.SendResponse(w, http.StatusInternalServerError, false, nil, "Failed to get application", appErr.Error())
Expand Down
5 changes: 3 additions & 2 deletions server/api/handlers/auth/login.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package auth

import (
"database/sql"
"encoding/json"
"errors"
"net/http"
"strings"

"github.com/corecollectives/mist/api/handlers"
"github.com/corecollectives/mist/api/middleware"
"github.com/corecollectives/mist/models"
"github.com/rs/zerolog/log"
"gorm.io/gorm"
)

func LoginHandler(w http.ResponseWriter, r *http.Request) {
Expand All @@ -28,7 +29,7 @@ func LoginHandler(w http.ResponseWriter, r *http.Request) {
return
}
user, err := models.GetUserByEmail(cred.Email)
if err == sql.ErrNoRows {
if errors.Is(err, gorm.ErrRecordNotFound) {
handlers.SendResponse(w, http.StatusUnauthorized, false, nil, "Invalid email or password", "Unauthorized")
return
} else if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions server/api/handlers/auth/me.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package auth

import (
"database/sql"
"errors"
"net/http"

"github.com/corecollectives/mist/api/handlers"
"github.com/corecollectives/mist/api/middleware"
"github.com/corecollectives/mist/models"
"github.com/corecollectives/mist/store"
"gorm.io/gorm"
)

func MeHandler(w http.ResponseWriter, r *http.Request) {
Expand All @@ -29,7 +29,7 @@ func MeHandler(w http.ResponseWriter, r *http.Request) {

userId := claims.UserID
user, err := models.GetUserByID(userId)
if errors.Is(err, sql.ErrNoRows) {
if errors.Is(err, gorm.ErrRecordNotFound) {
handlers.SendResponse(w, http.StatusOK, true, map[string]interface{}{"setupRequired": setupRequired, "user": nil}, "User not found", "")
return
} else if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions server/api/handlers/github/repositories.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package github

import (
"database/sql"
"encoding/json"
"errors"
"fmt"
"net/http"
"time"
Expand All @@ -11,6 +11,7 @@ import (
"github.com/corecollectives/mist/api/middleware"
"github.com/corecollectives/mist/github"
"github.com/corecollectives/mist/models"
"gorm.io/gorm"
)

type RepoListResponse struct {
Expand All @@ -26,7 +27,7 @@ func GetRepositories(w http.ResponseWriter, r *http.Request) {
}

installationID, err := models.GetInstallationID(int(userData.ID))
if err == sql.ErrNoRows {
if errors.Is(err, gorm.ErrRecordNotFound) {
handlers.SendResponse(w, http.StatusNotFound, false, nil, "no installation found for user", "No installation found")
return
}
Expand Down
10 changes: 5 additions & 5 deletions server/api/handlers/projects/createProject.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package projects

import (
"database/sql"
"encoding/json"
"net/http"

Expand Down Expand Up @@ -42,12 +41,13 @@ func CreateProject(w http.ResponseWriter, r *http.Request) {
OwnerID: userData.ID,
}
if input.Description != "" {
project.Description = sql.NullString{String: input.Description, Valid: true}
desc := input.Description
project.Description = &desc
} else {
project.Description = nil
}
if input.Tags != nil {
for _, tag := range input.Tags {
project.Tags = append(project.Tags, sql.NullString{String: tag, Valid: true})
}
project.Tags = input.Tags
}
err := project.InsertInDB()

Expand Down
5 changes: 3 additions & 2 deletions server/api/handlers/projects/deleteProject.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package projects

import (
"database/sql"
"errors"
"net/http"
"strconv"

"github.com/corecollectives/mist/api/handlers"
"github.com/corecollectives/mist/api/middleware"
"github.com/corecollectives/mist/models"
"gorm.io/gorm"
)

func DeleteProject(w http.ResponseWriter, r *http.Request) {
Expand All @@ -29,7 +30,7 @@ func DeleteProject(w http.ResponseWriter, r *http.Request) {
}

project, err := models.GetProjectByID(projectId)
if err == sql.ErrNoRows {
if errors.Is(err, gorm.ErrRecordNotFound) {
handlers.SendResponse(w, http.StatusNotFound, false, nil, "Project not found", "no project with that ID")
return
} else if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions server/api/handlers/projects/getProjectFromId.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package projects

import (
"database/sql"
"errors"
"net/http"
"strconv"

"github.com/corecollectives/mist/api/handlers"
"github.com/corecollectives/mist/api/middleware"
"github.com/corecollectives/mist/models"
"gorm.io/gorm"
)

func GetProjectFromId(w http.ResponseWriter, r *http.Request) {
Expand All @@ -31,7 +32,7 @@ func GetProjectFromId(w http.ResponseWriter, r *http.Request) {
}

project, err := models.GetProjectByID(projectId)
if err == sql.ErrNoRows {
if errors.Is(err, gorm.ErrRecordNotFound) {
handlers.SendResponse(w, http.StatusNotFound, false, nil, "Project not found", "no project with that ID")
return
} else if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions server/api/handlers/projects/updateMembers.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package projects

import (
"database/sql"
"encoding/json"
"errors"
"net/http"
"strconv"

"github.com/corecollectives/mist/api/handlers"
"github.com/corecollectives/mist/api/middleware"
"github.com/corecollectives/mist/models"
"gorm.io/gorm"
)

func UpdateMembers(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -38,7 +39,7 @@ func UpdateMembers(w http.ResponseWriter, r *http.Request) {
}

project, err := models.GetProjectByID(projectId)
if err == sql.ErrNoRows {
if errors.Is(err, gorm.ErrRecordNotFound) {
handlers.SendResponse(w, http.StatusNotFound, false, nil, "Project not found", "no such project")
return
} else if err != nil {
Expand Down
25 changes: 15 additions & 10 deletions server/api/handlers/projects/updateProject.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package projects

import (
"database/sql"
"encoding/json"
"errors"
"net/http"
"strconv"

"github.com/corecollectives/mist/api/handlers"
"github.com/corecollectives/mist/api/middleware"
"github.com/corecollectives/mist/models"
"gorm.io/gorm"
)

func UpdateProject(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -44,7 +45,7 @@ func UpdateProject(w http.ResponseWriter, r *http.Request) {
}

existingProject, err := models.GetProjectByID(projectId)
if err == sql.ErrNoRows {
if errors.Is(err, gorm.ErrRecordNotFound) {
handlers.SendResponse(w, http.StatusNotFound, false, nil, "Project not found", "no such project")
return
} else if err != nil {
Expand All @@ -57,19 +58,23 @@ func UpdateProject(w http.ResponseWriter, r *http.Request) {
return
}

tags := make([]sql.NullString, len(input.Tags))
for i, tag := range input.Tags {
tags[i] = sql.NullString{
String: tag,
Valid: true,
}
// tags := make([]string, len(input.Tags))
// for i, tag := range input.Tags {
// tags[i] = tag
// }
var desc *string
if input.Description != "" {
desc = &input.Description

} else {
desc = nil
}

project := &models.Project{
ID: projectId,
Name: input.Name,
Description: sql.NullString{String: input.Description, Valid: input.Description != ""},
Tags: tags,
Description: desc,
Tags: input.Tags,
}

err = models.UpdateProject(project)
Expand Down
9 changes: 6 additions & 3 deletions server/api/handlers/updates/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,13 +270,16 @@ func ClearStuckUpdate(w http.ResponseWriter, r *http.Request) {

// Only allow clearing in_progress updates
if updateLog.Status != "in_progress" {
handlers.SendResponse(w, http.StatusBadRequest, false, nil, "Can only clear in_progress updates", "Current status: "+updateLog.Status)
handlers.SendResponse(w, http.StatusBadRequest, false, nil, "Can only clear in_progress updates", "Current status: "+string(updateLog.Status))
return
}

currentLogs := ""
if updateLog.Logs != nil {
currentLogs = *updateLog.Logs
}
// Mark as failed with note that it was manually cleared
errMsg := "Update was manually cleared by administrator"
clearLog := updateLog.Logs + "\n⚠️ " + errMsg + "\n"
clearLog := currentLogs + "\n⚠️ " + errMsg + "\n"
err = models.UpdateUpdateLogStatus(id, "failed", clearLog, &errMsg)
if err != nil {
log.Error().Err(err).Int64("update_log_id", id).Msg("Failed to clear stuck update")
Expand Down
Loading
Loading