Skip to content

Commit

Permalink
fix: handle migration path conflicts for duplicate sql declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
davemooreuws committed Dec 19, 2024
1 parent 9c56463 commit 6bdc0f6
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
6 changes: 3 additions & 3 deletions cmd/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ var specCmd = &cobra.Command{
envVariables = map[string]string{}
}

spec, err := collector.ServiceRequirementsToSpec(proj.Name, envVariables, serviceRequirements, batchRequirements)
tui.CheckErr(err)

migrationImageContexts, err := collector.GetMigrationImageBuildContexts(serviceRequirements, batchRequirements, fs)
tui.CheckErr(err)
// Build images from contexts and provide updates on the builds
Expand Down Expand Up @@ -147,9 +150,6 @@ var specCmd = &cobra.Command{
outputFile = "./nitric-spec.json"
}

spec, err := collector.ServiceRequirementsToSpec(proj.Name, envVariables, serviceRequirements, batchRequirements)
tui.CheckErr(err)

marshaler := protojson.MarshalOptions{
Multiline: true,
Indent: " ",
Expand Down
6 changes: 3 additions & 3 deletions cmd/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,9 @@ var stackUpdateCmd = &cobra.Command{
envVariables["NITRIC_BETA_PROVIDERS"] = "true"
}

spec, err := collector.ServiceRequirementsToSpec(proj.Name, envVariables, serviceRequirements, batchRequirements)
tui.CheckErr(err)

migrationImageContexts, err := collector.GetMigrationImageBuildContexts(serviceRequirements, batchRequirements, fs)
tui.CheckErr(err)
// Build images from contexts and provide updates on the builds
Expand Down Expand Up @@ -267,9 +270,6 @@ var stackUpdateCmd = &cobra.Command{
}
}

spec, err := collector.ServiceRequirementsToSpec(proj.Name, envVariables, serviceRequirements, batchRequirements)
tui.CheckErr(err)

providerStdout := make(chan string)

// Step 4. Start the deployment provider server
Expand Down
24 changes: 24 additions & 0 deletions pkg/collector/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,16 +271,40 @@ func GetMigrationImageBuildContexts(allServiceRequirements []*ServiceRequirement
return imageBuildContexts, nil
}

func checkConflictingMigrations(allDatabases []map[string]*resourcespb.SqlDatabaseResource, resource map[string]*resourcespb.SqlDatabaseResource) error {
for _, dbs := range allDatabases {
for databaseName, dbConfig := range resource {
if existing, exists := dbs[databaseName]; exists {
if existing.Migrations.GetMigrationsPath() != dbConfig.Migrations.GetMigrationsPath() {
return fmt.Errorf("database '%s' has conflicting migrations paths; they must be identical", databaseName)
}
}
}
}

return nil
}

func buildDatabaseRequirements(allServiceRequirements []*ServiceRequirements, allBatchRequirements []*BatchRequirements, projectErrors *ProjectErrors) ([]*deploymentspb.Resource, error) {
resources := []*deploymentspb.Resource{}

allDatabases := []map[string]*resourcespb.SqlDatabaseResource{}

for _, serviceRequirements := range allServiceRequirements {
err := checkConflictingMigrations(allDatabases, serviceRequirements.sqlDatabases)
if err != nil {
return nil, err
}

allDatabases = append(allDatabases, serviceRequirements.sqlDatabases)
}

for _, batchRequirements := range allBatchRequirements {
err := checkConflictingMigrations(allDatabases, batchRequirements.sqlDatabases)
if err != nil {
return nil, err
}

allDatabases = append(allDatabases, batchRequirements.sqlDatabases)
}

Expand Down

0 comments on commit 6bdc0f6

Please sign in to comment.