diff --git a/src/commands/database/db-status.ts b/src/commands/database/db-status.ts index 346024c3384..a21a01ec49c 100644 --- a/src/commands/database/db-status.ts +++ b/src/commands/database/db-status.ts @@ -410,9 +410,13 @@ export const statusDb = async (options: DatabaseStatusOptions, command: BaseComm throw new Error(`You must be logged in with ${netlifyCommand()} login to target a remote branch.`) } - connectionString = await fetchBranchConnectionString({ siteId, accessToken, basePath }, branch) - fetchApplied = remoteAppliedMigrations({ siteId, accessToken, basePath, branch }) branchLabel = branch + if (siteHasDatabase) { + connectionString = await fetchBranchConnectionString({ siteId, accessToken, basePath }, branch) + fetchApplied = remoteAppliedMigrations({ siteId, accessToken, basePath, branch }) + } else { + fetchApplied = () => Promise.resolve([]) + } } else { isLocal = true branchLabel = 'local' diff --git a/src/commands/database/util/applied-migrations.ts b/src/commands/database/util/applied-migrations.ts index 1fecaf34634..42cf733a980 100644 --- a/src/commands/database/util/applied-migrations.ts +++ b/src/commands/database/util/applied-migrations.ts @@ -37,7 +37,7 @@ interface RemoteMigration { } interface ListMigrationsResponse { - migrations: RemoteMigration[] + migrations: RemoteMigration[] | null } export const remoteAppliedMigrations = @@ -59,7 +59,9 @@ export const remoteAppliedMigrations = } const data = (await response.json()) as ListMigrationsResponse - return data.migrations.filter((m) => m.applied).map((m) => ({ version: m.version, name: m.name, path: m.path })) + return ( + data.migrations?.filter((m) => m.applied).map((m) => ({ version: m.version, name: m.name, path: m.path })) ?? [] + ) } interface LocalOptions { diff --git a/tests/unit/commands/database/db-status.test.ts b/tests/unit/commands/database/db-status.test.ts index 8dc31ae79d3..2c6afed971e 100644 --- a/tests/unit/commands/database/db-status.test.ts +++ b/tests/unit/commands/database/db-status.test.ts @@ -767,6 +767,26 @@ describe('statusDb', () => { expect(jsonMessages[0]).toMatchObject({ target: 'feature-x' }) }) + test('does not fetch branch connection string or remote applied migrations when database is not enabled', async () => { + setupFetchRouter({ siteDatabase: null }) + + await expect(statusDb({ branch: 'feature-x' }, createMockCommand())).resolves.toBeUndefined() + + const fetchedUrls = mockFetch.mock.calls.map((c) => { + const u = c[0] as URL | string + return typeof u === 'string' ? u : u.toString() + }) + expect(fetchedUrls.some((u) => u.includes('/database/branch/'))).toBe(false) + expect(fetchedUrls.some((u) => u.includes('/database/migrations'))).toBe(false) + expect(mockConnectToDatabase).not.toHaveBeenCalled() + + const output = logMessages.join('\n') + expect(output).toContain('Netlify Database is not enabled for this project') + expect(output).toContain('Applied migrations (0)') + expect(output).toContain('Pending migrations (0)') + expect(output).not.toContain('Connected to database branch') + }) + test('throws a helpful error when the branch endpoint 404s', async () => { setupFetchRouter({ siteDatabase: { connection_string: PROD_CONN } })