Skip to content

Commit

Permalink
migration: added baseline option (#80)
Browse files Browse the repository at this point in the history
* migration: added `baseline` option

* chore: update doc
  • Loading branch information
giautm authored Aug 14, 2023
1 parent d43e4b0 commit b23df8f
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/resources/migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ resource "atlas_migration" "hello" {

### Optional

- `baseline` (String) An optional version to start the migration history from. See https://atlasgo.io/versioned/apply#existing-databases
- `cloud` (Block, Optional) (see [below for nested schema](#nestedblock--cloud))
- `dev_url` (String, Sensitive) The url of the dev-db see https://atlasgo.io/cli/url
- `dir` (String) the URL of the migration directory. dir or remote_dir block is required
Expand Down
6 changes: 6 additions & 0 deletions internal/provider/atlas_migration_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type (
DevURL types.String `tfsdk:"dev_url"`
RevisionsSchema types.String `tfsdk:"revisions_schema"`
Version types.String `tfsdk:"version"`
Baseline types.String `tfsdk:"baseline"`

Cloud *AtlasCloudBlock `tfsdk:"cloud"`
RemoteDir *RemoteDirBlock `tfsdk:"remote_dir"`
Expand Down Expand Up @@ -99,6 +100,10 @@ func (r *MigrationResource) Schema(_ context.Context, _ resource.SchemaRequest,
Optional: true,
Sensitive: true,
},
"baseline": schema.StringAttribute{
Description: "An optional version to start the migration history from. See https://atlasgo.io/versioned/apply#existing-databases",
Optional: true,
},
"revisions_schema": schema.StringAttribute{
Description: "The name of the schema the revisions table resides in",
Optional: true,
Expand Down Expand Up @@ -467,6 +472,7 @@ func (d *MigrationResourceModel) AtlasHCL(name string, devURL string, cloud *Atl
URL: d.URL.ValueString(),
DevURL: defaultString(d.DevURL, devURL),
DirURL: d.DirURL.ValueStringPointer(),
Baseline: d.Baseline.ValueString(),
RevisionsSchema: d.RevisionsSchema.ValueString(),
}
if d.Cloud != nil && d.Cloud.Token.ValueString() != "" {
Expand Down
3 changes: 3 additions & 0 deletions internal/provider/config/migrate.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ env {
{{- else }}
dir = "file://{{ or .DirURL "migrations" }}"
{{- end }}
{{- if .Baseline }}
baseline = "{{ .Baseline }}"
{{- end }}
{{- if .RevisionsSchema }}
revisions_schema = "{{ .RevisionsSchema }}"
{{- end }}
Expand Down
1 change: 1 addition & 0 deletions internal/provider/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type (
DirURL *string
RemoteDir *remoteDir

Baseline string
RevisionsSchema string
}
)
Expand Down
4 changes: 4 additions & 0 deletions internal/provider/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ func TestTemplate(t *testing.T) {
{name: "local", data: templateData{
URL: "mysql://user:pass@localhost:3306/tf-db",
}},
{name: "baseline", data: templateData{
URL: "mysql://user:pass@localhost:3306/tf-db",
Baseline: "100000",
}},
{name: "cloud-no-token", data: templateData{
URL: "mysql://user:pass@localhost:3306/tf-db",
RemoteDir: &remoteDir{
Expand Down
20 changes: 20 additions & 0 deletions internal/provider/testdata/TestTemplate/baseline-cfg.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@


env {
name = atlas.env
url = "mysql://user:pass@localhost:3306/tf-db"
migration {
dir = "file://migrations"
baseline = "100000"
}
format {
migrate {
apply = "{{ json . }}"
lint = "{{ json . }}"
status = "{{ json . }}"
}
}
lint {
format = "{{ json . }}"
}
}

0 comments on commit b23df8f

Please sign in to comment.