Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enables the tablefunc extension for the steampipe database. Closes #1154 #1184

Merged
merged 10 commits into from
Dec 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
12 changes: 5 additions & 7 deletions db/db_local/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,11 @@ func runInstall(firstInstall bool, spinner *spinner.Spinner) error {
display.StopSpinner(spinner)
return fmt.Errorf("Connection to database... FAILED!")
}
defer func() {
display.UpdateSpinnerMessage(spinner, "Completing configuration")
client.Close()
doThreeStepPostgresExit(process)
}()

display.UpdateSpinnerMessage(spinner, "Generating database passwords...")
// generate a password file for use later
Expand Down Expand Up @@ -285,13 +290,6 @@ func runInstall(firstInstall bool, spinner *spinner.Spinner) error {
return fmt.Errorf("Configuring Steampipe... FAILED!")
}

// close the client - otherwise, it may be difficult to stop the service
client.Close()

// force stop
display.UpdateSpinnerMessage(spinner, "Completing configuration")
err = doThreeStepPostgresExit(process)

return err
}

Expand Down
29 changes: 29 additions & 0 deletions db/db_local/start_services.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"sync"
"syscall"

"github.com/turbot/steampipe/db/db_common"
"github.com/turbot/steampipe/plugin_manager"

psutils "github.com/shirou/gopsutil/process"
Expand Down Expand Up @@ -180,6 +181,13 @@ func startDB(port int, listen StartListenType, invoker constants.Invoker) (start
return ServiceFailedToStart, err
}

// ensure that the necessary extensions are installed in the database
err = ensurePgExtensions(databaseName)
if err != nil {
// there was a problem with the installation
return ServiceFailedToStart, err
}

err = ensureTempTablePermissions(databaseName)
if err != nil {
return ServiceFailedToStart, err
Expand Down Expand Up @@ -438,6 +446,27 @@ func setupLogCollector(postgresCmd *exec.Cmd) (chan string, func(), error) {
return publishChannel, closeFunction, nil
}

// ensures that the necessary extensions are installed on the database
func ensurePgExtensions(databaseName string) error {
extensions := []string{
"tablefunc",
}

errors := []error{}
rootClient, err := createLocalDbClient(&CreateDbOptions{DatabaseName: databaseName, Username: constants.DatabaseSuperUser})
if err != nil {
return err
}
defer rootClient.Close()
for _, extn := range extensions {
_, err = rootClient.Exec(fmt.Sprintf("create extension if not exists %s", db_common.PgEscapeName(extn)))
if err != nil {
errors = append(errors, err)
}
}
return utils.CombineErrors(errors...)
}

// ensures that the 'steampipe' foreign server exists
// (re)install FDW and creates server if it doesn't
func ensureSteampipeServer(databaseName string) error {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
+----------+------------+------------+
| row_name | category_1 | category_2 |
+----------+------------+------------+
| test1 | val2 | val3 |
+----------+------------+------------+
4 changes: 2 additions & 2 deletions tests/acceptance/test_files/018.dynamic-schema.bats
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ load "$LIB_BATS_SUPPORT/load.bash"
rm -f $FILE_PATH/test_data/csv_plugin_test/b.csv

# run the query and verify - should fail
run steampipe query "select * from csv1.b"
run steampipe query "select * from csv1.b"
assert_output --partial 'does not exist'

rm -f $FILE_PATH/test_data/csv_plugin_test/b.csv
Expand Down Expand Up @@ -90,6 +90,6 @@ function setup() {

function teardown() {
# remove the files created as part of these tests
rm -f $STEAMPIPE_INSTALL_DIR/config/csv1.spc
rm -f $STEAMPIPE_INSTALL_DIR/config/csv*.spc
rm -f output.*
}
30 changes: 30 additions & 0 deletions tests/acceptance/test_files/020.service_extensions.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
load "$LIB_BATS_ASSERT/load.bash"
load "$LIB_BATS_SUPPORT/load.bash"

# tests for tablefunc module

@test "test crosstab function" {
# create table and insert values
steampipe query "CREATE TABLE ct(id SERIAL, rowid TEXT, attribute TEXT, value TEXT);"
steampipe query "INSERT INTO ct(rowid, attribute, value) VALUES('test1','att1','val1');"
steampipe query "INSERT INTO ct(rowid, attribute, value) VALUES('test1','att2','val2');"
steampipe query "INSERT INTO ct(rowid, attribute, value) VALUES('test1','att3','val3');"

# crosstab function
run steampipe query "SELECT * FROM crosstab('select rowid, attribute, value from ct where attribute = ''att2'' or attribute = ''att3'' order by 1,2') AS ct(row_name text, category_1 text, category_2 text);"
echo $output

# drop table
steampipe query "DROP TABLE ct"

# match output with expected
assert_equal "$output" "$(cat $TEST_DATA_DIR/expected_crosstab_results.txt)"
}

@test "test normal_rand function" {
# normal_rand function
steampipe query "SELECT * FROM normal_rand(10, 5, 3);"

# previous query should pass
assert_success
}