forked from kndndrj/nvim-dbee
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsqlite.go
More file actions
100 lines (87 loc) · 2.61 KB
/
Copy pathsqlite.go
File metadata and controls
100 lines (87 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package testhelpers
import (
"context"
"fmt"
"path/filepath"
"strings"
"time"
"github.com/docker/docker/api/types/container"
"github.com/kndndrj/nvim-dbee/dbee/adapters"
"github.com/kndndrj/nvim-dbee/dbee/core"
tc "github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
)
type SQLiteContainer struct {
tc.Container
ConnURL string
Driver *core.Connection
TempDir string
}
// NewSQLiteContainer creates a new sqlite container with
// default adapter and connection. The params.URL is overwritten.
// It uses a temporary directory (usually the test suite tempDir) to store the db file.
// The tmpDir is then mounted to the container and all the dependencies are installed
// in the container file, while still being able to connect to the db file in the host.
func NewSQLiteContainer(ctx context.Context, params *core.ConnectionParams, tmpDir string) (*SQLiteContainer, error) {
seedFile, err := GetTestDataFile("sqlite_seed.sql")
if err != nil {
return nil, err
}
dbName, containerDBPath := "test.db", "/container/db"
entrypointCmd := []string{
"apk add sqlite=3.48.0-r0",
fmt.Sprintf("sqlite3 %s/%s < %s", containerDBPath, dbName, seedFile.Name()),
"echo 'ready'",
"tail -f /dev/null", // hack to keep the container running indefinitely
}
req := tc.ContainerRequest{
Image: "alpine:3.21",
Files: []tc.ContainerFile{
{
Reader: seedFile,
ContainerFilePath: seedFile.Name(),
FileMode: 0o755,
},
},
HostConfigModifier: func(hc *container.HostConfig) {
hc.Binds = append(hc.Binds, fmt.Sprintf("%s:%s", tmpDir, containerDBPath))
},
Cmd: []string{"sh", "-c", strings.Join(entrypointCmd, " && ")},
WaitingFor: wait.ForLog("ready").WithStartupTimeout(5 * time.Second),
}
ctr, err := tc.GenericContainer(ctx, tc.GenericContainerRequest{
ContainerRequest: req,
ProviderType: GetContainerProvider(),
Started: true,
})
if err != nil {
return nil, err
}
if params.Type == "" {
params.Type = "sqlite"
}
connURL := filepath.Join(tmpDir, dbName)
if params.URL == "" {
params.URL = connURL
}
driver, err := adapters.NewConnection(params)
if err != nil {
return nil, err
}
return &SQLiteContainer{
Container: ctr,
ConnURL: connURL,
Driver: driver,
TempDir: tmpDir,
}, nil
}
// NewDriver helper function to create a new driver with the connection URL.
func (p *SQLiteContainer) NewDriver(params *core.ConnectionParams) (*core.Connection, error) {
if params.URL == "" {
params.URL = p.ConnURL
}
if params.Type == "" {
params.Type = "sqlite"
}
return adapters.NewConnection(params)
}