Skip to content

Commit

Permalink
Merge branch 'master' into atlas-disable-fk
Browse files Browse the repository at this point in the history
* master:
  doc/website/blog: versioned migrations (ent#2387)
  build(deps): bump github.com/spf13/cobra from 1.3.0 to 1.4.0 (ent#2386)
  go: upgrade atlas for tidb support (ent#2394)
  dialect/sql/schema: add method to create a named versioned migration … (ent#2385)
  docs: add maorlipchuk as a contributor for code (ent#2381)
  entc/gen: set Ref and Inverse for edge contains both From and To
  schema/field: expose RType.Implements method (ent#2379)
  docs: add imhuytq as a contributor for code (ent#2378)
  dialect/sql/schema: add name to versioned migration files (ent#2375)
  all: update atlasgo.io to latest (ent#2376)
  dialect/sql/schema: fix bug in atlas integration when working WithDropIndex/WithDropColumn (ent#2374)
  build(deps): bump actions/checkout from 2.4.0 to 3 (ent#2372)
  build(deps): bump actions/setup-node from 2.5.1 to 3 (ent#2365)
  • Loading branch information
zeevmoney committed Mar 15, 2022
2 parents 31fd4b7 + 41db628 commit f2d446f
Show file tree
Hide file tree
Showing 26 changed files with 628 additions and 92 deletions.
18 changes: 18 additions & 0 deletions .all-contributorsrc
Expand Up @@ -753,6 +753,24 @@
"contributions": [
"code"
]
},
{
"login": "imhuytq",
"name": "Huy TQ",
"avatar_url": "https://avatars.githubusercontent.com/u/5723282?v=4",
"profile": "https://huytq.com",
"contributions": [
"code"
]
},
{
"login": "maorlipchuk",
"name": "maorlipchuk",
"avatar_url": "https://avatars.githubusercontent.com/u/7034637?v=4",
"profile": "https://github.com/maorlipchuk",
"contributions": [
"code"
]
}
],
"contributorsPerLine": 7,
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/cd.yml
Expand Up @@ -13,8 +13,8 @@ jobs:
name: docs
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2.4.0
- uses: actions/setup-node@v2.5.1
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 14
- name: Install Dependencies
Expand Down
10 changes: 5 additions & 5 deletions .github/workflows/ci.yml
Expand Up @@ -13,7 +13,7 @@ jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2.4.0
- uses: actions/checkout@v3
- name: Run linters
uses: golangci/golangci-lint-action@v2.5.2
with:
Expand All @@ -25,7 +25,7 @@ jobs:
matrix:
go: ['1.17', '1.16']
steps:
- uses: actions/checkout@v2.4.0
- uses: actions/checkout@v3
- uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go }}
Expand Down Expand Up @@ -54,7 +54,7 @@ jobs:
generate:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2.4.0
- uses: actions/checkout@v3
- uses: actions/setup-go@v2
with:
go-version: '1.17'
Expand Down Expand Up @@ -226,7 +226,7 @@ jobs:
--health-timeout 5s
--health-retries 5
steps:
- uses: actions/checkout@v2.4.0
- uses: actions/checkout@v3
- uses: actions/setup-go@v2
with:
go-version: '1.17'
Expand Down Expand Up @@ -392,7 +392,7 @@ jobs:
--health-timeout 5s
--health-retries 5
steps:
- uses: actions/checkout@v2.4.0
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: actions/setup-go@v2
Expand Down
18 changes: 9 additions & 9 deletions dialect/sql/schema/atlas.go
Expand Up @@ -291,18 +291,18 @@ func (m *Migrate) setupAtlas() error {
if m.withFixture {
return errors.New("sql/schema: WithFixture(true) does not work in Atlas migration")
}
k := DropIndex | DropColumn
skip := DropIndex | DropColumn
if m.atlas.skip != NoChange {
k = m.atlas.skip
skip = m.atlas.skip
}
if m.dropIndexes {
k |= ^DropIndex
skip &= ^DropIndex
}
if m.dropColumns {
k |= ^DropColumn
skip &= ^DropColumn
}
if k == NoChange {
m.atlas.diff = append(m.atlas.diff, filterChanges(k))
if skip != NoChange {
m.atlas.diff = append(m.atlas.diff, filterChanges(skip))
}
if !m.withForeignKeys {
m.atlas.diff = append(m.atlas.diff, withoutForeignKeys)
Expand All @@ -329,7 +329,7 @@ func (m *Migrate) atCreate(ctx context.Context, tables ...*Table) error {
return err
}
}
plan, err := m.atDiff(ctx, tx, tables...)
plan, err := m.atDiff(ctx, tx, "", tables...)
if err != nil {
return err
}
Expand All @@ -355,7 +355,7 @@ func (m *Migrate) atCreate(ctx context.Context, tables ...*Table) error {
return tx.Commit()
}

func (m *Migrate) atDiff(ctx context.Context, conn dialect.ExecQuerier, tables ...*Table) (*migrate.Plan, error) {
func (m *Migrate) atDiff(ctx context.Context, conn dialect.ExecQuerier, name string, tables ...*Table) (*migrate.Plan, error) {
drv, err := m.atOpen(conn)
if err != nil {
return nil, err
Expand Down Expand Up @@ -385,7 +385,7 @@ func (m *Migrate) atDiff(ctx context.Context, conn dialect.ExecQuerier, tables .
return nil, err
}
// Plan changes.
return drv.PlanChanges(ctx, "", changes)
return drv.PlanChanges(ctx, name, changes)
}

type db struct{ dialect.ExecQuerier }
Expand Down
14 changes: 12 additions & 2 deletions dialect/sql/schema/migrate.go
Expand Up @@ -165,14 +165,24 @@ func (m *Migrate) Create(ctx context.Context, tables ...*Table) error {
// Diff compares the state read from the StateReader with the state defined by Ent.
// Changes will be written to migration files by the configures Planner.
func (m *Migrate) Diff(ctx context.Context, tables ...*Table) error {
return m.NamedDiff(ctx, "changes", tables...)
}

// NamedDiff compares the state read from the StateReader with the state defined by Ent.
// Changes will be written to migration files by the configures Planner.
func (m *Migrate) NamedDiff(ctx context.Context, name string, tables ...*Table) error {
if m.atlas.dir == nil {
return errors.New("no migration directory given")
}
plan, err := m.atDiff(ctx, m, tables...)
plan, err := m.atDiff(ctx, m, name, tables...)
if err != nil {
return err
}
return migrate.New(nil, m.atlas.dir, m.atlas.fmt).WritePlan(plan)
// Skip if the plan has no changes.
if len(plan.Changes) == 0 {
return nil
}
return migrate.NewPlanner(nil, m.atlas.dir, migrate.WithFormatter(m.atlas.fmt)).WritePlan(plan)
}

func (m *Migrate) create(ctx context.Context, tables ...*Table) error {
Expand Down
2 changes: 2 additions & 0 deletions doc/md/contributors.md
Expand Up @@ -114,6 +114,8 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
<td align="center"><a href="https://pedro.dev.br"><img src="https://avatars.githubusercontent.com/u/1251151?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Pedro Henrique</b></sub></a><br /><a href="https://github.com/ent/ent/commits?author=crossworth" title="Code">💻</a></td>
<td align="center"><a href="https://2jp.de"><img src="https://avatars.githubusercontent.com/u/7414374?v=4?s=100" width="100px;" alt=""/><br /><sub><b>MrParano1d</b></sub></a><br /><a href="https://github.com/ent/ent/commits?author=MrParano1d" title="Code">💻</a></td>
<td align="center"><a href="https://github.com/tprebs"><img src="https://avatars.githubusercontent.com/u/6523587?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Thomas Prebble</b></sub></a><br /><a href="https://github.com/ent/ent/commits?author=tprebs" title="Code">💻</a></td>
<td align="center"><a href="https://huytq.com"><img src="https://avatars.githubusercontent.com/u/5723282?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Huy TQ</b></sub></a><br /><a href="https://github.com/ent/ent/commits?author=imhuytq" title="Code">💻</a></td>
<td align="center"><a href="https://github.com/maorlipchuk"><img src="https://avatars.githubusercontent.com/u/7034637?v=4?s=100" width="100px;" alt=""/><br /><sub><b>maorlipchuk</b></sub></a><br /><a href="https://github.com/ent/ent/commits?author=maorlipchuk" title="Code">💻</a></td>
</tr>
</table>

Expand Down
23 changes: 14 additions & 9 deletions doc/md/versioned-migrations.md
Expand Up @@ -10,10 +10,13 @@ tool you like (like golang-migrate, Flyway, liquibase).

![atlas-versioned-migration-process](https://entgo.io/images/assets/migrate-atlas-versioned.png)

## Configuration
## Generating Versioned Migration Files

### From Client

In order to have Ent make the necessary changes to your code, you have to enable this feature with one of the two
options:
If you want to use an instantiated Ent client to create new migration files, you have to enable the versioned
migrations feature flag in order to have Ent make the necessary changes to the generated code. Depending on how you
execute the Ent code generator, you have to use one of the two options:

1. If you are using the default go generate configuration, simply add the `--feature sql/versioned-migration` to
the `ent/generate.go` file as follows:
Expand Down Expand Up @@ -47,12 +50,8 @@ func main() {
}
```

## Generating Versioned Migration Files

### From Client

After regenerating the project, there will be an extra `Diff` method on the Ent client that you can use to inspect the
connected database, compare it with the schema definitions and create sql statements needed to migrate the database to
connected database, compare it with the schema definitions and create SQL statements needed to migrate the database to
the graph.

```go
Expand Down Expand Up @@ -82,6 +81,8 @@ func main() {
}
// Write migration diff.
err = client.Schema.Diff(ctx, schema.WithDir(dir))
// You can use the following method to give the migration files a name.
// err = client.Schema.NamedDiff(ctx, "migration_name", schema.WithDir(dir))
if err != nil {
log.Fatalf("failed creating schema resources: %v", err)
}
Expand All @@ -93,7 +94,7 @@ You can then create a new set of migration files by simply calling `go run -mod=
### From Graph

You can also generate new migration files without an instantiated Ent client. This can be useful if you want to make the
migration file creation part of a go generate workflow.
migration file creation part of a go generate workflow. Note, that in this case enabling the feature flag is optional.

```go
package main
Expand Down Expand Up @@ -137,6 +138,10 @@ func main() {
if err := m.Diff(context.Background(), tbls...); err != nil {
log.Fatalln(err)
}
// You can use the following method to give the migration files a name.
// if err := m.NamedDiff(context.Background(), "migration_name", tbls...); err != nil {
// log.Fatalln(err)
// }
}
```

Expand Down

0 comments on commit f2d446f

Please sign in to comment.