-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable reading migration files from an embedded file system (#15)
- Loading branch information
Showing
10 changed files
with
110 additions
and
32 deletions.
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,56 @@ | ||
package main | ||
|
||
import ( | ||
"database/sql" | ||
"embed" | ||
"fmt" | ||
"log" | ||
"strings" | ||
|
||
"github.com/stimtech/go-migration/v2" | ||
|
||
_ "github.com/mattn/go-sqlite3" | ||
) | ||
|
||
// Simple example app with an embedded filesystem. | ||
// An sqlite database will be created, and a table added. | ||
// Run with 'go run embed.go'. | ||
// Two migrations are present in db/migration and will be loaded into the db in alphabetical order. | ||
|
||
//go:embed embedded-fs/*.sql | ||
var migFS embed.FS | ||
|
||
func main() { | ||
// connect to a new or existing sqlite datasource | ||
db, err := sql.Open("sqlite3", "db.sqlite") | ||
if err != nil { | ||
log.Fatal("failed to create datasource", err) | ||
} | ||
|
||
// run migration | ||
m := migration.New(db, | ||
// the embedded filesystem contains the folder specified, so this will still need to be configured. | ||
migration.Config{MigrationFolder: "embedded-fs"}, | ||
migration.FSOption{FileSystem: migFS}) | ||
err = m.Migrate() | ||
if err != nil { | ||
log.Fatal("failed to run database migration", err) | ||
} | ||
|
||
// list all tables in the database | ||
res, err := db.Query("select name from sqlite_master where type='table' order by name") | ||
if err != nil { | ||
log.Fatal("failed to query database", err) | ||
} | ||
|
||
var tables []string | ||
for res.Next() { | ||
var tn string | ||
if err := res.Scan(&tn); err != nil { | ||
log.Fatal("failed to scan row", err) | ||
} | ||
tables = append(tables, tn) | ||
} | ||
|
||
log.Println(fmt.Sprintf("Tables in database: %s", strings.Join(tables, ", "))) | ||
} |
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,3 @@ | ||
create table my_table ( | ||
id integer primary key | ||
); |
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,2 @@ | ||
alter table my_table add column test varchar(30); | ||
|
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 |
---|---|---|
@@ -1,13 +1,28 @@ | ||
module github.com/stimtech/go-migration/v2 | ||
|
||
go 1.15 | ||
go 1.19 | ||
|
||
require ( | ||
github.com/go-sql-driver/mysql v1.6.0 | ||
github.com/jackc/pgx/v4 v4.11.0 | ||
github.com/mattn/go-sqlite3 v1.14.9 | ||
github.com/stretchr/testify v1.8.0 | ||
go.uber.org/zap v1.24.0 | ||
) | ||
|
||
require ( | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/jackc/chunkreader/v2 v2.0.1 // indirect | ||
github.com/jackc/pgconn v1.8.1 // indirect | ||
github.com/jackc/pgio v1.0.0 // indirect | ||
github.com/jackc/pgpassfile v1.0.0 // indirect | ||
github.com/jackc/pgproto3/v2 v2.0.6 // indirect | ||
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect | ||
github.com/jackc/pgtype v1.7.0 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
go.uber.org/atomic v1.10.0 // indirect | ||
go.uber.org/multierr v1.9.0 // indirect | ||
go.uber.org/zap v1.24.0 | ||
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2 // indirect | ||
golang.org/x/text v0.3.3 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
) |
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