Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

feat: Add migration scripts for users and auth_tokens tables, including up and down operations

begoniezhaoa8143dc85a87
diff --git a/internal/container/container.go b/internal/container/container.go
index eacce3269..6b0846a92 100644
--- a/internal/container/container.go
+++ b/internal/container/container.go
@@ -275,16 +275,6 @@ func initDatabase(cfg *config.Config) (*gorm.DB, error) {
return nil, err
}
- // Auto-migrate database tables
- err = db.AutoMigrate(
- &types.User{},
- &types.AuthToken{},
- &types.KnowledgeBase{},
- )
- if err != nil {
- return nil, fmt.Errorf("failed to auto-migrate database tables: %v", err)
- }
-
// Run database migrations automatically (optional, can be disabled via env var)
// To disable auto-migration, set AUTO_MIGRATE=false
if os.Getenv("AUTO_MIGRATE") != "false" {
diff --git a/migrations/versioned/000001_create_users_and_auth_tokens.down.sql b/migrations/versioned/000001_create_users_and_auth_tokens.down.sql
new file mode 100644
index 000000000..d6cf7875d
--- /dev/null
+++ b/migrations/versioned/000001_create_users_and_auth_tokens.down.sql
@@ -0,0 +1,14 @@
+-- 000001_create_users_and_auth_tokens.down.sql
+-- Drop users and auth_tokens tables
+
+BEGIN;
+
+-- Drop foreign key constraints first
+ALTER TABLE auth_tokens DROP CONSTRAINT IF EXISTS fk_auth_tokens_user;
+ALTER TABLE users DROP CONSTRAINT IF EXISTS fk_users_tenant;
+
+-- Drop tables
+DROP TABLE IF EXISTS auth_tokens;
+DROP TABLE IF EXISTS users;
+
+COMMIT;
diff --git a/migrations/versioned/000001_create_users_and_auth_tokens.up.sql b/migrations/versioned/000001_create_users_and_auth_tokens.up.sql
new file mode 100644
index 000000000..62ed2629c
--- /dev/null
+++ b/migrations/versioned/000001_create_users_and_auth_tokens.up.sql
@@ -0,0 +1,71 @@
+-- 000001_create_users_and_auth_tokens.up.sql
+-- Create users and auth_tokens tables
+
+BEGIN;
+
+-- Create users table
+CREATE TABLE IF NOT EXISTS users (
+ id VARCHAR(36) PRIMARY KEY DEFAULT uuid_generate_v4(),
+ username VARCHAR(100) NOT NULL UNIQUE,
+ email VARCHAR(255) NOT NULL UNIQUE,
+ password_hash VARCHAR(255) NOT NULL,
+ avatar VARCHAR(500),
+ tenant_id INTEGER,
+ is_active BOOLEAN NOT NULL DEFAULT true,
+ created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
+ updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
+ deleted_at TIMESTAMP WITH TIME ZONE
+);
+
+COMMENT ON TABLE users IS 'User accounts in the system';
+COMMENT ON COLUMN users.id IS 'Unique identifier of the user';
+COMMENT ON COLUMN users.username IS 'Username of the user';
+COMMENT ON COLUMN users.email IS 'Email address of the user';
+COMMENT ON COLUMN users.password_hash IS 'Hashed password of the user';
+COMMENT ON COLUMN users.avatar IS 'Avatar URL of the user';
+COMMENT ON COLUMN users.tenant_id IS 'Tenant ID that the user belongs to';
+COMMENT ON COLUMN users.is_active IS 'Whether the user is active';
+
+-- Add indexes for users
+CREATE INDEX IF NOT EXISTS idx_users_username ON users(username);
+CREATE INDEX IF NOT EXISTS idx_users_email ON users(email);
+CREATE INDEX IF NOT EXISTS idx_users_tenant_id ON users(tenant_id);
+CREATE INDEX IF NOT EXISTS idx_users_deleted_at ON users(deleted_at);
+
+-- Add foreign key constraint for tenant_id
+ALTER TABLE users
+ ADD CONSTRAINT fk_users_tenant
+ FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE SET NULL;
+
+-- Create auth_tokens table
+CREATE TABLE IF NOT EXISTS auth_tokens (
+ id VARCHAR(36) PRIMARY KEY DEFAULT uuid_generate_v4(),
+ user_id VARCHAR(36) NOT NULL,
+ token TEXT NOT NULL,
+ token_type VARCHAR(50) NOT NULL,
+ expires_at TIMESTAMP WITH TIME ZONE NOT NULL,
+ is_revoked BOOLEAN NOT NULL DEFAULT false,
+ created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
+ updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
+);
+
+COMMENT ON TABLE auth_tokens IS 'Authentication tokens for users';
+COMMENT ON COLUMN auth_tokens.id IS 'Unique identifier of the token';
+COMMENT ON COLUMN auth_tokens.user_id IS 'User ID that owns this token';
+COMMENT ON COLUMN auth_tokens.token IS 'Token value (JWT or other format)';
+COMMENT ON COLUMN auth_tokens.token_type IS 'Token type (access_token, refresh_token)';
+COMMENT ON COLUMN auth_tokens.expires_at IS 'Token expiration time';
+COMMENT ON COLUMN auth_tokens.is_revoked IS 'Whether the token is revoked';
+
+-- Add indexes for auth_tokens
+CREATE INDEX IF NOT EXISTS idx_auth_tokens_user_id ON auth_tokens(user_id);
+CREATE INDEX IF NOT EXISTS idx_auth_tokens_token ON auth_tokens(token);
+CREATE INDEX IF NOT EXISTS idx_auth_tokens_token_type ON auth_tokens(token_type);
+CREATE INDEX IF NOT EXISTS idx_auth_tokens_expires_at ON auth_tokens(expires_at);
+
+-- Add foreign key constraint
+ALTER TABLE auth_tokens
+ ADD CONSTRAINT fk_auth_tokens_user
+ FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE;
+
+COMMIT;
diff --git a/migrations/versioned/000019_add_is_temporary_to_knowledge_bases.down.sql b/migrations/versioned/000019_add_is_temporary_to_knowledge_bases.down.sql
new file mode 100644
index 000000000..a0c7756f2
--- /dev/null
+++ b/migrations/versioned/000019_add_is_temporary_to_knowledge_bases.down.sql
@@ -0,0 +1,10 @@
+-- 000019_add_is_temporary_to_knowledge_bases.down.sql
+-- Remove is_temporary column from knowledge_bases table
+
+BEGIN;
+
+-- Drop is_temporary column
+ALTER TABLE knowledge_bases
+ DROP COLUMN IF EXISTS is_temporary;
+
+COMMIT;
diff --git a/migrations/versioned/000019_add_is_temporary_to_knowledge_bases.up.sql b/migrations/versioned/000019_add_is_temporary_to_knowledge_bases.up.sql
new file mode 100644
index 000000000..8805e32c2
--- /dev/null
+++ b/migrations/versioned/000019_add_is_temporary_to_knowledge_bases.up.sql
@@ -0,0 +1,12 @@
+-- 000019_add_is_temporary_to_knowledge_bases.up.sql
+-- Add is_temporary column to knowledge_bases table
+
+BEGIN;
+
+-- Add is_temporary column
+ALTER TABLE knowledge_bases
+ ADD COLUMN IF NOT EXISTS is_temporary BOOLEAN NOT NULL DEFAULT false;
+
+COMMENT ON COLUMN knowledge_bases.is_temporary IS 'Whether this knowledge base is temporary (ephemeral) and should be hidden from UI';
+
+COMMIT;