-
Notifications
You must be signed in to change notification settings - Fork 27
Replace manual config confirmations with automated cross-reference readiness check #38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Zertsov
wants to merge
6
commits into
main
Choose a base branch
from
voz/supa-migration
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e7e9f47
Add Supabase auto-export command and improve name extraction
Zertsov 84beacf
Add OAuth provider pre-flight check for Supabase migrations
Zertsov f3de174
Fetch OAuth providers from Supabase auth config instead of scanning u…
Zertsov 1d8e3ef
Replace manual config confirmations with automated cross-reference re…
Zertsov fb29ec6
Merge branch 'main' into voz/supa-migration
royanger 7f5ccb6
chore: Fixed lint errors and warings
royanger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| /** | ||
| * Supabase user export CLI | ||
| * | ||
| * Exports users from a Supabase Postgres database to a JSON file | ||
| * compatible with the migration script's Supabase transformer. | ||
| * | ||
| * Usage: | ||
| * bun run export:supabase | ||
| * bun run export:supabase --db-url postgresql://... --output users.json | ||
| * | ||
| * Environment variables: | ||
| * SUPABASE_DB_URL - Postgres connection string (alternative to --db-url flag) | ||
| */ | ||
| import 'dotenv/config'; | ||
| import * as p from '@clack/prompts'; | ||
| import color from 'picocolors'; | ||
| import { displayExportSummary, exportSupabaseUsers } from './supabase'; | ||
|
|
||
| async function main() { | ||
| p.intro(color.bgCyan(color.black('Supabase User Export'))); | ||
|
|
||
| // Parse CLI flags | ||
| const args = process.argv.slice(2); | ||
| let dbUrl = process.env.SUPABASE_DB_URL; | ||
| let outputFile = 'supabase-export.json'; | ||
|
|
||
| for (let i = 0; i < args.length; i++) { | ||
| if (args[i] === '--db-url' && args[i + 1]) { | ||
| dbUrl = args[i + 1]; | ||
| i++; | ||
| } else if (args[i] === '--output' && args[i + 1]) { | ||
| outputFile = args[i + 1]; | ||
| i++; | ||
| } | ||
| } | ||
|
|
||
| // Prompt for DB URL if not provided | ||
| if (!dbUrl) { | ||
| p.note( | ||
| `Find this in the Supabase Dashboard by clicking the ${color.bold('Connect')} button.\n\n${color.bold('Direct connection')} (requires IPv6):\n ${color.dim('postgresql://postgres:[PASSWORD]@db.[REF].supabase.co:5432/postgres')}\n\n${color.bold('Pooler connection')} (works on IPv4 — use this if direct fails):\n ${color.dim('postgres://postgres.[REF]:[PASSWORD]@aws-0-[REGION].pooler.supabase.com:6543/postgres')}\n\n${color.dim('Alternatively, run the export SQL in the Supabase SQL Editor and save the result as JSON.')}`, | ||
| 'Connection String' | ||
| ); | ||
|
|
||
| const input = await p.text({ | ||
| message: 'Enter your Supabase Postgres connection string', | ||
| placeholder: | ||
| 'postgresql://postgres:[PASSWORD]@db.[REF].supabase.co:5432/postgres', | ||
| validate: (value) => { | ||
| if (!value || value.trim() === '') { | ||
| return 'Connection string is required'; | ||
| } | ||
| if ( | ||
| !value.startsWith('postgresql://') && | ||
| !value.startsWith('postgres://') | ||
| ) { | ||
| return 'Must be a valid Postgres connection string (postgresql://...)'; | ||
| } | ||
| }, | ||
| }); | ||
|
|
||
| if (p.isCancel(input)) { | ||
| p.cancel('Export cancelled.'); | ||
| process.exit(0); | ||
| } | ||
|
|
||
| dbUrl = input; | ||
| } | ||
|
|
||
| const spinner = p.spinner(); | ||
| spinner.start('Connecting to Supabase database...'); | ||
|
|
||
| try { | ||
| const result = await exportSupabaseUsers(dbUrl, outputFile); | ||
| spinner.stop(`Found ${result.userCount} users`); | ||
|
|
||
| displayExportSummary(result); | ||
|
|
||
| p.log.info( | ||
| color.dim( | ||
| `Next step: run ${color.bold('bun run migrate')} and select "Supabase" with file "${outputFile}"` | ||
| ) | ||
| ); | ||
|
|
||
| p.outro(color.green('Export complete!')); | ||
| } catch (err) { | ||
| spinner.stop('Export failed'); | ||
| const message = err instanceof Error ? err.message : String(err); | ||
| p.log.error(color.red(message)); | ||
| process.exit(1); | ||
| } | ||
| } | ||
|
|
||
| void main(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| /** | ||
| * Supabase user export module | ||
| * | ||
| * Connects to a Supabase Postgres database and exports users from the auth.users table | ||
| * in a format compatible with the Supabase migration transformer. | ||
| * | ||
| * Includes: | ||
| * - encrypted_password (bcrypt hashes) — not available via Supabase Admin API | ||
| * - first_name extracted from raw_user_meta_data.display_name | ||
| * - All standard auth fields (email, phone, confirmation status, metadata) | ||
| */ | ||
| import { Client } from 'pg'; | ||
| import fs from 'fs'; | ||
| import path from 'path'; | ||
| import * as p from '@clack/prompts'; | ||
| import color from 'picocolors'; | ||
|
|
||
| /** | ||
| * SQL query that exports users in the format expected by the Supabase transformer. | ||
| * | ||
| * Extracts display_name from raw_user_meta_data as first_name so the transformer | ||
| * can map it directly without custom SQL from the user. | ||
| */ | ||
| const EXPORT_QUERY = ` | ||
| SELECT | ||
| id, | ||
| email, | ||
| email_confirmed_at, | ||
| encrypted_password, | ||
| phone, | ||
| phone_confirmed_at, | ||
| COALESCE( | ||
| raw_user_meta_data->>'display_name', | ||
| raw_user_meta_data->>'first_name', | ||
| raw_user_meta_data->>'name' | ||
| ) as first_name, | ||
| raw_user_meta_data->>'last_name' as last_name, | ||
| raw_user_meta_data, | ||
| raw_app_meta_data, | ||
| created_at | ||
| FROM auth.users | ||
| ORDER BY created_at | ||
| `; | ||
|
|
||
| interface ExportResult { | ||
| userCount: number; | ||
| outputPath: string; | ||
| fieldCoverage: { | ||
| email: number; | ||
| emailConfirmed: number; | ||
| password: number; | ||
| phone: number; | ||
| firstName: number; | ||
| lastName: number; | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Exports users from a Supabase Postgres database to a JSON file. | ||
| * | ||
| * @param dbUrl - Postgres connection string (e.g., postgresql://postgres:[email protected]:5432/postgres) | ||
| * @param outputFile - Output file path (relative to project root) | ||
| * @returns Export result with user count and field coverage stats | ||
| */ | ||
| export async function exportSupabaseUsers( | ||
| dbUrl: string, | ||
| outputFile: string | ||
| ): Promise<ExportResult> { | ||
| const client = new Client({ connectionString: dbUrl }); | ||
|
|
||
| try { | ||
| await client.connect(); | ||
| } catch (err) { | ||
| const message = err instanceof Error ? err.message : String(err); | ||
| throw new Error( | ||
| `Failed to connect to Supabase database: ${message}\n\n` + | ||
| `Connection string format: postgresql://postgres:[PASSWORD]@db.[PROJECT_REF].supabase.co:5432/postgres\n` + | ||
| `Find this in Supabase Dashboard → Settings → Database → Connection string` | ||
| ); | ||
| } | ||
|
|
||
| try { | ||
| interface SupabaseUserRow { | ||
| email: string | null; | ||
| email_confirmed_at: string | null; | ||
| encrypted_password: string | null; | ||
| phone: string | null; | ||
| first_name: string | null; | ||
| last_name: string | null; | ||
| [key: string]: unknown; | ||
| } | ||
|
|
||
| const { rows } = await client.query<SupabaseUserRow>(EXPORT_QUERY); | ||
|
|
||
| // Calculate field coverage | ||
| const coverage = { | ||
| email: 0, | ||
| emailConfirmed: 0, | ||
| password: 0, | ||
| phone: 0, | ||
| firstName: 0, | ||
| lastName: 0, | ||
| }; | ||
|
|
||
| for (const row of rows) { | ||
| if (row.email) coverage.email++; | ||
| if (row.email_confirmed_at) coverage.emailConfirmed++; | ||
| if (row.encrypted_password) coverage.password++; | ||
| if (row.phone) coverage.phone++; | ||
| if (row.first_name) coverage.firstName++; | ||
| if (row.last_name) coverage.lastName++; | ||
| } | ||
|
|
||
| // Write output | ||
| const outputPath = path.isAbsolute(outputFile) | ||
| ? outputFile | ||
| : path.join(process.cwd(), outputFile); | ||
|
|
||
| fs.writeFileSync(outputPath, JSON.stringify(rows, null, 2)); | ||
|
|
||
| return { | ||
| userCount: rows.length, | ||
| outputPath, | ||
| fieldCoverage: coverage, | ||
| }; | ||
| } finally { | ||
| await client.end(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Displays a summary of the export results with field coverage stats. | ||
| */ | ||
| export function displayExportSummary(result: ExportResult): void { | ||
| const { userCount, outputPath, fieldCoverage } = result; | ||
|
|
||
| const getIcon = (count: number, total: number): string => { | ||
| if (count === total) return color.green('●'); | ||
| if (count > 0) return color.yellow('○'); | ||
| return color.dim('○'); | ||
| }; | ||
|
|
||
| let summary = ''; | ||
| summary += `${getIcon(fieldCoverage.email, userCount)} ${color.dim(`${fieldCoverage.email}/${userCount} have email`)}\n`; | ||
| summary += `${getIcon(fieldCoverage.emailConfirmed, userCount)} ${color.dim(`${fieldCoverage.emailConfirmed}/${userCount} email confirmed`)}\n`; | ||
| summary += `${getIcon(fieldCoverage.password, userCount)} ${color.dim(`${fieldCoverage.password}/${userCount} have password hash`)}\n`; | ||
| summary += `${getIcon(fieldCoverage.phone, userCount)} ${color.dim(`${fieldCoverage.phone}/${userCount} have phone`)}\n`; | ||
| summary += `${getIcon(fieldCoverage.firstName, userCount)} ${color.dim(`${fieldCoverage.firstName}/${userCount} have first name`)}\n`; | ||
| summary += `${getIcon(fieldCoverage.lastName, userCount)} ${color.dim(`${fieldCoverage.lastName}/${userCount} have last name`)}`; | ||
|
|
||
| p.note(summary, 'Field Coverage'); | ||
| p.log.success( | ||
| `Exported ${color.bold(String(userCount))} users to ${color.dim(outputPath)}` | ||
| ); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Can we get the params and return info here for consistency?