-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #293 from cashapp/mtocker-asserty
Add asserty feature
- Loading branch information
Showing
4 changed files
with
131 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# What is this? | ||
|
||
Asserty is a package that provides a set of functions to help ensure that a database schema conforms to your expectations. | ||
|
||
Sure, you could probably use it in tests. But our goal for this is to use it as part of service readiness checks. We want to make sure that the database schema is what we expect it to be before we start to serve traffic. |
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,60 @@ | ||
// Package asserty offers functionality to assert for certain DB properties. | ||
package asserty | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"errors" | ||
"slices" | ||
|
||
"github.com/cashapp/spirit/pkg/table" | ||
_ "github.com/go-sql-driver/mysql" | ||
) | ||
|
||
type Table struct { | ||
ti *table.TableInfo | ||
} | ||
|
||
func LoadTable(db *sql.DB, schema, tableName string) (*Table, error) { | ||
ti := table.NewTableInfo(db, schema, tableName) | ||
if err := ti.SetInfo(context.TODO()); err != nil { | ||
return nil, err | ||
} | ||
return &Table{ti: ti}, nil | ||
} | ||
|
||
func (t *Table) ContainsColumns(columnNames ...string) error { | ||
for _, col := range columnNames { | ||
if !slices.Contains(t.ti.Columns, col) { | ||
return errors.New("missing column " + col + " on table " + t.ti.QuotedName) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (t *Table) NotContainsColumns(columnNames ...string) error { | ||
for _, col := range columnNames { | ||
if slices.Contains(t.ti.Columns, col) { | ||
return errors.New("unexpected column " + col + " on table " + t.ti.QuotedName) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (t *Table) ContainsIndexes(indexNames ...string) error { | ||
for _, idx := range indexNames { | ||
if !slices.Contains(t.ti.Indexes, idx) { | ||
return errors.New("missing index " + idx + " on table " + t.ti.QuotedName) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (t *Table) NotContainsIndexes(indexNames ...string) error { | ||
for _, idx := range indexNames { | ||
if slices.Contains(t.ti.Indexes, idx) { | ||
return errors.New("unexpected index " + idx + " on table " + t.ti.QuotedName) | ||
} | ||
} | ||
return nil | ||
} |
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,39 @@ | ||
package asserty | ||
|
||
import ( | ||
"database/sql" | ||
"testing" | ||
|
||
"github.com/cashapp/spirit/pkg/testutils" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestBasicUsage(t *testing.T) { | ||
db, err := sql.Open("mysql", testutils.DSN()) | ||
require.NoError(t, err) | ||
defer db.Close() | ||
|
||
testutils.RunSQL(t, `DROP TABLE IF EXISTS asserty_test`) | ||
table := `CREATE TABLE asserty_test ( | ||
id int(11) unsigned NOT NULL AUTO_INCREMENT, | ||
name varchar(115), | ||
age int(11) NOT NULL, | ||
PRIMARY KEY (id, age), | ||
INDEX idx_name (name), | ||
INDEX idx_age (age) | ||
)` | ||
testutils.RunSQL(t, table) | ||
|
||
tbl, err := LoadTable(db, "test", "asserty_test") | ||
require.NoError(t, err) | ||
require.NoError(t, tbl.ContainsColumns("name", "age")) | ||
require.Error(t, tbl.ContainsColumns("name", "somethingelse")) | ||
require.NoError(t, tbl.NotContainsColumns("col1", "col2")) | ||
require.Error(t, tbl.NotContainsColumns("col1", "name", "col2")) | ||
|
||
require.NoError(t, tbl.ContainsIndexes("idx_name", "idx_age")) | ||
require.Error(t, tbl.ContainsIndexes("idx_name", "idx_age", "idx_somethingelse")) | ||
require.NoError(t, tbl.NotContainsIndexes("idx_col1", "idx_col2")) | ||
require.Error(t, tbl.NotContainsIndexes("idx_col1", "idx_col2", "idx_name")) | ||
require.Error(t, tbl.NotContainsIndexes("idx_name")) | ||
} |
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