Skip to content

Commit

Permalink
fix: validate database name passed to cs command (#212)
Browse files Browse the repository at this point in the history
  • Loading branch information
duskpoet authored May 2, 2024
1 parent e40ce2b commit 3ca38c3
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
{
"databases": [
{ "name": "db1", "owner_name": "user1" },
{ "name": "db2", "owner_name": "user2" }
{ "name": "db2", "owner_name": "user2" },
{
"name": "test_db",
"owner_name": "test_user"
}
]
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"branch": {
"name": "test-branch-sunny",
"name": "test_branch",
"id": "br-sunny-branch-123456",
"primary": true,
"parent_id": "br-parent-branch-123456",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"databases": [
{ "name": "db1", "owner_name": "user1" },
{ "name": "db2", "owner_name": "user2" }
{ "name": "db2", "owner_name": "user2" },
{ "name": "test_db", "owner_name": "test_user" }
]
}
4 changes: 2 additions & 2 deletions snapshots/commands/branches.test.snap
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ created_at: 2021-01-01T00:00:00.000Z
`;

exports[`branches get by id test 1`] = `
"name: test-branch-sunny
"name: test_branch
id: br-sunny-branch-123456
primary: true
parent_id: br-parent-branch-123456
Expand All @@ -135,7 +135,7 @@ updated_at: 2019-01-01T00:00:00Z
`;

exports[`branches get by name test 1`] = `
"name: test-branch-sunny
"name: test_branch
id: br-sunny-branch-123456
primary: true
parent_id: br-parent-branch-123456
Expand Down
2 changes: 2 additions & 0 deletions snapshots/commands/databases.test.snap
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@ exports[`databases list test 1`] = `
owner_name: user1
- name: db2
owner_name: user2
- name: test_db
owner_name: test_user
"
`;
2 changes: 1 addition & 1 deletion snapshots/commands/set_context.test.snap
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`set_context should set the context get branch id overrides context set branch test 1`] = `
"name: test-branch-sunny
"name: test_branch
id: br-sunny-branch-123456
primary: true
parent_id: br-parent-branch-123456
Expand Down
20 changes: 19 additions & 1 deletion src/commands/connection_string.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe } from '@jest/globals';
import { describe, expect } from '@jest/globals';
import { testCliCommand } from '../test_utils/test_cli_command';

describe('connection_string', () => {
Expand Down Expand Up @@ -247,4 +247,22 @@ describe('connection_string', () => {
snapshot: true,
},
});

testCliCommand({
name: 'connection_string fails for non-existing database',
args: [
'connection-string',
'test_branch',
'--project-id',
'test',
'--database-name',
'non_existing_db',
'--role-name',
'test_role',
],
expected: {
code: 1,
stderr: expect.stringMatching(/Database not found/),
},
});
});
36 changes: 21 additions & 15 deletions src/commands/connection_string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,23 +131,29 @@ export const handler = async (
);
}));

const {
data: { databases: branchDatabases },
} = await props.apiClient.listProjectBranchDatabases(projectId, branchId);

const database =
props.databaseName ||
(await props.apiClient
.listProjectBranchDatabases(projectId, branchId)
.then(({ data }) => {
if (data.databases.length === 0) {
throw new Error(`No databases found for the branch: ${branchId}`);
}
if (data.databases.length === 1) {
return data.databases[0].name;
}
throw new Error(
`Multiple databases found for the branch, please provide one with the --database-name option: ${data.databases
.map((d) => d.name)
.join(', ')}`,
);
}));
(() => {
if (branchDatabases.length === 0) {
throw new Error(`No databases found for the branch: ${branchId}`);
}
if (branchDatabases.length === 1) {
return branchDatabases[0].name;
}
throw new Error(
`Multiple databases found for the branch, please provide one with the --database-name option: ${branchDatabases
.map((d) => d.name)
.join(', ')}`,
);
})();

if (!branchDatabases.find((d) => d.name === database)) {
throw new Error(`Database not found: ${database}`);
}

const {
data: { password },
Expand Down

0 comments on commit 3ca38c3

Please sign in to comment.