mirror of
https://github.com/lordmathis/lemma.git
synced 2025-11-06 07:54:22 +00:00
Remove too many debug messages
This commit is contained in:
@@ -113,7 +113,6 @@ type database struct {
|
||||
// Init initializes the database connection
|
||||
func Init(dbPath string, secretsService secrets.Service) (Database, error) {
|
||||
log := getLogger()
|
||||
log.Info("initializing database", "path", dbPath)
|
||||
|
||||
db, err := sql.Open("sqlite3", dbPath)
|
||||
if err != nil {
|
||||
@@ -136,7 +135,6 @@ func Init(dbPath string, secretsService secrets.Service) (Database, error) {
|
||||
secretsService: secretsService,
|
||||
}
|
||||
|
||||
log.Info("database initialized successfully")
|
||||
return database, nil
|
||||
}
|
||||
|
||||
@@ -148,17 +146,12 @@ func (db *database) Close() error {
|
||||
if err := db.DB.Close(); err != nil {
|
||||
return fmt.Errorf("failed to close database: %w", err)
|
||||
}
|
||||
|
||||
log.Info("database connection closed successfully")
|
||||
return nil
|
||||
}
|
||||
|
||||
// Helper methods for token encryption/decryption
|
||||
func (db *database) encryptToken(token string) (string, error) {
|
||||
log := getLogger()
|
||||
|
||||
if token == "" {
|
||||
log.Debug("skipping encryption for empty token")
|
||||
return "", nil
|
||||
}
|
||||
|
||||
@@ -167,15 +160,11 @@ func (db *database) encryptToken(token string) (string, error) {
|
||||
return "", fmt.Errorf("failed to encrypt token: %w", err)
|
||||
}
|
||||
|
||||
log.Debug("token encrypted successfully")
|
||||
return encrypted, nil
|
||||
}
|
||||
|
||||
func (db *database) decryptToken(token string) (string, error) {
|
||||
log := getLogger()
|
||||
|
||||
if token == "" {
|
||||
log.Debug("skipping decryption for empty token")
|
||||
return "", nil
|
||||
}
|
||||
|
||||
@@ -184,6 +173,5 @@ func (db *database) decryptToken(token string) (string, error) {
|
||||
return "", fmt.Errorf("failed to decrypt token: %w", err)
|
||||
}
|
||||
|
||||
log.Debug("token decrypted successfully")
|
||||
return decrypted, nil
|
||||
}
|
||||
|
||||
@@ -82,7 +82,6 @@ func (db *database) Migrate() error {
|
||||
log.Info("starting database migration")
|
||||
|
||||
// Create migrations table if it doesn't exist
|
||||
log.Debug("ensuring migrations table exists")
|
||||
_, err := db.Exec(`CREATE TABLE IF NOT EXISTS migrations (
|
||||
version INTEGER PRIMARY KEY
|
||||
)`)
|
||||
@@ -91,58 +90,49 @@ func (db *database) Migrate() error {
|
||||
}
|
||||
|
||||
// Get current version
|
||||
log.Debug("checking current migration version")
|
||||
var currentVersion int
|
||||
err = db.QueryRow("SELECT COALESCE(MAX(version), 0) FROM migrations").Scan(¤tVersion)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get current migration version: %w", err)
|
||||
}
|
||||
log.Info("current database version", "version", currentVersion)
|
||||
|
||||
// Apply new migrations
|
||||
for _, migration := range migrations {
|
||||
if migration.Version > currentVersion {
|
||||
log := log.With("migration_version", migration.Version)
|
||||
log.Info("applying migration")
|
||||
|
||||
tx, err := db.Begin()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to begin transaction for migration %d: %w", migration.Version, err)
|
||||
}
|
||||
|
||||
// Execute migration SQL
|
||||
log.Debug("executing migration SQL")
|
||||
_, err = tx.Exec(migration.SQL)
|
||||
if err != nil {
|
||||
if rbErr := tx.Rollback(); rbErr != nil {
|
||||
return fmt.Errorf("migration %d failed: %v, rollback failed: %v",
|
||||
migration.Version, err, rbErr)
|
||||
}
|
||||
log.Debug("successfully rolled back failed migration")
|
||||
return fmt.Errorf("migration %d failed: %w", migration.Version, err)
|
||||
}
|
||||
|
||||
// Update migrations table
|
||||
log.Debug("updating migrations version")
|
||||
_, err = tx.Exec("INSERT INTO migrations (version) VALUES (?)", migration.Version)
|
||||
if err != nil {
|
||||
if rbErr := tx.Rollback(); rbErr != nil {
|
||||
return fmt.Errorf("failed to update migration version: %v, rollback failed: %v",
|
||||
err, rbErr)
|
||||
}
|
||||
log.Debug("successfully rolled back failed version update")
|
||||
return fmt.Errorf("failed to update migration version: %w", err)
|
||||
}
|
||||
|
||||
// Commit transaction
|
||||
log.Debug("committing migration")
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to commit migration %d: %w", migration.Version, err)
|
||||
}
|
||||
|
||||
currentVersion = migration.Version
|
||||
log.Info("migration applied successfully", "new_version", currentVersion)
|
||||
log.Debug("migration applied", "new_version", currentVersion)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -10,12 +10,6 @@ import (
|
||||
|
||||
// CreateSession inserts a new session record into the database
|
||||
func (db *database) CreateSession(session *models.Session) error {
|
||||
log := getLogger().WithGroup("sessions")
|
||||
log.Debug("creating new session",
|
||||
"session_id", session.ID,
|
||||
"user_id", session.UserID,
|
||||
"expires_at", session.ExpiresAt)
|
||||
|
||||
_, err := db.Exec(`
|
||||
INSERT INTO sessions (id, user_id, refresh_token, expires_at, created_at)
|
||||
VALUES (?, ?, ?, ?, ?)`,
|
||||
@@ -25,17 +19,11 @@ func (db *database) CreateSession(session *models.Session) error {
|
||||
return fmt.Errorf("failed to store session: %w", err)
|
||||
}
|
||||
|
||||
log.Info("session created successfully",
|
||||
"session_id", session.ID,
|
||||
"user_id", session.UserID)
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetSessionByRefreshToken retrieves a session by its refresh token
|
||||
func (db *database) GetSessionByRefreshToken(refreshToken string) (*models.Session, error) {
|
||||
log := getLogger().WithGroup("sessions")
|
||||
log.Debug("fetching session by refresh token")
|
||||
|
||||
session := &models.Session{}
|
||||
err := db.QueryRow(`
|
||||
SELECT id, user_id, refresh_token, expires_at, created_at
|
||||
@@ -45,24 +33,17 @@ func (db *database) GetSessionByRefreshToken(refreshToken string) (*models.Sessi
|
||||
).Scan(&session.ID, &session.UserID, &session.RefreshToken, &session.ExpiresAt, &session.CreatedAt)
|
||||
|
||||
if err == sql.ErrNoRows {
|
||||
log.Debug("session not found or expired")
|
||||
return nil, fmt.Errorf("session not found or expired")
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to fetch session: %w", err)
|
||||
}
|
||||
|
||||
log.Debug("session retrieved successfully",
|
||||
"session_id", session.ID,
|
||||
"user_id", session.UserID)
|
||||
return session, nil
|
||||
}
|
||||
|
||||
// GetSessionByID retrieves a session by its ID
|
||||
func (db *database) GetSessionByID(sessionID string) (*models.Session, error) {
|
||||
log := getLogger().WithGroup("sessions")
|
||||
log.Debug("fetching session by ID", "session_id", sessionID)
|
||||
|
||||
session := &models.Session{}
|
||||
err := db.QueryRow(`
|
||||
SELECT id, user_id, refresh_token, expires_at, created_at
|
||||
@@ -72,24 +53,17 @@ func (db *database) GetSessionByID(sessionID string) (*models.Session, error) {
|
||||
).Scan(&session.ID, &session.UserID, &session.RefreshToken, &session.ExpiresAt, &session.CreatedAt)
|
||||
|
||||
if err == sql.ErrNoRows {
|
||||
log.Debug("session not found", "session_id", sessionID)
|
||||
return nil, fmt.Errorf("session not found")
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to fetch session: %w", err)
|
||||
}
|
||||
|
||||
log.Debug("session retrieved successfully",
|
||||
"session_id", session.ID,
|
||||
"user_id", session.UserID)
|
||||
return session, nil
|
||||
}
|
||||
|
||||
// DeleteSession removes a session from the database
|
||||
func (db *database) DeleteSession(sessionID string) error {
|
||||
log := getLogger().WithGroup("sessions")
|
||||
log.Debug("deleting session", "session_id", sessionID)
|
||||
|
||||
result, err := db.Exec("DELETE FROM sessions WHERE id = ?", sessionID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to delete session: %w", err)
|
||||
@@ -101,19 +75,15 @@ func (db *database) DeleteSession(sessionID string) error {
|
||||
}
|
||||
|
||||
if rowsAffected == 0 {
|
||||
log.Debug("no session found to delete", "session_id", sessionID)
|
||||
return fmt.Errorf("session not found")
|
||||
}
|
||||
|
||||
log.Info("session deleted successfully", "session_id", sessionID)
|
||||
return nil
|
||||
}
|
||||
|
||||
// CleanExpiredSessions removes all expired sessions from the database
|
||||
func (db *database) CleanExpiredSessions() error {
|
||||
log := getLogger().WithGroup("sessions")
|
||||
log.Info("cleaning expired sessions")
|
||||
|
||||
result, err := db.Exec("DELETE FROM sessions WHERE expires_at <= ?", time.Now())
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to clean expired sessions: %w", err)
|
||||
@@ -124,6 +94,6 @@ func (db *database) CleanExpiredSessions() error {
|
||||
return fmt.Errorf("failed to get rows affected: %w", err)
|
||||
}
|
||||
|
||||
log.Info("expired sessions cleaned successfully", "sessions_removed", rowsAffected)
|
||||
log.Info("cleaned expired sessions", "sessions_removed", rowsAffected)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -22,17 +22,13 @@ type UserStats struct {
|
||||
// If no secret exists, it generates and stores a new one
|
||||
func (db *database) EnsureJWTSecret() (string, error) {
|
||||
log := getLogger().WithGroup("system")
|
||||
log.Debug("ensuring JWT secret exists")
|
||||
|
||||
// First, try to get existing secret
|
||||
secret, err := db.GetSystemSetting(JWTSecretKey)
|
||||
if err == nil {
|
||||
log.Debug("existing JWT secret found")
|
||||
return secret, nil
|
||||
}
|
||||
|
||||
log.Info("no existing JWT secret found, generating new secret")
|
||||
|
||||
// Generate new secret if none exists
|
||||
newSecret, err := generateRandomSecret(32) // 256 bits
|
||||
if err != nil {
|
||||
@@ -45,30 +41,24 @@ func (db *database) EnsureJWTSecret() (string, error) {
|
||||
return "", fmt.Errorf("failed to store JWT secret: %w", err)
|
||||
}
|
||||
|
||||
log.Info("new JWT secret generated and stored successfully")
|
||||
log.Info("new JWT secret generated and stored")
|
||||
|
||||
return newSecret, nil
|
||||
}
|
||||
|
||||
// GetSystemSetting retrieves a system setting by key
|
||||
func (db *database) GetSystemSetting(key string) (string, error) {
|
||||
log := getLogger().WithGroup("system")
|
||||
log.Debug("retrieving system setting", "key", key)
|
||||
|
||||
var value string
|
||||
err := db.QueryRow("SELECT value FROM system_settings WHERE key = ?", key).Scan(&value)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
log.Debug("system setting retrieved successfully", "key", key)
|
||||
return value, nil
|
||||
}
|
||||
|
||||
// SetSystemSetting stores or updates a system setting
|
||||
func (db *database) SetSystemSetting(key, value string) error {
|
||||
log := getLogger().WithGroup("system")
|
||||
log.Debug("storing system setting", "key", key)
|
||||
|
||||
_, err := db.Exec(`
|
||||
INSERT INTO system_settings (key, value)
|
||||
VALUES (?, ?)
|
||||
@@ -79,7 +69,6 @@ func (db *database) SetSystemSetting(key, value string) error {
|
||||
return fmt.Errorf("failed to store system setting: %w", err)
|
||||
}
|
||||
|
||||
log.Info("system setting stored successfully", "key", key)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -95,15 +84,11 @@ func generateRandomSecret(bytes int) (string, error) {
|
||||
}
|
||||
|
||||
secret := base64.StdEncoding.EncodeToString(b)
|
||||
log.Debug("random secret generated successfully", "bytes", bytes)
|
||||
return secret, nil
|
||||
}
|
||||
|
||||
// GetSystemStats returns system-wide statistics
|
||||
func (db *database) GetSystemStats() (*UserStats, error) {
|
||||
log := getLogger().WithGroup("system")
|
||||
log.Debug("collecting system statistics")
|
||||
|
||||
stats := &UserStats{}
|
||||
|
||||
// Get total users
|
||||
@@ -111,14 +96,12 @@ func (db *database) GetSystemStats() (*UserStats, error) {
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get total users count: %w", err)
|
||||
}
|
||||
log.Debug("got total users count", "count", stats.TotalUsers)
|
||||
|
||||
// Get total workspaces
|
||||
err = db.QueryRow("SELECT COUNT(*) FROM workspaces").Scan(&stats.TotalWorkspaces)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get total workspaces count: %w", err)
|
||||
}
|
||||
log.Debug("got total workspaces count", "count", stats.TotalWorkspaces)
|
||||
|
||||
// Get active users (users with activity in last 30 days)
|
||||
err = db.QueryRow(`
|
||||
@@ -129,6 +112,5 @@ func (db *database) GetSystemStats() (*UserStats, error) {
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get active users count: %w", err)
|
||||
}
|
||||
log.Debug("got active users count", "count", stats.ActiveUsers)
|
||||
return stats, nil
|
||||
}
|
||||
|
||||
@@ -9,9 +9,7 @@ import (
|
||||
// CreateUser inserts a new user record into the database
|
||||
func (db *database) CreateUser(user *models.User) (*models.User, error) {
|
||||
log := getLogger().WithGroup("users")
|
||||
log.Debug("creating new user",
|
||||
"email", user.Email,
|
||||
"role", user.Role)
|
||||
log.Debug("creating user", "email", user.Email)
|
||||
|
||||
tx, err := db.Begin()
|
||||
if err != nil {
|
||||
@@ -40,7 +38,6 @@ func (db *database) CreateUser(user *models.User) (*models.User, error) {
|
||||
}
|
||||
|
||||
// Create default workspace with default settings
|
||||
log.Debug("creating default workspace for user", "user_id", user.ID)
|
||||
defaultWorkspace := &models.Workspace{
|
||||
UserID: user.ID,
|
||||
Name: "Main",
|
||||
@@ -54,9 +51,6 @@ func (db *database) CreateUser(user *models.User) (*models.User, error) {
|
||||
}
|
||||
|
||||
// Update user's last workspace ID
|
||||
log.Debug("updating user's last workspace",
|
||||
"user_id", user.ID,
|
||||
"workspace_id", defaultWorkspace.ID)
|
||||
_, err = tx.Exec("UPDATE users SET last_workspace_id = ? WHERE id = ?", defaultWorkspace.ID, user.ID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to update last workspace ID: %w", err)
|
||||
@@ -67,21 +61,15 @@ func (db *database) CreateUser(user *models.User) (*models.User, error) {
|
||||
return nil, fmt.Errorf("failed to commit transaction: %w", err)
|
||||
}
|
||||
|
||||
log.Debug("created user", "user_id", user.ID)
|
||||
|
||||
user.LastWorkspaceID = defaultWorkspace.ID
|
||||
log.Info("user created",
|
||||
"user_id", user.ID,
|
||||
"email", user.Email,
|
||||
"workspace_id", defaultWorkspace.ID)
|
||||
return user, nil
|
||||
}
|
||||
|
||||
// Helper function to create a workspace in a transaction
|
||||
func (db *database) createWorkspaceTx(tx *sql.Tx, workspace *models.Workspace) error {
|
||||
log := getLogger().WithGroup("users")
|
||||
log.Debug("creating workspace in transaction",
|
||||
"user_id", workspace.UserID,
|
||||
"name", workspace.Name)
|
||||
|
||||
result, err := tx.Exec(`
|
||||
INSERT INTO workspaces (
|
||||
user_id, name,
|
||||
@@ -106,16 +94,13 @@ func (db *database) createWorkspaceTx(tx *sql.Tx, workspace *models.Workspace) e
|
||||
}
|
||||
workspace.ID = int(id)
|
||||
|
||||
log.Debug("workspace created successfully",
|
||||
log.Debug("created user workspace",
|
||||
"workspace_id", workspace.ID,
|
||||
"user_id", workspace.UserID)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db *database) GetUserByID(id int) (*models.User, error) {
|
||||
log := getLogger().WithGroup("users")
|
||||
log.Debug("fetching user by ID", "user_id", id)
|
||||
|
||||
user := &models.User{}
|
||||
err := db.QueryRow(`
|
||||
SELECT
|
||||
@@ -127,21 +112,15 @@ func (db *database) GetUserByID(id int) (*models.User, error) {
|
||||
&user.Role, &user.CreatedAt, &user.LastWorkspaceID)
|
||||
|
||||
if err == sql.ErrNoRows {
|
||||
log.Debug("user not found", "user_id", id)
|
||||
return nil, fmt.Errorf("user not found")
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to fetch user: %w", err)
|
||||
}
|
||||
|
||||
log.Debug("user retrieved successfully", "user_id", id)
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (db *database) GetUserByEmail(email string) (*models.User, error) {
|
||||
log := getLogger().WithGroup("users")
|
||||
log.Debug("fetching user by email", "email", email)
|
||||
|
||||
user := &models.User{}
|
||||
err := db.QueryRow(`
|
||||
SELECT
|
||||
@@ -153,19 +132,16 @@ func (db *database) GetUserByEmail(email string) (*models.User, error) {
|
||||
&user.Role, &user.CreatedAt, &user.LastWorkspaceID)
|
||||
|
||||
if err == sql.ErrNoRows {
|
||||
log.Debug("user not found", "email", email)
|
||||
return nil, fmt.Errorf("user not found")
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to fetch user: %w", err)
|
||||
}
|
||||
|
||||
log.Debug("user retrieved successfully", "user_id", user.ID)
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (db *database) UpdateUser(user *models.User) error {
|
||||
log := getLogger().WithGroup("users")
|
||||
result, err := db.Exec(`
|
||||
UPDATE users
|
||||
SET email = ?, display_name = ?, password_hash = ?, role = ?, last_workspace_id = ?
|
||||
@@ -183,18 +159,13 @@ func (db *database) UpdateUser(user *models.User) error {
|
||||
}
|
||||
|
||||
if rowsAffected == 0 {
|
||||
log.Warn("no user found to update", "user_id", user.ID)
|
||||
return fmt.Errorf("user not found")
|
||||
}
|
||||
|
||||
log.Info("user updated", "user_id", user.ID)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db *database) GetAllUsers() ([]*models.User, error) {
|
||||
log := getLogger().WithGroup("users")
|
||||
log.Debug("fetching all users")
|
||||
|
||||
rows, err := db.Query(`
|
||||
SELECT
|
||||
id, email, display_name, role, created_at,
|
||||
@@ -219,16 +190,10 @@ func (db *database) GetAllUsers() ([]*models.User, error) {
|
||||
users = append(users, user)
|
||||
}
|
||||
|
||||
log.Debug("users retrieved successfully", "count", len(users))
|
||||
return users, nil
|
||||
}
|
||||
|
||||
func (db *database) UpdateLastWorkspace(userID int, workspaceName string) error {
|
||||
log := getLogger().WithGroup("users")
|
||||
log.Debug("updating last workspace",
|
||||
"user_id", userID,
|
||||
"workspace_name", workspaceName)
|
||||
|
||||
tx, err := db.Begin()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to begin transaction: %w", err)
|
||||
@@ -253,9 +218,6 @@ func (db *database) UpdateLastWorkspace(userID int, workspaceName string) error
|
||||
return fmt.Errorf("failed to commit transaction: %w", err)
|
||||
}
|
||||
|
||||
log.Info("last workspace updated",
|
||||
"user_id", userID,
|
||||
"workspace_id", workspaceID)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -271,48 +233,27 @@ func (db *database) DeleteUser(id int) error {
|
||||
|
||||
// Delete all user's workspaces first
|
||||
log.Debug("deleting user workspaces", "user_id", id)
|
||||
result, err := tx.Exec("DELETE FROM workspaces WHERE user_id = ?", id)
|
||||
_, err = tx.Exec("DELETE FROM workspaces WHERE user_id = ?", id)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to delete workspaces: %w", err)
|
||||
}
|
||||
|
||||
workspacesDeleted, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get deleted workspaces count: %w", err)
|
||||
}
|
||||
|
||||
// Delete the user
|
||||
log.Debug("deleting user record", "user_id", id)
|
||||
result, err = tx.Exec("DELETE FROM users WHERE id = ?", id)
|
||||
_, err = tx.Exec("DELETE FROM users WHERE id = ?", id)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to delete user: %w", err)
|
||||
}
|
||||
|
||||
userDeleted, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get deleted user count: %w", err)
|
||||
}
|
||||
|
||||
if userDeleted == 0 {
|
||||
log.Warn("no user found to delete", "user_id", id)
|
||||
return fmt.Errorf("user not found")
|
||||
}
|
||||
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to commit transaction: %w", err)
|
||||
}
|
||||
|
||||
log.Info("user deleted",
|
||||
"user_id", id,
|
||||
"workspaces_deleted", workspacesDeleted)
|
||||
log.Debug("deleted user", "user_id", id)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db *database) GetLastWorkspaceName(userID int) (string, error) {
|
||||
log := getLogger().WithGroup("users")
|
||||
log.Debug("fetching last workspace name", "user_id", userID)
|
||||
|
||||
var workspaceName string
|
||||
err := db.QueryRow(`
|
||||
SELECT
|
||||
@@ -323,30 +264,22 @@ func (db *database) GetLastWorkspaceName(userID int) (string, error) {
|
||||
Scan(&workspaceName)
|
||||
|
||||
if err == sql.ErrNoRows {
|
||||
log.Debug("no last workspace found", "user_id", userID)
|
||||
return "", fmt.Errorf("no last workspace found")
|
||||
}
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to fetch last workspace name: %w", err)
|
||||
}
|
||||
|
||||
log.Debug("last workspace name retrieved",
|
||||
"user_id", userID,
|
||||
"workspace_name", workspaceName)
|
||||
return workspaceName, nil
|
||||
}
|
||||
|
||||
// CountAdminUsers returns the number of admin users in the system
|
||||
func (db *database) CountAdminUsers() (int, error) {
|
||||
log := getLogger().WithGroup("users")
|
||||
log.Debug("counting admin users")
|
||||
|
||||
var count int
|
||||
err := db.QueryRow("SELECT COUNT(*) FROM users WHERE role = 'admin'").Scan(&count)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("failed to count admin users: %w", err)
|
||||
}
|
||||
|
||||
log.Debug("admin users counted successfully", "count", count)
|
||||
return count, nil
|
||||
}
|
||||
|
||||
@@ -16,7 +16,6 @@ func (db *database) CreateWorkspace(workspace *models.Workspace) error {
|
||||
|
||||
// Set default settings if not provided
|
||||
if workspace.Theme == "" {
|
||||
log.Debug("setting default workspace settings")
|
||||
workspace.SetDefaultSettings()
|
||||
}
|
||||
|
||||
@@ -47,17 +46,11 @@ func (db *database) CreateWorkspace(workspace *models.Workspace) error {
|
||||
}
|
||||
workspace.ID = int(id)
|
||||
|
||||
log.Info("workspace created",
|
||||
"workspace_id", workspace.ID,
|
||||
"user_id", workspace.UserID)
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetWorkspaceByID retrieves a workspace by its ID
|
||||
func (db *database) GetWorkspaceByID(id int) (*models.Workspace, error) {
|
||||
log := getLogger().WithGroup("workspaces")
|
||||
log.Debug("fetching workspace by ID", "workspace_id", id)
|
||||
|
||||
workspace := &models.Workspace{}
|
||||
var encryptedToken string
|
||||
|
||||
@@ -80,7 +73,6 @@ func (db *database) GetWorkspaceByID(id int) (*models.Workspace, error) {
|
||||
)
|
||||
|
||||
if err == sql.ErrNoRows {
|
||||
log.Debug("workspace not found", "workspace_id", id)
|
||||
return nil, fmt.Errorf("workspace not found")
|
||||
}
|
||||
if err != nil {
|
||||
@@ -93,19 +85,11 @@ func (db *database) GetWorkspaceByID(id int) (*models.Workspace, error) {
|
||||
return nil, fmt.Errorf("failed to decrypt token: %w", err)
|
||||
}
|
||||
|
||||
log.Debug("workspace retrieved",
|
||||
"workspace_id", id,
|
||||
"user_id", workspace.UserID)
|
||||
return workspace, nil
|
||||
}
|
||||
|
||||
// GetWorkspaceByName retrieves a workspace by its name and user ID
|
||||
func (db *database) GetWorkspaceByName(userID int, workspaceName string) (*models.Workspace, error) {
|
||||
log := getLogger().WithGroup("workspaces")
|
||||
log.Debug("fetching workspace by name",
|
||||
"user_id", userID,
|
||||
"workspace_name", workspaceName)
|
||||
|
||||
workspace := &models.Workspace{}
|
||||
var encryptedToken string
|
||||
|
||||
@@ -128,9 +112,6 @@ func (db *database) GetWorkspaceByName(userID int, workspaceName string) (*model
|
||||
)
|
||||
|
||||
if err == sql.ErrNoRows {
|
||||
log.Debug("workspace not found",
|
||||
"user_id", userID,
|
||||
"workspace_name", workspaceName)
|
||||
return nil, fmt.Errorf("workspace not found")
|
||||
}
|
||||
if err != nil {
|
||||
@@ -143,27 +124,18 @@ func (db *database) GetWorkspaceByName(userID int, workspaceName string) (*model
|
||||
return nil, fmt.Errorf("failed to decrypt token: %w", err)
|
||||
}
|
||||
|
||||
log.Debug("workspace retrieved successfully",
|
||||
"workspace_id", workspace.ID,
|
||||
"user_id", userID)
|
||||
return workspace, nil
|
||||
}
|
||||
|
||||
// UpdateWorkspace updates a workspace record in the database
|
||||
func (db *database) UpdateWorkspace(workspace *models.Workspace) error {
|
||||
log := getLogger().WithGroup("workspaces")
|
||||
log.Debug("updating workspace",
|
||||
"workspace_id", workspace.ID,
|
||||
"user_id", workspace.UserID,
|
||||
"git_enabled", workspace.GitEnabled)
|
||||
|
||||
// Encrypt token before storing
|
||||
encryptedToken, err := db.encryptToken(workspace.GitToken)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to encrypt token: %w", err)
|
||||
}
|
||||
|
||||
result, err := db.Exec(`
|
||||
_, err = db.Exec(`
|
||||
UPDATE workspaces
|
||||
SET
|
||||
name = ?,
|
||||
@@ -198,29 +170,11 @@ func (db *database) UpdateWorkspace(workspace *models.Workspace) error {
|
||||
return fmt.Errorf("failed to update workspace: %w", err)
|
||||
}
|
||||
|
||||
rowsAffected, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get rows affected: %w", err)
|
||||
}
|
||||
|
||||
if rowsAffected == 0 {
|
||||
log.Warn("no workspace found to update",
|
||||
"workspace_id", workspace.ID,
|
||||
"user_id", workspace.UserID)
|
||||
return fmt.Errorf("workspace not found")
|
||||
}
|
||||
|
||||
log.Debug("workspace updated",
|
||||
"workspace_id", workspace.ID,
|
||||
"user_id", workspace.UserID)
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetWorkspacesByUserID retrieves all workspaces for a user
|
||||
func (db *database) GetWorkspacesByUserID(userID int) ([]*models.Workspace, error) {
|
||||
log := getLogger().WithGroup("workspaces")
|
||||
log.Debug("fetching workspaces for user", "user_id", userID)
|
||||
|
||||
rows, err := db.Query(`
|
||||
SELECT
|
||||
id, user_id, name, created_at,
|
||||
@@ -265,20 +219,12 @@ func (db *database) GetWorkspacesByUserID(userID int) ([]*models.Workspace, erro
|
||||
return nil, fmt.Errorf("error iterating workspace rows: %w", err)
|
||||
}
|
||||
|
||||
log.Debug("workspaces retrieved successfully",
|
||||
"user_id", userID,
|
||||
"count", len(workspaces))
|
||||
return workspaces, nil
|
||||
}
|
||||
|
||||
// UpdateWorkspaceSettings updates only the settings portion of a workspace
|
||||
func (db *database) UpdateWorkspaceSettings(workspace *models.Workspace) error {
|
||||
log := getLogger().WithGroup("workspaces")
|
||||
log.Debug("updating workspace settings",
|
||||
"workspace_id", workspace.ID,
|
||||
"git_enabled", workspace.GitEnabled)
|
||||
|
||||
result, err := db.Exec(`
|
||||
_, err := db.Exec(`
|
||||
UPDATE workspaces
|
||||
SET
|
||||
theme = ?,
|
||||
@@ -310,142 +256,74 @@ func (db *database) UpdateWorkspaceSettings(workspace *models.Workspace) error {
|
||||
return fmt.Errorf("failed to update workspace settings: %w", err)
|
||||
}
|
||||
|
||||
rowsAffected, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get rows affected: %w", err)
|
||||
}
|
||||
|
||||
if rowsAffected == 0 {
|
||||
log.Warn("no workspace found to update settings",
|
||||
"workspace_id", workspace.ID)
|
||||
return fmt.Errorf("workspace not found")
|
||||
}
|
||||
|
||||
log.Info("workspace settings updated",
|
||||
"workspace_id", workspace.ID)
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteWorkspace removes a workspace record from the database
|
||||
func (db *database) DeleteWorkspace(id int) error {
|
||||
log := getLogger().WithGroup("workspaces")
|
||||
log.Debug("deleting workspace", "workspace_id", id)
|
||||
|
||||
result, err := db.Exec("DELETE FROM workspaces WHERE id = ?", id)
|
||||
_, err := db.Exec("DELETE FROM workspaces WHERE id = ?", id)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to delete workspace: %w", err)
|
||||
}
|
||||
|
||||
rowsAffected, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get rows affected: %w", err)
|
||||
}
|
||||
|
||||
if rowsAffected == 0 {
|
||||
log.Warn("no workspace found to delete", "workspace_id", id)
|
||||
return fmt.Errorf("workspace not found")
|
||||
}
|
||||
|
||||
log.Info("workspace deleted", "workspace_id", id)
|
||||
log.Debug("workspace deleted", "workspace_id", id)
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteWorkspaceTx removes a workspace record from the database within a transaction
|
||||
func (db *database) DeleteWorkspaceTx(tx *sql.Tx, id int) error {
|
||||
log := getLogger().WithGroup("workspaces")
|
||||
log.Debug("deleting workspace in transaction", "workspace_id", id)
|
||||
|
||||
result, err := tx.Exec("DELETE FROM workspaces WHERE id = ?", id)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to delete workspace in transaction: %w", err)
|
||||
}
|
||||
|
||||
rowsAffected, err := result.RowsAffected()
|
||||
_, err = result.RowsAffected()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get rows affected in transaction: %w", err)
|
||||
}
|
||||
|
||||
if rowsAffected == 0 {
|
||||
log.Warn("no workspace found to delete in transaction",
|
||||
"workspace_id", id)
|
||||
return fmt.Errorf("workspace not found")
|
||||
}
|
||||
|
||||
log.Debug("workspace deleted successfully in transaction",
|
||||
log.Debug("workspace deleted",
|
||||
"workspace_id", id)
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateLastWorkspaceTx sets the last workspace for a user in a transaction
|
||||
func (db *database) UpdateLastWorkspaceTx(tx *sql.Tx, userID, workspaceID int) error {
|
||||
log := getLogger().WithGroup("workspaces")
|
||||
log.Debug("updating last workspace in transaction",
|
||||
"user_id", userID,
|
||||
"workspace_id", workspaceID)
|
||||
|
||||
result, err := tx.Exec("UPDATE users SET last_workspace_id = ? WHERE id = ?",
|
||||
workspaceID, userID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to update last workspace in transaction: %w", err)
|
||||
}
|
||||
|
||||
rowsAffected, err := result.RowsAffected()
|
||||
_, err = result.RowsAffected()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get rows affected in transaction: %w", err)
|
||||
}
|
||||
|
||||
if rowsAffected == 0 {
|
||||
log.Warn("no user found to update last workspace",
|
||||
"user_id", userID)
|
||||
return fmt.Errorf("user not found")
|
||||
}
|
||||
|
||||
log.Debug("last workspace updated successfully in transaction",
|
||||
"user_id", userID,
|
||||
"workspace_id", workspaceID)
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateLastOpenedFile updates the last opened file path for a workspace
|
||||
func (db *database) UpdateLastOpenedFile(workspaceID int, filePath string) error {
|
||||
log := getLogger().WithGroup("workspaces")
|
||||
log.Debug("updating last opened file",
|
||||
"workspace_id", workspaceID,
|
||||
"file_path", filePath)
|
||||
|
||||
result, err := db.Exec("UPDATE workspaces SET last_opened_file_path = ? WHERE id = ?",
|
||||
_, err := db.Exec("UPDATE workspaces SET last_opened_file_path = ? WHERE id = ?",
|
||||
filePath, workspaceID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to update last opened file: %w", err)
|
||||
}
|
||||
|
||||
rowsAffected, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get rows affected: %w", err)
|
||||
}
|
||||
|
||||
if rowsAffected == 0 {
|
||||
log.Warn("no workspace found to update last opened file",
|
||||
"workspace_id", workspaceID)
|
||||
return fmt.Errorf("workspace not found")
|
||||
}
|
||||
|
||||
log.Debug("last opened file updated successfully",
|
||||
"workspace_id", workspaceID)
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetLastOpenedFile retrieves the last opened file path for a workspace
|
||||
func (db *database) GetLastOpenedFile(workspaceID int) (string, error) {
|
||||
log := getLogger().WithGroup("workspaces")
|
||||
log.Debug("fetching last opened file", "workspace_id", workspaceID)
|
||||
|
||||
var filePath sql.NullString
|
||||
err := db.QueryRow("SELECT last_opened_file_path FROM workspaces WHERE id = ?",
|
||||
workspaceID).Scan(&filePath)
|
||||
|
||||
if err == sql.ErrNoRows {
|
||||
log.Debug("workspace not found", "workspace_id", workspaceID)
|
||||
return "", fmt.Errorf("workspace not found")
|
||||
}
|
||||
if err != nil {
|
||||
@@ -453,21 +331,14 @@ func (db *database) GetLastOpenedFile(workspaceID int) (string, error) {
|
||||
}
|
||||
|
||||
if !filePath.Valid {
|
||||
log.Debug("no last opened file found", "workspace_id", workspaceID)
|
||||
return "", nil
|
||||
}
|
||||
|
||||
log.Debug("last opened file retrieved successfully",
|
||||
"workspace_id", workspaceID,
|
||||
"file_path", filePath.String)
|
||||
return filePath.String, nil
|
||||
}
|
||||
|
||||
// GetAllWorkspaces retrieves all workspaces in the database
|
||||
func (db *database) GetAllWorkspaces() ([]*models.Workspace, error) {
|
||||
log := getLogger().WithGroup("workspaces")
|
||||
log.Debug("fetching all workspaces")
|
||||
|
||||
rows, err := db.Query(`
|
||||
SELECT
|
||||
id, user_id, name, created_at,
|
||||
@@ -510,6 +381,5 @@ func (db *database) GetAllWorkspaces() ([]*models.Workspace, error) {
|
||||
return nil, fmt.Errorf("error iterating workspace rows: %w", err)
|
||||
}
|
||||
|
||||
log.Debug("all workspaces retrieved successfully", "count", len(workspaces))
|
||||
return workspaces, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user