Skip to content

Commit aa022dd

Browse files
author
Miguel Molina
authored
Merge pull request #148 from erizocosmico/docs/migrations
Kallax migrations documentation
2 parents b76d81c + 78a6b8e commit aa022dd

File tree

1 file changed

+99
-1
lines changed

1 file changed

+99
-1
lines changed

README.md

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Support for arrays of all basic Go types and all JSON and arrays operators is pr
3535
* [Querying JSON](#querying-json)
3636
* [Transactions](#transactions)
3737
* [Caveats](#caveats)
38+
* [Migrations](#migrations)
3839
* [Custom operators](#custom-operators)
3940
* [Debug SQL queries](#debug-sql-queries)
4041
* [Benchmarks](#benchmarks)
@@ -606,6 +607,102 @@ store.Transaction(func(s *UserStore) error {
606607
* `time.Time` fields will be truncated to remove its nanoseconds on `Save`, `Insert` or `Update`, since PostgreSQL will not be able to store them. PostgreSQL stores times with timezones as UTC internally. So, times will come back as UTC (you can use `Local` method to convert them back to the local timezone). You can change the timezone that will be used to bring times back from the database in [the PostgreSQL configuration](https://www.postgresql.org/docs/9.6/static/datatype-datetime.html).
607608
* Multidimensional arrays or slices are **not supported** except inside a JSON field.
608609

610+
## Migrations
611+
612+
Kallax can generate migrations for your schema automatically, if you want to. It is a process completely separated from the model generation, so it does not force you to generate your migrations using kallax.
613+
614+
Sometimes, kallax won't be able to infer a type or you will want a specific column type for a field. You can specify so with the `sqltype` struct tag on a field.
615+
616+
```go
617+
type Model struct {
618+
kallax.Model `table:"foo"`
619+
Stuff SuperCustomType `sqltype:"bytea"`
620+
}
621+
```
622+
623+
You can see the [**full list of default type mappings**](#type-mappings) between Go and SQL.
624+
625+
### Generate migrations
626+
627+
To generate a migration, you have to run the command `kallax migrate`.
628+
629+
```
630+
kallax migrate --input ./users/ --input ./posts/ --output ./migrations --name initial_schema
631+
```
632+
633+
The `migrate` command accepts the following flags:
634+
635+
| Name | Repeated | Description | Default |
636+
| --- | --- | --- | --- |
637+
| `--name` or `-n` | no | name of the migration file (will be converted to `a_snakecase_name`) | `migration` |
638+
| `--input` or `-i` | yes | every occurrence of this flag will specify a directory in which kallax models can be found. You can specify multiple times this flag if you have your models scattered across several packages | required |
639+
| `--output` or `-o` | no | destination folder where the migrations will be generated | `./migrations` |
640+
641+
Every single migration consists of 2 files:
642+
643+
- `TIMESTAMP_NAME.up.sql`: script that will upgrade your database to this version.
644+
- `TIMESTAMP_NAME.down.sql`: script that will downgrade your database to this version.
645+
646+
Additionally, there is a `lock.json` file where schema of the last migration is store to diff against the current models.
647+
648+
### Run migrations
649+
650+
To run a migration you can either use `kallax migrate up` or `kallax migrate down`. `up` will upgrade your database and `down` will downgrade it.
651+
652+
These are the flags available for `up` and `down`:
653+
654+
| Name | Description | Default |
655+
| --- | --- | --- |
656+
| `--dir` or `-d` | directory where your migrations are stored | `./migrations` |
657+
| `--dsn` | database connection string | required |
658+
| `--steps` or `-s` | maximum number of migrations to run | `0` |
659+
| `--all` | migrate all the way up (only available for `up` |
660+
| `--version` or `-v` | final version of the database we want after running the migration. The version is the timestamp value at the beginning of migration files | `0` |
661+
662+
* If no `--steps` or `--version` are provided to `down`, they will do nothing. If `--all` is provided to `up`, it will upgrade the database all the way up.
663+
* If `--steps` and `--version` are provided to either `up` or `down` it will use only `--version`, as it is more specific.
664+
665+
**Example:**
666+
667+
```
668+
kallax migrate up --dir ./my-migrations --dns 'user:pass@localhost:5432/dbname?sslmode=disable' --version 1493991142
669+
```
670+
671+
### Type mappings
672+
673+
| Go type | SQL type |
674+
| --- | --- |
675+
| `kallax.ULID` | `uuid` |
676+
| `kallax.UUID` | `uuid` |
677+
| `kallax.NumericID` | `serial` on primary keys, `bigint` on foreign keys |
678+
| `int64` on primary keys | `serial` |
679+
| `int64` on foreign keys and other fields| `bigint` |
680+
| `string` | `text` |
681+
| `rune` | `char(1)` |
682+
| `uint8` | `smallint` |
683+
| `int8` | `smallint` |
684+
| `byte` | `smallint` |
685+
| `uint16` | `integer` |
686+
| `int16` | `smallint` |
687+
| `uint32` | `bigint` |
688+
| `int32` | `integer` |
689+
| `uint` | `numeric(20)` |
690+
| `int` | `bigint` |
691+
| `int64` | `bigint` |
692+
| `uint64` | `numeric(20)` |
693+
| `float32` | `real` |
694+
| `float64` | `double` |
695+
| `bool` | `boolean` |
696+
| `url.URL` | `text` |
697+
| `time.Time` | `timestamptz` |
698+
| `time.Duration` | `bigint` |
699+
| `[]T` | `T'[]` * where `T'` is the SQL type of type `T` |
700+
| `map[K]V` | `jsonb` |
701+
| `struct` | `jsonb` |
702+
| `*struct` | `jsonb` |
703+
704+
Any other type must be explicitly specified.
705+
609706
## Custom operators
610707

611708
You can create custom operators with kallax using the `NewOperator` and `NewMultiOperator` functions.
@@ -665,7 +762,7 @@ func (g *gtInt) ToSql() (sql string, params []interface{}, err error) {
665762
query.Where(GtInt(SomeSchemaField, 9000))
666763
```
667764

668-
With most of the operators, `NewOperator` and `NewMultiOperator` are enough, so the usage of these functions is preferred over the completely custom approach. Use it only if there is no other way to build your custom operator.
765+
For most of the operators, `NewOperator` and `NewMultiOperator` are enough, so the usage of these functions is preferred over the completely custom approach. Use it only if there is no other way to build your custom operator.
669766

670767
## Debug SQL queries
671768

@@ -725,6 +822,7 @@ Source code of the benchmarks can be found on the [benchmarks](https://github.co
725822

726823
* Big thank you to the [Masterminds/squirrel](https://github.com/Masterminds/squirrel) library, which is an awesome query builder used internally in this ORM.
727824
* [lib/pq](https://github.com/lib/pq), the Golang PostgreSQL driver that ships with a ton of support for builtin Go types.
825+
* [mattes/migrate](https://github.com/mattes/migrate), a Golang library to manage database migrations.
728826

729827
## Contributing
730828

0 commit comments

Comments
 (0)