Skip to content

Commit

Permalink
Merge pull request #2131 from keboola/jt-psgo-918-rollback
Browse files Browse the repository at this point in the history
 fix: Preserve state on rollback
  • Loading branch information
jachym-tousek-keboola authored Nov 4, 2024
2 parents c89867f + 4e26407 commit d25244c
Show file tree
Hide file tree
Showing 239 changed files with 796 additions and 180 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func TestSinkRepository_Enable(t *testing.T) {
now = now.Add(time.Hour)
sink, err := repo.RollbackVersion(sinkKey, now, by, 2).Do(ctx).ResultOrErr()
require.NoError(t, err)
assert.True(t, sink.IsDisabled())
assert.False(t, sink.IsDisabled()) // Rollback intentionally doesn't change disabled/enabled state
assert.Equal(t, definition.VersionNumber(4), sink.VersionNumber())
}

Expand Down Expand Up @@ -293,6 +293,13 @@ func TestSinkRepository_EnableSinksOnSourceEnable(t *testing.T) {
source, err := d.DefinitionRepository().Source().RollbackVersion(sourceKey, now, by, 2).Do(ctx).ResultOrErr()
require.NoError(t, err)
assert.Equal(t, definition.VersionNumber(4), source.Version.Number)
assert.False(t, source.IsDisabled()) // Rollback intentionally doesn't change disabled/enabled state
}
{
// Disable the Source
now = now.Add(time.Hour)
source, err := d.DefinitionRepository().Source().Disable(sourceKey, now, by, "test").Do(ctx).ResultOrErr()
require.NoError(t, err)
assert.True(t, source.IsDisabled())
}
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ func (r *Repository) RollbackVersion(k key.SinkKey, now time.Time, by definition
// Prepare the new value
versionDescription := fmt.Sprintf(`Rollback to version "%d".`, targetVersion.Version.Number)
updated = deepcopy.Copy(*targetVersion).(definition.Sink)

// Preserve Disabled and Deleted states from the latest version.
// Without this it would be possible to bypass the Enable/Undelete event and avoid billing.
updated.Disabled = deepcopy.Copy(latestVersion.Disabled).(*definition.Disabled)
updated.Enabled = deepcopy.Copy(latestVersion.Enabled).(*definition.Enabled)
updated.Deleted = deepcopy.Copy(latestVersion.Deleted).(*definition.Deleted)
updated.Undeleted = deepcopy.Copy(latestVersion.Undeleted).(*definition.Undeleted)

// Increase version
updated.Version = latestVersion.Version
updated.IncrementVersion(updated, now, by, versionDescription)
return r.save(ctx, now, by, latestVersion, &updated)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ func (r *Repository) RollbackVersion(k key.SourceKey, now time.Time, by definiti
// Prepare the new value
versionDescription := fmt.Sprintf(`Rollback to version "%d".`, targetVersion.Version.Number)
updated = deepcopy.Copy(*targetVersion).(definition.Source)

// Preserve Disabled and Deleted states from the latest version.
// Without this it would be possible to bypass the Enable/Undelete event and avoid billing.
updated.Disabled = deepcopy.Copy(latestVersion.Disabled).(*definition.Disabled)
updated.Enabled = deepcopy.Copy(latestVersion.Enabled).(*definition.Enabled)
updated.Deleted = deepcopy.Copy(latestVersion.Deleted).(*definition.Deleted)
updated.Undeleted = deepcopy.Copy(latestVersion.Undeleted).(*definition.Undeleted)

// Increase version
updated.Version = latestVersion.Version
updated.IncrementVersion(updated, now, by, versionDescription)
return r.save(ctx, now, by, latestVersion, &updated)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"path": "/v1/branches/default/sources",
"path": "/v1/branches/%%TEST_DEFAULT_BRANCH_ID%%/sources",
"method": "POST",
"headers": {
"Content-Type": "application/json",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"path": "/v1/branches/default/sources/my-source/sinks",
"path": "/v1/branches/%%TEST_DEFAULT_BRANCH_ID%%/sources/my-source/sinks",
"method": "POST",
"headers": {
"Content-Type": "application/json",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"path": "/v1/branches/default/sources",
"path": "/v1/branches/%%TEST_DEFAULT_BRANCH_ID%%/sources",
"method": "POST",
"headers": {
"Content-Type": "application/json",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"path": "/v1/branches/default/sources/my-source/sinks",
"path": "/v1/branches/%%TEST_DEFAULT_BRANCH_ID%%/sources/my-source/sinks",
"method": "POST",
"headers": {
"Content-Type": "application/json",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"path": "/v1/branches/default/sources",
"path": "/v1/branches/%%TEST_DEFAULT_BRANCH_ID%%/sources",
"method": "POST",
"headers": {
"Content-Type": "application/json",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"path": "/v1/branches/default/sources/my-source/sinks",
"path": "/v1/branches/%%TEST_DEFAULT_BRANCH_ID%%/sources/my-source/sinks",
"method": "POST",
"headers": {
"Content-Type": "application/json",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"path": "/v1/branches/default/sources",
"path": "/v1/branches/%%TEST_DEFAULT_BRANCH_ID%%/sources",
"method": "POST",
"headers": {
"Content-Type": "application/json",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"path": "/v1/branches/default/sources/my-source/sinks",
"path": "/v1/branches/%%TEST_DEFAULT_BRANCH_ID%%/sources/my-source/sinks",
"method": "POST",
"headers": {
"Content-Type": "application/json",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"path": "/v1/branches/default/sources",
"path": "/v1/branches/%%TEST_DEFAULT_BRANCH_ID%%/sources",
"method": "POST",
"headers": {
"Content-Type": "application/json",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"path": "/v1/branches/default/sources/my-source/sinks",
"path": "/v1/branches/%%TEST_DEFAULT_BRANCH_ID%%/sources/my-source/sinks",
"method": "POST",
"headers": {
"Content-Type": "application/json",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"path": "/v1/branches/default/sources",
"path": "/v1/branches/%%TEST_DEFAULT_BRANCH_ID%%/sources",
"method": "POST",
"headers": {
"Content-Type": "application/json",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"path": "/v1/branches/default/sources/my-source/sinks",
"path": "/v1/branches/%%TEST_DEFAULT_BRANCH_ID%%/sources/my-source/sinks",
"method": "POST",
"headers": {
"Content-Type": "application/json",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"path": "/v1/branches/default/sources",
"path": "/v1/branches/%%TEST_DEFAULT_BRANCH_ID%%/sources",
"method": "POST",
"headers": {
"Content-Type": "application/json",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"path": "/v1/branches/default/sources/my-source/sinks",
"path": "/v1/branches/%%TEST_DEFAULT_BRANCH_ID%%/sources/my-source/sinks",
"method": "POST",
"headers": {
"Content-Type": "application/json",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"path": "/v1/branches/default/sources",
"path": "/v1/branches/%%TEST_DEFAULT_BRANCH_ID%%/sources",
"method": "POST",
"headers": {
"Content-Type": "application/json",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"path": "/v1/branches/default/sources/my-source/sinks",
"path": "/v1/branches/%%TEST_DEFAULT_BRANCH_ID%%/sources/my-source/sinks",
"method": "POST",
"headers": {
"Content-Type": "application/json",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"path": "/v1/branches/default/sources",
"path": "/v1/branches/%%TEST_DEFAULT_BRANCH_ID%%/sources",
"method": "POST",
"headers": {
"Content-Type": "application/json",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"path": "/v1/branches/default/sources/my-source/sinks",
"path": "/v1/branches/%%TEST_DEFAULT_BRANCH_ID%%/sources/my-source/sinks",
"method": "POST",
"headers": {
"Content-Type": "application/json",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"path": "/v1/branches/default/sources/my-source/sinks",
"path": "/v1/branches/%%TEST_DEFAULT_BRANCH_ID%%/sources/my-source/sinks",
"method": "POST",
"headers": {
"Content-Type": "application/json"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"path": "/v1/branches/default/sources",
"path": "/v1/branches/%%TEST_DEFAULT_BRANCH_ID%%/sources",
"method": "POST",
"headers": {
"Content-Type": "application/json",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"path": "/v1/branches/default/sources/my-source/sinks",
"path": "/v1/branches/%%TEST_DEFAULT_BRANCH_ID%%/sources/my-source/sinks",
"method": "POST",
"headers": {
"Content-Type": "application/json",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"path": "/v1/branches/default/sources/my-source/sinks",
"path": "/v1/branches/%%TEST_DEFAULT_BRANCH_ID%%/sources/my-source/sinks",
"method": "POST",
"headers": {
"Content-Type": "application/json",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"path": "/v1/branches/default/sources",
"path": "/v1/branches/%%TEST_DEFAULT_BRANCH_ID%%/sources",
"method": "POST",
"headers": {
"Content-Type": "application/json",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"path": "/v1/branches/default/sources/my-source/sinks",
"path": "/v1/branches/%%TEST_DEFAULT_BRANCH_ID%%/sources/my-source/sinks",
"method": "POST",
"headers": {
"Content-Type": "application/json",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"path": "/v1/branches/default/sources",
"path": "/v1/branches/%%TEST_DEFAULT_BRANCH_ID%%/sources",
"method": "POST",
"headers": {
"Content-Type": "application/json",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"path": "/v1/branches/default/sources/my-source/sinks",
"path": "/v1/branches/%%TEST_DEFAULT_BRANCH_ID%%/sources/my-source/sinks",
"method": "POST",
"headers": {
"Content-Type": "application/json",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"path": "/v1/branches/default/sources/my-source/sinks",
"path": "/v1/branches/%%TEST_DEFAULT_BRANCH_ID%%/sources/my-source/sinks",
"method": "POST",
"headers": {
"Content-Type": "application/json",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"path": "/v1/branches/default/sources",
"path": "/v1/branches/%%TEST_DEFAULT_BRANCH_ID%%/sources",
"method": "POST",
"headers": {
"Content-Type": "application/json",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"path": "/v1/branches/default/sources/my-source/sinks",
"path": "/v1/branches/%%TEST_DEFAULT_BRANCH_ID%%/sources/my-source/sinks",
"method": "POST",
"headers": {
"Content-Type": "application/json",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"path": "/v1/branches/default/sources/my-source/sinks",
"path": "/v1/branches/%%TEST_DEFAULT_BRANCH_ID%%/sources/my-source/sinks",
"method": "POST",
"headers": {
"Content-Type": "application/json",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"path": "/v1/branches/default/sources/my-source/sinks",
"path": "/v1/branches/%%TEST_DEFAULT_BRANCH_ID%%/sources/my-source/sinks",
"method": "POST",
"headers": {
"Content-Type": "application/json",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"path": "/v1/branches/default/sources",
"path": "/v1/branches/%%TEST_DEFAULT_BRANCH_ID%%/sources",
"method": "POST",
"headers": {
"Content-Type": "application/json",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"path": "/v1/branches/default/sources/my-source/sinks",
"path": "/v1/branches/%%TEST_DEFAULT_BRANCH_ID%%/sources/my-source/sinks",
"method": "POST",
"headers": {
"Content-Type": "application/json",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
202
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"taskId": "api.create.source/%%TEST_DEFAULT_BRANCH_ID%%/my-source/%s",
"type": "api.create.source",
"url": "https://stream.keboola.local/v1/tasks/api.create.source/%%TEST_DEFAULT_BRANCH_ID%%/my-source/%s",
"status": "processing",
"isFinished": false,
"createdAt": "%s"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"path": "/v1/branches/%%TEST_DEFAULT_BRANCH_ID%%/sources",
"method": "POST",
"headers": {
"Content-Type": "application/json",
"X-StorageApi-Token": "%%TEST_KBC_STORAGE_API_TOKEN%%"
},
"body": {
"name": "My Source",
"type": "http"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"taskId": "api.create.source/%%TEST_DEFAULT_BRANCH_ID%%/my-source/%s",
"type": "api.create.source",
"url": "https://stream.keboola.local/v1/tasks/api.create.source/%%TEST_DEFAULT_BRANCH_ID%%/my-source/%s",
"status": "success",
"isFinished": true,
"createdAt": "%s",
"finishedAt": "%s",
"duration": %d,
"result": "Source has been created successfully.",
"outputs": {
"url": "https://stream.keboola.local/v1/branches/%%TEST_DEFAULT_BRANCH_ID%%/sources/my-source",
"projectId": %%TEST_KBC_PROJECT_ID%%,
"branchId": %%TEST_DEFAULT_BRANCH_ID%%,
"sourceId": "my-source"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"path": "<<001-create-source:response.url>>",
"method": "GET",
"headers": {
"X-StorageApi-Token": "%%TEST_KBC_STORAGE_API_TOKEN%%"
},
"repeat": {
"until": "status != 'processing'",
"timeout": 60
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
202
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"taskId": "api.create.sink/%%TEST_DEFAULT_BRANCH_ID%%/my-source/my-sink/%s",
"type": "api.create.sink",
"url": "https://stream.keboola.local/v1/tasks/api.create.sink/%%TEST_DEFAULT_BRANCH_ID%%/my-source/my-sink/%s",
"status": "processing",
"isFinished": false,
"createdAt": "%s"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"path": "/v1/branches/%%TEST_DEFAULT_BRANCH_ID%%/sources/my-source/sinks",
"method": "POST",
"headers": {
"Content-Type": "application/json",
"X-StorageApi-Token": "%%TEST_KBC_STORAGE_API_TOKEN%%"
},
"body": {
"name": "My Sink",
"type": "table",
"table": {
"type": "keboola",
"tableId": "in.c-my-bucket.my-table",
"mapping": {
"columns": [
{
"type": "uuid",
"name": "id"
},
{
"type": "body",
"name": "body"
}
]
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
200
Loading

0 comments on commit d25244c

Please sign in to comment.