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

This commit is contained in:
begoniezhao
2025-12-04 20:50:50 +08:00
parent 1aa66bfa10
commit a8143dc85a
5 changed files with 107 additions and 10 deletions
-10
View File
@@ -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" {
@@ -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;
@@ -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;
@@ -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;
@@ -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;