<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: poshiya parth s</title>
    <description>The latest articles on Forem by poshiya parth s (@parth24072001).</description>
    <link>https://forem.com/parth24072001</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1169265%2F784b1a87-34e3-434c-a7ae-7c081db3030f.jpeg</url>
      <title>Forem: poshiya parth s</title>
      <link>https://forem.com/parth24072001</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/parth24072001"/>
    <language>en</language>
    <item>
      <title>Supabase Managing database migrations across multiple environments (Local, Staging, Production)</title>
      <dc:creator>poshiya parth s</dc:creator>
      <pubDate>Sat, 04 Oct 2025 13:12:31 +0000</pubDate>
      <link>https://forem.com/parth24072001/supabase-managing-database-migrations-across-multiple-environments-local-staging-production-4emg</link>
      <guid>https://forem.com/parth24072001/supabase-managing-database-migrations-across-multiple-environments-local-staging-production-4emg</guid>
      <description>&lt;h2&gt;
  
  
  Quick Reference Table
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;#&lt;/th&gt;
&lt;th&gt;Operation&lt;/th&gt;
&lt;th&gt;Makefile Command&lt;/th&gt;
&lt;th&gt;Direct Command (Shortened)&lt;/th&gt;
&lt;th&gt;Safety&lt;/th&gt;
&lt;th&gt;Confirmation&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;Copy staging → local&lt;/td&gt;
&lt;td&gt;&lt;code&gt;make local-import-staging&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;npx supabase db reset --local&lt;/code&gt; + dumps&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;None&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;Copy production → local&lt;/td&gt;
&lt;td&gt;&lt;code&gt;make local-import-production&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;npx supabase db reset --local&lt;/code&gt; + dumps&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;None&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;Create new migration&lt;/td&gt;
&lt;td&gt;&lt;code&gt;make local-create-migration&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;npx supabase db diff -f name&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;None&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;Diff local to file&lt;/td&gt;
&lt;td&gt;&lt;code&gt;make local-get-migration&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;npx supabase db diff -f name --local&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;None&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;Apply migrations locally&lt;/td&gt;
&lt;td&gt;&lt;code&gt;make local-apply-migration&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;npx supabase db push --local&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;None&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;Reset local DB&lt;/td&gt;
&lt;td&gt;&lt;code&gt;make local-reset&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;npx supabase db reset --local&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;⚠️&lt;/td&gt;
&lt;td&gt;&lt;code&gt;yes&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;CLI authentication&lt;/td&gt;
&lt;td&gt;&lt;code&gt;make supabase-login&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;npx supabase login&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;None&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;td&gt;Diff staging to file&lt;/td&gt;
&lt;td&gt;&lt;code&gt;make staging-get-migration&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;npx supabase db diff -f name --db-url "$STAGING_DB_URL"&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;None&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;9&lt;/td&gt;
&lt;td&gt;Deploy to staging&lt;/td&gt;
&lt;td&gt;&lt;code&gt;make staging-apply-migration&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;npx supabase db push --db-url "$STAGING_DB_URL"&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;⚠️&lt;/td&gt;
&lt;td&gt;&lt;code&gt;yes&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;Reset staging DB&lt;/td&gt;
&lt;td&gt;&lt;code&gt;make staging-reset&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;psql "$STAGING_DB_URL" -c "DROP SCHEMA..."&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;🔶&lt;/td&gt;
&lt;td&gt;&lt;code&gt;RESET-STAGING&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;11&lt;/td&gt;
&lt;td&gt;Diff production to file&lt;/td&gt;
&lt;td&gt;&lt;code&gt;make prod-get-migration&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;npx supabase db diff -f name --db-url "$PROD_DB_URL"&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;None&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;12&lt;/td&gt;
&lt;td&gt;Deploy to production&lt;/td&gt;
&lt;td&gt;&lt;code&gt;make prod-apply-migration&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;npx supabase db push --db-url "$PROD_DB_URL"&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;🔴&lt;/td&gt;
&lt;td&gt;&lt;code&gt;APPLY-TO-PRODUCTION&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;13&lt;/td&gt;
&lt;td&gt;Promote staging → prod&lt;/td&gt;
&lt;td&gt;&lt;code&gt;make prod-apply-staging-migration&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;npx supabase db dump&lt;/code&gt; + &lt;code&gt;psql "$PROD_DB_URL"&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;🔴&lt;/td&gt;
&lt;td&gt;&lt;code&gt;APPLY-TO-PRODUCTION&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;14&lt;/td&gt;
&lt;td&gt;Reset production DB&lt;/td&gt;
&lt;td&gt;&lt;code&gt;make prod-reset&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;psql "$PROD_DB_URL" -c "DROP SCHEMA..."&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;🔴🔴&lt;/td&gt;
&lt;td&gt;&lt;code&gt;DELETE-ALL-PRODUCTION-DATA&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;15&lt;/td&gt;
&lt;td&gt;Verify tools installed&lt;/td&gt;
&lt;td&gt;&lt;code&gt;make check-deps&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;node --version &amp;amp;&amp;amp; npx supabase --version&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;None&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;16&lt;/td&gt;
&lt;td&gt;Remove dump files&lt;/td&gt;
&lt;td&gt;&lt;code&gt;make clean&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;rm -f *.sql&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;None&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;17&lt;/td&gt;
&lt;td&gt;Show DB URLs&lt;/td&gt;
&lt;td&gt;&lt;code&gt;make check-env&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;echo $STAGING_DB_URL&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;None&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;18&lt;/td&gt;
&lt;td&gt;Clear migration history&lt;/td&gt;
&lt;td&gt;&lt;code&gt;make delete-migrations&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;psql -c "TRUNCATE supabase_migrations..."&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;⚠️&lt;/td&gt;
&lt;td&gt;&lt;code&gt;yes&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Managing database migrations across multiple environments (Local, Staging, Production) can be challenging. This comprehensive guide shows you two approaches: using a Makefile-based workflow system for automation and safety, or running direct commands for more control.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Choose your approach:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;🔧 &lt;strong&gt;With Makefile&lt;/strong&gt;: Automated, safe, with built-in confirmations (requires setup files)&lt;/li&gt;
&lt;li&gt;⚡ &lt;strong&gt;Direct Commands&lt;/strong&gt;: Manual, flexible, no setup required (jump to the "Direct Commands" section)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Why Use a Migration Management System?
&lt;/h2&gt;

&lt;p&gt;Traditional database migration management often leads to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;🔴 Accidental production changes&lt;/li&gt;
&lt;li&gt;🔴 Manual command errors&lt;/li&gt;
&lt;li&gt;🔴 Inconsistent environments&lt;/li&gt;
&lt;li&gt;🔴 Lost migration history&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A structured approach provides:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;✅ Safety confirmations for destructive operations&lt;/li&gt;
&lt;li&gt;✅ Automated workflows&lt;/li&gt;
&lt;li&gt;✅ Clear environment separation&lt;/li&gt;
&lt;li&gt;✅ Version-controlled migrations&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;Before getting started, ensure you have:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Node.js and npm&lt;/strong&gt; installed&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Supabase CLI&lt;/strong&gt; installed:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   npm &lt;span class="nb"&gt;install &lt;/span&gt;supabase &lt;span class="nt"&gt;--save-dev&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Docker Desktop&lt;/strong&gt; running (required for local Supabase)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;PostgreSQL client tools&lt;/strong&gt; (psql) - optional but recommended&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Initialize your project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx supabase init
npx supabase start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;If using the Makefile approach&lt;/strong&gt;, additionally run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;chmod&lt;/span&gt; +x scripts/&lt;span class="k"&gt;*&lt;/span&gt;.sh  &lt;span class="c"&gt;# Make scripts executable&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Two Approaches to Choose From
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Approach 1: Using Makefile (Requires Setup Files)
&lt;/h3&gt;

&lt;p&gt;If you have access to the Makefile and helper scripts, this approach provides automation and safety features.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pros:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;✅ Simple, memorable commands&lt;/li&gt;
&lt;li&gt;✅ Built-in safety confirmations&lt;/li&gt;
&lt;li&gt;✅ Automated workflows&lt;/li&gt;
&lt;li&gt;✅ Less chance of errors&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Cons:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;❌ Requires Makefile and script files&lt;/li&gt;
&lt;li&gt;❌ Less flexibility for custom operations&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Continue reading the sections below for Makefile commands.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Approach 2: Direct Commands (No Setup Required)
&lt;/h3&gt;

&lt;p&gt;If you don't have the Makefile or prefer manual control, you can run Supabase CLI and psql commands directly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pros:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;✅ No setup files needed&lt;/li&gt;
&lt;li&gt;✅ Full control over operations&lt;/li&gt;
&lt;li&gt;✅ Easy to integrate into CI/CD&lt;/li&gt;
&lt;li&gt;✅ Works anywhere&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Cons:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;❌ More verbose commands&lt;/li&gt;
&lt;li&gt;❌ Manual safety checks&lt;/li&gt;
&lt;li&gt;❌ Higher chance of typos&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Skip to the "Direct Commands (Without Makefile)" section below.&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Project Structure (For Makefile Approach)
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;your-project/
├── Makefile                           # Main command interface
├── .env                               # Environment variables (gitignored)
├── scripts/
│   ├── apply-to-local.sh             # Apply dumps to local
│   ├── dump-staging-migrations.sh    # Export staging schema
│   ├── apply-migrations-to-prod.sh   # Push to production
│   └── reset-remote-db.sh            # Reset remote databases
└── supabase/
    └── migrations/                    # Migration files
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; If you don't have these files, use the Direct Commands approach instead.&lt;/p&gt;

&lt;h2&gt;
  
  
  Environment Setup
&lt;/h2&gt;

&lt;p&gt;Create a &lt;code&gt;.env&lt;/code&gt; file in your project root:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Local Environment (default)&lt;/span&gt;
&lt;span class="nv"&gt;LOCAL_DB_URL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;postgresql://postgres:postgres@localhost:54322/postgres

&lt;span class="c"&gt;# Staging Environment&lt;/span&gt;
&lt;span class="nv"&gt;STAGING_DB_URL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;postgresql://postgres:YOUR_PASSWORD@db.YOUR_STAGING_REF.supabase.co:5432/postgres
&lt;span class="nv"&gt;STAGING_PROJECT_REF&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;your-staging-ref

&lt;span class="c"&gt;# Production Environment&lt;/span&gt;
&lt;span class="nv"&gt;PROD_DB_URL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;postgresql://postgres.YOUR_PROD_REF:YOUR_PASSWORD@aws-1-ap-south-1.pooler.supabase.com:6543/postgres
&lt;span class="nv"&gt;PROD_PROJECT_REF&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;your-production-ref
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;⚠️ Security Note&lt;/strong&gt;: Never commit &lt;code&gt;.env&lt;/code&gt; to version control! Add it to &lt;code&gt;.gitignore&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Complete Command Reference
&lt;/h2&gt;

&lt;h3&gt;
  
  
  🖥️ Local Environment Commands
&lt;/h3&gt;

&lt;h4&gt;
  
  
  1. Import Staging to Local
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;With Makefile:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;make local-import-staging
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Direct Command:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Set staging URL&lt;/span&gt;
&lt;span class="nv"&gt;STAGING_DB_URL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"postgresql://postgres:YOUR_PASSWORD@db.YOUR_STAGING_REF.supabase.co:5432/postgres"&lt;/span&gt;

&lt;span class="c"&gt;# Reset and import&lt;/span&gt;
npx supabase db reset &lt;span class="nt"&gt;--local&lt;/span&gt;
npx supabase db dump &lt;span class="nt"&gt;--db-url&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$STAGING_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="nt"&gt;--schema&lt;/span&gt; public &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; staging_schema.sql
npx supabase db dump &lt;span class="nt"&gt;--db-url&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$STAGING_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="nt"&gt;--data-only&lt;/span&gt; &lt;span class="nt"&gt;-f&lt;/span&gt; staging_data.sql
psql &lt;span class="s2"&gt;"postgresql://postgres:postgres@localhost:54322/postgres"&lt;/span&gt; &amp;lt; staging_schema.sql
psql &lt;span class="s2"&gt;"postgresql://postgres:postgres@localhost:54322/postgres"&lt;/span&gt; &amp;lt; staging_data.sql
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;What it does:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Resets local database (drops existing data)&lt;/li&gt;
&lt;li&gt;Dumps staging schema and data&lt;/li&gt;
&lt;li&gt;Applies both to local environment&lt;/li&gt;
&lt;li&gt;Perfect for testing staging data locally&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Use case:&lt;/strong&gt; You want to debug an issue reported in staging with real data.&lt;/p&gt;




&lt;h4&gt;
  
  
  2. Import Production to Local
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;With Makefile:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;make local-import-production
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Direct Command:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Set production URL&lt;/span&gt;
&lt;span class="nv"&gt;PROD_DB_URL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"postgresql://postgres.YOUR_PROD_REF:YOUR_PASSWORD@aws-1-ap-south-1.pooler.supabase.com:6543/postgres"&lt;/span&gt;

&lt;span class="c"&gt;# Reset and import&lt;/span&gt;
npx supabase db reset &lt;span class="nt"&gt;--local&lt;/span&gt;
npx supabase db dump &lt;span class="nt"&gt;--db-url&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$PROD_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="nt"&gt;--schema&lt;/span&gt; public &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; production_schema.sql
npx supabase db dump &lt;span class="nt"&gt;--db-url&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$PROD_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="nt"&gt;--data-only&lt;/span&gt; &lt;span class="nt"&gt;-f&lt;/span&gt; production_data.sql
psql &lt;span class="s2"&gt;"postgresql://postgres:postgres@localhost:54322/postgres"&lt;/span&gt; &amp;lt; production_schema.sql
psql &lt;span class="s2"&gt;"postgresql://postgres:postgres@localhost:54322/postgres"&lt;/span&gt; &amp;lt; production_data.sql
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;What it does:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Resets local database&lt;/li&gt;
&lt;li&gt;Dumps production schema and data&lt;/li&gt;
&lt;li&gt;Applies both to local environment&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Use case:&lt;/strong&gt; Investigating a production bug or testing a hotfix with production data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;⚠️ Warning&lt;/strong&gt;: Production data may contain sensitive information. Handle with care!&lt;/p&gt;




&lt;h4&gt;
  
  
  3. Create New Migration
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;With Makefile:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;make local-create-migration
&lt;span class="c"&gt;# Enter migration name when prompted&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Direct Command:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx supabase db diff &lt;span class="nt"&gt;-f&lt;/span&gt; add_user_profiles_table
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;What it does:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Creates a new timestamped migration file in &lt;code&gt;supabase/migrations/&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Captures your local database changes&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example workflow:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Make changes to your local database via Supabase Studio&lt;/span&gt;
&lt;span class="c"&gt;# Then capture them:&lt;/span&gt;
npx supabase db diff &lt;span class="nt"&gt;-f&lt;/span&gt; add_user_profiles
&lt;span class="c"&gt;# Creates: 20251004123045_add_user_profiles.sql&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h4&gt;
  
  
  4. Get Migration from Local Changes
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;With Makefile:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;make local-get-migration
&lt;span class="c"&gt;# Enter migration name when prompted&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Direct Command:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx supabase db diff &lt;span class="nt"&gt;-f&lt;/span&gt; capture_all_changes &lt;span class="nt"&gt;--local&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;What it does:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Diffs your local database against the migration history&lt;/li&gt;
&lt;li&gt;Generates a new migration file with the differences&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Use case:&lt;/strong&gt; You've made multiple changes and want to capture them all as one migration.&lt;/p&gt;




&lt;h4&gt;
  
  
  5. Apply Local Migrations
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;With Makefile:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;make local-apply-migration
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Direct Command:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx supabase db push &lt;span class="nt"&gt;--local&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;What it does:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Applies all pending migrations to local database&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Use case:&lt;/strong&gt; After pulling new migrations from Git, sync your local database.&lt;/p&gt;




&lt;h4&gt;
  
  
  6. Reset Local Database
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;With Makefile:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;make local-reset
&lt;span class="c"&gt;# Type 'yes' to confirm&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Direct Command:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx supabase db reset &lt;span class="nt"&gt;--local&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;What it does:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Drops all local data&lt;/li&gt;
&lt;li&gt;Reapplies all migrations from scratch&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Use case:&lt;/strong&gt; Clean slate for testing or recovering from a corrupted local state.&lt;/p&gt;




&lt;h4&gt;
  
  
  7. Supabase Login
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;With Makefile:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;make supabase-login
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Direct Command:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx supabase login
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;What it does:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Authenticates with Supabase CLI&lt;/li&gt;
&lt;li&gt;Required before running remote operations&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  🔶 Staging Environment Commands
&lt;/h3&gt;

&lt;h4&gt;
  
  
  8. Get Migration from Staging
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;With Makefile:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;make staging-get-migration
&lt;span class="c"&gt;# Enter migration name when prompted&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Direct Command:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;STAGING_DB_URL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"postgresql://postgres:YOUR_PASSWORD@db.YOUR_STAGING_REF.supabase.co:5432/postgres"&lt;/span&gt;
npx supabase db diff &lt;span class="nt"&gt;-f&lt;/span&gt; migration_from_staging &lt;span class="nt"&gt;--db-url&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$STAGING_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;What it does:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Diffs staging database against local migrations&lt;/li&gt;
&lt;li&gt;Creates a migration file capturing staging-specific changes&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Use case:&lt;/strong&gt; Someone made manual changes in staging that need to be migrated to code.&lt;/p&gt;




&lt;h4&gt;
  
  
  9. Apply Migration to Staging
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;With Makefile:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;make staging-apply-migration
&lt;span class="c"&gt;# Type 'yes' to confirm&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Direct Command:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;STAGING_DB_URL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"postgresql://postgres:YOUR_PASSWORD@db.YOUR_STAGING_REF.supabase.co:5432/postgres"&lt;/span&gt;
npx supabase db push &lt;span class="nt"&gt;--db-url&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$STAGING_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;What it does:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Pushes local migrations to staging&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Use case:&lt;/strong&gt; Deploying tested changes from local to staging for QA testing.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Safety level&lt;/strong&gt;: ⚠️ Medium - affects staging environment&lt;/p&gt;




&lt;h4&gt;
  
  
  10. Reset Staging Database
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;With Makefile:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;make staging-reset
&lt;span class="c"&gt;# Type 'RESET-STAGING' to confirm&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Direct Command:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;STAGING_DB_URL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"postgresql://postgres:YOUR_PASSWORD@db.YOUR_STAGING_REF.supabase.co:5432/postgres"&lt;/span&gt;

&lt;span class="c"&gt;# Execute reset&lt;/span&gt;
psql &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$STAGING_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="nt"&gt;-c&lt;/span&gt; &lt;span class="s2"&gt;"DROP SCHEMA IF EXISTS public CASCADE; CREATE SCHEMA public; GRANT ALL ON SCHEMA public TO postgres; GRANT ALL ON SCHEMA public TO public;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;What it does:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Drops public schema&lt;/li&gt;
&lt;li&gt;Recreates empty schema&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Use case:&lt;/strong&gt; Starting fresh after testing or clearing corrupted staging data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Safety level&lt;/strong&gt;: 🔶 High - destroys staging data&lt;/p&gt;




&lt;h3&gt;
  
  
  🔴 Production Environment Commands
&lt;/h3&gt;

&lt;h4&gt;
  
  
  11. Get Migration from Production
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;With Makefile:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;make prod-get-migration
&lt;span class="c"&gt;# Enter migration name when prompted&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Direct Command:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;PROD_DB_URL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"postgresql://postgres.YOUR_PROD_REF:YOUR_PASSWORD@aws-1-ap-south-1.pooler.supabase.com:6543/postgres"&lt;/span&gt;
npx supabase db diff &lt;span class="nt"&gt;-f&lt;/span&gt; migration_from_production &lt;span class="nt"&gt;--db-url&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$PROD_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;What it does:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Diffs production database against local migrations&lt;/li&gt;
&lt;li&gt;Creates migration file capturing production state&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Use case:&lt;/strong&gt; Documenting manual production hotfixes in code.&lt;/p&gt;




&lt;h4&gt;
  
  
  12. Apply Migration to Production
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;With Makefile:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;make prod-apply-migration
&lt;span class="c"&gt;# Type 'APPLY-TO-PRODUCTION' to confirm&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Direct Command:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;PROD_DB_URL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"postgresql://postgres.YOUR_PROD_REF:YOUR_PASSWORD@aws-1-ap-south-1.pooler.supabase.com:6543/postgres"&lt;/span&gt;
npx supabase db push &lt;span class="nt"&gt;--db-url&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$PROD_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;What it does:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Pushes local migrations to production&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Use case:&lt;/strong&gt; Final deployment after successful staging tests.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Safety level&lt;/strong&gt;: 🔴 Critical - affects production&lt;/p&gt;




&lt;h4&gt;
  
  
  13. Apply Staging Migration to Production
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;With Makefile:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;make prod-apply-staging-migration
&lt;span class="c"&gt;# Type 'APPLY-TO-PRODUCTION' to confirm&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Direct Command:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;STAGING_DB_URL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"postgresql://postgres:YOUR_PASSWORD@db.YOUR_STAGING_REF.supabase.co:5432/postgres"&lt;/span&gt;
&lt;span class="nv"&gt;PROD_DB_URL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"postgresql://postgres.YOUR_PROD_REF:YOUR_PASSWORD@aws-1-ap-south-1.pooler.supabase.com:6543/postgres"&lt;/span&gt;

&lt;span class="c"&gt;# Dump staging&lt;/span&gt;
npx supabase db dump &lt;span class="nt"&gt;--db-url&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$STAGING_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="nt"&gt;--schema&lt;/span&gt; public &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; staging_to_prod_&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;date&lt;/span&gt; +%Y%m%d_%H%M%S&lt;span class="si"&gt;)&lt;/span&gt;.sql

&lt;span class="c"&gt;# Review the file first!&lt;/span&gt;
&lt;span class="nb"&gt;cat &lt;/span&gt;staging_to_prod_&lt;span class="k"&gt;*&lt;/span&gt;.sql

&lt;span class="c"&gt;# Apply to production&lt;/span&gt;
psql &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$PROD_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &amp;lt; staging_to_prod_&lt;span class="k"&gt;*&lt;/span&gt;.sql
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;What it does:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Dumps current staging schema&lt;/li&gt;
&lt;li&gt;Creates timestamped migration file&lt;/li&gt;
&lt;li&gt;Applies staging state to production&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Use case:&lt;/strong&gt; Promoting a tested staging environment to production.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Safety level&lt;/strong&gt;: 🔴 Critical - affects production&lt;/p&gt;




&lt;h4&gt;
  
  
  14. Reset Production Database
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;With Makefile:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;make prod-reset
&lt;span class="c"&gt;# Type 'DELETE-ALL-PRODUCTION-DATA' to confirm&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Direct Command:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;PROD_DB_URL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"postgresql://postgres.YOUR_PROD_REF:YOUR_PASSWORD@aws-1-ap-south-1.pooler.supabase.com:6543/postgres"&lt;/span&gt;

&lt;span class="c"&gt;# ⚠️⚠️⚠️ EXTREMELY DANGEROUS ⚠️⚠️⚠️&lt;/span&gt;
psql &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$PROD_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="nt"&gt;-c&lt;/span&gt; &lt;span class="s2"&gt;"DROP SCHEMA IF EXISTS public CASCADE; CREATE SCHEMA public; GRANT ALL ON SCHEMA public TO postgres; GRANT ALL ON SCHEMA public TO public;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;What it does:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Drops public schema in production&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Use case:&lt;/strong&gt; Rare! Only for complete environment resets or catastrophic recovery.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Safety level&lt;/strong&gt;: 🔴🔴 Extremely Dangerous - destroys all production data&lt;/p&gt;




&lt;h3&gt;
  
  
  🔧 Utility Commands
&lt;/h3&gt;

&lt;h4&gt;
  
  
  15. Check Dependencies
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;With Makefile:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;make check-deps
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Direct Command:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;node &lt;span class="nt"&gt;--version&lt;/span&gt;
npx &lt;span class="nt"&gt;--version&lt;/span&gt;
npx supabase &lt;span class="nt"&gt;--version&lt;/span&gt;
psql &lt;span class="nt"&gt;--version&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;What it does:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Verifies Node.js, npm, and Supabase CLI are installed&lt;/li&gt;
&lt;li&gt;Confirms all required tools are available&lt;/li&gt;
&lt;/ul&gt;




&lt;h4&gt;
  
  
  16. Clean Dump Files
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;With Makefile:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;make clean
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Direct Command:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;rm&lt;/span&gt; &lt;span class="nt"&gt;-f&lt;/span&gt; staging_schema.sql staging_data.sql production_schema.sql production_data.sql staging_migration_&lt;span class="k"&gt;*&lt;/span&gt;.sql
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;What it does:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Removes generated SQL dump files&lt;/li&gt;
&lt;/ul&gt;




&lt;h4&gt;
  
  
  17. Check Environment Configuration
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;With Makefile:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;make check-env
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Direct Command:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Local: postgresql://postgres:postgres@localhost:54322/postgres"&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Staging: &lt;/span&gt;&lt;span class="nv"&gt;$STAGING_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Production: &lt;/span&gt;&lt;span class="nv"&gt;$PROD_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;What it does:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Displays configured database URLs&lt;/li&gt;
&lt;li&gt;Helps verify environment setup&lt;/li&gt;
&lt;/ul&gt;




&lt;h4&gt;
  
  
  18. Delete Migration Records
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;With Makefile:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;make delete-migrations
&lt;span class="c"&gt;# Type 'yes' to confirm&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Direct Command:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# View current migrations&lt;/span&gt;
psql &lt;span class="s2"&gt;"postgresql://postgres:postgres@localhost:54322/postgres"&lt;/span&gt; &lt;span class="nt"&gt;-c&lt;/span&gt; &lt;span class="s2"&gt;"SELECT * FROM supabase_migrations.schema_migrations;"&lt;/span&gt;

&lt;span class="c"&gt;# Delete all migration records&lt;/span&gt;
psql &lt;span class="s2"&gt;"postgresql://postgres:postgres@localhost:54322/postgres"&lt;/span&gt; &lt;span class="nt"&gt;-c&lt;/span&gt; &lt;span class="s2"&gt;"TRUNCATE supabase_migrations.schema_migrations;"&lt;/span&gt;

&lt;span class="c"&gt;# Or delete specific migration&lt;/span&gt;
psql &lt;span class="s2"&gt;"postgresql://postgres:postgres@localhost:54322/postgres"&lt;/span&gt; &lt;span class="nt"&gt;-c&lt;/span&gt; &lt;span class="s2"&gt;"DELETE FROM supabase_migrations.schema_migrations WHERE version = '20251004123045';"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;What it does:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Clears migration history from database&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Use case:&lt;/strong&gt; When migration history is corrupted or needs cleanup.&lt;/p&gt;

&lt;h2&gt;
  
  
  Real-World Workflows
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Workflow 1: Feature Development
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;With Makefile:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# 1. Start with fresh staging data&lt;/span&gt;
make local-import-staging

&lt;span class="c"&gt;# 2. Develop your feature locally&lt;/span&gt;
&lt;span class="c"&gt;# Make database changes via Supabase Studio or SQL&lt;/span&gt;

&lt;span class="c"&gt;# 3. Create migration&lt;/span&gt;
make local-create-migration
&lt;span class="c"&gt;# Enter: add_comments_feature&lt;/span&gt;

&lt;span class="c"&gt;# 4. Test locally&lt;/span&gt;
npm run dev

&lt;span class="c"&gt;# 5. Deploy to staging&lt;/span&gt;
make staging-apply-migration

&lt;span class="c"&gt;# 6. After QA approval, deploy to production&lt;/span&gt;
make prod-apply-migration
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Direct Commands:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# 1. Import staging data&lt;/span&gt;
&lt;span class="nv"&gt;STAGING_DB_URL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"postgresql://postgres:YOUR_PASSWORD@db.YOUR_STAGING_REF.supabase.co:5432/postgres"&lt;/span&gt;
npx supabase db reset &lt;span class="nt"&gt;--local&lt;/span&gt;
npx supabase db dump &lt;span class="nt"&gt;--db-url&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$STAGING_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="nt"&gt;--schema&lt;/span&gt; public &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; staging_schema.sql
npx supabase db dump &lt;span class="nt"&gt;--db-url&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$STAGING_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="nt"&gt;--data-only&lt;/span&gt; &lt;span class="nt"&gt;-f&lt;/span&gt; staging_data.sql
psql &lt;span class="s2"&gt;"postgresql://postgres:postgres@localhost:54322/postgres"&lt;/span&gt; &amp;lt; staging_schema.sql
psql &lt;span class="s2"&gt;"postgresql://postgres:postgres@localhost:54322/postgres"&lt;/span&gt; &amp;lt; staging_data.sql

&lt;span class="c"&gt;# 2. Develop feature (make changes)&lt;/span&gt;

&lt;span class="c"&gt;# 3. Create migration&lt;/span&gt;
npx supabase db diff &lt;span class="nt"&gt;-f&lt;/span&gt; add_comments_feature

&lt;span class="c"&gt;# 4. Test locally&lt;/span&gt;
npm run dev

&lt;span class="c"&gt;# 5. Deploy to staging&lt;/span&gt;
npx supabase db push &lt;span class="nt"&gt;--db-url&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$STAGING_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;

&lt;span class="c"&gt;# 6. Deploy to production&lt;/span&gt;
&lt;span class="nv"&gt;PROD_DB_URL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"postgresql://postgres.YOUR_PROD_REF:YOUR_PASSWORD@aws-1-ap-south-1.pooler.supabase.com:6543/postgres"&lt;/span&gt;
npx supabase db push &lt;span class="nt"&gt;--db-url&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$PROD_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Workflow 2: Hotfix in Production
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;With Makefile:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# 1. Import production data locally&lt;/span&gt;
make local-import-production

&lt;span class="c"&gt;# 2. Reproduce and fix the issue&lt;/span&gt;

&lt;span class="c"&gt;# 3. Create migration for the fix&lt;/span&gt;
make local-create-migration
&lt;span class="c"&gt;# Enter: hotfix_user_permissions&lt;/span&gt;

&lt;span class="c"&gt;# 4. Test thoroughly locally&lt;/span&gt;

&lt;span class="c"&gt;# 5. Apply directly to production (after approval)&lt;/span&gt;
make prod-apply-migration
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Direct Commands:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# 1. Import production data&lt;/span&gt;
&lt;span class="nv"&gt;PROD_DB_URL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"postgresql://postgres.YOUR_PROD_REF:YOUR_PASSWORD@aws-1-ap-south-1.pooler.supabase.com:6543/postgres"&lt;/span&gt;
npx supabase db reset &lt;span class="nt"&gt;--local&lt;/span&gt;
npx supabase db dump &lt;span class="nt"&gt;--db-url&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$PROD_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="nt"&gt;--schema&lt;/span&gt; public &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; prod_schema.sql
npx supabase db dump &lt;span class="nt"&gt;--db-url&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$PROD_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="nt"&gt;--data-only&lt;/span&gt; &lt;span class="nt"&gt;-f&lt;/span&gt; prod_data.sql
psql &lt;span class="s2"&gt;"postgresql://postgres:postgres@localhost:54322/postgres"&lt;/span&gt; &amp;lt; prod_schema.sql
psql &lt;span class="s2"&gt;"postgresql://postgres:postgres@localhost:54322/postgres"&lt;/span&gt; &amp;lt; prod_data.sql

&lt;span class="c"&gt;# 2. Fix the issue&lt;/span&gt;

&lt;span class="c"&gt;# 3. Create migration&lt;/span&gt;
npx supabase db diff &lt;span class="nt"&gt;-f&lt;/span&gt; hotfix_user_permissions

&lt;span class="c"&gt;# 4. Test thoroughly&lt;/span&gt;

&lt;span class="c"&gt;# 5. Apply to production&lt;/span&gt;
npx supabase db push &lt;span class="nt"&gt;--db-url&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$PROD_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Workflow 3: Environment Synchronization
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;With Makefile:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Sync staging to production&lt;/span&gt;
make prod-apply-staging-migration

&lt;span class="c"&gt;# Or sync production back to staging&lt;/span&gt;
make staging-reset
make prod-get-migration
make staging-apply-migration
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Direct Commands:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Sync staging to production&lt;/span&gt;
&lt;span class="nv"&gt;STAGING_DB_URL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"postgresql://postgres:YOUR_PASSWORD@db.YOUR_STAGING_REF.supabase.co:5432/postgres"&lt;/span&gt;
&lt;span class="nv"&gt;PROD_DB_URL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"postgresql://postgres.YOUR_PROD_REF:YOUR_PASSWORD@aws-1-ap-south-1.pooler.supabase.com:6543/postgres"&lt;/span&gt;

&lt;span class="c"&gt;# Dump staging&lt;/span&gt;
npx supabase db dump &lt;span class="nt"&gt;--db-url&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$STAGING_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="nt"&gt;--schema&lt;/span&gt; public &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; staging_to_prod.sql

&lt;span class="c"&gt;# Apply to production&lt;/span&gt;
psql &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$PROD_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &amp;lt; staging_to_prod.sql

&lt;span class="c"&gt;# Or sync production to staging&lt;/span&gt;
npx supabase db dump &lt;span class="nt"&gt;--db-url&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$PROD_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="nt"&gt;--schema&lt;/span&gt; public &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; prod_to_staging.sql
psql &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$STAGING_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &amp;lt; prod_to_staging.sql
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Workflow 4: Debugging with Real Data
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;With Makefile:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Import production data for investigation&lt;/span&gt;
make local-import-production

&lt;span class="c"&gt;# Investigate the issue&lt;/span&gt;
&lt;span class="c"&gt;# No changes needed? Just clean up&lt;/span&gt;
make local-reset
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Direct Commands:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Import production data&lt;/span&gt;
&lt;span class="nv"&gt;PROD_DB_URL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"postgresql://postgres.YOUR_PROD_REF:YOUR_PASSWORD@aws-1-ap-south-1.pooler.supabase.com:6543/postgres"&lt;/span&gt;
npx supabase db reset &lt;span class="nt"&gt;--local&lt;/span&gt;
npx supabase db dump &lt;span class="nt"&gt;--db-url&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$PROD_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="nt"&gt;--schema&lt;/span&gt; public &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; prod_schema.sql
npx supabase db dump &lt;span class="nt"&gt;--db-url&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$PROD_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="nt"&gt;--data-only&lt;/span&gt; &lt;span class="nt"&gt;-f&lt;/span&gt; prod_data.sql
psql &lt;span class="s2"&gt;"postgresql://postgres:postgres@localhost:54322/postgres"&lt;/span&gt; &amp;lt; prod_schema.sql
psql &lt;span class="s2"&gt;"postgresql://postgres:postgres@localhost:54322/postgres"&lt;/span&gt; &amp;lt; prod_data.sql

&lt;span class="c"&gt;# Investigate&lt;/span&gt;

&lt;span class="c"&gt;# Clean up&lt;/span&gt;
npx supabase db reset &lt;span class="nt"&gt;--local&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Script Deep Dive (For Makefile Approach)
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; This section is only relevant if you're using the Makefile approach with helper scripts. If you're using direct commands, skip this section.&lt;/p&gt;

&lt;h3&gt;
  
  
  apply-to-local.sh
&lt;/h3&gt;

&lt;p&gt;This script intelligently applies SQL files to your local database:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Features:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Accepts multiple SQL files as arguments&lt;/li&gt;
&lt;li&gt;Defaults to &lt;code&gt;staging_schema.sql&lt;/code&gt; and &lt;code&gt;staging_data.sql&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Prefers local &lt;code&gt;psql&lt;/code&gt; if available&lt;/li&gt;
&lt;li&gt;Falls back to Docker exec if psql isn't installed&lt;/li&gt;
&lt;li&gt;Applies files in order with error handling&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Usage examples:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Apply default files&lt;/span&gt;
./scripts/apply-to-local.sh

&lt;span class="c"&gt;# Apply specific files in order&lt;/span&gt;
./scripts/apply-to-local.sh schema.sql data.sql

&lt;span class="c"&gt;# Apply single migration&lt;/span&gt;
./scripts/apply-to-local.sh supabase/migrations/20251004_my_migration.sql
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  dump-staging-migrations.sh
&lt;/h3&gt;

&lt;p&gt;Exports staging database schema to a timestamped file:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What it captures:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Public schema structure&lt;/li&gt;
&lt;li&gt;Tables, columns, constraints&lt;/li&gt;
&lt;li&gt;Indexes and foreign keys&lt;/li&gt;
&lt;li&gt;Functions and triggers&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Output example:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;staging_migration_20251004_143052.sql
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  apply-migrations-to-prod.sh
&lt;/h3&gt;

&lt;p&gt;Safely applies staging migrations to production:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Safety features:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Finds latest staging migration file&lt;/li&gt;
&lt;li&gt;Displays file information&lt;/li&gt;
&lt;li&gt;Requires explicit confirmation&lt;/li&gt;
&lt;li&gt;Uses PostgreSQL transaction handling&lt;/li&gt;
&lt;li&gt;Reports success/failure clearly&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  reset-remote-db.sh
&lt;/h3&gt;

&lt;p&gt;Resets remote databases with extreme caution:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How it works:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Accepts DB URL and environment name&lt;/li&gt;
&lt;li&gt;Drops public schema entirely&lt;/li&gt;
&lt;li&gt;Recreates empty public schema&lt;/li&gt;
&lt;li&gt;Restores default permissions&lt;/li&gt;
&lt;li&gt;Uses Node.js pg client for reliability&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Safety Features Explained
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Confirmation Levels
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Safety Level&lt;/th&gt;
&lt;th&gt;Confirmation Required&lt;/th&gt;
&lt;th&gt;Use Case&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;✅ Safe&lt;/td&gt;
&lt;td&gt;None&lt;/td&gt;
&lt;td&gt;Read-only or local operations&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;⚠️ Medium&lt;/td&gt;
&lt;td&gt;Type &lt;code&gt;yes&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Staging operations&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;🔶 High&lt;/td&gt;
&lt;td&gt;Type &lt;code&gt;RESET-STAGING&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Destructive staging operations&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;🔴 Critical&lt;/td&gt;
&lt;td&gt;Type &lt;code&gt;APPLY-TO-PRODUCTION&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Production modifications&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;🔴🔴 Extreme&lt;/td&gt;
&lt;td&gt;Type &lt;code&gt;DELETE-ALL-PRODUCTION-DATA&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Production destruction&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Best Practices
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Always Test Locally First&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   make local-apply-migration  &lt;span class="c"&gt;# Test here first&lt;/span&gt;
   make staging-apply-migration  &lt;span class="c"&gt;# Then here&lt;/span&gt;
   make prod-apply-migration  &lt;span class="c"&gt;# Finally here&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Review Migrations Before Applying&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   &lt;span class="c"&gt;# Check what will be applied&lt;/span&gt;
   &lt;span class="nb"&gt;cat &lt;/span&gt;supabase/migrations/20251004_my_migration.sql
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Backup Before Major Changes&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   &lt;span class="c"&gt;# Makefile creates backups automatically during imports&lt;/span&gt;
   &lt;span class="c"&gt;# But you can also manually dump production&lt;/span&gt;
   make prod-get-migration
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Version Control Everything&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   git add supabase/migrations/
   git commit &lt;span class="nt"&gt;-m&lt;/span&gt; &lt;span class="s2"&gt;"feat: add user profiles table"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Document Database Changes&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Use descriptive migration names&lt;/li&gt;
&lt;li&gt;Add comments in migration files&lt;/li&gt;
&lt;li&gt;Update README with schema changes&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Troubleshooting
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Common Issues
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1. "Permission denied" on scripts&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;chmod&lt;/span&gt; +x scripts/&lt;span class="k"&gt;*&lt;/span&gt;.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. "psql: command not found"&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# macOS&lt;/span&gt;
brew &lt;span class="nb"&gt;install &lt;/span&gt;postgresql

&lt;span class="c"&gt;# Ubuntu/Debian&lt;/span&gt;
&lt;span class="nb"&gt;sudo &lt;/span&gt;apt-get &lt;span class="nb"&gt;install &lt;/span&gt;postgresql-client

&lt;span class="c"&gt;# Windows&lt;/span&gt;
&lt;span class="c"&gt;# Download from postgresql.org&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3. "Connection refused" to local DB&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Check if Supabase is running&lt;/span&gt;
npx supabase status

&lt;span class="c"&gt;# Start if not running&lt;/span&gt;
npx supabase start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;4. "Docker container not found"&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Ensure Docker Desktop is running&lt;/span&gt;
&lt;span class="c"&gt;# Restart Supabase&lt;/span&gt;
npx supabase stop
npx supabase start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;5. Migration conflicts&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Reset local and reimport&lt;/span&gt;
make local-reset
make local-import-staging

&lt;span class="c"&gt;# Or manually resolve conflicts in migration files&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;6. ".env variables not loading"&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Check file exists&lt;/span&gt;
&lt;span class="nb"&gt;ls&lt;/span&gt; &lt;span class="nt"&gt;-la&lt;/span&gt; .env

&lt;span class="c"&gt;# Check variable names match exactly&lt;/span&gt;
make check-env
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Advanced Tips
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Custom Migrations
&lt;/h3&gt;

&lt;p&gt;Create complex migrations with multiple statements:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- supabase/migrations/20251004_custom.sql&lt;/span&gt;
&lt;span class="k"&gt;BEGIN&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;-- Add new table&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;user_profiles&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="n"&gt;UUID&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="n"&gt;gen_random_uuid&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
  &lt;span class="n"&gt;user_id&lt;/span&gt; &lt;span class="n"&gt;UUID&lt;/span&gt; &lt;span class="k"&gt;REFERENCES&lt;/span&gt; &lt;span class="n"&gt;auth&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;users&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="n"&gt;display_name&lt;/span&gt; &lt;span class="nb"&gt;TEXT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;avatar_url&lt;/span&gt; &lt;span class="nb"&gt;TEXT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;created_at&lt;/span&gt; &lt;span class="n"&gt;TIMESTAMPTZ&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="n"&gt;NOW&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;-- Add RLS policies&lt;/span&gt;
&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;user_profiles&lt;/span&gt; &lt;span class="n"&gt;ENABLE&lt;/span&gt; &lt;span class="k"&gt;ROW&lt;/span&gt; &lt;span class="k"&gt;LEVEL&lt;/span&gt; &lt;span class="k"&gt;SECURITY&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="n"&gt;POLICY&lt;/span&gt; &lt;span class="nv"&gt;"Users can view all profiles"&lt;/span&gt;
  &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;user_profiles&lt;/span&gt; &lt;span class="k"&gt;FOR&lt;/span&gt; &lt;span class="k"&gt;SELECT&lt;/span&gt;
  &lt;span class="k"&gt;TO&lt;/span&gt; &lt;span class="n"&gt;authenticated&lt;/span&gt;
  &lt;span class="k"&gt;USING&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="n"&gt;POLICY&lt;/span&gt; &lt;span class="nv"&gt;"Users can update own profile"&lt;/span&gt;
  &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;user_profiles&lt;/span&gt; &lt;span class="k"&gt;FOR&lt;/span&gt; &lt;span class="k"&gt;UPDATE&lt;/span&gt;
  &lt;span class="k"&gt;TO&lt;/span&gt; &lt;span class="n"&gt;authenticated&lt;/span&gt;
  &lt;span class="k"&gt;USING&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;auth&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;uid&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;user_id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;-- Add indexes&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;idx_user_profiles_user_id&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;user_profiles&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user_id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;COMMIT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Selective Data Import
&lt;/h3&gt;

&lt;p&gt;Modify &lt;code&gt;apply-to-local.sh&lt;/code&gt; to exclude specific tables:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Dump without sensitive tables&lt;/span&gt;
npx supabase db dump &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--db-url&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$STAGING_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--exclude-table&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;user_secrets &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--exclude-table&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;payment_info &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;-f&lt;/span&gt; staging_data_safe.sql
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Automated Backups
&lt;/h3&gt;

&lt;p&gt;Add to your deployment pipeline:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Before production deployment&lt;/span&gt;
&lt;span class="nv"&gt;timestamp&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;date&lt;/span&gt; +%Y%m%d_%H%M%S&lt;span class="si"&gt;)&lt;/span&gt;
npx supabase db dump &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--db-url&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$PROD_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;-f&lt;/span&gt; &lt;span class="s2"&gt;"backups/prod_backup_&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;timestamp&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;.sql"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Quick Reference Table
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;#&lt;/th&gt;
&lt;th&gt;Command&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;th&gt;Safety&lt;/th&gt;
&lt;th&gt;Confirmation&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;&lt;code&gt;local-import-staging&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Copy staging → local&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;None&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;&lt;code&gt;local-import-production&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Copy production → local&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;None&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;&lt;code&gt;local-create-migration&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Create new migration&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;None&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;&lt;code&gt;local-get-migration&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Diff local to file&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;None&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;&lt;code&gt;local-apply-migration&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Apply migrations locally&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;None&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;&lt;code&gt;local-reset&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Reset local DB&lt;/td&gt;
&lt;td&gt;⚠️&lt;/td&gt;
&lt;td&gt;&lt;code&gt;yes&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;&lt;code&gt;supabase-login&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;CLI authentication&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;None&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;td&gt;&lt;code&gt;staging-get-migration&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Diff staging to file&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;None&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;9&lt;/td&gt;
&lt;td&gt;&lt;code&gt;staging-apply-migration&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Deploy to staging&lt;/td&gt;
&lt;td&gt;⚠️&lt;/td&gt;
&lt;td&gt;&lt;code&gt;yes&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;&lt;code&gt;staging-reset&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Reset staging DB&lt;/td&gt;
&lt;td&gt;🔶&lt;/td&gt;
&lt;td&gt;&lt;code&gt;RESET-STAGING&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;11&lt;/td&gt;
&lt;td&gt;&lt;code&gt;prod-get-migration&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Diff production to file&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;None&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;12&lt;/td&gt;
&lt;td&gt;&lt;code&gt;prod-apply-migration&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Deploy to production&lt;/td&gt;
&lt;td&gt;🔴&lt;/td&gt;
&lt;td&gt;&lt;code&gt;APPLY-TO-PRODUCTION&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;13&lt;/td&gt;
&lt;td&gt;&lt;code&gt;prod-apply-staging-migration&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Promote staging → prod&lt;/td&gt;
&lt;td&gt;🔴&lt;/td&gt;
&lt;td&gt;&lt;code&gt;APPLY-TO-PRODUCTION&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;14&lt;/td&gt;
&lt;td&gt;&lt;code&gt;prod-reset&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Reset production DB&lt;/td&gt;
&lt;td&gt;🔴🔴&lt;/td&gt;
&lt;td&gt;&lt;code&gt;DELETE-ALL-PRODUCTION-DATA&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;15&lt;/td&gt;
&lt;td&gt;&lt;code&gt;check-deps&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Verify tools installed&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;None&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;16&lt;/td&gt;
&lt;td&gt;&lt;code&gt;clean&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Remove dump files&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;None&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;17&lt;/td&gt;
&lt;td&gt;&lt;code&gt;check-env&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Show DB URLs&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;None&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;18&lt;/td&gt;
&lt;td&gt;&lt;code&gt;delete-migrations&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Clear migration history&lt;/td&gt;
&lt;td&gt;⚠️&lt;/td&gt;
&lt;td&gt;&lt;code&gt;yes&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;This Makefile-based system provides a robust, safe, and efficient way to manage Supabase databases across multiple environments. Key benefits:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Safety First&lt;/strong&gt;: Multiple confirmation levels prevent accidents&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Automation&lt;/strong&gt;: Reduces manual errors and saves time&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Clarity&lt;/strong&gt;: Clear command names and workflows&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Flexibility&lt;/strong&gt;: Supports complex migration scenarios&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Team-Friendly&lt;/strong&gt;: Consistent commands across team members&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Next Steps
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Set up your &lt;code&gt;.env&lt;/code&gt; file with connection strings&lt;/li&gt;
&lt;li&gt;Make scripts executable: &lt;code&gt;chmod +x scripts/*.sh&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Start with local operations to familiarize yourself&lt;/li&gt;
&lt;li&gt;Gradually adopt staging and production workflows&lt;/li&gt;
&lt;li&gt;Customize scripts to fit your specific needs&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Resources
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://supabase.com/docs/guides/cli" rel="noopener noreferrer"&gt;Supabase CLI Documentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.postgresql.org/docs/" rel="noopener noreferrer"&gt;PostgreSQL Documentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://makefiletutorial.com/" rel="noopener noreferrer"&gt;Makefile Tutorial&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;







&lt;h2&gt;
  
  
  Direct Commands Reference (Complete List)
&lt;/h2&gt;

&lt;p&gt;Below is a comprehensive reference of all commands you can use without the Makefile. These commands give you full control and can be used in any environment.&lt;/p&gt;

&lt;h3&gt;
  
  
  Setup &amp;amp; Authentication
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Install Supabase CLI&lt;/span&gt;
npm &lt;span class="nb"&gt;install &lt;/span&gt;supabase &lt;span class="nt"&gt;--save-dev&lt;/span&gt;

&lt;span class="c"&gt;# Initialize Supabase&lt;/span&gt;
npx supabase init

&lt;span class="c"&gt;# Start local Supabase&lt;/span&gt;
npx supabase start

&lt;span class="c"&gt;# Stop local Supabase&lt;/span&gt;
npx supabase stop

&lt;span class="c"&gt;# Check status&lt;/span&gt;
npx supabase status

&lt;span class="c"&gt;# Login to Supabase&lt;/span&gt;
npx supabase login

&lt;span class="c"&gt;# Check Supabase version&lt;/span&gt;
npx supabase &lt;span class="nt"&gt;--version&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Local Database Operations
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Reset local database (applies all migrations from scratch)&lt;/span&gt;
npx supabase db reset &lt;span class="nt"&gt;--local&lt;/span&gt;

&lt;span class="c"&gt;# Push local migrations to local database&lt;/span&gt;
npx supabase db push &lt;span class="nt"&gt;--local&lt;/span&gt;

&lt;span class="c"&gt;# Create a new migration file&lt;/span&gt;
npx supabase db diff &lt;span class="nt"&gt;-f&lt;/span&gt; add_user_profiles

&lt;span class="c"&gt;# Generate migration from local database changes&lt;/span&gt;
npx supabase db diff &lt;span class="nt"&gt;-f&lt;/span&gt; capture_changes &lt;span class="nt"&gt;--local&lt;/span&gt;

&lt;span class="c"&gt;# Apply a specific migration file&lt;/span&gt;
psql &lt;span class="s2"&gt;"postgresql://postgres:postgres@localhost:54322/postgres"&lt;/span&gt; &amp;lt; supabase/migrations/20251004_your_migration.sql

&lt;span class="c"&gt;# Dump local database schema&lt;/span&gt;
npx supabase db dump &lt;span class="nt"&gt;--local&lt;/span&gt; &lt;span class="nt"&gt;--schema&lt;/span&gt; public &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; local_schema.sql

&lt;span class="c"&gt;# Dump local database data&lt;/span&gt;
npx supabase db dump &lt;span class="nt"&gt;--local&lt;/span&gt; &lt;span class="nt"&gt;--data-only&lt;/span&gt; &lt;span class="nt"&gt;-f&lt;/span&gt; local_data.sql

&lt;span class="c"&gt;# Dump specific tables only&lt;/span&gt;
npx supabase db dump &lt;span class="nt"&gt;--local&lt;/span&gt; &lt;span class="nt"&gt;--table&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;users&lt;/span&gt; &lt;span class="nt"&gt;--table&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;posts &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; specific_tables.sql
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Staging Database Operations
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Set your staging DB URL (do this once per session)&lt;/span&gt;
&lt;span class="nb"&gt;export &lt;/span&gt;&lt;span class="nv"&gt;STAGING_DB_URL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"postgresql://postgres:YOUR_PASSWORD@db.YOUR_STAGING_REF.supabase.co:5432/postgres"&lt;/span&gt;

&lt;span class="c"&gt;# Dump staging schema&lt;/span&gt;
npx supabase db dump &lt;span class="nt"&gt;--db-url&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$STAGING_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="nt"&gt;--schema&lt;/span&gt; public &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; staging_schema.sql

&lt;span class="c"&gt;# Dump staging data&lt;/span&gt;
npx supabase db dump &lt;span class="nt"&gt;--db-url&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$STAGING_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="nt"&gt;--data-only&lt;/span&gt; &lt;span class="nt"&gt;-f&lt;/span&gt; staging_data.sql

&lt;span class="c"&gt;# Dump staging schema and data together&lt;/span&gt;
npx supabase db dump &lt;span class="nt"&gt;--db-url&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$STAGING_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; staging_full.sql

&lt;span class="c"&gt;# Generate migration from staging database&lt;/span&gt;
npx supabase db diff &lt;span class="nt"&gt;-f&lt;/span&gt; migration_from_staging &lt;span class="nt"&gt;--db-url&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$STAGING_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;

&lt;span class="c"&gt;# Push local migrations to staging&lt;/span&gt;
npx supabase db push &lt;span class="nt"&gt;--db-url&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$STAGING_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;

&lt;span class="c"&gt;# Apply specific migration to staging&lt;/span&gt;
psql &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$STAGING_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &amp;lt; supabase/migrations/20251004_your_migration.sql

&lt;span class="c"&gt;# Reset staging database (⚠️ drops all data)&lt;/span&gt;
psql &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$STAGING_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="nt"&gt;-c&lt;/span&gt; &lt;span class="s2"&gt;"DROP SCHEMA IF EXISTS public CASCADE; CREATE SCHEMA public; GRANT ALL ON SCHEMA public TO postgres; GRANT ALL ON SCHEMA public TO public;"&lt;/span&gt;

&lt;span class="c"&gt;# Backup staging before changes&lt;/span&gt;
npx supabase db dump &lt;span class="nt"&gt;--db-url&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$STAGING_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; staging_backup_&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;date&lt;/span&gt; +%Y%m%d_%H%M%S&lt;span class="si"&gt;)&lt;/span&gt;.sql
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Production Database Operations
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Set your production DB URL (do this once per session)&lt;/span&gt;
&lt;span class="nb"&gt;export &lt;/span&gt;&lt;span class="nv"&gt;PROD_DB_URL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"postgresql://postgres.YOUR_PROD_REF:YOUR_PASSWORD@aws-1-ap-south-1.pooler.supabase.com:6543/postgres"&lt;/span&gt;

&lt;span class="c"&gt;# Dump production schema&lt;/span&gt;
npx supabase db dump &lt;span class="nt"&gt;--db-url&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$PROD_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="nt"&gt;--schema&lt;/span&gt; public &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; production_schema.sql

&lt;span class="c"&gt;# Dump production data&lt;/span&gt;
npx supabase db dump &lt;span class="nt"&gt;--db-url&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$PROD_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="nt"&gt;--data-only&lt;/span&gt; &lt;span class="nt"&gt;-f&lt;/span&gt; production_data.sql

&lt;span class="c"&gt;# Dump production schema and data together&lt;/span&gt;
npx supabase db dump &lt;span class="nt"&gt;--db-url&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$PROD_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; production_full.sql

&lt;span class="c"&gt;# Generate migration from production database&lt;/span&gt;
npx supabase db diff &lt;span class="nt"&gt;-f&lt;/span&gt; migration_from_production &lt;span class="nt"&gt;--db-url&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$PROD_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;

&lt;span class="c"&gt;# Push local migrations to production (⚠️ CRITICAL)&lt;/span&gt;
npx supabase db push &lt;span class="nt"&gt;--db-url&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$PROD_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;

&lt;span class="c"&gt;# Apply specific migration to production (⚠️ CRITICAL)&lt;/span&gt;
psql &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$PROD_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &amp;lt; supabase/migrations/20251004_your_migration.sql

&lt;span class="c"&gt;# Reset production database (⚠️⚠️⚠️ EXTREMELY DANGEROUS)&lt;/span&gt;
psql &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$PROD_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="nt"&gt;-c&lt;/span&gt; &lt;span class="s2"&gt;"DROP SCHEMA IF EXISTS public CASCADE; CREATE SCHEMA public; GRANT ALL ON SCHEMA public TO postgres; GRANT ALL ON SCHEMA public TO public;"&lt;/span&gt;

&lt;span class="c"&gt;# Always backup production before changes!&lt;/span&gt;
npx supabase db dump &lt;span class="nt"&gt;--db-url&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$PROD_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; prod_backup_&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;date&lt;/span&gt; +%Y%m%d_%H%M%S&lt;span class="si"&gt;)&lt;/span&gt;.sql
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Import Database to Local
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Import staging to local&lt;/span&gt;
npx supabase db reset &lt;span class="nt"&gt;--local&lt;/span&gt;
npx supabase db dump &lt;span class="nt"&gt;--db-url&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$STAGING_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="nt"&gt;--schema&lt;/span&gt; public &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; staging_schema.sql
npx supabase db dump &lt;span class="nt"&gt;--db-url&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$STAGING_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="nt"&gt;--data-only&lt;/span&gt; &lt;span class="nt"&gt;-f&lt;/span&gt; staging_data.sql
psql &lt;span class="s2"&gt;"postgresql://postgres:postgres@localhost:54322/postgres"&lt;/span&gt; &amp;lt; staging_schema.sql
psql &lt;span class="s2"&gt;"postgresql://postgres:postgres@localhost:54322/postgres"&lt;/span&gt; &amp;lt; staging_data.sql

&lt;span class="c"&gt;# Import production to local&lt;/span&gt;
npx supabase db reset &lt;span class="nt"&gt;--local&lt;/span&gt;
npx supabase db dump &lt;span class="nt"&gt;--db-url&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$PROD_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="nt"&gt;--schema&lt;/span&gt; public &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; production_schema.sql
npx supabase db dump &lt;span class="nt"&gt;--db-url&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$PROD_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="nt"&gt;--data-only&lt;/span&gt; &lt;span class="nt"&gt;-f&lt;/span&gt; production_data.sql
psql &lt;span class="s2"&gt;"postgresql://postgres:postgres@localhost:54322/postgres"&lt;/span&gt; &amp;lt; production_schema.sql
psql &lt;span class="s2"&gt;"postgresql://postgres:postgres@localhost:54322/postgres"&lt;/span&gt; &amp;lt; production_data.sql

&lt;span class="c"&gt;# Import with error handling (stops on first error)&lt;/span&gt;
psql &lt;span class="s2"&gt;"postgresql://postgres:postgres@localhost:54322/postgres"&lt;/span&gt; &lt;span class="nt"&gt;-v&lt;/span&gt; &lt;span class="nv"&gt;ON_ERROR_STOP&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;1 &amp;lt; your_file.sql
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Using Docker (if psql not installed locally)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Get Docker container ID for Supabase DB&lt;/span&gt;
&lt;span class="nv"&gt;DB_CONTAINER_ID&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;docker ps &lt;span class="nt"&gt;-q&lt;/span&gt; &lt;span class="nt"&gt;--filter&lt;/span&gt; &lt;span class="s2"&gt;"label=com.docker.compose.service=db"&lt;/span&gt; &lt;span class="nt"&gt;--filter&lt;/span&gt; &lt;span class="s2"&gt;"name=supabase-db"&lt;/span&gt; | &lt;span class="nb"&gt;head&lt;/span&gt; &lt;span class="nt"&gt;-n&lt;/span&gt; 1&lt;span class="si"&gt;)&lt;/span&gt;

&lt;span class="c"&gt;# Check if container is found&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$DB_CONTAINER_ID&lt;/span&gt;

&lt;span class="c"&gt;# Apply SQL file via Docker&lt;/span&gt;
&lt;span class="nb"&gt;cat &lt;/span&gt;your_file.sql | docker &lt;span class="nb"&gt;exec&lt;/span&gt; &lt;span class="nt"&gt;-i&lt;/span&gt; &lt;span class="nv"&gt;$DB_CONTAINER_ID&lt;/span&gt; psql &lt;span class="nt"&gt;-U&lt;/span&gt; postgres &lt;span class="nt"&gt;-d&lt;/span&gt; postgres &lt;span class="nt"&gt;-v&lt;/span&gt; &lt;span class="nv"&gt;ON_ERROR_STOP&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;1

&lt;span class="c"&gt;# Execute SQL command via Docker&lt;/span&gt;
docker &lt;span class="nb"&gt;exec&lt;/span&gt; &lt;span class="nt"&gt;-i&lt;/span&gt; &lt;span class="nv"&gt;$DB_CONTAINER_ID&lt;/span&gt; psql &lt;span class="nt"&gt;-U&lt;/span&gt; postgres &lt;span class="nt"&gt;-d&lt;/span&gt; postgres &lt;span class="nt"&gt;-c&lt;/span&gt; &lt;span class="s2"&gt;"SELECT * FROM your_table;"&lt;/span&gt;

&lt;span class="c"&gt;# Dump database via Docker&lt;/span&gt;
docker &lt;span class="nb"&gt;exec&lt;/span&gt; &lt;span class="nv"&gt;$DB_CONTAINER_ID&lt;/span&gt; pg_dump &lt;span class="nt"&gt;-U&lt;/span&gt; postgres &lt;span class="nt"&gt;-d&lt;/span&gt; postgres &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; dump.sql

&lt;span class="c"&gt;# Interactive psql session via Docker&lt;/span&gt;
docker &lt;span class="nb"&gt;exec&lt;/span&gt; &lt;span class="nt"&gt;-it&lt;/span&gt; &lt;span class="nv"&gt;$DB_CONTAINER_ID&lt;/span&gt; psql &lt;span class="nt"&gt;-U&lt;/span&gt; postgres &lt;span class="nt"&gt;-d&lt;/span&gt; postgres
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Advanced Operations
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Dump specific tables only&lt;/span&gt;
npx supabase db dump &lt;span class="nt"&gt;--db-url&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$STAGING_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="nt"&gt;--table&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;users&lt;/span&gt; &lt;span class="nt"&gt;--table&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;posts &lt;span class="nt"&gt;-f&lt;/span&gt; specific_tables.sql

&lt;span class="c"&gt;# Exclude specific tables from dump (useful for sensitive data)&lt;/span&gt;
npx supabase db dump &lt;span class="nt"&gt;--db-url&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$STAGING_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="nt"&gt;--exclude-table&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;sensitive_data &lt;span class="nt"&gt;--exclude-table&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;secrets &lt;span class="nt"&gt;--data-only&lt;/span&gt; &lt;span class="nt"&gt;-f&lt;/span&gt; safe_data.sql

&lt;span class="c"&gt;# Dump with custom schema (not just public)&lt;/span&gt;
npx supabase db dump &lt;span class="nt"&gt;--db-url&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$STAGING_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="nt"&gt;--schema&lt;/span&gt; public &lt;span class="nt"&gt;--schema&lt;/span&gt; auth &lt;span class="nt"&gt;--schema&lt;/span&gt; storage &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; full_schema.sql

&lt;span class="c"&gt;# Create migration comparing two databases&lt;/span&gt;
npx supabase db diff &lt;span class="nt"&gt;--linked&lt;/span&gt; &lt;span class="nt"&gt;--schema&lt;/span&gt; public

&lt;span class="c"&gt;# View migration history in database&lt;/span&gt;
psql &lt;span class="s2"&gt;"postgresql://postgres:postgres@localhost:54322/postgres"&lt;/span&gt; &lt;span class="nt"&gt;-c&lt;/span&gt; &lt;span class="s2"&gt;"SELECT * FROM supabase_migrations.schema_migrations ORDER BY version;"&lt;/span&gt;

&lt;span class="c"&gt;# Manually add migration record (if migration was applied outside Supabase)&lt;/span&gt;
psql &lt;span class="s2"&gt;"postgresql://postgres:postgres@localhost:54322/postgres"&lt;/span&gt; &lt;span class="nt"&gt;-c&lt;/span&gt; &lt;span class="s2"&gt;"INSERT INTO supabase_migrations.schema_migrations (version) VALUES ('20251004123045');"&lt;/span&gt;

&lt;span class="c"&gt;# Delete specific migration record&lt;/span&gt;
psql &lt;span class="s2"&gt;"postgresql://postgres:postgres@localhost:54322/postgres"&lt;/span&gt; &lt;span class="nt"&gt;-c&lt;/span&gt; &lt;span class="s2"&gt;"DELETE FROM supabase_migrations.schema_migrations WHERE version = '20251004123045';"&lt;/span&gt;

&lt;span class="c"&gt;# Clear all migration records (⚠️ use with caution)&lt;/span&gt;
psql &lt;span class="s2"&gt;"postgresql://postgres:postgres@localhost:54322/postgres"&lt;/span&gt; &lt;span class="nt"&gt;-c&lt;/span&gt; &lt;span class="s2"&gt;"TRUNCATE supabase_migrations.schema_migrations;"&lt;/span&gt;

&lt;span class="c"&gt;# View all migration records&lt;/span&gt;
psql &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$STAGING_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="nt"&gt;-c&lt;/span&gt; &lt;span class="s2"&gt;"SELECT version, inserted_at FROM supabase_migrations.schema_migrations ORDER BY version DESC LIMIT 10;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Database Inspection Commands
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Test database connection&lt;/span&gt;
psql &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$STAGING_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="nt"&gt;-c&lt;/span&gt; &lt;span class="s2"&gt;"SELECT version();"&lt;/span&gt;

&lt;span class="c"&gt;# List all databases&lt;/span&gt;
psql &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$STAGING_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="nt"&gt;-c&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\l&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;

&lt;span class="c"&gt;# List all tables in public schema&lt;/span&gt;
psql &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$STAGING_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="nt"&gt;-c&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\d&lt;/span&gt;&lt;span class="s2"&gt;t public.*"&lt;/span&gt;

&lt;span class="c"&gt;# Describe table structure&lt;/span&gt;
psql &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$STAGING_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="nt"&gt;-c&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\d&lt;/span&gt;&lt;span class="s2"&gt; public.users"&lt;/span&gt;

&lt;span class="c"&gt;# Get database size&lt;/span&gt;
psql &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$STAGING_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="nt"&gt;-c&lt;/span&gt; &lt;span class="s2"&gt;"SELECT pg_size_pretty(pg_database_size('postgres'));"&lt;/span&gt;

&lt;span class="c"&gt;# Get table sizes (sorted by size)&lt;/span&gt;
psql &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$STAGING_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="nt"&gt;-c&lt;/span&gt; &lt;span class="s2"&gt;"
SELECT 
  schemaname, 
  tablename, 
  pg_size_pretty(pg_total_relation_size(schemaname||'.'||tablename)) AS size,
  pg_total_relation_size(schemaname||'.'||tablename) AS bytes
FROM pg_tables 
WHERE schemaname = 'public' 
ORDER BY pg_total_relation_size(schemaname||'.'||tablename) DESC;
"&lt;/span&gt;

&lt;span class="c"&gt;# Count rows in all tables&lt;/span&gt;
psql &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$STAGING_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="nt"&gt;-c&lt;/span&gt; &lt;span class="s2"&gt;"
SELECT 
  schemaname,
  tablename,
  n_live_tup AS row_count
FROM pg_stat_user_tables
WHERE schemaname = 'public'
ORDER BY n_live_tup DESC;
"&lt;/span&gt;

&lt;span class="c"&gt;# View active connections&lt;/span&gt;
psql &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$STAGING_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="nt"&gt;-c&lt;/span&gt; &lt;span class="s2"&gt;"
SELECT 
  datname, 
  usename, 
  client_addr, 
  state
FROM pg_stat_activity 
WHERE datname = 'postgres';
"&lt;/span&gt;

&lt;span class="c"&gt;# Check PostgreSQL version&lt;/span&gt;
psql &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$STAGING_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="nt"&gt;-c&lt;/span&gt; &lt;span class="s2"&gt;"SHOW server_version;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Utility Commands
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Check if required tools are installed&lt;/span&gt;
node &lt;span class="nt"&gt;--version&lt;/span&gt;
npm &lt;span class="nt"&gt;--version&lt;/span&gt;
npx &lt;span class="nt"&gt;--version&lt;/span&gt;
npx supabase &lt;span class="nt"&gt;--version&lt;/span&gt;
psql &lt;span class="nt"&gt;--version&lt;/span&gt;
docker &lt;span class="nt"&gt;--version&lt;/span&gt;

&lt;span class="c"&gt;# Set environment variables temporarily&lt;/span&gt;
&lt;span class="nb"&gt;export &lt;/span&gt;&lt;span class="nv"&gt;LOCAL_DB_URL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"postgresql://postgres:postgres@localhost:54322/postgres"&lt;/span&gt;
&lt;span class="nb"&gt;export &lt;/span&gt;&lt;span class="nv"&gt;STAGING_DB_URL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"postgresql://postgres:YOUR_PASSWORD@db.YOUR_STAGING_REF.supabase.co:5432/postgres"&lt;/span&gt;
&lt;span class="nb"&gt;export &lt;/span&gt;&lt;span class="nv"&gt;PROD_DB_URL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"postgresql://postgres.YOUR_PROD_REF:YOUR_PASSWORD@aws-1-ap-south-1.pooler.supabase.com:6543/postgres"&lt;/span&gt;

&lt;span class="c"&gt;# Display current environment variables&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Local: &lt;/span&gt;&lt;span class="nv"&gt;$LOCAL_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Staging: &lt;/span&gt;&lt;span class="nv"&gt;$STAGING_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Production: &lt;/span&gt;&lt;span class="nv"&gt;$PROD_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;

&lt;span class="c"&gt;# Clean up dump files&lt;/span&gt;
&lt;span class="nb"&gt;rm&lt;/span&gt; &lt;span class="nt"&gt;-f&lt;/span&gt; staging_schema.sql staging_data.sql production_schema.sql production_data.sql staging_migration_&lt;span class="k"&gt;*&lt;/span&gt;.sql

&lt;span class="c"&gt;# Create backup directory&lt;/span&gt;
&lt;span class="nb"&gt;mkdir&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; backups

&lt;span class="c"&gt;# Backup with timestamp&lt;/span&gt;
npx supabase db dump &lt;span class="nt"&gt;--db-url&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$PROD_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; backups/prod_backup_&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;date&lt;/span&gt; +%Y%m%d_%H%M%S&lt;span class="si"&gt;)&lt;/span&gt;.sql
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Complete Workflow Examples (No Makefile)
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Example 1: Create and Deploy a Migration&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# 1. Make changes locally (via Supabase Studio or direct SQL)&lt;/span&gt;
&lt;span class="c"&gt;# For example, create a table in Supabase Studio&lt;/span&gt;

&lt;span class="c"&gt;# 2. Create migration from your changes&lt;/span&gt;
npx supabase db diff &lt;span class="nt"&gt;-f&lt;/span&gt; add_user_profiles

&lt;span class="c"&gt;# 3. Review the generated migration file&lt;/span&gt;
&lt;span class="nb"&gt;cat &lt;/span&gt;supabase/migrations/&lt;span class="k"&gt;*&lt;/span&gt;_add_user_profiles.sql

&lt;span class="c"&gt;# 4. Apply to local (test it works)&lt;/span&gt;
npx supabase db push &lt;span class="nt"&gt;--local&lt;/span&gt;

&lt;span class="c"&gt;# 5. Verify it worked&lt;/span&gt;
psql &lt;span class="s2"&gt;"postgresql://postgres:postgres@localhost:54322/postgres"&lt;/span&gt; &lt;span class="nt"&gt;-c&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\d&lt;/span&gt;&lt;span class="s2"&gt; public.user_profiles"&lt;/span&gt;

&lt;span class="c"&gt;# 6. Apply to staging&lt;/span&gt;
&lt;span class="nb"&gt;export &lt;/span&gt;&lt;span class="nv"&gt;STAGING_DB_URL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"postgresql://postgres:YOUR_PASSWORD@db.YOUR_STAGING_REF.supabase.co:5432/postgres"&lt;/span&gt;
npx supabase db push &lt;span class="nt"&gt;--db-url&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$STAGING_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;

&lt;span class="c"&gt;# 7. Test in staging environment&lt;/span&gt;
&lt;span class="c"&gt;# Run your application tests&lt;/span&gt;

&lt;span class="c"&gt;# 8. Apply to production (after thorough testing)&lt;/span&gt;
&lt;span class="nb"&gt;export &lt;/span&gt;&lt;span class="nv"&gt;PROD_DB_URL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"postgresql://postgres.YOUR_PROD_REF:YOUR_PASSWORD@aws-1-ap-south-1.pooler.supabase.com:6543/postgres"&lt;/span&gt;
npx supabase db push &lt;span class="nt"&gt;--db-url&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$PROD_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;Example 2: Import Production Data Locally&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# 1. Set production URL&lt;/span&gt;
&lt;span class="nb"&gt;export &lt;/span&gt;&lt;span class="nv"&gt;PROD_DB_URL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"postgresql://postgres.YOUR_PROD_REF:YOUR_PASSWORD@aws-1-ap-south-1.pooler.supabase.com:6543/postgres"&lt;/span&gt;

&lt;span class="c"&gt;# 2. Reset local database&lt;/span&gt;
npx supabase db reset &lt;span class="nt"&gt;--local&lt;/span&gt;

&lt;span class="c"&gt;# 3. Dump production schema&lt;/span&gt;
npx supabase db dump &lt;span class="nt"&gt;--db-url&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$PROD_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="nt"&gt;--schema&lt;/span&gt; public &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; prod_schema.sql

&lt;span class="c"&gt;# 4. Dump production data&lt;/span&gt;
npx supabase db dump &lt;span class="nt"&gt;--db-url&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$PROD_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="nt"&gt;--data-only&lt;/span&gt; &lt;span class="nt"&gt;-f&lt;/span&gt; prod_data.sql

&lt;span class="c"&gt;# 5. Apply schema to local&lt;/span&gt;
psql &lt;span class="s2"&gt;"postgresql://postgres:postgres@localhost:54322/postgres"&lt;/span&gt; &amp;lt; prod_schema.sql

&lt;span class="c"&gt;# 6. Apply data to local&lt;/span&gt;
psql &lt;span class="s2"&gt;"postgresql://postgres:postgres@localhost:54322/postgres"&lt;/span&gt; &amp;lt; prod_data.sql

&lt;span class="c"&gt;# 7. Verify import&lt;/span&gt;
psql &lt;span class="s2"&gt;"postgresql://postgres:postgres@localhost:54322/postgres"&lt;/span&gt; &lt;span class="nt"&gt;-c&lt;/span&gt; &lt;span class="s2"&gt;"SELECT COUNT(*) FROM users;"&lt;/span&gt;

&lt;span class="c"&gt;# 8. Clean up dump files&lt;/span&gt;
&lt;span class="nb"&gt;rm &lt;/span&gt;prod_schema.sql prod_data.sql
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;Example 3: Sync Staging to Production&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# 1. Set environment variables&lt;/span&gt;
&lt;span class="nb"&gt;export &lt;/span&gt;&lt;span class="nv"&gt;STAGING_DB_URL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"postgresql://postgres:YOUR_PASSWORD@db.YOUR_STAGING_REF.supabase.co:5432/postgres"&lt;/span&gt;
&lt;span class="nb"&gt;export &lt;/span&gt;&lt;span class="nv"&gt;PROD_DB_URL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"postgresql://postgres.YOUR_PROD_REF:YOUR_PASSWORD@aws-1-ap-south-1.pooler.supabase.com:6543/postgres"&lt;/span&gt;

&lt;span class="c"&gt;# 2. Backup production first!&lt;/span&gt;
npx supabase db dump &lt;span class="nt"&gt;--db-url&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$PROD_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; prod_backup_&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;date&lt;/span&gt; +%Y%m%d_%H%M%S&lt;span class="si"&gt;)&lt;/span&gt;.sql

&lt;span class="c"&gt;# 3. Dump staging schema&lt;/span&gt;
npx supabase db dump &lt;span class="nt"&gt;--db-url&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$STAGING_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="nt"&gt;--schema&lt;/span&gt; public &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; staging_to_prod.sql

&lt;span class="c"&gt;# 4. Review the file (IMPORTANT!)&lt;/span&gt;
&lt;span class="nb"&gt;cat &lt;/span&gt;staging_to_prod.sql
&lt;span class="c"&gt;# or use a text editor&lt;/span&gt;
&lt;span class="c"&gt;# code staging_to_prod.sql&lt;/span&gt;

&lt;span class="c"&gt;# 5. Apply to production (⚠️ CRITICAL OPERATION)&lt;/span&gt;
psql &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$PROD_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &amp;lt; staging_to_prod.sql

&lt;span class="c"&gt;# 6. Verify production&lt;/span&gt;
psql &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$PROD_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="nt"&gt;-c&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\d&lt;/span&gt;&lt;span class="s2"&gt;t public.*"&lt;/span&gt;

&lt;span class="c"&gt;# 7. Clean up&lt;/span&gt;
&lt;span class="nb"&gt;rm &lt;/span&gt;staging_to_prod.sql
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;Example 4: Hotfix Production Issue&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# 1. Import production data locally to reproduce issue&lt;/span&gt;
&lt;span class="nb"&gt;export &lt;/span&gt;&lt;span class="nv"&gt;PROD_DB_URL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"postgresql://postgres.YOUR_PROD_REF:YOUR_PASSWORD@aws-1-ap-south-1.pooler.supabase.com:6543/postgres"&lt;/span&gt;
npx supabase db reset &lt;span class="nt"&gt;--local&lt;/span&gt;
npx supabase db dump &lt;span class="nt"&gt;--db-url&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$PROD_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="nt"&gt;--schema&lt;/span&gt; public &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; prod_schema.sql
npx supabase db dump &lt;span class="nt"&gt;--db-url&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$PROD_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="nt"&gt;--data-only&lt;/span&gt; &lt;span class="nt"&gt;-f&lt;/span&gt; prod_data.sql
psql &lt;span class="s2"&gt;"postgresql://postgres:postgres@localhost:54322/postgres"&lt;/span&gt; &amp;lt; prod_schema.sql
psql &lt;span class="s2"&gt;"postgresql://postgres:postgres@localhost:54322/postgres"&lt;/span&gt; &amp;lt; prod_data.sql

&lt;span class="c"&gt;# 2. Reproduce the bug locally&lt;/span&gt;

&lt;span class="c"&gt;# 3. Fix the issue (modify database via Studio or SQL)&lt;/span&gt;

&lt;span class="c"&gt;# 4. Create migration&lt;/span&gt;
npx supabase db diff &lt;span class="nt"&gt;-f&lt;/span&gt; hotfix_user_permissions

&lt;span class="c"&gt;# 5. Test the fix locally&lt;/span&gt;
npm run dev

&lt;span class="c"&gt;# 6. Review migration file&lt;/span&gt;
&lt;span class="nb"&gt;cat &lt;/span&gt;supabase/migrations/&lt;span class="k"&gt;*&lt;/span&gt;_hotfix_user_permissions.sql

&lt;span class="c"&gt;# 7. Apply to production (after approval)&lt;/span&gt;
npx supabase db push &lt;span class="nt"&gt;--db-url&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$PROD_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;

&lt;span class="c"&gt;# 8. Verify fix in production&lt;/span&gt;
psql &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$PROD_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="nt"&gt;-c&lt;/span&gt; &lt;span class="s2"&gt;"SELECT * FROM your_affected_table LIMIT 5;"&lt;/span&gt;

&lt;span class="c"&gt;# 9. Apply same fix to staging for consistency&lt;/span&gt;
&lt;span class="nb"&gt;export &lt;/span&gt;&lt;span class="nv"&gt;STAGING_DB_URL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"postgresql://postgres:YOUR_PASSWORD@db.YOUR_STAGING_REF.supabase.co:5432/postgres"&lt;/span&gt;
npx supabase db push &lt;span class="nt"&gt;--db-url&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$STAGING_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Environment Variables Setup
&lt;/h3&gt;

&lt;p&gt;For easier command usage, create a &lt;code&gt;.env&lt;/code&gt; file or add to your shell profile:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Option 1: Using .env file&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Create &lt;code&gt;.env&lt;/code&gt; in your project root:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Local Environment&lt;/span&gt;
&lt;span class="nv"&gt;LOCAL_DB_URL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;postgresql://postgres:postgres@localhost:54322/postgres

&lt;span class="c"&gt;# Staging Environment&lt;/span&gt;
&lt;span class="nv"&gt;STAGING_DB_URL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;postgresql://postgres:YOUR_PASSWORD@db.YOUR_STAGING_REF.supabase.co:5432/postgres
&lt;span class="nv"&gt;STAGING_PROJECT_REF&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;your-staging-ref

&lt;span class="c"&gt;# Production Environment&lt;/span&gt;
&lt;span class="nv"&gt;PROD_DB_URL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;postgresql://postgres.YOUR_PROD_REF:YOUR_PASSWORD@aws-1-ap-south-1.pooler.supabase.com:6543/postgres
&lt;span class="nv"&gt;PROD_PROJECT_REF&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;your-production-ref
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then load it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Load .env file&lt;/span&gt;
&lt;span class="nb"&gt;export&lt;/span&gt; &lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;cat&lt;/span&gt; .env | xargs&lt;span class="si"&gt;)&lt;/span&gt;

&lt;span class="c"&gt;# Or use in a script&lt;/span&gt;
&lt;span class="nb"&gt;source&lt;/span&gt; .env
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Option 2: Add to shell profile&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Add to &lt;code&gt;~/.bashrc&lt;/code&gt; or &lt;code&gt;~/.zshrc&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Supabase Database URLs&lt;/span&gt;
&lt;span class="nb"&gt;export &lt;/span&gt;&lt;span class="nv"&gt;LOCAL_DB_URL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"postgresql://postgres:postgres@localhost:54322/postgres"&lt;/span&gt;
&lt;span class="nb"&gt;export &lt;/span&gt;&lt;span class="nv"&gt;STAGING_DB_URL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"postgresql://postgres:YOUR_PASSWORD@db.YOUR_STAGING_REF.supabase.co:5432/postgres"&lt;/span&gt;
&lt;span class="nb"&gt;export &lt;/span&gt;&lt;span class="nv"&gt;PROD_DB_URL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"postgresql://postgres.YOUR_PROD_REF:YOUR_PASSWORD@aws-1-ap-south-1.pooler.supabase.com:6543/postgres"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Reload your shell:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;source&lt;/span&gt; ~/.bashrc
&lt;span class="c"&gt;# or&lt;/span&gt;
&lt;span class="nb"&gt;source&lt;/span&gt; ~/.zshrc
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Tips for Direct Commands
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Always use full DB URLs&lt;/strong&gt; - Don't rely on shortcuts or aliases&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Backup before destructive operations&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   npx supabase db dump &lt;span class="nt"&gt;--db-url&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$PROD_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; backup_&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;date&lt;/span&gt; +%Y%m%d&lt;span class="si"&gt;)&lt;/span&gt;.sql
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Use transactions for multiple changes&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;   &lt;span class="k"&gt;BEGIN&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="c1"&gt;-- Your changes here&lt;/span&gt;
   &lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="k"&gt;ADD&lt;/span&gt; &lt;span class="k"&gt;COLUMN&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="nb"&gt;INTEGER&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="k"&gt;COMMIT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="c1"&gt;-- Or ROLLBACK; if something goes wrong&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Check migration history before applying&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   psql &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="nt"&gt;-c&lt;/span&gt; &lt;span class="s2"&gt;"SELECT * FROM supabase_migrations.schema_migrations ORDER BY version DESC LIMIT 5;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Review migration files before applying&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   &lt;span class="nb"&gt;cat &lt;/span&gt;supabase/migrations/20251004_your_migration.sql
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Use error handling&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   psql &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="nt"&gt;-v&lt;/span&gt; &lt;span class="nv"&gt;ON_ERROR_STOP&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;1 &amp;lt; your_migration.sql
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Test in local first, then staging, then production&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   &lt;span class="c"&gt;# Local&lt;/span&gt;
   npx supabase db push &lt;span class="nt"&gt;--local&lt;/span&gt;

   &lt;span class="c"&gt;# Staging&lt;/span&gt;
   npx supabase db push &lt;span class="nt"&gt;--db-url&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$STAGING_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;

   &lt;span class="c"&gt;# Production (after testing)&lt;/span&gt;
   npx supabase db push &lt;span class="nt"&gt;--db-url&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$PROD_DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Create aliases for common commands&lt;/strong&gt; (add to shell profile):
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   &lt;span class="nb"&gt;alias &lt;/span&gt;sp-local&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"npx supabase db push --local"&lt;/span&gt;
   &lt;span class="nb"&gt;alias &lt;/span&gt;sp-staging&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"npx supabase db push --db-url &lt;/span&gt;&lt;span class="se"&gt;\$&lt;/span&gt;&lt;span class="s2"&gt;STAGING_DB_URL"&lt;/span&gt;
   &lt;span class="nb"&gt;alias &lt;/span&gt;sp-prod&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"npx supabase db push --db-url &lt;/span&gt;&lt;span class="se"&gt;\$&lt;/span&gt;&lt;span class="s2"&gt;PROD_DB_URL"&lt;/span&gt;
   &lt;span class="nb"&gt;alias &lt;/span&gt;sp-diff&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"npx supabase db diff -f"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Quick Command Cheatsheet
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Task&lt;/th&gt;
&lt;th&gt;Command&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Start Supabase&lt;/td&gt;
&lt;td&gt;&lt;code&gt;npx supabase start&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Stop Supabase&lt;/td&gt;
&lt;td&gt;&lt;code&gt;npx supabase stop&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Check status&lt;/td&gt;
&lt;td&gt;&lt;code&gt;npx supabase status&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Login&lt;/td&gt;
&lt;td&gt;&lt;code&gt;npx supabase login&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Reset local&lt;/td&gt;
&lt;td&gt;&lt;code&gt;npx supabase db reset --local&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Create migration&lt;/td&gt;
&lt;td&gt;&lt;code&gt;npx supabase db diff -f name&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Apply locally&lt;/td&gt;
&lt;td&gt;&lt;code&gt;npx supabase db push --local&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Apply to staging&lt;/td&gt;
&lt;td&gt;&lt;code&gt;npx supabase db push --db-url "$STAGING_DB_URL"&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Apply to prod&lt;/td&gt;
&lt;td&gt;&lt;code&gt;npx supabase db push --db-url "$PROD_DB_URL"&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Dump schema&lt;/td&gt;
&lt;td&gt;&lt;code&gt;npx supabase db dump --db-url "$URL" --schema public &amp;gt; file.sql&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Dump data&lt;/td&gt;
&lt;td&gt;&lt;code&gt;npx supabase db dump --db-url "$URL" --data-only -f file.sql&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Apply SQL&lt;/td&gt;
&lt;td&gt;&lt;code&gt;psql "$DB_URL" &amp;lt; file.sql&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Execute SQL&lt;/td&gt;
&lt;td&gt;&lt;code&gt;psql "$DB_URL" -c "SQL"&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;View migrations&lt;/td&gt;
&lt;td&gt;&lt;code&gt;psql "$DB_URL" -c "SELECT * FROM supabase_migrations.schema_migrations;"&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;--- Manually add migration record&lt;br&gt;
psql "postgresql://postgres:postgres@localhost:54322/postgres" -c "INSERT INTO supabase_migrations.schema_migrations (version) VALUES ('20251004123045');"&lt;/p&gt;
&lt;h1&gt;
  
  
  Delete migration record
&lt;/h1&gt;

&lt;p&gt;psql "postgresql://postgres:postgres@localhost:54322/postgres" -c "DELETE FROM supabase_migrations.schema_migrations WHERE version = '20251004123045';"&lt;/p&gt;
&lt;h1&gt;
  
  
  Clear all migration records
&lt;/h1&gt;

&lt;p&gt;psql "postgresql://postgres:postgres@localhost:54322/postgres" -c "TRUNCATE supabase_migrations.schema_migrations;"&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
### Utility Commands

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
bash&lt;/p&gt;
&lt;h1&gt;
  
  
  Check PostgreSQL version
&lt;/h1&gt;

&lt;p&gt;psql --version&lt;/p&gt;
&lt;h1&gt;
  
  
  Test database connection
&lt;/h1&gt;

&lt;p&gt;psql "$STAGING_DB_URL" -c "SELECT version();"&lt;/p&gt;
&lt;h1&gt;
  
  
  List all databases
&lt;/h1&gt;

&lt;p&gt;psql "$STAGING_DB_URL" -c "\l"&lt;/p&gt;
&lt;h1&gt;
  
  
  List all tables in public schema
&lt;/h1&gt;

&lt;p&gt;psql "$STAGING_DB_URL" -c "\dt public.*"&lt;/p&gt;
&lt;h1&gt;
  
  
  Describe table structure
&lt;/h1&gt;

&lt;p&gt;psql "$STAGING_DB_URL" -c "\d public.your_table"&lt;/p&gt;
&lt;h1&gt;
  
  
  Get database size
&lt;/h1&gt;

&lt;p&gt;psql "$STAGING_DB_URL" -c "SELECT pg_size_pretty(pg_database_size('postgres'));"&lt;/p&gt;
&lt;h1&gt;
  
  
  Get table sizes
&lt;/h1&gt;

&lt;p&gt;psql "$STAGING_DB_URL" -c "SELECT schemaname, tablename, pg_size_pretty(pg_total_relation_size(schemaname||'.'||tablename)) AS size FROM pg_tables WHERE schemaname = 'public' ORDER BY pg_total_relation_size(schemaname||'.'||tablename) DESC;"&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
### Environment Variables for Scripts

Create a `.env` file or export these variables:

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
bash&lt;/p&gt;
&lt;h1&gt;
  
  
  Local
&lt;/h1&gt;

&lt;p&gt;export LOCAL_DB_URL="postgresql://postgres:postgres@localhost:54322/postgres"&lt;/p&gt;
&lt;h1&gt;
  
  
  Staging
&lt;/h1&gt;

&lt;p&gt;export STAGING_DB_URL="postgresql://postgres:&lt;a href="mailto:YOUR_PASSWORD@db.YOUR_STAGING_REF.supabase.co"&gt;YOUR_PASSWORD@db.YOUR_STAGING_REF.supabase.co&lt;/a&gt;:5432/postgres"&lt;br&gt;
export STAGING_PROJECT_REF="your-staging-ref"&lt;/p&gt;
&lt;h1&gt;
  
  
  Production
&lt;/h1&gt;

&lt;p&gt;export PROD_DB_URL="postgresql://postgres.YOUR_PROD_REF:&lt;a href="mailto:YOUR_PASSWORD@aws-1-ap-south-1.pooler.supabase.com"&gt;YOUR_PASSWORD@aws-1-ap-south-1.pooler.supabase.com&lt;/a&gt;:6543/postgres"&lt;br&gt;
export PROD_PROJECT_REF="your-production-ref"&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
### Quick Command Reference

| Operation | Command |
|-----------|---------|
| Start local Supabase | `npx supabase start` |
| Stop local Supabase | `npx supabase stop` |
| Check status | `npx supabase status` |
| Reset local DB | `npx supabase db reset --local` |
| Create migration | `npx supabase db diff -f name` |
| Apply migrations locally | `npx supabase db push --local` |
| Apply to staging | `npx supabase db push --db-url "$STAGING_DB_URL"` |
| Apply to production | `npx supabase db push --db-url "$PROD_DB_URL"` |
| Dump schema | `npx supabase db dump --db-url "$URL" --schema public &amp;gt; file.sql` |
| Dump data | `npx supabase db dump --db-url "$URL" --data-only -f file.sql` |
| Apply SQL file | `psql "$DB_URL" &amp;lt; file.sql` |
| Execute SQL | `psql "$DB_URL" -c "SQL COMMAND"` |

### Complete Workflow Examples (No Makefile)

**Example 1: Create and Deploy a Migration**

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
bash&lt;/p&gt;
&lt;h1&gt;
  
  
  1. Make changes locally (via Supabase Studio or SQL)
&lt;/h1&gt;
&lt;h1&gt;
  
  
  2. Create migration
&lt;/h1&gt;

&lt;p&gt;npx supabase db diff -f add_user_profiles&lt;/p&gt;
&lt;h1&gt;
  
  
  3. Apply to local (test)
&lt;/h1&gt;

&lt;p&gt;npx supabase db push --local&lt;/p&gt;
&lt;h1&gt;
  
  
  4. Apply to staging
&lt;/h1&gt;

&lt;p&gt;npx supabase db push --db-url "$STAGING_DB_URL"&lt;/p&gt;
&lt;h1&gt;
  
  
  5. Apply to production (after testing)
&lt;/h1&gt;

&lt;p&gt;npx supabase db push --db-url "$PROD_DB_URL"&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
**Example 2: Import Production Data Locally**

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
bash&lt;/p&gt;
&lt;h1&gt;
  
  
  1. Reset local database
&lt;/h1&gt;

&lt;p&gt;npx supabase db reset --local&lt;/p&gt;
&lt;h1&gt;
  
  
  2. Dump production
&lt;/h1&gt;

&lt;p&gt;npx supabase db dump --db-url "$PROD_DB_URL" --schema public &amp;gt; prod_schema.sql&lt;br&gt;
npx supabase db dump --db-url "$PROD_DB_URL" --data-only -f prod_data.sql&lt;/p&gt;
&lt;h1&gt;
  
  
  3. Apply to local
&lt;/h1&gt;

&lt;p&gt;psql "postgresql://postgres:postgres@localhost:54322/postgres" &amp;lt; prod_schema.sql&lt;br&gt;
psql "postgresql://postgres:postgres@localhost:54322/postgres" &amp;lt; prod_data.sql&lt;/p&gt;
&lt;h1&gt;
  
  
  4. Verify
&lt;/h1&gt;

&lt;p&gt;psql "postgresql://postgres:postgres@localhost:54322/postgres" -c "SELECT COUNT(*) FROM your_table;"&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
**Example 3: Sync Staging to Production**

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
bash&lt;/p&gt;
&lt;h1&gt;
  
  
  1. Dump staging schema
&lt;/h1&gt;

&lt;p&gt;npx supabase db dump --db-url "$STAGING_DB_URL" --schema public &amp;gt; staging_to_prod.sql&lt;/p&gt;
&lt;h1&gt;
  
  
  2. Review the file
&lt;/h1&gt;

&lt;p&gt;cat staging_to_prod.sql&lt;/p&gt;
&lt;h1&gt;
  
  
  3. Backup production first
&lt;/h1&gt;

&lt;p&gt;npx supabase db dump --db-url "$PROD_DB_URL" --schema public &amp;gt; prod_backup_$(date +%Y%m%d).sql&lt;/p&gt;
&lt;h1&gt;
  
  
  4. Apply to production
&lt;/h1&gt;

&lt;p&gt;psql "$PROD_DB_URL" &amp;lt; staging_to_prod.sql&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
### Tips for Direct Commands

1. **Always use full DB URLs** - Don't rely on shortcuts
2. **Test with --dry-run** when available
3. **Backup before destructive operations**
4. **Use transactions** for multiple changes:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
sql&lt;br&gt;
   BEGIN;&lt;br&gt;
   -- Your changes here&lt;br&gt;
   COMMIT;&lt;br&gt;
   -- Or ROLLBACK; if something goes wrong&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;5. **Check migration history** before applying:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
bash&lt;br&gt;
   psql "$DB_URL" -c "SELECT * FROM supabase_migrations.schema_migrations;"&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
---

## Conclusion

This guide provides two powerful approaches to managing Supabase databases across multiple environments:

### Choose Your Approach

**Use Makefile if you:**
- ✅ Want simplified, memorable commands
- ✅ Need built-in safety confirmations
- ✅ Prefer automation over manual control
- ✅ Work in a team with consistent workflows

**Use Direct Commands if you:**
- ✅ Don't have access to setup files
- ✅ Need maximum flexibility
- ✅ Integrate with CI/CD pipelines
- ✅ Prefer explicit control over each operation

### Key Benefits

**Safety First**: 
- Multiple confirmation levels prevent accidents (with Makefile)
- Explicit commands reduce ambiguity (with direct commands)

**Automation**: 
- Reduces manual errors and saves time
- Consistent workflows across team members

**Clarity**: 
- Clear command names and purposes
- Easy to understand what each operation does

**Flexibility**: 
- Supports complex migration scenarios
- Adaptable to different workflows

### Best Practices Summary

1. **Always Test Locally First**
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
bash&lt;br&gt;
   # Test here first&lt;br&gt;
   npx supabase db push --local&lt;/p&gt;

&lt;p&gt;# Then staging&lt;br&gt;
   npx supabase db push --db-url "$STAGING_DB_URL"&lt;/p&gt;

&lt;p&gt;# Finally production&lt;br&gt;
   npx supabase db push --db-url "$PROD_DB_URL"&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
2. **Backup Before Major Changes**
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
bash&lt;br&gt;
   npx supabase db dump --db-url "$PROD_DB_URL" &amp;gt; backup_$(date +%Y%m%d).sql&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
3. **Review Migrations Before Applying**
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
bash&lt;br&gt;
   cat supabase/migrations/20251004_your_migration.sql&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
4. **Version Control Everything**
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
bash&lt;br&gt;
   git add supabase/migrations/&lt;br&gt;
   git commit -m "feat: add user profiles table"&lt;br&gt;
   git push&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
5. **Document Database Changes**
   - Use descriptive migration names
   - Add comments in migration files
   - Update documentation with schema changes

6. **Use Transactions for Complex Changes**
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
sql&lt;br&gt;
   BEGIN;&lt;br&gt;
   -- Multiple operations&lt;br&gt;
   ALTER TABLE users ADD COLUMN status TEXT;&lt;br&gt;
   UPDATE users SET status = 'active';&lt;br&gt;
   COMMIT;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
7. **Monitor Migration Status**
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
bash&lt;br&gt;
   psql "$DB_URL" -c "SELECT * FROM supabase_migrations.schema_migrations ORDER BY version DESC LIMIT 10;"&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
### Common Gotchas to Avoid

❌ **Don't skip environments** - Always test in local → staging → production order

❌ **Don't bypass confirmations** - They exist to prevent disasters

❌ **Don't forget backups** - Especially before production changes

❌ **Don't commit sensitive data** - Never commit `.env` files or connection strings

❌ **Don't apply untested migrations** - Review and test thoroughly

❌ **Don't ignore migration conflicts** - Resolve them properly

❌ **Don't modify existing migrations** - Create new ones instead

### Troubleshooting Quick Reference

| Problem | Solution |
|---------|----------|
| Permission denied on scripts | `chmod +x scripts/*.sh` |
| psql not found | Install PostgreSQL client tools |
| Connection refused | Check if Supabase is running: `npx supabase status` |
| Docker container not found | Ensure Docker Desktop is running |
| Migration conflicts | Reset and reimport: `npx supabase db reset --local` |
| .env not loading | Check file exists and variable names match |
| Slow migrations | Check database size and indexes |
| Authentication failed | Run `npx supabase login` |

### Next Steps

1. **Set up your environment**
   - Install required tools
   - Configure `.env` file (or export variables)
   - Initialize Supabase locally

2. **Choose your approach**
   - With Makefile: Set up project structure and scripts
   - Direct commands: Bookmark this guide for reference

3. **Start with local operations**
   - Practice creating and applying migrations
   - Get comfortable with the workflow

4. **Gradually adopt staging workflow**
   - Test deployments in staging first
   - Build confidence with the process

5. **Implement production workflow**
   - Always backup before changes
   - Follow the testing pyramid: local → staging → production

6. **Customize for your needs**
   - Adapt workflows to your team's requirements
   - Add custom scripts or aliases
   - Integrate with CI/CD pipelines

### Additional Resources

- **[Supabase CLI Documentation](https://supabase.com/docs/guides/cli)** - Official CLI reference
- **[PostgreSQL Documentation](https://www.postgresql.org/docs/)** - Database fundamentals
- **[Supabase Migration Guide](https://supabase.com/docs/guides/cli/local-development#database-migrations)** - Official migration docs
- **[Database Migration Best Practices](https://supabase.com/blog/database-migrations)** - Supabase blog
- **[GNU Make Manual](https://www.gnu.org/software/make/manual/)** - Makefile reference

### Getting Help

If you encounter issues:

1. **Check Supabase logs**:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
bash&lt;br&gt;
   npx supabase logs&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
2. **Verify connection**:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
bash&lt;br&gt;
   psql "$DB_URL" -c "SELECT version();"&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
3. **Check migration status**:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
bash&lt;br&gt;
   psql "$DB_URL" -c "SELECT * FROM supabase_migrations.schema_migrations;"&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
4. **Review migration files**:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
bash&lt;br&gt;
   ls -la supabase/migrations/&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;


5. **Consult Supabase support**:
   - Discord: [https://discord.supabase.com](https://discord.supabase.com)
   - GitHub Discussions: [https://github.com/supabase/supabase/discussions](https://github.com/supabase/supabase/discussions)
   - Documentation: [https://supabase.com/docs](https://supabase.com/docs)

---

## Final Thoughts

Database migrations don't have to be scary. With the right tools and workflows, you can safely manage changes across multiple environments with confidence.

**Remember the golden rules:**

1. 🧪 **Test thoroughly** - Local → Staging → Production
2. 💾 **Backup always** - Before any production change
3. 📝 **Document everything** - Future you will thank you
4. 🔒 **Review carefully** - Double-check before applying
5. 🚀 **Deploy confidently** - With proper testing and backups

Whether you use the Makefile approach for convenience or direct commands for flexibility, the most important thing is consistency and safety in your workflow.

**With great power comes great responsibility. Always verify which environment you're working with, especially when running production commands!** 🚀

---

*Happy migrating! Have questions or suggestions? Feel free to adapt this workflow to your specific needs!*

---

## Appendix: Command Comparison Table

### Side-by-Side Comparison

| Operation | Makefile | Direct Command |
|-----------|----------|----------------|
| **Import Staging** | `make local-import-staging` | `npx supabase db reset --local &amp;amp;&amp;amp; npx supabase db dump --db-url "$STAGING_DB_URL" --schema public &amp;gt; staging_schema.sql &amp;amp;&amp;amp; npx supabase db dump --db-url "$STAGING_DB_URL" --data-only -f staging_data.sql &amp;amp;&amp;amp; psql "postgresql://postgres:postgres@localhost:54322/postgres" &amp;lt; staging_schema.sql &amp;amp;&amp;amp; psql "postgresql://postgres:postgres@localhost:54322/postgres" &amp;lt; staging_data.sql` |
| **Create Migration** | `make local-create-migration` | `npx supabase db diff -f migration_name` |
| **Apply Locally** | `make local-apply-migration` | `npx supabase db push --local` |
| **Apply to Staging** | `make staging-apply-migration` | `npx supabase db push --db-url "$STAGING_DB_URL"` |
| **Apply to Production** | `make prod-apply-migration` | `npx supabase db push --db-url "$PROD_DB_URL"` |
| **Reset Local** | `make local-reset` | `npx supabase db reset --local` |

### Pros and Cons

| Aspect | Makefile | Direct Commands |
|--------|----------|-----------------|
| **Ease of Use** | ✅ Very simple | ⚠️ More complex |
| **Setup Required** | ⚠️ Yes (files needed) | ✅ No setup |
| **Safety** | ✅ Built-in confirmations | ⚠️ Manual checks |
| **Flexibility** | ⚠️ Fixed workflows | ✅ Full control |
| **CI/CD Integration** | ⚠️ Requires files | ✅ Easy integration |
| **Learning Curve** | ✅ Low | ⚠️ Medium |
| **Customization** | ⚠️ Requires editing files | ✅ Highly customizable |
| **Team Consistency** | ✅ Enforced | ⚠️ Manual |

---

**That's it! You're now equipped with everything you need to manage Supabase migrations like a pro!** 🎉
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>postgres</category>
      <category>database</category>
      <category>tooling</category>
      <category>devops</category>
    </item>
    <item>
      <title>dropdown open in portal for using table in react</title>
      <dc:creator>poshiya parth s</dc:creator>
      <pubDate>Tue, 09 Jul 2024 05:42:53 +0000</pubDate>
      <link>https://forem.com/parth24072001/dropdown-open-in-portal-for-using-table-in-react-2odd</link>
      <guid>https://forem.com/parth24072001/dropdown-open-in-portal-for-using-table-in-react-2odd</guid>
      <description>&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState, useRef, useEffect } from "react";
import ReactDOM from "react-dom";

const PortalWrapper = React.forwardRef(({ children }, ref) =&amp;gt; {
  const [openDropdownMenu, setOpenDropdownMenu] = useState(false);
  const [position, setPosition] = useState({
    left: 0,
    top: 0,
    width: "100%",
  });

  const dropdownRef = useRef(null);

  const handleDropdDownClick = () =&amp;gt; {
    setOpenDropdownMenu(!openDropdownMenu);
    if (ref &amp;amp;&amp;amp; "current" in ref &amp;amp;&amp;amp; ref.current) {
      const { top, left, height } = ref.current.getBoundingClientRect();
      setPosition({
        top: top + height,
        left,
        width: `${ref.current.clientWidth}px`,
      });
    }
  };

  useEffect(() =&amp;gt; {
    if (openDropdownMenu &amp;amp;&amp;amp; dropdownRef.current) {
      dropdownRef.current.style.top = `${position.top}px`;
      dropdownRef.current.style.left = `${position.left}px`;
      dropdownRef.current.style.display = "none";
    }
  }, [openDropdownMenu, position]);

  return (
    &amp;lt;div ref={ref} className="w-full" onClick={handleDropdDownClick}&amp;gt;
      {children(position)}
      {openDropdownMenu &amp;amp;&amp;amp;
        ReactDOM.createPortal(
          &amp;lt;div ref={dropdownRef} style={{ position: "absolute" }}&amp;gt;
            {children(position)}
          &amp;lt;/div&amp;gt;,
          document.body
        )}
    &amp;lt;/div&amp;gt;
  );
});
export default PortalWrapper;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; &amp;lt;PortalWrapper ref={dropdownRef}&amp;gt;
                      {() =&amp;gt;
                        (itIsOwner(user?.agent_role) ||
                          user?.platform_owner) &amp;amp;&amp;amp; (
                          &amp;lt;Select
                            styles={CustomStyle}
                            onChange={(selectedOption) =&amp;gt;
                              handleActionChange(
                                selectedOption,
                                contactInfo,
                                index
                              )
                            }
                            options={agentData?.map((option) =&amp;gt; ({
                              value: option,
                              label: `${option.first_name} `,
                            }))}
                            placeholder="Esther Howard"
                            className={clsx(
                              "dropdown_list !w-40 cursor-pointer userFilter mt-[0.4375rem]"
                            )}
                            classNamePrefix="dropdown_list-contacts"
                            menuPosition="fixed"
                            menuPlacement="auto"
                            menuShouldScrollIntoView={false}
                            menuPortalTarget={document.body}
                          /&amp;gt;
                        )
                      }
                    &amp;lt;/PortalWrapper&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>react</category>
      <category>dropdown</category>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Using React Portals to Render Elements Anywhere in the DOM</title>
      <dc:creator>poshiya parth s</dc:creator>
      <pubDate>Tue, 09 Jul 2024 05:42:41 +0000</pubDate>
      <link>https://forem.com/parth24072001/using-react-portals-to-render-elements-anywhere-in-the-dom-1h9h</link>
      <guid>https://forem.com/parth24072001/using-react-portals-to-render-elements-anywhere-in-the-dom-1h9h</guid>
      <description>&lt;p&gt;&lt;strong&gt;Introduction&lt;/strong&gt;&lt;br&gt;
React Portals provide a powerful way to render components outside the main DOM hierarchy, allowing for greater flexibility in placing elements. This is particularly useful for creating modals, tooltips, and other components that need to be rendered at a different location in the DOM. In this guide, we will demonstrate how to use React Portals to open and render elements anywhere in your application.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prerequisites&lt;/strong&gt;&lt;br&gt;
Before we begin, ensure you have a basic understanding of React and functional components. Familiarity with hooks (useState, useEffect) will be beneficial.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1: Setting Up the Project&lt;/strong&gt;&lt;br&gt;
First, create a new React project or navigate to your existing project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx create-react-app react-portal-tutorial
cd react-portal-tutorial
npm start

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 2: Creating the Portal Component&lt;/strong&gt;&lt;br&gt;
Create a new file PortalComponent.js and add the following code to define a reusable portal component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react';
import ReactDOM from 'react-dom';

const PortalComponent = ({ children, containerId }) =&amp;gt; {
  const container = document.getElementById(containerId);
  return container ? ReactDOM.createPortal(children, container) : null;
};

export default PortalComponent;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 3: Adding the Target Element in the DOM&lt;/strong&gt;&lt;br&gt;
Ensure you have an element in your index.html or another component where the portal can render its content. For instance, add this div in your public/index.html:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;body&amp;gt;
  &amp;lt;div id="root"&amp;gt;&amp;lt;/div&amp;gt;
  &amp;lt;div id="header-children"&amp;gt;&amp;lt;/div&amp;gt; &amp;lt;!-- Target Element for Portal --&amp;gt;
&amp;lt;/body&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 4: Implementing the Portal in Your Component&lt;/strong&gt;&lt;br&gt;
Create a component, App.js, and use the PortalComponent to render content inside the target element (header-children):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState } from 'react';
import PortalComponent from './PortalComponent';

const App = () =&amp;gt; {
  const [isSearch, setIsSearch] = useState(false);
  const [isDelete, setIsDelete] = useState(false);

  const handleCancel = () =&amp;gt; {
    setIsSearch(false);
    setIsDelete(false);
  };

  const onDeleteClick = () =&amp;gt; setIsDelete(true);
  const onSearchClick = () =&amp;gt; setIsSearch(true);

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h1&amp;gt;React Portal Example&amp;lt;/h1&amp;gt;
      &amp;lt;button onClick={onSearchClick}&amp;gt;Search&amp;lt;/button&amp;gt;
      &amp;lt;button onClick={onDeleteClick}&amp;gt;Delete&amp;lt;/button&amp;gt;
      &amp;lt;div id="header-children"&amp;gt;&amp;lt;/div&amp;gt;
      &amp;lt;PortalComponent containerId="header-children"&amp;gt;
        &amp;lt;div className="gap-5 flex"&amp;gt;
          {isSearch || isDelete ? (
            &amp;lt;button
              className="text-xl leading-8 font-semibold text-caribbeangreen"
              onClick={handleCancel}
            &amp;gt;
              Cancel
            &amp;lt;/button&amp;gt;
          ) : (
            &amp;lt;HistoryActions
              history={history}
              onDeleteClick={onDeleteClick}
              onSearchClick={onSearchClick}
            /&amp;gt;
          )}
        &amp;lt;/div&amp;gt;
      &amp;lt;/PortalComponent&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

const HistoryActions = ({ history, onDeleteClick, onSearchClick }) =&amp;gt; (
  &amp;lt;div&amp;gt;
    &amp;lt;button onClick={onSearchClick}&amp;gt;Search History&amp;lt;/button&amp;gt;
    &amp;lt;button onClick={onDeleteClick}&amp;gt;Delete History&amp;lt;/button&amp;gt;
  &amp;lt;/div&amp;gt;
);

export default App;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 5: Styling the Portal Content&lt;/strong&gt;&lt;br&gt;
Add necessary styles in your index.css or another CSS file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.gap-5 {
  gap: 1.25rem;
}

.flex {
  display: flex;
}

.text-caribbeangreen {
  color: #00FF7F;
}

.text-xl {
  font-size: 1.25rem;
}

.leading-8 {
  line-height: 2rem;
}

.font-semibold {
  font-weight: 600;
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
In this tutorial, we've covered how to use React Portals to render components outside the main DOM hierarchy. By following these steps, you can create flexible and powerful UI elements that can be rendered anywhere in your application. This technique is especially useful for components like modals, tooltips, and context menus.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;first file&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const headerChildrenRenderer = document.getElementById("header-children");

  {createPortal(
          &amp;lt;div className="gap-5 flex"&amp;gt;
            {isSearch || isDelete ? (
              &amp;lt;button
                className="text-xl leading-8 font-semibold text-caribbeangreen"
                onClick={handleCancel}
              &amp;gt;
                Cancel
              &amp;lt;/button&amp;gt;
            ) : (
              &amp;lt;HistroyActions
                history={history}
                onDeleteClick={onDeleteClick}
                onSearchClick={onSearchClick}
              /&amp;gt;
            )}
          &amp;lt;/div&amp;gt;,
          headerChildrenRenderer as HTMLElement
        )}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;second file&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// ref for when we want to open above element
        &amp;lt;div id='header-children'&amp;gt;&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>Creating Modals in React: A Comprehensive Guide to Opening Elements in Modals</title>
      <dc:creator>poshiya parth s</dc:creator>
      <pubDate>Tue, 09 Jul 2024 05:42:32 +0000</pubDate>
      <link>https://forem.com/parth24072001/creating-modals-in-react-a-comprehensive-guide-to-opening-elements-in-modals-49j1</link>
      <guid>https://forem.com/parth24072001/creating-modals-in-react-a-comprehensive-guide-to-opening-elements-in-modals-49j1</guid>
      <description>&lt;p&gt;&lt;strong&gt;Introduction&lt;/strong&gt;&lt;br&gt;
Modals are an essential part of web applications, providing a way to display content over the current page without navigating away. This guide will walk you through the steps to create a modal in React, including how to use the React Portal for rendering modals and managing their visibility with state. By the end of this tutorial, you'll be able to implement modals that can open various elements dynamically.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prerequisites&lt;/strong&gt;&lt;br&gt;
Before we dive into the code, make sure you have a basic understanding of React and functional components. Familiarity with hooks (useState, useEffect) will be beneficial.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1: Setting Up the Project&lt;/strong&gt;&lt;br&gt;
First, create a new React project or navigate to your existing project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx create-react-app react-modal-tutorial
cd react-modal-tutorial
npm start

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 2: Creating the Modal Component&lt;/strong&gt;&lt;br&gt;
Create a new file ModalPortal.js and add the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useEffect } from 'react';
import ReactDOM from 'react-dom';

const ModalPortal = ({ open, children }) =&amp;gt; {
  useEffect(() =&amp;gt; {
    const rootElement = document.body;
    if (open) {
      rootElement.classList.add('overlay');
    } else {
      rootElement.classList.remove('overlay');
    }
  }, [open]);

  if (!open) return null;

  return ReactDOM.createPortal(
    &amp;lt;div className="modalContent"&amp;gt;{children}&amp;lt;/div&amp;gt;,
    document.body
  );
};

export default ModalPortal;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 3: Styling the Modal&lt;/strong&gt;&lt;br&gt;
In your index.css or a separate CSS file, add the following styles:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.modalContent {
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  position: fixed;
  width: 100%;
  max-width: 500px;
  z-index: 300;
  overflow-y: auto;
  overflow-x: hidden;
}

@media (max-width: 550px) {
  .modalContent {
    left: 15px;
    right: 15px;
  }
}

.overlay {
  overflow: hidden;
}

@screen maxLg {
  .overlay {
    transition: all 0.3s ease-in-out;
    width: 100%;
    height: 100%;
    position: fixed;
  }
  .overlay::before {
    content: "";
    z-index: 1;
    background: rgba(0, 0, 0, 0.5);
    transition: all 0.3s ease-in-out;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
  }
}

.overlay:before {
  content: "";
  background-color: rgba(0, 0, 0, 0.5);
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  width: 100%;
  height: 100%;
  z-index: 200;
  backdrop-blur: 6px;
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 4: Implementing the Modal in Your Component&lt;/strong&gt;&lt;br&gt;
Create a component, App.js, and use the ModalPortal to open a modal with some content:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState } from 'react';
import ModalPortal from './ModalPortal';

const App = () =&amp;gt; {
  const [isModalOpen, setIsModalOpen] = useState(false);

  const handleOpenModal = () =&amp;gt; setIsModalOpen(true);
  const handleCloseModal = () =&amp;gt; setIsModalOpen(false);

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;button onClick={handleOpenModal}&amp;gt;Open Modal&amp;lt;/button&amp;gt;
      &amp;lt;ModalPortal open={isModalOpen}&amp;gt;
        &amp;lt;div className="modalContent"&amp;gt;
          &amp;lt;h2&amp;gt;Modal Title&amp;lt;/h2&amp;gt;
          &amp;lt;p&amp;gt;This is the modal content.&amp;lt;/p&amp;gt;
          &amp;lt;button onClick={handleCloseModal}&amp;gt;Close&amp;lt;/button&amp;gt;
        &amp;lt;/div&amp;gt;
      &amp;lt;/ModalPortal&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

export default App;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 5: Dynamically Rendering Elements in the Modal&lt;/strong&gt;&lt;br&gt;
To render different elements dynamically in the modal, you can pass the content as children to the ModalPortal component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState } from 'react';
import ModalPortal from './ModalPortal';

const App = () =&amp;gt; {
  const [isModalOpen, setIsModalOpen] = useState(false);
  const [modalContent, setModalContent] = useState(null);

  const handleOpenModal = (content) =&amp;gt; {
    setModalContent(content);
    setIsModalOpen(true);
  };
  const handleCloseModal = () =&amp;gt; setIsModalOpen(false);

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;button onClick={() =&amp;gt; handleOpenModal(&amp;lt;p&amp;gt;Content 1&amp;lt;/p&amp;gt;)}&amp;gt;Open Modal with Content 1&amp;lt;/button&amp;gt;
      &amp;lt;button onClick={() =&amp;gt; handleOpenModal(&amp;lt;p&amp;gt;Content 2&amp;lt;/p&amp;gt;)}&amp;gt;Open Modal with Content 2&amp;lt;/button&amp;gt;
      &amp;lt;ModalPortal open={isModalOpen}&amp;gt;
        &amp;lt;div className="modalContent"&amp;gt;
          &amp;lt;h2&amp;gt;Modal Title&amp;lt;/h2&amp;gt;
          {modalContent}
          &amp;lt;button onClick={handleCloseModal}&amp;gt;Close&amp;lt;/button&amp;gt;
        &amp;lt;/div&amp;gt;
      &amp;lt;/ModalPortal&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

export default App;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
In this tutorial, we've covered how to create a modal in React using React Portal and manage its state with hooks. We've also learned how to dynamically render different elements within the modal. With these techniques, you can enhance the user experience of your web application by incorporating interactive and dynamic modals. Happy coding!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Additional Resources&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;1. React Documentation&lt;/li&gt;
&lt;li&gt;2. CSS Tricks for Modals&lt;/li&gt;
&lt;li&gt;3. React Portals&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>react</category>
      <category>modalportal</category>
    </item>
    <item>
      <title>Implementing Autosize Textarea in React: A Complete Guide</title>
      <dc:creator>poshiya parth s</dc:creator>
      <pubDate>Tue, 09 Jul 2024 05:42:21 +0000</pubDate>
      <link>https://forem.com/parth24072001/implementing-autosize-textarea-in-react-a-complete-guide-5pg</link>
      <guid>https://forem.com/parth24072001/implementing-autosize-textarea-in-react-a-complete-guide-5pg</guid>
      <description>&lt;p&gt;&lt;strong&gt;Introduction&lt;/strong&gt;&lt;br&gt;
Creating a seamless user experience often hinges on the small details, such as a well-designed text area for user input. In this blog post, we’ll dive into the implementation of a dynamic text area using React, which adjusts its height based on the input content and provides a smooth user experience.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Component Breakdown&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;CommonTextArea Component&lt;/strong&gt;&lt;br&gt;
The CommonTextArea component is a reusable component that encapsulates the logic for a dynamic text area. Here’s a detailed look at its structure and functionality:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import clsx from "clsx";
import { useLayoutEffect, useState } from "react";

export default function CommonTextArea({
  inputRef,
  isFocuse,
  isInputError,
  onChangeInput,
  handleKeyDown,
  setIsFocuse,
  textBoxValue,
  parentClassName,
  textAreaClassName,
  placeHolder,
}: {
  inputRef: React.RefObject&amp;lt;HTMLTextAreaElement&amp;gt;;
  isFocuse?: boolean;
  isInputError?: boolean;
  onChangeInput: (event: React.ChangeEvent&amp;lt;HTMLTextAreaElement&amp;gt;) =&amp;gt; void;
  handleKeyDown: (event: React.KeyboardEvent&amp;lt;HTMLTextAreaElement&amp;gt;) =&amp;gt; void;
  setIsFocuse: React.Dispatch&amp;lt;React.SetStateAction&amp;lt;boolean&amp;gt;&amp;gt;;
  textBoxValue?: string;
  parentClassName?: string;
  textAreaClassName?: string;
  placeHolder?: string;
}) {
  const [scrollBar, setScrollBar] = useState&amp;lt;boolean&amp;gt;(false);

  useLayoutEffect(() =&amp;gt; {
    const textArea: any =
      inputRef.current ?? document.getElementById("text_area_input");

    if (textArea) {
      textArea.style.height = "0px";
      const scrollHeight = textArea.scrollHeight;
      textArea.style.height = scrollHeight + "px";
      if (scrollHeight &amp;gt;= 200) {
        setScrollBar(true);
      } else {
        setScrollBar(false);
      }
    }
  }, [inputRef, textBoxValue]);

  return (
    &amp;lt;div
      className={clsx(
        {
          "form-input-error": isInputError,
          "ask-question-input-focus": isFocuse,
        },
        parentClassName
      )}
      onClick={() =&amp;gt; {
        setIsFocuse(true);
      }}
    &amp;gt;
      &amp;lt;textarea
        id="text_area_input"
        ref={inputRef}
        className={clsx(textAreaClassName, "textarea_design", {
          "overflow-y-auto": scrollBar,
        })}
        placeholder={placeHolder}
        value={textBoxValue}
        onClick={() =&amp;gt; {
          setIsFocuse(true);
        }}
        onKeyDown={handleKeyDown}
        style={{ height: "57px" }}
        onChange={onChangeInput}
      /&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Key Features&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Dynamic Height Adjustment&lt;/strong&gt;: The text area dynamically adjusts its height based on the input content, ensuring that users have a comfortable and responsive typing experience.&lt;br&gt;
&lt;strong&gt;Conditional Styling&lt;/strong&gt;: The component uses the clsx library to conditionally apply styles based on the component's state, such as focus or error states.&lt;br&gt;
&lt;strong&gt;Scrollable Content&lt;/strong&gt;: When the content exceeds a certain height, a scrollbar appears, ensuring that the text area remains user-friendly and does not overflow.&lt;br&gt;
&lt;strong&gt;App Component Integration&lt;/strong&gt;&lt;br&gt;
The CommonTextArea component can be easily integrated into any React application. Here’s how you can use it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const inputRef = useRef&amp;lt;HTMLTextAreaElement | null&amp;gt;(null);
const [isFocuse, setIsFocuse] = useState&amp;lt;boolean&amp;gt;(false);
const [isInputError, setIsInputError] = useState&amp;lt;boolean&amp;gt;(false);

const onChangeInput = (e: React.ChangeEvent&amp;lt;HTMLTextAreaElement&amp;gt;) =&amp;gt; {
  setChatPrompt(e.target.value);
  setIsInputError(false);
};

const handleKeyDown = (e: React.KeyboardEvent&amp;lt;HTMLTextAreaElement&amp;gt;) =&amp;gt; {
  setIsFocuse(true);

  if (e.key === "Enter" &amp;amp;&amp;amp; !e.shiftKey) {
    e.preventDefault();
    onSubmitdata(); // submit event
  }
};

useEffect(() =&amp;gt; {
  inputRef?.current?.focus();
}, [inputRef]);

return (
  &amp;lt;CommonTextArea
    inputRef={inputRef}
    isFocuse={isFocuse}
    isInputError={isInputError}
    onChangeInput={onChangeInput}
    handleKeyDown={handleKeyDown}
    setIsFocuse={setIsFocuse}
    textBoxValue={"default text box value"}
    parentClassName={"text_area_parent "}
    textAreaClassName={"ask-question-input caret-raisinblack"}
  /&amp;gt;
);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;CSS Styling&lt;/strong&gt;&lt;br&gt;
Proper styling is crucial for a great user experience. Here are the styles used in this implementation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.form-input-error {
  border-color: red;
  background-color: #f5f5f5;
}

.ask-question-input-focus {
  outline: none;
  border-color: #0f9d58;
  color: #2d2d2d;
  background-color: #e6f4ea;
}

.text_area_parent {
  width: 100%;
  background-color: #f5f5f5;
  display: flex;
  justify-content: start;
  align-items: center;
  padding: 5px;
  border-radius: 10px;
  font-size: 16px;
  border: 1px solid transparent;
  transition: border-color 0.3s, background-color 0.3s;
}

.ask-question-input {
  width: 100%;
  background-color: transparent;
  font-size: 16px;
  border: none;
  outline: none;
  resize: none;
  overflow: hidden;
  max-height: 230px;
  padding: 14px;
}

.ask-question-input::-webkit-scrollbar-thumb {
  background-color: #d9d9d9;
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
By implementing a dynamic text area in your React application, you can significantly enhance user experience, making text inputs more user-friendly and visually appealing. The CommonTextArea component is highly customizable and can be adapted to various use cases, ensuring a consistent and responsive design throughout your application.&lt;/p&gt;

&lt;p&gt;[(&lt;a href="https://andarist.github.io/react-textarea-autosize/)" rel="noopener noreferrer"&gt;https://andarist.github.io/react-textarea-autosize/)&lt;/a&gt;]&lt;br&gt;
[&lt;a href="https://www.npmjs.com/package/react-textarea-autosize" rel="noopener noreferrer"&gt;https://www.npmjs.com/package/react-textarea-autosize&lt;/a&gt;]&lt;/p&gt;

&lt;p&gt;also you dont want to custom then you can use this package&lt;/p&gt;

</description>
      <category>react</category>
      <category>autosize</category>
      <category>textarea</category>
      <category>frontend</category>
    </item>
    <item>
      <title>How To AutoScroll In React?Creating Smooth Auto-Scrolling Functionality in React</title>
      <dc:creator>poshiya parth s</dc:creator>
      <pubDate>Tue, 09 Jul 2024 05:42:08 +0000</pubDate>
      <link>https://forem.com/parth24072001/how-to-autoscroll-in-reactcreating-smooth-auto-scrolling-functionality-in-react-1o33</link>
      <guid>https://forem.com/parth24072001/how-to-autoscroll-in-reactcreating-smooth-auto-scrolling-functionality-in-react-1o33</guid>
      <description>&lt;p&gt;Are you looking to enhance the user experience in your React application by implementing smooth auto-scrolling functionality? Look no further! In this blog post, we'll guide you through how to create a custom React hook that automatically scrolls to the bottom of a container when new content is added. Let's dive in!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Implementing Auto-Scroll Hook&lt;/strong&gt;&lt;br&gt;
We'll start by creating a custom React hook named useAutoScroll.tsx that handles the auto-scrolling functionality. This hook will utilize the useEffect hook to trigger the scroll action whenever new content is added to the container.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

import { useEffect } from "react";

function useScrollToBottom(element: HTMLDivElement | null, data: any) {
  const scrollToBottom = () =&amp;gt; {
    if (element) {
      element.scrollIntoView({ behavior: "smooth" });
    }
  };

  useEffect(() =&amp;gt; {
    scrollToBottom();
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [data]);
}

export default useAutoScroll;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Using the Auto-Scroll Hook&lt;/strong&gt;&lt;br&gt;
Next, let's see how you can use the useAutoScroll hook in your component.&lt;/p&gt;

&lt;p&gt;In your component file (demoFile.tsx), you can use the useAutoScroll hook by passing a reference to the target element and the dependency that triggers the scroll.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

const scrollRef = useRef&amp;lt;HTMLDivElement | null&amp;gt;(null);
useAutoScroll(scrollRef.current, dependency);

// Ensure that this &amp;lt;div&amp;gt; is added after the scrolling content
return (
  &amp;lt;div ref={scrollRef} /&amp;gt;
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By following these steps and incorporating the useAutoScroll hook into your React components, you can achieve a seamless auto-scrolling experience for your users as new content is dynamically loaded.&lt;/p&gt;

&lt;p&gt;Enhance your React application with this auto-scrolling functionality to keep your users engaged and provide a smooth browsing experience. Happy coding! 🚀&lt;/p&gt;

&lt;p&gt;That's it for our blog post on creating smooth auto-scrolling functionality in React. We hope this guide helps you implement this feature seamlessly in your projects. Stay tuned for more frontend development tips and tricks!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>react</category>
      <category>autoscroll</category>
    </item>
    <item>
      <title>iphone safari issue</title>
      <dc:creator>poshiya parth s</dc:creator>
      <pubDate>Tue, 09 Jul 2024 05:41:03 +0000</pubDate>
      <link>https://forem.com/parth24072001/iphone-safari-issue-2n0n</link>
      <guid>https://forem.com/parth24072001/iphone-safari-issue-2n0n</guid>
      <description>&lt;p&gt;**&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Safari iphone input zoom , bottom scroll, scroll to bottom , chatgpt textarea&lt;/em&gt;
&lt;/h2&gt;

&lt;p&gt;**&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;When we click on textbox that moment iPhone Safari that textbox zoom hear is the solution in react we use first and second for the next js&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffl3horjn40adjkpzz0l5.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffl3horjn40adjkpzz0l5.jpg" alt=" " width="800" height="506"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;meta
      name="viewport"
      content="width=device-width,initial-scale=1.0, maximum-scale=1, user-scalable=no"
    /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Viewport } from "next/dist/lib/metadata/types/extra-types";

export const viewport: Viewport = {
  width: "device-width",
  initialScale: 1,
  maximumScale: 1,
  userScalable: false,
  interactiveWidget: "overlays-content",
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2.&lt;br&gt;
&lt;strong&gt;also iphone safari bottom scroll is default behaviour&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsnh1pc31xdrh6kdgyyeu.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsnh1pc31xdrh6kdgyyeu.jpg" alt=" " width="800" height="1733"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ios</category>
      <category>iphone</category>
      <category>safari</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Kaboom 🔥! Supercharge Your React App: Saving Refetches with React Query Magic</title>
      <dc:creator>poshiya parth s</dc:creator>
      <pubDate>Thu, 29 Feb 2024 05:08:42 +0000</pubDate>
      <link>https://forem.com/parth24072001/kaboom-supercharge-your-react-app-saving-refetches-with-react-query-magic-365m</link>
      <guid>https://forem.com/parth24072001/kaboom-supercharge-your-react-app-saving-refetches-with-react-query-magic-365m</guid>
      <description>&lt;p&gt;Greetings, wonderful folks of the internet! 🌟 Today, we embark on a journey to explore a real-life application that not only saves precious API costs but also leverages the magic of optimistic updates in React Query. Now, you might be wondering, "How exactly do we achieve this feat?" Well, buckle up, because we're about to dive into some captivating insights.&lt;/p&gt;

&lt;p&gt;In my pursuit of knowledge and inspiration, I stumbled upon a gem of a resource that fueled the spark for this blog. The blog post titled Effective React Query Keys caught my attention, particularly for its articulate explanation of caching. The way it demystifies the intricacies of caching is simply awe-inspiring.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Unveiling the Optimistic Updates Marvel&lt;br&gt;
The Challenge: API Costs 📉&lt;/strong&gt;&lt;br&gt;
API costs can be a significant concern in any application. Fetching data from the server incurs expenses, and optimizing this process becomes paramount. This is where React Query and optimistic updates come into play, offering a strategic approach to minimize API costs while enhancing the user experience.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Solution: Optimistic Updates in React Query 🚀&lt;/strong&gt;&lt;br&gt;
Optimistic updates allow us to update the UI optimistically, even before the server responds. This not only provides users with a seamless and responsive interface but also reduces the need for unnecessary API calls. The real question becomes, "How do we integrate this into our React Query workflow?"&lt;/p&gt;

&lt;p&gt;Just have a look at the below code for a while and we will start tinkering the same in a couple of blog lines ahead.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useMutation, useQueryClient } from "@tanstack/react-query";
import { deleteEditFunction } from "../api";

const useDeleteEditCall = (userId) =&amp;gt; {
  const queryClient = useQueryClient();

  return useMutation(
    (userId) =&amp;gt; deleteEditFunction(userId),
    {
      onSuccess: () =&amp;gt; {  
        queryClient.setQueryData(["get-previous-key"], (previousdata) =&amp;gt; {
          const filterData = previousdata.data.body.filter((user) =&amp;gt; {
            return user.userId !== userId;
          });
          previousdata.data.body = filterData;
          return previousdata;
        });
      },
      onError: (error) =&amp;gt; {
        console.log(error);
      },
    }
  );
};

export default useDeleteEditCall;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt;&lt;br&gt;
In our system, we have implemented a caching key, identified as get-previous-key. This key serves a crucial role in optimizing data retrieval by enabling a caching experience. Here's a breakdown of how it works:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Caching Mechanism&lt;/strong&gt;: When you initiate a request to fetch data, you include the caching key (get-previous-key) in the query. This key acts as a trigger for the caching mechanism.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Optimizing Data Retrieval&lt;/strong&gt;: The caching response obtained through the key allows for manipulation of the data based on specific requirements. This manipulation can involve filtering, sorting, or modifying the response without the need for an additional API call.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example Scenario&lt;/strong&gt; : Todo List: Let's consider a practical example with a todo list. Assume the initial response contains four todo items. The power of the caching key lies in its ability to selectively modify this response.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Dynamic Data Modification&lt;/strong&gt;: In the case of the todo response, if there's a need to delete an item from the result set, the caching key facilitates this modification. For instance, if the goal is to remove the last todo item, this can be achieved by manipulating the caching response without triggering a re-fetch.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Efficiency Gains&lt;/strong&gt;: By utilizing this approach, unnecessary API calls are avoided, leading to improved efficiency. Additionally, it allows for optimistic updates, ensuring a seamless user experience.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Reducing Latency&lt;/strong&gt;: The caching key contributes to reducing latency as it enables local manipulation of data, eliminating the need for repeated server requests for similar operations.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Customizable Optimization&lt;/strong&gt;: The caching key provides a customizable way to optimize data handling. Depending on the specific use case, it allows for tailoring the caching response to suit the application's requirements.&lt;/p&gt;

&lt;p&gt;In summary, the get-previous-key plays a pivotal role in enhancing the caching experience, providing flexibility in manipulating responses, and ultimately optimizing data retrieval processes in a way that aligns with the needs of the application.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Understanding React Query&lt;/strong&gt;:&lt;br&gt;
Brief overview of React Query and its key features.&lt;br&gt;
Explanation of how React Query manages and caches API calls.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Setting Up React Query&lt;/strong&gt;:&lt;br&gt;
Installation and basic configuration of React Query in a React project.&lt;br&gt;
Configuring a global QueryClient to manage API data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Optimizing GET Requests&lt;/strong&gt;:&lt;br&gt;
How React Query automatically caches GET requests and eliminates redundant API calls.&lt;br&gt;
Utilizing the useQuery hook for efficient data fetching.&lt;br&gt;
Handling stale and fresh data scenarios.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Integration with DELETE, POST, and PUT Operations&lt;/strong&gt;:&lt;br&gt;
Introduction to the useMutation hook for managing non-GET operations.&lt;br&gt;
Writing mutations for DELETE, POST, and PUT requests using React Query.&lt;br&gt;
Maintaining data consistency across different API operations.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Saving GET Requests on Non-GET Operations&lt;/strong&gt;:&lt;br&gt;
Strategies for ensuring that GET requests are saved and optimized when performing DELETE, POST, and PUT operations.&lt;br&gt;
Leveraging the onSuccess callback to update and invalidate cached data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Handling Errors and Edge Cases&lt;/strong&gt;:&lt;br&gt;
Implementing error handling with React Query for a seamless user experience.&lt;br&gt;
Addressing edge cases such as partial data updates and rollbacks.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Best Practices and Tips&lt;/strong&gt;:&lt;br&gt;
Best practices for structuring queries and mutations in complex applications.&lt;br&gt;
Tips for optimizing performance and minimizing unnecessary API calls.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Real-world Examples&lt;/strong&gt;:&lt;br&gt;
Demonstrations of implementing React Query in real-world scenarios with various API interactions.&lt;br&gt;
Code snippets and practical examples for better understanding.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;:&lt;br&gt;
Our hero in this quest is the get-previous-key. This key unlocks the door to efficient data manipulation, enabling us to perform optimistic updates with finesse. As we explore its applications, we'll witness how it transforms the landscape of API cost optimization in React Query.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Journeying Together&lt;/strong&gt;&lt;br&gt;
Join me on this expedition as we decode the intricacies of effective React Query keys, delve into the world of caching, and master the art of optimistic updates. Together, we'll unravel the potential to save costs, enhance performance, and elevate the user experience.&lt;/p&gt;

&lt;p&gt;In the spirit of knowledge sharing, let's make our React Query endeavors not just effective but downright spectacular! 🚀✨&lt;/p&gt;

&lt;p&gt;Stay tuned for more insights, and don't forget to check out the blog that sparked this exciting exploration! Until then, happy coding! 💻🌈&lt;/p&gt;

&lt;p&gt;That all the beautiful people on the internet , lets connect on &lt;a href="https://twitter.com/poshiyaparth24" rel="noopener noreferrer"&gt;twitter&lt;/a&gt; &lt;/p&gt;

</description>
    </item>
    <item>
      <title>Mastering Multi-Select Options with React-Select/Creatable</title>
      <dc:creator>poshiya parth s</dc:creator>
      <pubDate>Mon, 26 Feb 2024 04:31:35 +0000</pubDate>
      <link>https://forem.com/parth24072001/mastering-multi-select-options-with-react-selectcreatable-i36</link>
      <guid>https://forem.com/parth24072001/mastering-multi-select-options-with-react-selectcreatable-i36</guid>
      <description>&lt;p&gt;&lt;strong&gt;Content:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;React-Select/Creatable is a powerful library that enhances the user experience when dealing with multi-select options in React applications. In this blog post, we will explore how to effectively implement and leverage React-Select/Creatable to create a seamless and intuitive multiple-selection component.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Understanding the Basics&lt;/strong&gt;&lt;br&gt;
React-Select/Creatable provides a flexible and customizable solution for handling multi-select options. The Creatable component, in particular, allows users to create new options on-the-fly, making it an excellent choice for scenarios where dynamic data entry is required.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Setting Up the Environment&lt;/strong&gt;&lt;br&gt;
To get started, make sure you have React and React-Select/Creatable installed in your project. You can install it using npm or yarn:&lt;/p&gt;

&lt;p&gt;npm install react-select&lt;br&gt;
or&lt;br&gt;
yarn add react-select&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Building the Multi-Select Component&lt;/strong&gt;&lt;br&gt;
The provided code showcases a React component (App) that utilizes React-Select/Creatable for multi-select functionality. Let's break down some key elements:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Filter Options&lt;/strong&gt;: The filterOptions array defines the types of filters available for selection (e.g., State, Pincode, District).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;State Management&lt;/strong&gt;: The component uses React's useState hook to manage the state of selected parameters (paramsData) and whether the selection is complete (apply).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Dynamic Search Suggestions&lt;/strong&gt;: The onInputChange function dynamically generates search suggestions based on user input and selected filter options.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Effect Hook&lt;/strong&gt;: An useEffect hook monitors changes in the selected parameters and triggers the application of filters.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Customization and Styling&lt;/strong&gt;&lt;br&gt;
React-Select/Creatable provides various customization options to tailor the appearance and behavior of the multi-select component. The components prop is used to override the default behavior of the clear indicator.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Integration in Your Project&lt;/strong&gt;&lt;br&gt;
To integrate this multi-select component into your project, simply import and render the App component where needed. Customize the styles and appearance according to your project's design guidelines.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
React-Select/Creatable simplifies the implementation of a feature-rich multi-select component in React applications. Whether you're managing tags, categories, or any other form of multiple selections, React-Select/Creatable provides an elegant and user-friendly solution.&lt;/p&gt;

&lt;p&gt;Start incorporating React-Select/Creatable into your projects today to empower users with a seamless and efficient multi-select experience!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useState, useEffect } from "react";
import Creatable from "react-select/creatable";
import { intersection, isEmpty } from "lodash";

const Search = () =&amp;gt; {
  let Placeholder = "Search ";
  const filterOptions = [
    {
      label: "State",
      value: "state",
    },
    {
      label: "Pincode",
      value: "pincode",
    },
    {
      label: "District",
      value: "district",
    },
  ];

  const [apply, setApply] = useState(false);

  const [paramsData, setParamsData] = useState([]);

  const [searchOptions, setSearchOptions] = useState([]);

  const onChange = (params) =&amp;gt; {
    if (isEmpty(paramsData)) {
      setParamsData(params);
    } else if (
      disableInput &amp;amp;&amp;amp;
      isEmpty(params) &amp;amp;&amp;amp;
      Placeholder === "Search Admin"
    ) {
      setParamsData([]);
    } else if (
      disableInput &amp;amp;&amp;amp;
      !isEmpty(params) &amp;amp;&amp;amp;
      Placeholder === "Search Admin"
    ) {
      return;
    } else {
      setParamsData(params);
    }
  };

  const disableInput = paramsData.length === filterOptions.length;
  const onInputChange = (seachKey) =&amp;gt; {
    if (!seachKey) {
      setSearchOptions([]);
      return;
    } else if (seachKey.length !== 0) {
      const paramsFieldValues = paramsData.map((param) =&amp;gt; param.field);
      const filterOptionValues = filterOptions.map((option) =&amp;gt; option.value);

      const commonValues = intersection(paramsFieldValues, filterOptionValues);

      const filteredFilterOptions = filterOptions.filter(
        (option) =&amp;gt; !commonValues.includes(option.value)
      );
      const options = filteredFilterOptions.map((option) =&amp;gt; {
        return {
          label: `${option.label} : ${seachKey}`,
          value: seachKey,
          field: option.value,
        };
      });

      setSearchOptions(options);
    } else {
      const options = filterOptions.map((option) =&amp;gt; {
        return {
          label: `${option.label} : ${seachKey}`,
          value: seachKey,
          field: option.value,
        };
      });
      setSearchOptions(options);
    }
  };

  useEffect(() =&amp;gt; {
    if (isEmpty(paramsData)) {
      setApply &amp;amp;&amp;amp; setApply(!apply);
    }
  }, [paramsData]);
  return (
    &amp;lt;div className="!cursor-pointer"&amp;gt;
      &amp;lt;Creatable
        classNamePrefix="multitag provider_modal"
        options={searchOptions}
        placeholder={Placeholder}
        isMulti
        value={!isEmpty(paramsData) ? paramsData : null}
        className=" w-[350px] maxSm:w-[290px]"
        onChange={onChange}
        onInputChange={onInputChange}
        isClearable
        components={{
          ClearIndicator: () =&amp;gt; null,
        }}
      /&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

export default Search;






&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>Creating Dynamic PDF Downloads with React: A Guide Using jsPDF and jspdf-autotable</title>
      <dc:creator>poshiya parth s</dc:creator>
      <pubDate>Thu, 22 Feb 2024 04:42:56 +0000</pubDate>
      <link>https://forem.com/parth24072001/creating-dynamic-pdf-downloads-with-react-a-guide-using-jspdf-and-jspdf-autotable-4kbp</link>
      <guid>https://forem.com/parth24072001/creating-dynamic-pdf-downloads-with-react-a-guide-using-jspdf-and-jspdf-autotable-4kbp</guid>
      <description>&lt;p&gt;&lt;strong&gt;Introduction:&lt;/strong&gt;&lt;br&gt;
Briefly introduce the concept of exporting tables to PDF from a React application.&lt;br&gt;
Highlight the importance of providing users with the ability to download tabular data for offline use.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Section 1: Setting up the Project&lt;/strong&gt;&lt;br&gt;
1.1 Install Dependencies:&lt;br&gt;
Guide readers on installing the necessary packages using npm.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;npm install jspdf jspdf-autotable&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;1.2 Importing Libraries:&lt;br&gt;
Demonstrate how to import jsPDF and jspdf-autotable in your React component.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Section 2: Creating a Sample Table&lt;/strong&gt;&lt;br&gt;
2.1 Define Table Structure:&lt;br&gt;
Create a sample table with headers and dummy data.&lt;br&gt;
Explain the importance of having a well-structured table for PDF generation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Section 3: Generating PDF with jspdf-autotable&lt;/strong&gt;&lt;br&gt;
3.1 Writing the generatePDF Function:&lt;br&gt;
Define the generatePDF function that utilizes jsPDF and jspdf-autotable.&lt;br&gt;
Explain the purpose of each step in the function.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3.2 Customizing Table Styles:&lt;/strong&gt;&lt;br&gt;
Explore different styling options available in jspdf-autotable for customizing the appearance of the PDF table.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Section 4: Adding Page Numbers and Titles&lt;/strong&gt;&lt;br&gt;
4.1 Enhancing User Experience:&lt;br&gt;
Show how to add page numbers and titles to each page of the generated PDF for better readability.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Section 5: Integrating with React&lt;/strong&gt;&lt;br&gt;
5.1 Button Component:&lt;br&gt;
Create a React button component to trigger the PDF generation.&lt;br&gt;
Utilize the onClick event to call the generatePDF function.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Section 6: Conclusion&lt;/strong&gt;&lt;br&gt;
Summarize the benefits of using jsPDF and jspdf-autotable for PDF generation in a React application.&lt;br&gt;
Encourage readers to explore further customization options and integrate it into their projects.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion:&lt;/strong&gt;&lt;br&gt;
Express the importance of providing users with downloadable content.&lt;br&gt;
Invite readers to share their experiences and feedback.&lt;br&gt;
Feel free to customize the content and add more details based on your preferences and the level of depth you want to achieve in each section.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Section 7: Handling Large Tables&lt;/strong&gt;&lt;br&gt;
7.1 Pagination for Large Datasets:&lt;br&gt;
Explain the limitations of handling a large number of rows in a single table.&lt;br&gt;
Introduce the concept of paginating the data for more manageable PDF generation.&lt;/p&gt;

&lt;p&gt;7.2 Implementing Pagination:&lt;br&gt;
Demonstrate how to implement pagination for large tables using libraries like react-table or custom pagination logic.&lt;br&gt;
Showcase how to generate PDFs for each page of the table separately.&lt;/p&gt;

&lt;p&gt;7.3 Dynamic Loading of Data:&lt;br&gt;
Explore the option of dynamically loading data as the user navigates through pages to optimize performance.&lt;br&gt;
Discuss how lazy loading or infinite scrolling techniques can be beneficial.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Section 8: Performance Optimization&lt;/strong&gt;&lt;br&gt;
8.1 Virtualization Techniques:&lt;br&gt;
Discuss the importance of virtualization to handle a large number of DOM elements efficiently.&lt;br&gt;
Introduce libraries like react-virtualized that can be integrated to enhance performance.&lt;/p&gt;

&lt;p&gt;8.2 Asynchronous PDF Generation:&lt;br&gt;
Explain the benefits of asynchronous PDF generation to prevent blocking the UI during the process.&lt;br&gt;
Utilize async/await to handle the generation process in the background.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Section 9: User Experience Considerations&lt;/strong&gt;&lt;br&gt;
9.1 Loading Indicators:&lt;br&gt;
Implement loading indicators to inform users about the ongoing PDF generation process, especially for large datasets.&lt;/p&gt;

&lt;p&gt;9.2 Providing Download Progress:&lt;br&gt;
If feasible, demonstrate how to provide users with a download progress indicator to keep them informed about the PDF generation status.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Section 10: Conclusion and Future Considerations&lt;/strong&gt;&lt;br&gt;
10.1 Recap and Summary:&lt;br&gt;
Summarize the key points discussed in handling large tables and optimizing PDF generation.&lt;/p&gt;

&lt;p&gt;10.2 Future Considerations:&lt;br&gt;
Encourage readers to explore other optimization techniques and libraries that may emerge in the future.&lt;br&gt;
Discuss any potential updates or improvements in the jsPDF or jspdf-autotable libraries related to performance.&lt;br&gt;
By addressing the challenges of handling large datasets in the blog post, you provide valuable insights to readers who may encounter similar issues in their projects. It also showcases your awareness of practical considerations when working with extensive data sets.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react';

import jsPDF from "jspdf";
import "jspdf-autotable";

export function App(props) {
    const generatePDF = () =&amp;gt; {
    const doc = new jsPDF("l", "mm", "legal");

    doc.text("left heading", 10, 10);
    doc.text("right header", doc.internal.pageSize.width - 10, 10, {
      align: "right",
    });
    doc.setFontSize(12);

    const jsonBody = [];
    const headerData = convertToFormDataArray(["EmployeeID","FirstName","LastName","DateOfBirth","Department","Salary"]);

  const dummyData = [
  [1, 'John', 'Doe', '1985-03-15', 'IT', 80000],
  [2, 'Jane', 'Smith', '1990-07-22', 'HR', 75000],
  [3, 'Bob', 'Johnson', '1988-11-10', 'Marketing', 85000],
  [4, 'Alice', 'Williams', '1995-05-03', 'Finance', 90000],
  [5, 'Charlie', 'Brown', '1980-09-28', 'Operations', 78000]
];

    jsonBody.push(...dummyData);

    function convertToFormDataArray(array) {
      return array.map((header) =&amp;gt; ({ header }));
    }

    // doc.text(pdfHeader, centerX, 20);

    const addPageNumbersAndTitles = () =&amp;gt; {
      const pageCount = doc.internal.getNumberOfPages();
      for (let i = 1; i &amp;lt;= pageCount; i++) {
        doc.autoTable({
          // head: [[pdfHeader], pdfField], // Use the first header for all pages
          head: [
            [
              {
                content: " table Heading 1",
                colSpan: headerData.length,
                styles: { halign: "center", fillColor: [22, 160, 133] },
              },
            ],
            [
              {
                content: " table heading 2",
                colSpan: headerData.length,
                styles: { halign: "center", fillColor: [22, 160, 133] },
              },
            ],

          ["EmployeeID","FirstName","LastName","DateOfBirth","Department","Salary"],
          ],
          body: jsonBody,
          startY: 20,
          columnStyles: { email: { cellWidth: "wrap" } },
          theme: "striped",
          didDrawPage: () =&amp;gt; {
            const pageWidth = doc.internal.pageSize.width;
            const pageHeight = doc.internal.pageSize.height;
            doc.setFontSize(10);
            const pages = doc.internal.getNumberOfPages();
            doc.setFontSize(10);
            for (let j = 1; j &amp;lt; pages + 1; j++) {
              const horizontalPos = pageWidth / 2;
              const verticalPos = pageHeight - 10;
              doc.setPage(j);
              doc.text(`Page ${j}`, horizontalPos, verticalPos, {
                align: "center",
              });
            }
          },
          columns: headerData,
        });
      }
    };

    addPageNumbersAndTitles();

    doc.save(`${"pdfName"}.pdf`);
  };
   return (
    &amp;lt;&amp;gt;
      &amp;lt;button
        onClick={generatePDF}
        className={
          "btn-primary ml-auto flex justify-center items-center gap-2"
        }
      &amp;gt;

        &amp;lt;p&amp;gt;Download PDF&amp;lt;/p&amp;gt;
      &amp;lt;/button&amp;gt;
    &amp;lt;/&amp;gt;
  );
}





&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>Simplify Asynchronous State Management with React Query's useMutation Hook</title>
      <dc:creator>poshiya parth s</dc:creator>
      <pubDate>Mon, 19 Feb 2024 04:42:44 +0000</pubDate>
      <link>https://forem.com/parth24072001/simplify-asynchronous-state-management-with-react-querys-usemutation-hook-pji</link>
      <guid>https://forem.com/parth24072001/simplify-asynchronous-state-management-with-react-querys-usemutation-hook-pji</guid>
      <description>&lt;p&gt;&lt;strong&gt;What is useMutation?&lt;/strong&gt;&lt;br&gt;
useMutation is a hook provided by React Query that facilitates making asynchronous mutations, such as POST, DELETE, or PATCH requests to a server. It abstracts away much of the boilerplate code traditionally associated with managing asynchronous state in React applications.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Setting up useMutation&lt;/strong&gt;&lt;br&gt;
Setting up a mutation call with useMutation is straightforward. You define a mutation function that encapsulates the logic for your API call and specify callbacks for handling success and error scenarios. Let's take a look at an example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqncln48ythkctmhquw11.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqncln48ythkctmhquw11.png" alt=" " width="800" height="840"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjq9fz7db924feimrquw7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjq9fz7db924feimrquw7.png" alt=" " width="800" height="396"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz33h57778yza16951299.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz33h57778yza16951299.png" alt=" " width="800" height="331"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Benefits of useMutation&lt;/strong&gt;&lt;br&gt;
Simplified Syntax: With useMutation, you can perform mutation calls with minimal code, reducing the complexity of managing asynchronous operations.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Automatic Caching and Invalidation&lt;/strong&gt;: React Query automatically handles caching and invalidation of data, ensuring that your UI remains consistent and up-to-date.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Error Handling&lt;/strong&gt;: The hook provides built-in error handling capabilities, allowing you to easily display error messages or handle retries.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Introduction to React Query&lt;/strong&gt;: Provide a brief overview of React Query and its core features, such as data fetching, caching, and mutations.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Comparisons with Other State Management Solutions&lt;/strong&gt;: Discuss how React Query's approach to handling asynchronous state differs from other solutions like Redux or traditional React state management.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Examples of Real-world Use Cases&lt;/strong&gt;: Share examples of scenarios where useMutation can be particularly useful, such as user authentication, form submissions, or updating user profiles.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Optimistic Updates&lt;/strong&gt;: Explain how React Query supports optimistic updates, allowing you to update the UI immediately while waiting for the mutation to complete.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Advanced Configuration Options&lt;/strong&gt;: Dive into more advanced configuration options available with useMutation, such as specifying custom error handling logic or controlling the behavior of retries.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Testing&lt;/strong&gt;: Discuss strategies for testing components that use useMutation, including mocking API calls and testing different mutation states.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Performance Considerations&lt;/strong&gt;: Touch on performance considerations when using React Query, such as query and mutation caching strategies, and how to optimize performance for larger-scale applications.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Community and Resources&lt;/strong&gt;: Highlight the active community around React Query and point readers to additional resources such as documentation, tutorials, and community forums for further learning.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>A Comprehensive Guide to Using React Query's useQuery for API Calls</title>
      <dc:creator>poshiya parth s</dc:creator>
      <pubDate>Thu, 15 Feb 2024 04:36:20 +0000</pubDate>
      <link>https://forem.com/parth24072001/a-comprehensive-guide-to-using-react-querys-usequery-for-api-calls-fki</link>
      <guid>https://forem.com/parth24072001/a-comprehensive-guide-to-using-react-querys-usequery-for-api-calls-fki</guid>
      <description>&lt;p&gt;&lt;strong&gt;Introduction:&lt;/strong&gt;&lt;br&gt;
In modern React applications, managing asynchronous data fetching is crucial for delivering a seamless user experience. One powerful library that simplifies this process is react-query. This blog post will provide a comprehensive guide on how to use the useQuery hook from the react-query library for making API calls in a React application.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Setting Up the API Call Function:&lt;/strong&gt;&lt;br&gt;
Start by creating a function to handle the API call. In the provided code, getadmin is used to fetch admin information. Ensure that this function is capable of making the necessary API requests.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Creating a Custom Hook with useQuery:&lt;/strong&gt;&lt;br&gt;
Introduce a custom hook, such as useapiQuery, which utilizes useQuery from the react-query library. This hook encapsulates the logic for making API calls, managing loading states, and handling errors. Customize it based on your application's specific requirements.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Destructuring the Query Result:&lt;/strong&gt;&lt;br&gt;
Explore the various properties provided by the useQuery hook, such as data, isLoading, isFetching, error, and more. Destructuring these properties allows easy access to the current state of the data fetching process.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwn7rg0batz7jv2zm65rg.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwn7rg0batz7jv2zm65rg.jpeg" alt=" " width="599" height="269"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff0b4ev327xpgufjc0ikn.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff0b4ev327xpgufjc0ikn.jpeg" alt=" " width="800" height="404"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;isLoading:&lt;/strong&gt;&lt;br&gt;
Indicates whether the query is currently in a loading state.&lt;br&gt;
true if data is being fetched, false otherwise.&lt;br&gt;
Useful for displaying loading indicators or conditional rendering during data retrieval.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;isFetching:&lt;/strong&gt;&lt;br&gt;
Represents whether background refetching is currently occurring.&lt;br&gt;
true if the query is actively fetching data in the background, false otherwise.&lt;br&gt;
Useful for providing real-time updates or indicating ongoing background data refresh.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;error:&lt;/strong&gt;&lt;br&gt;
Contains any error that occurred during the data fetching process.&lt;br&gt;
When an error occurs, this property holds information about the error, enabling developers to handle and display error messages appropriately.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;errorUpdatedAt:&lt;/strong&gt;&lt;br&gt;
Indicates the timestamp when the error state was last updated.&lt;br&gt;
Useful for tracking when the error occurred and providing context on the timeline of error events.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;fetchStatus:&lt;/strong&gt;&lt;br&gt;
Represents the overall status of the data fetching process.&lt;br&gt;
Possible values include "idle," "loading," "success," or "error."&lt;br&gt;
Offers a high-level overview of the current state, aiding in conditional rendering and logic handling.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;isError:&lt;/strong&gt;&lt;br&gt;
A boolean flag indicating whether an error occurred during the data fetching process.&lt;br&gt;
true if an error occurred, false otherwise.&lt;br&gt;
Provides a simple way to check for errors and adjust the application's behavior accordingly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;isFetched:&lt;/strong&gt;&lt;br&gt;
A boolean flag indicating whether the data fetching process has been successfully completed.&lt;br&gt;
true if the data was fetched successfully, false otherwise.&lt;br&gt;
Helpful for determining if the data is available for rendering and performing subsequent actions.&lt;/p&gt;

&lt;p&gt;Certainly! The react-query library's useQuery hook provides a variety of properties that offer insights into the status and behavior of the asynchronous query&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fog1m8y1oxbfsahef32zk.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fog1m8y1oxbfsahef32zk.jpeg" alt=" " width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
