-
Notifications
You must be signed in to change notification settings - Fork 16
feat: add Installer for no-CLI platforms like Lovable #551
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
jumski
wants to merge
1
commit into
12-11-add_migration_installer_api_endpoints_for_no-cli_platforms
Choose a base branch
from
12-19-add_installer.run_api_for_simplified_lovable_integration
base: 12-11-add_migration_installer_api_endpoints_for_no-cli_platforms
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
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,8 @@ | ||
| import { createInstallerHandler } from './server.ts'; | ||
|
|
||
| export const Installer = { | ||
| run: (token: string) => { | ||
| const handler = createInstallerHandler(token); | ||
| Deno.serve({}, handler); | ||
| }, | ||
| }; |
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,176 @@ | ||
| import type { InstallerResult, StepResult } from './types.ts'; | ||
| import postgres from 'postgres'; | ||
| import { MigrationRunner } from '../control-plane/migrations/index.ts'; | ||
| import { extractProjectId } from '../control-plane/server.ts'; | ||
|
|
||
| // Dependency injection for testability | ||
| export interface InstallerDeps { | ||
| getEnv: (key: string) => string | undefined; | ||
| } | ||
|
|
||
| const defaultDeps: InstallerDeps = { | ||
| getEnv: (key) => Deno.env.get(key), | ||
| }; | ||
|
|
||
| export function createInstallerHandler( | ||
| expectedToken: string, | ||
| deps: InstallerDeps = defaultDeps | ||
| ): (req: Request) => Promise<Response> { | ||
| return async (req: Request) => { | ||
| // Validate token from query params first (fail fast) | ||
| const url = new URL(req.url); | ||
| const token = url.searchParams.get('token'); | ||
|
|
||
| if (token !== expectedToken) { | ||
| return jsonResponse( | ||
| { | ||
| success: false, | ||
| message: | ||
| 'Invalid or missing token. Use the exact URL from your Lovable prompt.', | ||
| }, | ||
| 401 | ||
| ); | ||
| } | ||
|
|
||
| // Read env vars inside handler (not at module level) | ||
| const supabaseUrl = deps.getEnv('SUPABASE_URL'); | ||
| const serviceRoleKey = deps.getEnv('SUPABASE_SERVICE_ROLE_KEY'); | ||
| const dbUrl = deps.getEnv('SUPABASE_DB_URL'); | ||
|
|
||
| if (!supabaseUrl || !serviceRoleKey) { | ||
| return jsonResponse( | ||
| { | ||
| success: false, | ||
| message: 'Missing SUPABASE_URL or SUPABASE_SERVICE_ROLE_KEY', | ||
| }, | ||
| 500 | ||
| ); | ||
| } | ||
|
|
||
| if (!dbUrl) { | ||
| return jsonResponse( | ||
| { | ||
| success: false, | ||
| message: 'Missing SUPABASE_DB_URL', | ||
| }, | ||
| 500 | ||
| ); | ||
| } | ||
|
|
||
| console.log('pgflow installer starting...'); | ||
|
|
||
| // Create database connection | ||
| const sql = postgres(dbUrl, { prepare: false }); | ||
|
|
||
| let secrets: StepResult; | ||
| let migrations: StepResult; | ||
|
|
||
| try { | ||
| // Step 1: Configure vault secrets | ||
| console.log('Configuring vault secrets...'); | ||
| secrets = await configureSecrets(sql, supabaseUrl, serviceRoleKey); | ||
|
|
||
| if (!secrets.success) { | ||
| const result: InstallerResult = { | ||
| success: false, | ||
| secrets, | ||
| migrations: { | ||
| success: false, | ||
| status: 0, | ||
| error: 'Skipped - secrets failed', | ||
| }, | ||
| message: 'Failed to configure vault secrets.', | ||
| }; | ||
| return jsonResponse(result, 500); | ||
| } | ||
|
|
||
| // Step 2: Run migrations | ||
| console.log('Running migrations...'); | ||
| migrations = await runMigrations(sql); | ||
|
|
||
| const result: InstallerResult = { | ||
| success: secrets.success && migrations.success, | ||
| secrets, | ||
| migrations, | ||
| message: migrations.success | ||
| ? 'pgflow installed successfully! Vault secrets configured and migrations applied.' | ||
| : 'Secrets configured but migrations failed. Check the error details.', | ||
| }; | ||
|
|
||
| console.log('Installer complete:', result.message); | ||
| return jsonResponse(result, result.success ? 200 : 500); | ||
| } finally { | ||
| await sql.end(); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Configure vault secrets for pgflow | ||
| */ | ||
| async function configureSecrets( | ||
| sql: postgres.Sql, | ||
| supabaseUrl: string, | ||
| serviceRoleKey: string | ||
| ): Promise<StepResult> { | ||
| try { | ||
| const projectId = extractProjectId(supabaseUrl); | ||
| if (!projectId) { | ||
| return { | ||
| success: false, | ||
| status: 500, | ||
| error: 'Could not extract project ID from SUPABASE_URL', | ||
| }; | ||
| } | ||
|
|
||
| // Upsert secrets (delete + create pattern) in single transaction | ||
| await sql.begin(async (tx) => { | ||
| await tx`DELETE FROM vault.secrets WHERE name = 'supabase_project_id'`; | ||
| await tx`SELECT vault.create_secret(${projectId}, 'supabase_project_id')`; | ||
|
|
||
| await tx`DELETE FROM vault.secrets WHERE name = 'supabase_service_role_key'`; | ||
| await tx`SELECT vault.create_secret(${serviceRoleKey}, 'supabase_service_role_key')`; | ||
| }); | ||
|
|
||
| return { | ||
| success: true, | ||
| status: 200, | ||
| data: { configured: ['supabase_project_id', 'supabase_service_role_key'] }, | ||
| }; | ||
| } catch (error) { | ||
| return { | ||
| success: false, | ||
| status: 500, | ||
| error: error instanceof Error ? error.message : 'Unknown error', | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Run pending migrations | ||
| */ | ||
| async function runMigrations(sql: postgres.Sql): Promise<StepResult> { | ||
| try { | ||
| const runner = new MigrationRunner(sql); | ||
| const result = await runner.up(); | ||
|
|
||
| return { | ||
| success: result.success, | ||
| status: result.success ? 200 : 500, | ||
| data: result, | ||
| }; | ||
| } catch (error) { | ||
| return { | ||
| success: false, | ||
| status: 500, | ||
| error: error instanceof Error ? error.message : 'Unknown error', | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| function jsonResponse(data: unknown, status: number): Response { | ||
| return new Response(JSON.stringify(data, null, 2), { | ||
| status, | ||
| headers: { 'Content-Type': 'application/json' }, | ||
| }); | ||
| } |
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,13 @@ | ||
| export interface StepResult { | ||
| success: boolean; | ||
| status: number; | ||
| data?: unknown; | ||
| error?: string; | ||
| } | ||
|
|
||
| export interface InstallerResult { | ||
| success: boolean; | ||
| secrets: StepResult; | ||
| migrations: StepResult; | ||
| message: string; | ||
| } |
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,118 @@ | ||
| import { assertEquals, assertMatch } from '@std/assert'; | ||
| import { | ||
| createInstallerHandler, | ||
| type InstallerDeps, | ||
| } from '../../../src/installer/server.ts'; | ||
|
|
||
| // Helper to create mock dependencies | ||
| function createMockDeps(overrides?: Partial<InstallerDeps>): InstallerDeps { | ||
| return { | ||
| getEnv: (key: string) => | ||
| ({ | ||
| SUPABASE_URL: 'https://test-project.supabase.co', | ||
| SUPABASE_SERVICE_ROLE_KEY: 'test-service-role-key', | ||
| SUPABASE_DB_URL: 'postgresql://postgres:postgres@localhost:54322/postgres', | ||
| })[key], | ||
| ...overrides, | ||
| }; | ||
| } | ||
|
|
||
| // Helper to create a request with optional token | ||
| function createRequest(token?: string): Request { | ||
| const url = token | ||
| ? `http://localhost/pgflow-installer?token=${token}` | ||
| : 'http://localhost/pgflow-installer'; | ||
| return new Request(url); | ||
| } | ||
|
|
||
| // ============================================================ | ||
| // Token validation tests | ||
| // ============================================================ | ||
|
|
||
| Deno.test('Installer Handler - returns 401 when token missing', async () => { | ||
| const deps = createMockDeps(); | ||
| const handler = createInstallerHandler('expected-token', deps); | ||
|
|
||
| const request = createRequest(); // no token | ||
| const response = await handler(request); | ||
|
|
||
| assertEquals(response.status, 401); | ||
| const data = await response.json(); | ||
| assertEquals(data.success, false); | ||
| assertMatch(data.message, /Invalid or missing token/); | ||
| }); | ||
|
|
||
| Deno.test('Installer Handler - returns 401 when token incorrect', async () => { | ||
| const deps = createMockDeps(); | ||
| const handler = createInstallerHandler('expected-token', deps); | ||
|
|
||
| const request = createRequest('wrong-token'); | ||
| const response = await handler(request); | ||
|
|
||
| assertEquals(response.status, 401); | ||
| const data = await response.json(); | ||
| assertEquals(data.success, false); | ||
| assertMatch(data.message, /Invalid or missing token/); | ||
| }); | ||
|
|
||
| // ============================================================ | ||
| // Environment variable validation tests | ||
| // ============================================================ | ||
|
|
||
| Deno.test('Installer Handler - returns 500 when SUPABASE_URL missing', async () => { | ||
| const deps = createMockDeps({ | ||
| getEnv: (key: string) => | ||
| ({ | ||
| SUPABASE_SERVICE_ROLE_KEY: 'test-key', | ||
| // SUPABASE_URL is undefined | ||
| })[key], | ||
| }); | ||
| const handler = createInstallerHandler('valid-token', deps); | ||
|
|
||
| const request = createRequest('valid-token'); | ||
| const response = await handler(request); | ||
|
|
||
| assertEquals(response.status, 500); | ||
| const data = await response.json(); | ||
| assertEquals(data.success, false); | ||
| assertMatch(data.message, /Missing SUPABASE_URL or SUPABASE_SERVICE_ROLE_KEY/); | ||
| }); | ||
|
|
||
| Deno.test('Installer Handler - returns 500 when SUPABASE_SERVICE_ROLE_KEY missing', async () => { | ||
| const deps = createMockDeps({ | ||
| getEnv: (key: string) => | ||
| ({ | ||
| SUPABASE_URL: 'https://test.supabase.co', | ||
| // SUPABASE_SERVICE_ROLE_KEY is undefined | ||
| })[key], | ||
| }); | ||
| const handler = createInstallerHandler('valid-token', deps); | ||
|
|
||
| const request = createRequest('valid-token'); | ||
| const response = await handler(request); | ||
|
|
||
| assertEquals(response.status, 500); | ||
| const data = await response.json(); | ||
| assertEquals(data.success, false); | ||
| assertMatch(data.message, /Missing SUPABASE_URL or SUPABASE_SERVICE_ROLE_KEY/); | ||
| }); | ||
|
|
||
| Deno.test('Installer Handler - returns 500 when SUPABASE_DB_URL missing', async () => { | ||
| const deps = createMockDeps({ | ||
| getEnv: (key: string) => | ||
| ({ | ||
| SUPABASE_URL: 'https://test.supabase.co', | ||
| SUPABASE_SERVICE_ROLE_KEY: 'test-key', | ||
| // SUPABASE_DB_URL is undefined | ||
| })[key], | ||
| }); | ||
| const handler = createInstallerHandler('valid-token', deps); | ||
|
|
||
| const request = createRequest('valid-token'); | ||
| const response = await handler(request); | ||
|
|
||
| assertEquals(response.status, 500); | ||
| const data = await response.json(); | ||
| assertEquals(data.success, false); | ||
| assertMatch(data.message, /Missing SUPABASE_DB_URL/); | ||
| }); | ||
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.
Critical: Tests mock the wrong implementation entirely
The tests mock a
fetchfunction and test HTTP calls to Control Plane endpoints (/secrets/configure,/migrations/up), but the actual implementation inserver.tsdoesn't use HTTP at all.The real implementation:
configureSecrets(sql, supabaseUrl, serviceRoleKey)which executes SQL queriesrunMigrations(sql)which usesMigrationRunnerThe tests:
fetch(not used by real code)https://test-project.supabase.co/functions/v1/pgflow/secrets/configureThese tests will pass but provide zero coverage of the actual functionality. The real code could be completely broken and these tests would still pass.
Fix: Rewrite tests to:
postgresclient instead offetchconfigureSecrets()andrunMigrations()Spotted by Graphite Agent

Is this helpful? React 👍 or 👎 to let us know.