Skip to content

Commit

Permalink
refactor: Simplify exported API
Browse files Browse the repository at this point in the history
There are too many exported identifiers in package godfish. A developer
interested in making a new driver implementation really just needs to
know about the Driver interface and not much else. Split up the code so
that the only exposed functionality may build:

- a Driver implementation (pkg godfish/drivers/...)
- a standalone binary (pkg godfish/internal/cmd)

The material that was in pkg godfish is important to get that working,
but is probably not necessary for outside consumers. Most of these
changes involve moving material from pkg godfish to pkg internal,
adjusting callers, and ensuring that pkg godfish's exported API doesn't
reference pkg internal.

Other notable changes are:

- Change UpdateSchemaMigrations signature. Putting the Direction type in
  a method signature complicates things IMO; in practice, it means up or
  down, so a boolean makes more sense here. The Direction type is useful
  for disambiguating labels for the direction in a migration filename,
  but that's not a concern for a Driver implementation.
- Remove func CreateSchemaMigrations because it's not used anywhere and
  the functionality is already in a Driver method.
- Move buildtime versioning info variables to pkg cmd because the
  Version type is now in pkg internal.
- Move validation for direction label to ensure it's always validated,
  not just in the subcommand.
- Move pkg info stuff into pkg internal, this simplifies dependencies.
  • Loading branch information
rafaelespinoza committed Jan 16, 2022
1 parent 88b0f7d commit 8a21e47
Show file tree
Hide file tree
Showing 32 changed files with 438 additions and 378 deletions.
18 changes: 9 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ PKG_IMPORT_PATH=github.com/rafaelespinoza/godfish

# inject this metadata when building a binary.
define LDFLAGS
-X $(PKG_IMPORT_PATH)/internal/version.BranchName=$(shell git rev-parse --abbrev-ref HEAD) \
-X $(PKG_IMPORT_PATH)/internal/version.BuildTime=$(shell date --rfc-3339=seconds --utc | tr ' ' 'T') \
-X $(PKG_IMPORT_PATH)/internal/version.CommitHash=$(shell git rev-parse --short=7 HEAD) \
-X $(PKG_IMPORT_PATH)/internal/version.GoVersion=$(shell $(GO) version | awk '{ print $$3 }') \
-X $(PKG_IMPORT_PATH)/internal/version.Tag=$(shell git describe --tag)
-X $(PKG_IMPORT_PATH)/internal/cmd.versionBranchName=$(shell git rev-parse --abbrev-ref HEAD) \
-X $(PKG_IMPORT_PATH)/internal/cmd.versionBuildTime=$(shell date --rfc-3339=seconds --utc | tr ' ' 'T') \
-X $(PKG_IMPORT_PATH)/internal/cmd.versionCommitHash=$(shell git rev-parse --short=7 HEAD) \
-X $(PKG_IMPORT_PATH)/internal/cmd.versionGoVersion=$(shell $(GO) version | awk '{ print $$3 }') \
-X $(PKG_IMPORT_PATH)/internal/cmd.versionTag=$(shell git describe --tag)
endef

test:
Expand All @@ -23,31 +23,31 @@ clean:
cassandra:
$(GO) build -o $(BIN) -v \
-ldflags "$(LDFLAGS) \
-X $(PKG_IMPORT_PATH)/internal/version.Driver=cassandra" \
-X $(PKG_IMPORT_PATH)/internal/cmd.versionDriver=cassandra" \
./drivers/cassandra/godfish
cassandra-test:
$(GO) test $(ARGS) ./drivers/cassandra

postgres:
$(GO) build -o $(BIN) -v \
-ldflags "$(LDFLAGS) \
-X $(PKG_IMPORT_PATH)/internal/version.Driver=postgres" \
-X $(PKG_IMPORT_PATH)/internal/cmd.versionDriver=postgres" \
./drivers/postgres/godfish
postgres-test:
$(GO) test $(ARGS) ./drivers/postgres

mysql:
$(GO) build -o $(BIN) -v \
-ldflags "$(LDFLAGS) \
-X $(PKG_IMPORT_PATH)/internal/version.Driver=mysql" \
-X $(PKG_IMPORT_PATH)/internal/cmd.versionDriver=mysql" \
./drivers/mysql/godfish
mysql-test:
$(GO) test $(ARGS) ./drivers/mysql

sqlite3:
CGO_ENABLED=1 $(GO) build -o $(BIN) -v \
-ldflags "$(LDFLAGS) \
-X $(PKG_IMPORT_PATH)/internal/version.Driver=sqlite3" \
-X $(PKG_IMPORT_PATH)/internal/cmd.versionDriver=sqlite3" \
./drivers/sqlite3/godfish
sqlite3-test:
$(GO) test $(ARGS) ./drivers/sqlite3
2 changes: 1 addition & 1 deletion driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type Driver interface {
// UpdateSchemaMigrations records a timestamped version of a migration that
// has been successfully applied by adding a new row to the schema
// migrations table.
UpdateSchemaMigrations(dir Direction, version string) error
UpdateSchemaMigrations(forward bool, version string) error
}

// AppliedVersions represents an iterative list of migrations that have been run
Expand Down
4 changes: 2 additions & 2 deletions drivers/cassandra/cassandra.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ func (d *driver) AppliedVersions() (out godfish.AppliedVersions, err error) {
return
}

func (d *driver) UpdateSchemaMigrations(dir godfish.Direction, version string) (err error) {
func (d *driver) UpdateSchemaMigrations(forward bool, version string) (err error) {
conn := d.connection
if dir == godfish.DirForward {
if forward {
err = conn.Query(`
INSERT INTO schema_migrations (migration_id)
VALUES (?)`,
Expand Down
4 changes: 2 additions & 2 deletions drivers/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ func (d *driver) AppliedVersions() (out godfish.AppliedVersions, err error) {
return
}

func (d *driver) UpdateSchemaMigrations(dir godfish.Direction, version string) (err error) {
func (d *driver) UpdateSchemaMigrations(forward bool, version string) (err error) {
conn := d.connection
if dir == godfish.DirForward {
if forward {
_, err = conn.Exec(`
INSERT INTO schema_migrations (migration_id)
VALUES (?)`,
Expand Down
4 changes: 2 additions & 2 deletions drivers/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ func (d *driver) AppliedVersions() (out godfish.AppliedVersions, err error) {
return
}

func (d *driver) UpdateSchemaMigrations(dir godfish.Direction, version string) (err error) {
func (d *driver) UpdateSchemaMigrations(forward bool, version string) (err error) {
conn := d.connection
if dir == godfish.DirForward {
if forward {
_, err = conn.Exec(`
INSERT INTO schema_migrations (migration_id)
VALUES ($1)
Expand Down
4 changes: 2 additions & 2 deletions drivers/sqlite3/sqlite3.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ func (d *driver) AppliedVersions() (out godfish.AppliedVersions, err error) {
return
}

func (d *driver) UpdateSchemaMigrations(dir godfish.Direction, version string) (err error) {
func (d *driver) UpdateSchemaMigrations(forward bool, version string) (err error) {
conn := d.connection
if dir == godfish.DirForward {
if forward {
_, err = conn.Exec(`
INSERT INTO schema_migrations (migration_id)
VALUES ($1)`,
Expand Down
Loading

0 comments on commit 8a21e47

Please sign in to comment.