Menu

Migrating from Manual to CLI Database Operations

Good to know

CLI-based database operations are supported from Nexty v2.1.0 and nexty-flu-kontext v1.2.0 onwards. If you're using an earlier version, follow this guide to upgrade your database workflow.

Step 1: Add Database Commands to Package.json

Add commands to package.json

{
  // ...
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "next lint",
    "analyze": "ANALYZE=true next build",
    "db:push": "supabase db push", // Copy these db commands to your package.json
    "db:pull": "supabase db pull",
    "db:reset": "supabase db reset",
    "db:new-migration": "supabase migration new",
    "db:gen-types": "supabase gen types typescript --project-id $(cat supabase/.temp/project-ref) --schema public > lib/supabase/types.ts",
    "db:login": "supabase login",
    "db:link": "supabase link",
    "db:update": "npm run db:push && npm run db:gen-types"
  },
  // ...
}

Step 2: Connect to Your Supabase Project

Execute npm run db:login and npm run db:link sequentially to automatically create the supabase folder and link the remote Supabase project.

Step 3: Synchronize Database Schema

Execute npm run db:pull to pull the database schema from Supabase. This step requires Docker to be installed, please install it yourself. The execution may take a while, you need to wait a few minutes.

After completion, a sql file will appear in supabase/migrations. This file syncs all the schema definitions from Supabase, which are all the database definitions you previously executed manually in the SQL Editor. Now your local database is synced with the remote one, and you can use commands for operations without manual execution.

Now you can choose to delete the original data folder and the sql files inside it, or keep them, but don't move them into supabase/migrations, because supabase/migrations is now synced with Supabase, and redundant files will cause conflicts.

Summary

After completing this migration, any new sql files only need to be placed in supabase/migrations, and you can follow the steps in Database Development Examples for operations.