-
Notifications
You must be signed in to change notification settings - Fork 508
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
357 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
//go:build !no_vertica | ||
// +build !no_vertica | ||
|
||
package main | ||
|
||
import ( | ||
_ "github.com/vertica/vertica-sql-go" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package testdb | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
"log" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/ory/dockertest/v3" | ||
"github.com/ory/dockertest/v3/docker" | ||
_ "github.com/vertica/vertica-sql-go" | ||
) | ||
|
||
const ( | ||
// https://hub.docker.com/r/vertica/vertica-ce | ||
VERTICA_IMAGE = "vertica/vertica-ce" | ||
VERTICA_VERSION = "12.0.0-0" | ||
VERTICA_DB = "testdb" | ||
) | ||
|
||
func newVertica(opts ...OptionsFunc) (*sql.DB, func(), error) { | ||
option := &options{} | ||
for _, f := range opts { | ||
f(option) | ||
} | ||
// Uses a sensible default on windows (tcp/http) and linux/osx (socket). | ||
pool, err := dockertest.NewPool("") | ||
if err != nil { | ||
return nil, nil, fmt.Errorf("failed to connect to docker: %v", err) | ||
} | ||
options := &dockertest.RunOptions{ | ||
Repository: VERTICA_IMAGE, | ||
Tag: VERTICA_VERSION, | ||
Env: []string{ | ||
"VERTICA_DB_NAME=" + VERTICA_DB, | ||
"VMART_ETL_SCRIPT=", // Don't install VMART data inside container. | ||
}, | ||
Labels: map[string]string{"goose_test": "1"}, | ||
PortBindings: make(map[docker.Port][]docker.PortBinding), | ||
// Prevent package installation for faster container startup. | ||
Mounts: []string{"/tmp/empty:/opt/vertica/packages"}, | ||
} | ||
if option.bindPort > 0 { | ||
options.PortBindings[docker.Port("5433/tcp")] = []docker.PortBinding{ | ||
{HostPort: strconv.Itoa(option.bindPort)}, | ||
} | ||
} | ||
container, err := pool.RunWithOptions( | ||
options, | ||
func(config *docker.HostConfig) { | ||
// Set AutoRemove to true so that stopped container goes away by itself. | ||
config.AutoRemove = true | ||
config.RestartPolicy = docker.RestartPolicy{Name: "no"} | ||
}, | ||
) | ||
if err != nil { | ||
return nil, nil, fmt.Errorf("failed to create docker container: %v", err) | ||
} | ||
cleanup := func() { | ||
if option.debug { | ||
// User must manually delete the Docker container. | ||
return | ||
} | ||
if err := pool.Purge(container); err != nil { | ||
log.Printf("failed to purge resource: %v", err) | ||
} | ||
} | ||
verticaInfo := fmt.Sprintf("vertica://%s:%s@%s:%s/%s", | ||
"dbadmin", | ||
"", | ||
"localhost", | ||
container.GetPort("5433/tcp"), // Fetch port dynamically assigned to container | ||
VERTICA_DB, | ||
) | ||
|
||
var db *sql.DB | ||
// Give vertica a head start since the container takes a little bit to start up. | ||
time.Sleep(time.Second * 15) | ||
|
||
// Exponential backoff-retry, because the application in the container | ||
// might not be ready to accept connections yet. | ||
if err := pool.Retry( | ||
func() error { | ||
var err error | ||
db, err = sql.Open("vertica", verticaInfo) | ||
if err != nil { | ||
return err | ||
} | ||
return db.Ping() | ||
}, | ||
); err != nil { | ||
return nil, cleanup, fmt.Errorf("could not connect to docker database: %v", err) | ||
} | ||
return db, cleanup, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
-- +goose Up | ||
-- +goose StatementBegin | ||
CREATE SCHEMA IF NOT EXISTS testing; | ||
-- +goose StatementEnd | ||
|
||
-- +goose Down | ||
-- +goose StatementBegin | ||
DROP SCHEMA IF EXISTS testing; | ||
-- +goose StatementEnd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
-- +goose Up | ||
-- +goose StatementBegin | ||
CREATE TABLE IF NOT EXISTS testing.dim_test_scd | ||
( | ||
test_key BIGINT NOT NULL, | ||
test_id UUID NOT NULL, | ||
valid_from DATE NOT NULL, | ||
valid_to DATE NOT NULL, | ||
is_current BOOLEAN NOT NULL | ||
DEFAULT (valid_to = '9999/12/31'), | ||
external_id VARCHAR(100) | ||
) UNSEGMENTED ALL NODES; | ||
-- +goose StatementEnd | ||
-- +goose StatementBegin | ||
CREATE PROJECTION IF NOT EXISTS testing.dim_test_scd_proj_is_current AS | ||
SELECT test_key, | ||
test_id, | ||
valid_from, | ||
valid_to, | ||
is_current, | ||
external_id | ||
FROM testing.dim_test_scd | ||
ORDER BY is_current, test_id | ||
SEGMENTED BY HASH(test_key) ALL NODES; | ||
-- +goose StatementEnd | ||
-- +goose StatementBegin | ||
CREATE OR REPLACE VIEW testing.Test AS | ||
SELECT test_key, | ||
test_id, | ||
external_id | ||
FROM testing.dim_test_scd | ||
WHERE is_current = true; | ||
-- +goose StatementEnd | ||
|
||
-- +goose Down | ||
-- +goose StatementBegin | ||
DROP VIEW IF EXISTS testing.Test; | ||
-- +goose StatementEnd | ||
-- +goose StatementBegin | ||
DROP PROJECTION IF EXISTS testing.dim_test_scd_proj_is_current; | ||
-- +goose StatementEnd | ||
-- +goose StatementBegin | ||
DROP TABLE IF EXISTS testing.dim_test_scd; | ||
-- +goose StatementEnd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
-- +goose Up | ||
-- +goose StatementBegin | ||
INSERT INTO testing.dim_test_scd VALUES (1, '575a0dd4-bd97-44ac-aae0-987090181da8', '2021-10-02', '2021-10-03', false, '123'); | ||
-- +goose StatementEnd | ||
-- +goose StatementBegin | ||
INSERT INTO testing.dim_test_scd VALUES (2, '575a0dd4-bd97-44ac-aae0-987090181da8', '2021-10-03', '2021-10-04', false, '456'); | ||
-- +goose StatementEnd | ||
-- +goose StatementBegin | ||
INSERT INTO testing.dim_test_scd VALUES (3, '575a0dd4-bd97-44ac-aae0-987090181da8', '2021-10-04', '9999-12-31', true, '789'); | ||
-- +goose StatementEnd | ||
|
||
-- +goose Down | ||
-- +goose StatementBegin | ||
DELETE FROM testing.dim_test_scd where test_id = '575a0dd4-bd97-44ac-aae0-987090181da8'; | ||
-- +goose StatementEnd |
Oops, something went wrong.