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

feat: add postges schema #19

Merged
merged 1 commit into from
Apr 13, 2024
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
11 changes: 5 additions & 6 deletions tempdb/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,23 @@ type Querier interface {
DeleteDatabase(ctx context.Context, name string) error
}

func New(dbUrl string) (Querier, error) {
parsedUrl, err := url.Parse(dbUrl)
func New(dbURL string) (Querier, error) {
parsedURL, err := url.Parse(dbURL)
if err != nil {
return nil, err
}
querier, err := querierFromUrl(parsedUrl)
querier, err := querierFromURL(parsedURL)
if err != nil {
return nil, fmt.Errorf("cannot create querier: %w", err)
}
return querier, nil

}

func querierFromUrl(url *url.URL) (Querier, error) {
func querierFromURL(url *url.URL) (Querier, error) {
dialect := url.Scheme

switch dialect {
case "postgresql":
case "postgresql", "postgres":
q, err := postgres.New(url)
if err != nil {
return nil, fmt.Errorf("cannot create postgres: %w", err)
Expand Down
19 changes: 13 additions & 6 deletions tempdb/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package tempdb
import (
"testing"

"github.com/Alviner/drillerfy/tempdb/postgres"
"github.com/Alviner/drillerfy/utils_test"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/Alviner/drillerfy/tempdb/postgres"
)

func TestCreate(t *testing.T) {
Expand All @@ -15,17 +15,25 @@ func TestCreate(t *testing.T) {
require := require.New(t)
assert := assert.New(t)

t.Run("postgresql", func(t *testing.T) {
t.Parallel()
// act
db, err := New("postgresql://pguser:pgpass@localhost:5432/pgdb")
require.NoError(err)
// assert
assert.IsType(new(postgres.Postgres), db)
})
t.Run("postgres", func(t *testing.T) {
t.Parallel()
//act
db, err := New(utils_test.PostgresDNS(t))
// act
db, err := New("postgres://pguser:pgpass@localhost:5432/pgdb")
require.NoError(err)
// assert
assert.IsType(new(postgres.Postgres), db)
})
t.Run("unknown", func(t *testing.T) {
t.Parallel()
//act
// act
_, err := New("unknown://database.url")
// assert
require.EqualErrorf(
Expand All @@ -35,5 +43,4 @@ func TestCreate(t *testing.T) {
"unknown",
)
})

}
25 changes: 13 additions & 12 deletions tempdb/postgres/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import (
"testing"
"time"

"github.com/Alviner/drillerfy/utils_test"
"github.com/Alviner/drillerfy/utils_test/postgres"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/Alviner/drillerfy/utils_test"
"github.com/Alviner/drillerfy/utils_test/postgres"
)

func TestPostgres(t *testing.T) {
Expand All @@ -19,7 +20,7 @@ func TestPostgres(t *testing.T) {
require := require.New(t)
assert := assert.New(t)

dbUrl, err := url.Parse(utils_test.PostgresDNS(t))
dbURL, err := url.Parse(utils_test.PostgresDNS(t))
require.NoError(err)

t.Run("create", func(t *testing.T) {
Expand All @@ -29,13 +30,13 @@ func TestPostgres(t *testing.T) {
ctx, done := context.WithTimeout(context.Background(), 2*time.Second)
defer done()

pg, err := New(dbUrl)
pg, err := New(dbURL)
require.NoError(err)
//act
// act
require.NoError(pg.CreateDatabase(ctx, dbName, ""))
defer func() { require.NoError(pg.DeleteDatabase(ctx, dbName)) }()
// assert
names, err := postgres.DBNames(t, dbUrl)
names, err := postgres.DBNames(t, dbURL)
require.NoError(err)
assert.Contains(names, dbName)
})
Expand All @@ -47,17 +48,17 @@ func TestPostgres(t *testing.T) {
ctx, done := context.WithTimeout(context.Background(), 5*time.Second)
defer done()

pg, err := New(dbUrl)
pg, err := New(dbURL)
require.NoError(err)

require.NoError(pg.CreateDatabase(ctx, templateName, ""))
defer func() { require.NoError(pg.DeleteDatabase(ctx, templateName)) }()
//act
// act
require.NoError(pg.CreateDatabase(ctx, dbName, templateName))
defer func() { require.NoError(pg.DeleteDatabase(ctx, dbName)) }()

// assert
names, err := postgres.DBNames(t, dbUrl)
names, err := postgres.DBNames(t, dbURL)
require.NoError(err)
assert.Contains(names, dbName)
})
Expand All @@ -69,15 +70,15 @@ func TestPostgres(t *testing.T) {
ctx, done := context.WithTimeout(context.Background(), 5*time.Second)
defer done()

pg, err := New(dbUrl)
pg, err := New(dbURL)
require.NoError(err)

require.NoError(pg.CreateDatabase(ctx, dbName, ""))
//act
// act
require.NoError(pg.DeleteDatabase(ctx, dbName))

// assert
names, err := postgres.DBNames(t, dbUrl)
names, err := postgres.DBNames(t, dbURL)
require.NoError(err)
assert.NotContains(names, dbName)
})
Expand Down
Loading