Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
1c8cee8
Introduce "connect" command to connect to Lakebase instances
anton-107 Jun 26, 2025
930dcb9
Psql command (#3209)
anton-107 Jul 9, 2025
a612406
fix acceptance test for help
anton-107 Jul 9, 2025
6f21257
do not use default dbname if it is provided in extra args
anton-107 Jul 9, 2025
84ad8ba
do not use default port if it is provided in extra args
anton-107 Jul 9, 2025
9e8153d
remove test for condition when psql is not found in path
anton-107 Jul 9, 2025
967236b
acceptance test should use fake psql command
anton-107 Jul 9, 2025
2e08e8c
change the path in the acc script so that fake psql is found in path …
anton-107 Jul 9, 2025
2fc6e37
Disable psql test on Windows runners (#3241)
anton-107 Jul 11, 2025
2d8e3cc
set echo-arguments.sh as executable
anton-107 Jul 11, 2025
8e87091
improve parameters validation + add tests
anton-107 Jul 11, 2025
4f3284d
improve error messaging
anton-107 Jul 11, 2025
abb336d
add NEXT_CHANGELOG.md
anton-107 Jul 11, 2025
92ed62f
omit Args field for psql command
anton-107 Jul 14, 2025
4d71f3b
omit Args field for psql command
anton-107 Jul 14, 2025
c853383
Update NEXT_CHANGELOG.md
anton-107 Jul 14, 2025
b868d6f
replace printing errors with returning formatted errors from connect.go
anton-107 Jul 14, 2025
e28dbb0
use cmdio.LogString to output lines
anton-107 Jul 14, 2025
d78ff0e
change output lines for status and postgres version
anton-107 Jul 14, 2025
9c0632e
change output lines for status and postgres version, fix the spacing
anton-107 Jul 15, 2025
d864462
show drop-down with instances name in case the instance name is not s…
anton-107 Jul 15, 2025
b7e8e33
add completions for psql command (#3257)
anton-107 Jul 16, 2025
99cda31
only try to connect if database instance is in AVAILABLE state
anton-107 Jul 16, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEXT_CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
### Dependency updates

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

### Bundles

Expand Down
5 changes: 5 additions & 0 deletions acceptance/cmd/psql/completions/out.test.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Local = true
Cloud = false

[EnvMatrix]
DATABRICKS_CLI_DEPLOYMENT = ["terraform", "direct-exp"]
6 changes: 6 additions & 0 deletions acceptance/cmd/psql/completions/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

=== Command should show instances names in autocomplete:
my-database
another-database
:4
Completion ended with directive: ShellCompDirectiveNoFileComp
2 changes: 2 additions & 0 deletions acceptance/cmd/psql/completions/script
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
title "Command should show instances names in autocomplete:\n"
$CLI __complete psql ""
10 changes: 10 additions & 0 deletions acceptance/cmd/psql/completions/test.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[[Server]]
Pattern = "GET /api/2.0/database/instances"
Response.Body = '''
{
"database_instances": [
{"name": "my-database"},
{"name": "another-database"}
]
}
'''
5 changes: 5 additions & 0 deletions acceptance/cmd/psql/not-available/out.test.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Local = true
Cloud = false

[EnvMatrix]
DATABRICKS_CLI_DEPLOYMENT = ["terraform", "direct-exp"]
44 changes: 44 additions & 0 deletions acceptance/cmd/psql/not-available/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

=== Command should exit if the instance is not available
>>> musterr [CLI] psql database-starting
Connecting to Databricks Database Instance database-starting ...
Postgres version: 14
Database instance status: STARTING
Please retry when the instance becomes available
Error: database instance is not ready for accepting connections

Exit code (musterr): 1

>>> musterr [CLI] psql database-updating
Connecting to Databricks Database Instance database-updating ...
Postgres version: 14
Database instance status: UPDATING
Please retry when the instance becomes available
Error: database instance is not ready for accepting connections

Exit code (musterr): 1

>>> musterr [CLI] psql database-stopped
Connecting to Databricks Database Instance database-stopped ...
Postgres version: 14
Database instance status: STOPPED
Error: database instance is not ready for accepting connections

Exit code (musterr): 1

>>> musterr [CLI] psql database-failing-over
Connecting to Databricks Database Instance database-failing-over ...
Postgres version: 14
Database instance status: FAILING_OVER
Please retry when the instance becomes available
Error: database instance is not ready for accepting connections

Exit code (musterr): 1

>>> musterr [CLI] psql database-deleting
Connecting to Databricks Database Instance database-deleting ...
Postgres version: 14
Database instance status: DELETING
Error: database instance is not ready for accepting connections

Exit code (musterr): 1
6 changes: 6 additions & 0 deletions acceptance/cmd/psql/not-available/script
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
title "Command should exit if the instance is not available"
trace musterr $CLI psql database-starting
trace musterr $CLI psql database-updating
trace musterr $CLI psql database-stopped
trace musterr $CLI psql database-failing-over
trace musterr $CLI psql database-deleting
44 changes: 44 additions & 0 deletions acceptance/cmd/psql/not-available/test.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
[[Server]]
Pattern = "GET /api/2.0/database/instances/database-starting"
Response.Body = '''
{
"state": "STARTING",
"pg_version": "14"
}
'''

[[Server]]
Pattern = "GET /api/2.0/database/instances/database-updating"
Response.Body = '''
{
"state": "UPDATING",
"pg_version": "14"
}
'''

[[Server]]
Pattern = "GET /api/2.0/database/instances/database-stopped"
Response.Body = '''
{
"state": "STOPPED",
"pg_version": "14"
}
'''

[[Server]]
Pattern = "GET /api/2.0/database/instances/database-failing-over"
Response.Body = '''
{
"state": "FAILING_OVER",
"pg_version": "14"
}
'''

[[Server]]
Pattern = "GET /api/2.0/database/instances/database-deleting"
Response.Body = '''
{
"state": "DELETING",
"pg_version": "14"
}
'''
9 changes: 9 additions & 0 deletions acceptance/cmd/psql/simple/echo-arguments.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash
#
# This script prints its arguments and exits.
# The test script renames this script to "psql" in order to capture the arguments that the CLI passes to psql command.
#
echo "echo-arguments.sh was called with the following arguments: $@"
echo "PGPASSWORD=${PGPASSWORD}"
echo "PGSSLMODE=${PGSSLMODE}"
exit 0
8 changes: 8 additions & 0 deletions acceptance/cmd/psql/simple/out.test.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Local = true
Cloud = false

[GOOS]
windows = false

[EnvMatrix]
DATABRICKS_CLI_DEPLOYMENT = ["terraform", "direct-exp"]
82 changes: 82 additions & 0 deletions acceptance/cmd/psql/simple/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@

=== Test should use a fake psql command:
>>> psql --version
echo-arguments.sh was called with the following arguments: --version
PGPASSWORD=
PGSSLMODE=

=== Command should require instance name:
>>> musterr [CLI] psql
Error: could not find any Database instances in the workspace. Please manually specify required argument: DATABASE_INSTANCE_NAME

Exit code (musterr): 1

=== Command should require instance name even if extra arguments are provided:
>>> musterr [CLI] psql -- --dbname=my-database
Error: could not find any Database instances in the workspace. Please manually specify required argument: DATABASE_INSTANCE_NAME

Exit code (musterr): 1

=== Command should use default db name if it is not specified:
>>> [CLI] psql my-database
Connecting to Databricks Database Instance my-database ...
Postgres version: 14
Database instance status: AVAILABLE
Successfully fetched database credentials
Launching psql with connection to my-database.my-host.com...
echo-arguments.sh was called with the following arguments: --host=my-database.my-host.com --username=[USERNAME] --port=5432 --dbname=databricks_postgres
PGPASSWORD=my-secret-token
PGSSLMODE=require

=== Command should by-pass extra arguments to psql:
>>> [CLI] psql my-database -- -c SELECT * FROM my_table --echo-all
Connecting to Databricks Database Instance my-database ...
Postgres version: 14
Database instance status: AVAILABLE
Successfully fetched database credentials
Launching psql with connection to my-database.my-host.com...
echo-arguments.sh was called with the following arguments: --host=my-database.my-host.com --username=[USERNAME] --port=5432 --dbname=databricks_postgres -c SELECT * FROM my_table --echo-all
PGPASSWORD=my-secret-token
PGSSLMODE=require

=== Command should use the db name from extra arguments when specified:
>>> [CLI] psql my-database -- --dbname=db1
Connecting to Databricks Database Instance my-database ...
Postgres version: 14
Database instance status: AVAILABLE
Successfully fetched database credentials
Launching psql with connection to my-database.my-host.com...
echo-arguments.sh was called with the following arguments: --host=my-database.my-host.com --username=[USERNAME] --port=5432 --dbname=db1
PGPASSWORD=my-secret-token
PGSSLMODE=require

>>> [CLI] psql my-database -- -d db2
Connecting to Databricks Database Instance my-database ...
Postgres version: 14
Database instance status: AVAILABLE
Successfully fetched database credentials
Launching psql with connection to my-database.my-host.com...
echo-arguments.sh was called with the following arguments: --host=my-database.my-host.com --username=[USERNAME] --port=5432 -d db2
PGPASSWORD=my-secret-token
PGSSLMODE=require

=== Command should use the port from extra arguments when specified:
>>> [CLI] psql my-database -- --dbname=db1 -p 3000
Connecting to Databricks Database Instance my-database ...
Postgres version: 14
Database instance status: AVAILABLE
Successfully fetched database credentials
Launching psql with connection to my-database.my-host.com...
echo-arguments.sh was called with the following arguments: --host=my-database.my-host.com --username=[USERNAME] --dbname=db1 -p 3000
PGPASSWORD=my-secret-token
PGSSLMODE=require

>>> [CLI] psql my-database -- -d db2 --port=3001
Connecting to Databricks Database Instance my-database ...
Postgres version: 14
Database instance status: AVAILABLE
Successfully fetched database credentials
Launching psql with connection to my-database.my-host.com...
echo-arguments.sh was called with the following arguments: --host=my-database.my-host.com --username=[USERNAME] -d db2 --port=3001
PGPASSWORD=my-secret-token
PGSSLMODE=require
31 changes: 31 additions & 0 deletions acceptance/cmd/psql/simple/script
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
mv echo-arguments.sh psql

cleanup() {
rm psql
}
trap cleanup EXIT

export PATH="$(pwd):$PATH"

title "Test should use a fake psql command:"
trace psql --version

title "Command should require instance name:"
trace musterr $CLI psql

title "Command should require instance name even if extra arguments are provided:"
trace musterr $CLI psql -- --dbname=my-database

title "Command should use default db name if it is not specified:"
trace $CLI psql my-database

title "Command should by-pass extra arguments to psql:"
trace $CLI psql my-database -- -c "SELECT * FROM my_table" --echo-all

title "Command should use the db name from extra arguments when specified:"
trace $CLI psql my-database -- --dbname=db1
trace $CLI psql my-database -- -d db2

title "Command should use the port from extra arguments when specified:"
trace $CLI psql my-database -- --dbname=db1 -p 3000
trace $CLI psql my-database -- -d db2 --port=3001
32 changes: 32 additions & 0 deletions acceptance/cmd/psql/simple/test.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# This acceptance test is disabled on Windows runners because
# the current argument capturing method does not work on windows-latest GitHub Runner.
#
# See PR #3228 for documented attempts to fix this issue:
# https://github.com/databricks/cli/pull/3228
GOOS.windows = false

[[Server]]
Pattern = "GET /api/2.0/database/instances/my-database"
Response.Body = '''
{
"state": "AVAILABLE",
"pg_version": "14",
"read_write_dns": "my-database.my-host.com"
}
'''

[[Server]]
Pattern = "GET /api/2.0/database/instances"
Response.Body = '''
{
"database_instances": []
}
'''

[[Server]]
Pattern = "POST /api/2.0/database/credentials"
Response.Body = '''
{
"token": "my-secret-token"
}
'''
1 change: 1 addition & 0 deletions acceptance/help/output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ Clean Rooms

Database
database Database Instances provide access to a database via REST API or direct SQL.
psql Connect to the specified Database Instance

Quality Monitor v2
quality-monitor-v2 Manage data quality of UC objects (currently support schema).
Expand Down
3 changes: 3 additions & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"strings"

"github.com/databricks/cli/cmd/psql"

"github.com/databricks/cli/cmd/account"
"github.com/databricks/cli/cmd/api"
"github.com/databricks/cli/cmd/auth"
Expand Down Expand Up @@ -101,6 +103,7 @@ func New(ctx context.Context) *cobra.Command {
cli.AddCommand(api.New())
cli.AddCommand(auth.New())
cli.AddCommand(bundle.New())
cli.AddCommand(psql.New())
cli.AddCommand(configure.New())
cli.AddCommand(fs.New())
cli.AddCommand(labs.New(ctx))
Expand Down
Loading
Loading