Skip to content

Commit

Permalink
fix schema inspect format
Browse files Browse the repository at this point in the history
  • Loading branch information
ronenlu committed Jan 9, 2025
1 parent c74e0c5 commit 9bc2498
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 21 deletions.
42 changes: 22 additions & 20 deletions atlasaction/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -843,7 +843,7 @@ func (a *Actions) MonitorSchema(ctx context.Context) error {
params := &atlasexec.SchemaInspectParams{
Schema: id.Schemas,
Exclude: id.Schemas,
Format: `{{ printf "# %s\n%s\n%s" .Hash .MarshalHCL .RedactedURL }}`,
Format: `{{ printf "# %s\n%s" .Hash .MarshalHCL }}`,
}
if db.String() != "" {
params.URL = db.String()
Expand All @@ -852,22 +852,6 @@ func (a *Actions) MonitorSchema(ctx context.Context) error {
params.ConfigURL = config
params.Env = env
}
res, err := a.Atlas.SchemaInspect(ctx, params)
if err != nil {
return fmt.Errorf("failed to inspect the schema: %w", err)
}
fmt.Println("result is:\n", res)
parts := strings.SplitN(res, "\n", 3)
for _, p := range parts {
fmt.Println("part is:\n", p)
}
var (
hash = strings.TrimPrefix(parts[0], "# ")
hcl = parts[1]
)
if id.URL == "" {
id.URL = parts[2] // Get the URL from the inspect output.
}
cc, err := a.cloudClient(ctx, "cloud-token")
if err != nil {
return err
Expand All @@ -876,13 +860,31 @@ func (a *Actions) MonitorSchema(ctx context.Context) error {
if err != nil {
return fmt.Errorf("failed to get the schema snapshot hash: %w", err)
}
input := &cloud.PushSnapshotInput{
ScopeIdent: id,
HashMatch: strings.HasPrefix(h, "h1:") && OldAgentHash(hcl) == h || hash == h,
res, err := a.Atlas.SchemaInspect(ctx, params)
if err != nil {
return fmt.Errorf("failed to inspect the schema: %w", err)
}
var (
parts = strings.SplitN(res, "\n", 2)
hash = strings.TrimPrefix(parts[0], "# ")
hcl = parts[1]
input = &cloud.PushSnapshotInput{
ScopeIdent: id,
HashMatch: strings.HasPrefix(h, "h1:") && OldAgentHash(hcl) == h || hash == h,
}
)
if !input.HashMatch {
input.Snapshot = &cloud.SnapshotInput{Hash: hash, HCL: hcl}
}
// Get the URL if not provided.
if id.URL == "" {
if id.URL, err = a.Atlas.SchemaInspect(ctx, &atlasexec.SchemaInspectParams{
ConfigURL: config,
Env: env,
}); err != nil {
return fmt.Errorf("failed to inspect the schema: %w", err)
}
}
u, err := cc.PushSnapshot(ctx, input)
if err != nil {
return fmt.Errorf("failed to push the schema snapshot: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion atlasaction/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ func TestMonitorSchema(t *testing.T) {
return nil
},
schemaInspect: func(_ context.Context, p *atlasexec.SchemaInspectParams) (string, error) {
return fmt.Sprintf("# %s\n%s\n%s", tt.newHash, tt.hcl, ""), nil
return fmt.Sprintf("# %s\n%s", tt.newHash, tt.hcl), nil
},
}
cc = &mockCloudClient{hash: tt.latestHash}
Expand Down

0 comments on commit 9bc2498

Please sign in to comment.