dialect/sql/schema: file based type store (#2644)

* dialect/sql/schema: file based type store

This PR adds support for a file based type storage when using versioned migrations. The file called `.ent_types` is written to the migration directory alongside the migration files and will be kept in sync for every migration file generation run.

In order to not break existing code, where the type storage might differ for different deployment, global unique ID mut be enabled by using a new option. This will also be raised as an error to the user when attempting to use versioned migrations and global unique ID.

Documentation will be added to this PR once feedback on the code is gathered.

* apply CR

* fix tests

* change format of types file to exclude it from atlas.sum file

* docs and drift test

* apply CR
This commit is contained in:
Jannik Clausen
2022-06-15 16:10:15 +02:00
committed by GitHub
parent 195be2d98d
commit 7017cbc898
12 changed files with 416 additions and 42 deletions

View File

@@ -67,7 +67,10 @@ func TestMySQL(t *testing.T) {
require.NoError(t, err, root.Exec(ctx, "DROP DATABASE IF EXISTS versioned_migrate", []interface{}{}, new(sql.Result)))
require.NoError(t, root.Exec(ctx, "CREATE DATABASE IF NOT EXISTS versioned_migrate", []interface{}{}, new(sql.Result)))
defer root.Exec(ctx, "DROP DATABASE IF EXISTS versioned_migrate", []interface{}{}, new(sql.Result))
Versioned(t, drv, versioned.NewClient(versioned.Driver(drv)))
vdrv, err := sql.Open("mysql", fmt.Sprintf("root:pass@tcp(localhost:%d)/versioned_migrate?parseTime=True", port))
require.NoError(t, err, "connecting to versioned migrate database")
defer vdrv.Close()
Versioned(t, vdrv, versioned.NewClient(versioned.Driver(vdrv)))
})
}
}
@@ -228,11 +231,15 @@ func Versioned(t *testing.T, drv sql.ExecQuerier, client *versioned.Client) {
template.Must(template.New("name").Parse(`{{ range .Changes }}{{ printf "%s;\n" .Cmd }}{{ end }}`)),
)
require.NoError(t, err)
opts := []schema.MigrateOption{schema.WithDir(dir), schema.WithGlobalUniqueID(true), schema.WithFormatter(format)}
opts := []schema.MigrateOption{
schema.WithDir(dir),
schema.WithUniversalID(),
schema.WithFormatter(format),
}
// Compared to empty database.
require.NoError(t, client.Schema.NamedDiff(ctx, "first", opts...))
require.Equal(t, 1, countFiles(t, dir))
require.Equal(t, 2, countFiles(t, dir))
// Apply the migrations.
fs, err := dir.Files()
@@ -242,14 +249,14 @@ func Versioned(t *testing.T, drv sql.ExecQuerier, client *versioned.Client) {
for sc.Scan() {
if sc.Text() != "" {
_, err := drv.ExecContext(ctx, sc.Text())
require.NoError(t, err)
require.NoError(t, err, sc.Text())
}
}
}
// Diff again - there should not be a new file.
require.NoError(t, client.Schema.NamedDiff(ctx, "second", opts...))
require.Equal(t, 1, countFiles(t, dir))
require.Equal(t, 2, countFiles(t, dir))
}
func V1ToV2(t *testing.T, dialect string, clientv1 *entv1.Client, clientv2 *entv2.Client) {