-- 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;