mirror of
https://github.com/lordmathis/lemma.git
synced 2025-11-05 15:44:21 +00:00
Remove system settings functionality and related database migrations
This commit is contained in:
@@ -39,7 +39,7 @@ Lemma can be configured using environment variables. Here are the available conf
|
|||||||
- `LEMMA_PORT`: Port to run the server on (default: "8080")
|
- `LEMMA_PORT`: Port to run the server on (default: "8080")
|
||||||
- `LEMMA_DOMAIN`: Domain name where the application is hosted for cookie authentication
|
- `LEMMA_DOMAIN`: Domain name where the application is hosted for cookie authentication
|
||||||
- `LEMMA_CORS_ORIGINS`: Comma-separated list of allowed CORS origins
|
- `LEMMA_CORS_ORIGINS`: Comma-separated list of allowed CORS origins
|
||||||
- `LEMMA_JWT_SIGNING_KEY`: Key used for signing JWT tokens
|
- `LEMMA_JWT_SIGNING_KEY`: Key used for signing JWT tokens. If not provided, a key will be automatically generated and stored in `{LEMMA_WORKDIR}/secrets/jwt_signing_key`
|
||||||
- `LEMMA_LOG_LEVEL`: Logging level (defaults to DEBUG in development mode, INFO in production)
|
- `LEMMA_LOG_LEVEL`: Logging level (defaults to DEBUG in development mode, INFO in production)
|
||||||
- `LEMMA_RATE_LIMIT_REQUESTS`: Number of allowed requests per window (default: 100)
|
- `LEMMA_RATE_LIMIT_REQUESTS`: Number of allowed requests per window (default: 100)
|
||||||
- `LEMMA_RATE_LIMIT_WINDOW`: Duration of the rate limit window (default: 15m)
|
- `LEMMA_RATE_LIMIT_WINDOW`: Duration of the rate limit window (default: 15m)
|
||||||
|
|||||||
@@ -68,11 +68,9 @@ type SessionStore interface {
|
|||||||
CleanExpiredSessions() error
|
CleanExpiredSessions() error
|
||||||
}
|
}
|
||||||
|
|
||||||
// SystemStore defines the methods for interacting with system settings and stats in the database
|
// SystemStore defines the methods for interacting with system stats in the database
|
||||||
type SystemStore interface {
|
type SystemStore interface {
|
||||||
GetSystemStats() (*UserStats, error)
|
GetSystemStats() (*UserStats, error)
|
||||||
GetSystemSetting(key string) (string, error)
|
|
||||||
SetSystemSetting(key, value string) error
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type StructScanner interface {
|
type StructScanner interface {
|
||||||
|
|||||||
@@ -5,5 +5,4 @@ DROP INDEX IF EXISTS idx_sessions_user_id;
|
|||||||
DROP INDEX IF EXISTS idx_workspaces_user_id;
|
DROP INDEX IF EXISTS idx_workspaces_user_id;
|
||||||
DROP TABLE IF EXISTS sessions;
|
DROP TABLE IF EXISTS sessions;
|
||||||
DROP TABLE IF EXISTS workspaces;
|
DROP TABLE IF EXISTS workspaces;
|
||||||
DROP TABLE IF EXISTS system_settings;
|
|
||||||
DROP TABLE IF EXISTS users;
|
DROP TABLE IF EXISTS users;
|
||||||
@@ -46,14 +46,6 @@ CREATE TABLE IF NOT EXISTS sessions (
|
|||||||
FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE
|
FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE
|
||||||
);
|
);
|
||||||
|
|
||||||
-- Create system_settings table for application settings
|
|
||||||
CREATE TABLE IF NOT EXISTS system_settings (
|
|
||||||
key TEXT PRIMARY KEY,
|
|
||||||
value TEXT NOT NULL,
|
|
||||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
||||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
||||||
);
|
|
||||||
|
|
||||||
-- Create indexes for performance
|
-- Create indexes for performance
|
||||||
CREATE INDEX IF NOT EXISTS idx_sessions_user_id ON sessions(user_id);
|
CREATE INDEX IF NOT EXISTS idx_sessions_user_id ON sessions(user_id);
|
||||||
CREATE INDEX IF NOT EXISTS idx_sessions_expires_at ON sessions(expires_at);
|
CREATE INDEX IF NOT EXISTS idx_sessions_expires_at ON sessions(expires_at);
|
||||||
|
|||||||
@@ -5,5 +5,4 @@ DROP INDEX IF EXISTS idx_sessions_user_id;
|
|||||||
DROP INDEX IF EXISTS idx_workspaces_user_id;
|
DROP INDEX IF EXISTS idx_workspaces_user_id;
|
||||||
DROP TABLE IF EXISTS sessions;
|
DROP TABLE IF EXISTS sessions;
|
||||||
DROP TABLE IF EXISTS workspaces;
|
DROP TABLE IF EXISTS workspaces;
|
||||||
DROP TABLE IF EXISTS system_settings;
|
|
||||||
DROP TABLE IF EXISTS users;
|
DROP TABLE IF EXISTS users;
|
||||||
@@ -45,14 +45,6 @@ CREATE TABLE IF NOT EXISTS sessions (
|
|||||||
FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE
|
FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE
|
||||||
);
|
);
|
||||||
|
|
||||||
-- Create system_settings table for application settings
|
|
||||||
CREATE TABLE IF NOT EXISTS system_settings (
|
|
||||||
key TEXT PRIMARY KEY,
|
|
||||||
value TEXT NOT NULL,
|
|
||||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
||||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
||||||
);
|
|
||||||
|
|
||||||
-- Create indexes for performance
|
-- Create indexes for performance
|
||||||
CREATE INDEX IF NOT EXISTS idx_sessions_user_id ON sessions(user_id);
|
CREATE INDEX IF NOT EXISTS idx_sessions_user_id ON sessions(user_id);
|
||||||
CREATE INDEX IF NOT EXISTS idx_sessions_expires_at ON sessions(expires_at);
|
CREATE INDEX IF NOT EXISTS idx_sessions_expires_at ON sessions(expires_at);
|
||||||
|
|||||||
@@ -26,7 +26,6 @@ func TestMigrate(t *testing.T) {
|
|||||||
"users",
|
"users",
|
||||||
"workspaces",
|
"workspaces",
|
||||||
"sessions",
|
"sessions",
|
||||||
"system_settings",
|
|
||||||
"schema_migrations",
|
"schema_migrations",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,6 @@
|
|||||||
package db
|
package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/rand"
|
|
||||||
"encoding/base64"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -13,55 +11,6 @@ type UserStats struct {
|
|||||||
ActiveUsers int `json:"activeUsers"` // Users with activity in last 30 days
|
ActiveUsers int `json:"activeUsers"` // Users with activity in last 30 days
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetSystemSetting retrieves a system setting by key
|
|
||||||
func (db *database) GetSystemSetting(key string) (string, error) {
|
|
||||||
var value string
|
|
||||||
query := db.NewQuery().
|
|
||||||
Select("value").
|
|
||||||
From("system_settings").
|
|
||||||
Where("key = ").
|
|
||||||
Placeholder(key)
|
|
||||||
err := db.QueryRow(query.String(), query.args...).Scan(&value)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
return value, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetSystemSetting stores or updates a system setting
|
|
||||||
func (db *database) SetSystemSetting(key, value string) error {
|
|
||||||
query := db.NewQuery().
|
|
||||||
Insert("system_settings", "key", "value").
|
|
||||||
Values(2).
|
|
||||||
AddArgs(key, value).
|
|
||||||
Write("ON CONFLICT(key) DO UPDATE SET value = ").
|
|
||||||
Placeholder(value)
|
|
||||||
|
|
||||||
_, err := db.Exec(query.String(), query.args...)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to store system setting: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// generateRandomSecret generates a cryptographically secure random string
|
|
||||||
func generateRandomSecret(bytes int) (string, error) {
|
|
||||||
log := getLogger().WithGroup("system")
|
|
||||||
log.Debug("generating random secret", "bytes", bytes)
|
|
||||||
|
|
||||||
b := make([]byte, bytes)
|
|
||||||
_, err := rand.Read(b)
|
|
||||||
if err != nil {
|
|
||||||
return "", fmt.Errorf("failed to generate random bytes: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
secret := base64.StdEncoding.EncodeToString(b)
|
|
||||||
return secret, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetSystemStats returns system-wide statistics
|
// GetSystemStats returns system-wide statistics
|
||||||
func (db *database) GetSystemStats() (*UserStats, error) {
|
func (db *database) GetSystemStats() (*UserStats, error) {
|
||||||
stats := &UserStats{}
|
stats := &UserStats{}
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package db_test
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -24,99 +23,6 @@ func TestSystemOperations(t *testing.T) {
|
|||||||
t.Fatalf("failed to run migrations: %v", err)
|
t.Fatalf("failed to run migrations: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Run("GetSystemSettings", func(t *testing.T) {
|
|
||||||
t.Run("non-existent setting", func(t *testing.T) {
|
|
||||||
_, err := database.GetSystemSetting("nonexistent-key")
|
|
||||||
if err == nil {
|
|
||||||
t.Error("expected error for non-existent key, got nil")
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("existing setting", func(t *testing.T) {
|
|
||||||
// First set a value
|
|
||||||
err := database.SetSystemSetting("test-key", "test-value")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("failed to set system setting: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Then get it back
|
|
||||||
value, err := database.GetSystemSetting("test-key")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("failed to get system setting: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if value != "test-value" {
|
|
||||||
t.Errorf("got value %q, want %q", value, "test-value")
|
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("SetSystemSettings", func(t *testing.T) {
|
|
||||||
testCases := []struct {
|
|
||||||
name string
|
|
||||||
key string
|
|
||||||
value string
|
|
||||||
wantErr bool
|
|
||||||
errContains string
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "new setting",
|
|
||||||
key: "new-key",
|
|
||||||
value: "new-value",
|
|
||||||
wantErr: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "update existing setting",
|
|
||||||
key: "update-key",
|
|
||||||
value: "original-value",
|
|
||||||
wantErr: false,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, tc := range testCases {
|
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
|
||||||
err := database.SetSystemSetting(tc.key, tc.value)
|
|
||||||
if tc.wantErr {
|
|
||||||
if err == nil {
|
|
||||||
t.Error("expected error, got nil")
|
|
||||||
} else if !strings.Contains(err.Error(), tc.errContains) {
|
|
||||||
t.Errorf("error = %v, want error containing %v", err, tc.errContains)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("unexpected error: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Verify the setting was stored
|
|
||||||
stored, err := database.GetSystemSetting(tc.key)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("failed to retrieve stored setting: %v", err)
|
|
||||||
}
|
|
||||||
if stored != tc.value {
|
|
||||||
t.Errorf("got value %q, want %q", stored, tc.value)
|
|
||||||
}
|
|
||||||
|
|
||||||
// For the update case, test updating the value
|
|
||||||
if tc.name == "update existing setting" {
|
|
||||||
newValue := "updated-value"
|
|
||||||
err := database.SetSystemSetting(tc.key, newValue)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("failed to update setting: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
stored, err := database.GetSystemSetting(tc.key)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("failed to retrieve updated setting: %v", err)
|
|
||||||
}
|
|
||||||
if stored != newValue {
|
|
||||||
t.Errorf("got updated value %q, want %q", stored, newValue)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("GetSystemStats", func(t *testing.T) {
|
t.Run("GetSystemStats", func(t *testing.T) {
|
||||||
// Create some test users and sessions
|
// Create some test users and sessions
|
||||||
users := []*models.User{
|
users := []*models.User{
|
||||||
|
|||||||
Reference in New Issue
Block a user