Add complete database schema and ORM models
Schema (002_tables.sql): - users: accounts with nick, password hash, timestamps - auth_tokens: per-device tokens with expiry, linked to users - channels: chat rooms with topic and mode flags - channel_members: membership with per-user modes (+o, +v) - messages: channel/DM history with structured JSON meta - message_queue: per-user pending delivery queue - sessions: server-held session state with idle timeout - server_links: federation peer configuration Models (internal/models/): - All models embed Base for database access - Relation methods on models: User.Channels(), User.QueuedMessages(), Channel.Members(), Channel.RecentMessages(), ChannelMember.User(), ChannelMember.Channel(), AuthToken.User(), Session.User() - IDs are UUIDs (TEXT), not auto-increment integers - JSON tags use camelCase per lint rules All tables verified: migrations apply cleanly, 0 lint issues.
This commit is contained in:
@@ -11,7 +11,6 @@ import (
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.eeqj.de/sneak/chat/internal/config"
|
||||
"git.eeqj.de/sneak/chat/internal/logger"
|
||||
@@ -51,19 +50,9 @@ func (s *Database) GetDB() *sql.DB {
|
||||
return s.db
|
||||
}
|
||||
|
||||
// NewChannel creates a Channel model instance with the db reference injected.
|
||||
func (s *Database) NewChannel(id int64, name, topic, modes string, createdAt, updatedAt time.Time) *models.Channel {
|
||||
c := &models.Channel{
|
||||
ID: id,
|
||||
Name: name,
|
||||
Topic: topic,
|
||||
Modes: modes,
|
||||
CreatedAt: createdAt,
|
||||
UpdatedAt: updatedAt,
|
||||
}
|
||||
c.SetDB(s)
|
||||
|
||||
return c
|
||||
// Hydrate injects the database reference into any model that embeds Base.
|
||||
func (s *Database) Hydrate(m interface{ SetDB(d models.DB) }) {
|
||||
m.SetDB(s)
|
||||
}
|
||||
|
||||
// New creates a new Database instance and registers lifecycle hooks.
|
||||
|
||||
@@ -1,8 +1,85 @@
|
||||
CREATE TABLE IF NOT EXISTS channels (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT NOT NULL UNIQUE,
|
||||
topic TEXT NOT NULL DEFAULT '',
|
||||
modes TEXT NOT NULL DEFAULT '',
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
-- Users: accounts and authentication
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id TEXT PRIMARY KEY, -- UUID
|
||||
nick TEXT NOT NULL UNIQUE,
|
||||
password_hash TEXT NOT NULL,
|
||||
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
last_seen_at DATETIME
|
||||
);
|
||||
|
||||
-- Auth tokens: one user can have multiple active tokens (multiple devices)
|
||||
CREATE TABLE IF NOT EXISTS auth_tokens (
|
||||
token TEXT PRIMARY KEY, -- random token string
|
||||
user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
expires_at DATETIME, -- NULL = no expiry
|
||||
last_used_at DATETIME
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_auth_tokens_user_id ON auth_tokens(user_id);
|
||||
|
||||
-- Channels: chat rooms
|
||||
CREATE TABLE IF NOT EXISTS channels (
|
||||
id TEXT PRIMARY KEY, -- UUID
|
||||
name TEXT NOT NULL UNIQUE, -- #general, etc.
|
||||
topic TEXT NOT NULL DEFAULT '',
|
||||
modes TEXT NOT NULL DEFAULT '', -- +i, +m, +s, +t, +n
|
||||
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
-- Channel members: who is in which channel, with per-user modes
|
||||
CREATE TABLE IF NOT EXISTS channel_members (
|
||||
channel_id TEXT NOT NULL REFERENCES channels(id) ON DELETE CASCADE,
|
||||
user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
modes TEXT NOT NULL DEFAULT '', -- +o (operator), +v (voice)
|
||||
joined_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (channel_id, user_id)
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_channel_members_user_id ON channel_members(user_id);
|
||||
|
||||
-- Messages: channel and DM history (rotated per MAX_HISTORY)
|
||||
CREATE TABLE IF NOT EXISTS messages (
|
||||
id TEXT PRIMARY KEY, -- UUID
|
||||
ts DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
from_user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
from_nick TEXT NOT NULL, -- denormalized for history
|
||||
target TEXT NOT NULL, -- #channel name or user UUID for DMs
|
||||
type TEXT NOT NULL DEFAULT 'message', -- message, action, notice, join, part, quit, topic, mode, nick, system
|
||||
body TEXT NOT NULL DEFAULT '',
|
||||
meta TEXT NOT NULL DEFAULT '{}', -- JSON extensible metadata
|
||||
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_messages_target_ts ON messages(target, ts);
|
||||
CREATE INDEX IF NOT EXISTS idx_messages_from_user ON messages(from_user_id);
|
||||
|
||||
-- Message queue: per-user pending delivery (unread messages)
|
||||
CREATE TABLE IF NOT EXISTS message_queue (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
message_id TEXT NOT NULL REFERENCES messages(id) ON DELETE CASCADE,
|
||||
queued_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
UNIQUE(user_id, message_id)
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_message_queue_user_id ON message_queue(user_id, queued_at);
|
||||
|
||||
-- Sessions: server-held session state
|
||||
CREATE TABLE IF NOT EXISTS sessions (
|
||||
id TEXT PRIMARY KEY, -- UUID
|
||||
user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
last_active_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
expires_at DATETIME -- idle timeout
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_sessions_user_id ON sessions(user_id);
|
||||
|
||||
-- Server links: federation peer configuration
|
||||
CREATE TABLE IF NOT EXISTS server_links (
|
||||
id TEXT PRIMARY KEY, -- UUID
|
||||
name TEXT NOT NULL UNIQUE, -- human-readable peer name
|
||||
url TEXT NOT NULL, -- base URL of peer server
|
||||
shared_key_hash TEXT NOT NULL, -- hashed shared secret
|
||||
is_active INTEGER NOT NULL DEFAULT 1,
|
||||
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
last_seen_at DATETIME
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user