https://github.com/corverroos/truss
Truss is a simple golang mysql schema management library
https://github.com/corverroos/truss
golang golang-library mysql schema sql
Last synced: about 1 month ago
JSON representation
Truss is a simple golang mysql schema management library
- Host: GitHub
- URL: https://github.com/corverroos/truss
- Owner: corverroos
- Created: 2020-09-12T07:04:53.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2021-05-14T11:50:47.000Z (about 5 years ago)
- Last Synced: 2025-02-22T20:14:03.971Z (over 1 year ago)
- Topics: golang, golang-library, mysql, schema, sql
- Language: Go
- Homepage:
- Size: 27.3 KB
- Stars: 1
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# truss
> truss /trŭs/
>
> n. A rigid framework, as of wooden beams or metal bars, designed to support a structure, such as a roof.
>
> v. to support, strengthen, or stiffen by or as if by a truss.
Truss is a simple golang mysql schema management library that provides the following features:
- `truss.Connect` returns a `*sql.DB` for a production use.
- `truss.Migrate` schema management via migration queries (roll forward only).
- `truss.ConnectForTesting` returns testing `*sql.DB` for a temp database with the current schema.
- `truss.TestSchema` provides a snapshot of the current schema for explicit tracking of changes and to quickly view the current state.
## TL;DR
Common usage is to use `truss` in your `db` package:
Define a simple slice of migration queries in `db/migrations.go`.
```go
package db
// migrations is an append-only list of all migrations over time.
var migrations = []string{`
CREATE TABLE users (
id BIGINT NOT NULL AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
type INT NOT NULL,
PRIMARY KEY (id),
INDEX by_name (name)
);`, `
ALTEST TABLE users ADD COLUMN surname VARCHAR(255) AFTER name;
`,
}
```
Ensure the latest schema is applied on startup and in tests in `db/db.go`.
```go
// Connect returns a database connection and ensures latest migrations are applied
func Connect(uri string) (*sql.DB, error) {
dbc, err := truss.Connect(uri)
if err != nil {
return nil, err
}
err = truss.Migrate(context.Background(), dbc, migrations)
if err != nil {
return nil, err
}
return dbc, nil
}
// ConnectForTesting returns a database connection for a temp database with latest schema.
func ConnectForTesting(t *testing.T) *sql.DB {
return truss.ConnectForTesting(t, migrations...)
}
```
Maintain an explicit snapshot of the latest schema in `migrations_test.go`.
```go
package db
var update = flag.Bool("update", false, "update schema file")
//go:generate go test -update -run=TestSchema
func TestSchema(t *testing.T) {
truss.TestSchema(t, "schema.sql", *update, migrations...)
}
```
Which will generate the following `db/schema.sql` file that is checked into the git.
```sql
-- Schema generated by truss. DO NOT EDIT.
CREATE TABLE `users` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`surname` varchar(255),
`type` int(11) NOT NULL,
`created_at` datetime(3) NOT NULL,
PRIMARY KEY (`id`),
KEY `by_name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
CREATE TABLE `migrations` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`query_hash` char(64) NOT NULL,
`schema_hash` char(64) NOT NULL,
`created_at` datetime(3) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
```