Skip to content
Open
8 changes: 6 additions & 2 deletions src/commands/database/db-status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This explicitly is not using enabled check, because when used with @netlify/database-proxy and NETLIFY_DB_URL (IIRC) it is reported as true even if database is not actually created yet, so fetchBranchConnectionString that is hitting an API would return failure.

The enabled logic might need some more thinking about desired interaction with @netlify/database-proxy, but this seems a bit out of scope for this change.

connectionString = await fetchBranchConnectionString({ siteId, accessToken, basePath }, branch)
fetchApplied = remoteAppliedMigrations({ siteId, accessToken, basePath, branch })
} else {
fetchApplied = () => Promise.resolve([])
}
} else {
isLocal = true
branchLabel = 'local'
Expand Down
6 changes: 4 additions & 2 deletions src/commands/database/util/applied-migrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ interface RemoteMigration {
}

interface ListMigrationsResponse {
migrations: RemoteMigration[]
migrations: RemoteMigration[] | null
}

export const remoteAppliedMigrations =
Expand All @@ -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 {
Expand Down
20 changes: 20 additions & 0 deletions tests/unit/commands/database/db-status.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 } })

Expand Down
Loading