From cfd22dfc9c78d34513162ba492dee40ca3b72c57 Mon Sep 17 00:00:00 2001 From: Mason Payne Date: Thu, 22 Aug 2024 00:05:50 -0600 Subject: [PATCH] refactor into an importable library and keep an example --- libsqlDB/db.go => db.go | 2 +- libsqlDB/embedded.go => embedded.go | 4 +++- main.go => examples/basic-usage/main.go | 12 +++++++++--- .../basic-usage}/migrations/001_create-tables.sql | 0 go.mod | 2 +- libsqlDB/remote-only.go => remote-only.go | 6 ++++-- 6 files changed, 18 insertions(+), 8 deletions(-) rename libsqlDB/db.go => db.go (99%) rename libsqlDB/embedded.go => embedded.go (98%) rename main.go => examples/basic-usage/main.go (64%) rename {libsqlDB => examples/basic-usage}/migrations/001_create-tables.sql (100%) rename libsqlDB/remote-only.go => remote-only.go (93%) diff --git a/libsqlDB/db.go b/db.go similarity index 99% rename from libsqlDB/db.go rename to db.go index 8a18e85..bf2956a 100644 --- a/libsqlDB/db.go +++ b/db.go @@ -1,4 +1,4 @@ -package libsqlDB +package libsqldb import ( "database/sql" diff --git a/libsqlDB/embedded.go b/embedded.go similarity index 98% rename from libsqlDB/embedded.go rename to embedded.go index 98a4bc3..237ae69 100644 --- a/libsqlDB/embedded.go +++ b/embedded.go @@ -1,6 +1,6 @@ //go:build !windows -package libsqlDB +package libsqldb import ( "database/sql" @@ -26,6 +26,8 @@ var syncInterval = 200 * time.Millisecond func NewLibSqlDB(primaryUrl string, migrationFiles embed.FS, opts ...Options) (*LibSqlDB, error) { l := libSqlDefaults() + _migrationFiles = migrationFiles + for _, option := range opts { err := option(l) if err != nil { diff --git a/main.go b/examples/basic-usage/main.go similarity index 64% rename from main.go rename to examples/basic-usage/main.go index 8cfb04e..6007db4 100644 --- a/main.go +++ b/examples/basic-usage/main.go @@ -1,18 +1,24 @@ package main import ( + "embed" "fmt" + "git.sa.vin/payne8/libsqldb" "os" ) +//go:embed migrations/*.sql +var migrationFiles embed.FS + func main() { primaryUrl := os.Getenv("LIBSQL_DATABASE_URL") authToken := os.Getenv("LIBSQL_AUTH_TOKEN") - tdb, err := libsqlDB.NewLibSqlDB( + tdb, err := libsqldb.NewLibSqlDB( primaryUrl, - libsqlDB.WithAuthToken(authToken), - libsqlDB.WithLocalDBName("local.db"), + migrationFiles, + libsqldb.WithAuthToken(authToken), + libsqldb.WithLocalDBName("local.db"), ) if err != nil { fmt.Fprintf(os.Stderr, "failed to open db %s: %s", primaryUrl, err) diff --git a/libsqlDB/migrations/001_create-tables.sql b/examples/basic-usage/migrations/001_create-tables.sql similarity index 100% rename from libsqlDB/migrations/001_create-tables.sql rename to examples/basic-usage/migrations/001_create-tables.sql diff --git a/go.mod b/go.mod index d15f693..d7ea3f8 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module {{.ModulePath}} +module git.sa.vin/payne8/libsqldb go 1.21 diff --git a/libsqlDB/remote-only.go b/remote-only.go similarity index 93% rename from libsqlDB/remote-only.go rename to remote-only.go index e398179..95dfbf0 100644 --- a/libsqlDB/remote-only.go +++ b/remote-only.go @@ -1,6 +1,6 @@ //go:build windows -package libsqlDB +package libsqldb import ( "database/sql" @@ -8,7 +8,7 @@ import ( "fmt" "time" - multierror "github.com/hashicorp/go-multierror" + "github.com/hashicorp/go-multierror" _ "github.com/tursodatabase/libsql-client-go/libsql" ) @@ -26,6 +26,8 @@ type LibSqlDB struct { func NewLibSqlDB(primaryUrl string, migrationFiles embed.FS, opts ...Options) (*LibSqlDB, error) { l := &LibSqlDB{} + _migrationFiles = migrationFiles + for _, option := range opts { err := option(l) if err != nil {