Skip to content
Development
Command

/db-migrate

Создать database migration с правильным процессом

From plugin
claude-code-starter
19020 skills5 agents20 commands
Install
$ npx -y skills add alexeykrol/claude-code-starter --agent claude-code

How it fires

How this command gets triggered: by you, by Claude, or both.

  • Fires itselfClaude auto-loads it when your prompt matches the work.
  • You can call itInvoke it directly when you want it.
  • Slash command/db-migrate

Context preview

What this command does when you run it.

Создать database migration с правильным процессом

Command definition

db-migrate.md
description: Создать database migration с правильным процессом

Создай database migration следуя лучшим практикам.

**ВАЖНО: Миграции - критическая часть. Тестируй все тщательно!**

Процесс:

1. Анализ текущей схемы БД

Прочитай и проанализируй:

# Найди файлы схемы БД
find . -name "schema.*" -o -name "*.prisma" -o -name "*migration*"

# Посмотри последние миграции
ls -la supabase/migrations/ || ls -la prisma/migrations/ || ls -la migrations/

Прочитай:

  • Текущую схему БД
  • Последние миграции
  • Database documentation (если есть в ARCHITECTURE.md)

2. Пойми требования

Спроси себя:

  • Какие изменения в схеме нужны?
  • Есть ли существующие данные, которые нужно сохранить?
  • Нужна ли обратная совместимость?
  • Есть ли зависимости от других таблиц?

3. Спланируй миграцию

**Типы изменений:**

**Безопасные (можно делать на проде):**

  • ✅ ADD column (с DEFAULT или NULL)
  • ✅ ADD index (concurrent)
  • ✅ ADD new table
  • ✅ ADD constraint (NOT VALID, потом VALIDATE)

**Опасные (требуют осторожности):**

  • ⚠️ DROP column (может сломать приложение)
  • ⚠️ RENAME column (нужна двухфазная миграция)
  • ⚠️ CHANGE column type (может потерять данные)
  • ⚠️ ADD NOT NULL (сначала заполни данные)

**Очень опасные (только с downtime):**

  • 🔴 DROP table
  • 🔴 CHANGE primary key
  • 🔴 Большая структурная переделка

4. Создай migration файл

**Naming convention:**

YYYYMMDDHHMMSS_descriptive_name.sql

Пример: `20250110120000_add_user_preferences_table.sql`

**Структура миграции:**

-- Migration: Add user preferences table
-- Created: 2025-01-10
-- Author: Claude Code
-- Description: Add table to store user preferences with foreign key to users

-- ============================================
-- Up Migration
-- ============================================

BEGIN;

-- Create table
CREATE TABLE IF NOT EXISTS user_preferences (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
  theme VARCHAR(20) DEFAULT 'light' CHECK (theme IN ('light', 'dark', 'auto')),
  language VARCHAR(10) DEFAULT 'en',
  notifications_enabled BOOLEAN DEFAULT true,
  created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
  updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),

  -- Constraints
  CONSTRAINT unique_user_preferences UNIQUE(user_id)
);

-- Create indexes
CREATE INDEX idx_user_preferences_user_id ON user_preferences(user_id);

-- Add comments
COMMENT ON TABLE user_preferences IS 'Stores user-specific preferences';
COMMENT ON COLUMN user_preferences.theme IS 'UI theme preference';

-- Enable Row Level Security
ALTER TABLE user_preferences ENABLE ROW LEVEL SECURITY;

-- Create RLS policies
CREATE POLICY "Users can view own preferences"
  ON user_preferences
  FOR SELECT
  USING (auth.uid() = user_id);

CREATE POLICY "Users can update own preferences"
  ON user_preferences
  FOR UPDATE
  USING (auth.uid() = user_id);

CREATE POLICY "Users can insert own preferences"
  ON user_preferences
  FOR INSERT
  WITH CHECK (auth.uid() = user_id);

COMMIT;

-- ============================================
-- Down Migration (Rollback)
-- ============================================

-- Uncomment to enable rollback:
-- BEGIN;
-- DROP TABLE IF EXISTS user_preferences CASCADE;
-- COMMIT;

5. Создай TypeScript types (если используется TypeScript)

**Файл: `src/types/database.ts` или обнови существующий:**

// Database Types
export interface UserPreferences {
  id: string;
  user_id: string;
  theme: 'light' | 'dark' | 'auto';
  language: string;
  notifications_enabled: boolean;
  created_at: string;
  updated_at: string;
}

// Database Tables
export interface Database {
  public: {
    Tables: {
      user_preferences: {
        Row: UserPreferences;
        Insert: Omit<UserPreferences, 'id' | 'created_at' | 'updated_at'>;
        Update: Partial<Omit<UserPreferences, 'id' | 'created_at'>>;
      };
      // ... other tables
    };
  };
}

6. Тестирование миграции

**В Development:**

# Применить миграцию
make db-migrate
# или
npm run db:migrate
# или
supabase db push

# Проверить что таблица создана
# (команда зависит от вашей БД)

# Тестировать операции
# - INSERT тестовые данные
# - SELECT проверить чтение
# - UPDATE проверить обновление
# - DELETE проверить удаление
# - Проверить RLS policies

**Rollback тест:**

# Откатить миграцию
make db-rollback
# или
npm run db:rollback

# Проверить что откат работает
# Применить снова для продолжения работы
make db-migrate

7. Обнови документацию

**Обнови ARCHITECTURE.md:**

### Database Schema

#### user_preferences
Stores user-specific UI and notification preferences.

**Columns:**
- `id` (UUID, PK) - Unique identifier
- `user_id` (UUID, FK → users.id) - Reference to user
- `theme` (VARCHAR) - UI theme: 'light', 'dark', 'auto'
- `language` (VARCHAR) - Preferred language code
- `notifications_enabled` (BOOLEAN) - Email notifications toggle
- `created_at` (TIMESTAMP) - Record creation time
- `updated_at` (TIMESTAMP) - Last update time

**Constraints:**
- One preference record per user (unique user_id)
- Cascading delete when user is deleted

**Security:**
- RLS enabled
- Users can only view/edit their own preferences

8. Обнови связанный код

**Создай/обнови API endpoints:**

// Example: API route for preferences
import { Database } from '@/types/database';

export async function GET(req: Request) {
  const supabase = createClient<Database>();

  const { data, error } = await supabase
    .from('user_preferences')
    .select('*')
    .single();

  if (error) {
    return Response.json({ error: error.message }, { status: 400 });
  }

  return Response.json(data);
}

9. Создай коммит

Используй `/commit` команду со следующими изменениями:

  • Migration SQL файл
  • TypeScript types
  • Обновленная документация
  • Новый/обновленный код использующий новую схему

10. Security Checklist для миграций

  • [ ] RLS (Row Level Secur
Read more
Ships withclaude-code-starter

Claude Code Starter — это готовая управляющая среда для проектов, в которых основной рабочий агент — Claude Code.

Get the whole plugin