class CreateApiTokens < ActiveRecord::Migration[8.1] def change create_table :api_tokens do |t| t.references :user, null: false, foreign_key: true, index: true t.string :token, null: false t.string :name t.datetime :last_used_at t.datetime :expires_at t.timestamps end add_index :api_tokens, :token, unique: true # Enable Row Level Security execute <<-SQL ALTER TABLE api_tokens ENABLE ROW LEVEL SECURITY; CREATE POLICY api_tokens_isolation_policy ON api_tokens USING (user_id = current_setting('app.current_user_id', true)::bigint); SQL end end