Skip to content

Commit f98d891

Browse files
anton-107pietern
andauthored
Introduce "psql" command to connect to Lakebase instances (#3128)
## Changes <!-- Brief summary of your changes that is easy to understand --> Adds "databricks psql" command to Databricks CLI Usage: `databricks psql <your db instance name>` Command also supports auto-complete and selector for the instance name: `databricks psql<ENTER>` for drop-down `databricks psql<TAB><TAB>` for auto-complete ## Why <!-- Why are these changes needed? Provide the context that the reviewer might be missing. For example, were there any decisions behind the change that are not reflected in the code itself? --> Lakebase provides API calls to fetch database credentials, but then it is up to the user to run their `psql` client and provide those credentials there. This change allows users to connect to their Lakebase instance with a single command ## Tests <!-- How have you tested the changes? --> Manual local tests via building the CLI and running the introduced command against Databricks workspace Local-only acceptance test added for Linux and Darwin platforms Manual end-to-end test on Windows platform <!-- If your PR needs to be included in the release notes for next release, add a separate entry in NEXT_CHANGELOG.md as part of your PR. --> --------- Co-authored-by: Pieter Noordhuis <pieter.noordhuis@databricks.com>
1 parent be5cb45 commit f98d891

File tree

18 files changed

+499
-0
lines changed

18 files changed

+499
-0
lines changed

NEXT_CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* Upgrade Go SDK to 0.75.0 ([#3256](https://github.com/databricks/cli/pull/3256))
99

1010
### CLI
11+
* Add `databricks psql` command to connect to Lakebase with a single command ([#3128](https://github.com/databricks/cli/pull/3128))
1112

1213
### Bundles
1314

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Local = true
2+
Cloud = false
3+
4+
[EnvMatrix]
5+
DATABRICKS_CLI_DEPLOYMENT = ["terraform", "direct-exp"]
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
=== Command should show instances names in autocomplete:
3+
my-database
4+
another-database
5+
:4
6+
Completion ended with directive: ShellCompDirectiveNoFileComp
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
title "Command should show instances names in autocomplete:\n"
2+
$CLI __complete psql ""
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[[Server]]
2+
Pattern = "GET /api/2.0/database/instances"
3+
Response.Body = '''
4+
{
5+
"database_instances": [
6+
{"name": "my-database"},
7+
{"name": "another-database"}
8+
]
9+
}
10+
'''
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Local = true
2+
Cloud = false
3+
4+
[EnvMatrix]
5+
DATABRICKS_CLI_DEPLOYMENT = ["terraform", "direct-exp"]
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
=== Command should exit if the instance is not available
3+
>>> musterr [CLI] psql database-starting
4+
Connecting to Databricks Database Instance database-starting ...
5+
Postgres version: 14
6+
Database instance status: STARTING
7+
Please retry when the instance becomes available
8+
Error: database instance is not ready for accepting connections
9+
10+
Exit code (musterr): 1
11+
12+
>>> musterr [CLI] psql database-updating
13+
Connecting to Databricks Database Instance database-updating ...
14+
Postgres version: 14
15+
Database instance status: UPDATING
16+
Please retry when the instance becomes available
17+
Error: database instance is not ready for accepting connections
18+
19+
Exit code (musterr): 1
20+
21+
>>> musterr [CLI] psql database-stopped
22+
Connecting to Databricks Database Instance database-stopped ...
23+
Postgres version: 14
24+
Database instance status: STOPPED
25+
Error: database instance is not ready for accepting connections
26+
27+
Exit code (musterr): 1
28+
29+
>>> musterr [CLI] psql database-failing-over
30+
Connecting to Databricks Database Instance database-failing-over ...
31+
Postgres version: 14
32+
Database instance status: FAILING_OVER
33+
Please retry when the instance becomes available
34+
Error: database instance is not ready for accepting connections
35+
36+
Exit code (musterr): 1
37+
38+
>>> musterr [CLI] psql database-deleting
39+
Connecting to Databricks Database Instance database-deleting ...
40+
Postgres version: 14
41+
Database instance status: DELETING
42+
Error: database instance is not ready for accepting connections
43+
44+
Exit code (musterr): 1
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
title "Command should exit if the instance is not available"
2+
trace musterr $CLI psql database-starting
3+
trace musterr $CLI psql database-updating
4+
trace musterr $CLI psql database-stopped
5+
trace musterr $CLI psql database-failing-over
6+
trace musterr $CLI psql database-deleting
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
[[Server]]
2+
Pattern = "GET /api/2.0/database/instances/database-starting"
3+
Response.Body = '''
4+
{
5+
"state": "STARTING",
6+
"pg_version": "14"
7+
}
8+
'''
9+
10+
[[Server]]
11+
Pattern = "GET /api/2.0/database/instances/database-updating"
12+
Response.Body = '''
13+
{
14+
"state": "UPDATING",
15+
"pg_version": "14"
16+
}
17+
'''
18+
19+
[[Server]]
20+
Pattern = "GET /api/2.0/database/instances/database-stopped"
21+
Response.Body = '''
22+
{
23+
"state": "STOPPED",
24+
"pg_version": "14"
25+
}
26+
'''
27+
28+
[[Server]]
29+
Pattern = "GET /api/2.0/database/instances/database-failing-over"
30+
Response.Body = '''
31+
{
32+
"state": "FAILING_OVER",
33+
"pg_version": "14"
34+
}
35+
'''
36+
37+
[[Server]]
38+
Pattern = "GET /api/2.0/database/instances/database-deleting"
39+
Response.Body = '''
40+
{
41+
"state": "DELETING",
42+
"pg_version": "14"
43+
}
44+
'''
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/bash
2+
#
3+
# This script prints its arguments and exits.
4+
# The test script renames this script to "psql" in order to capture the arguments that the CLI passes to psql command.
5+
#
6+
echo "echo-arguments.sh was called with the following arguments: $@"
7+
echo "PGPASSWORD=${PGPASSWORD}"
8+
echo "PGSSLMODE=${PGSSLMODE}"
9+
exit 0

0 commit comments

Comments
 (0)