Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle migration path conflicts for identical database declarations #836

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
28 changes: 28 additions & 0 deletions pkg/collector/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,16 +271,44 @@ 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 dbConfig.Migrations == nil {
continue
}

if existing.Migrations.GetMigrationsPath() != dbConfig.Migrations.GetMigrationsPath() {
davemooreuws marked this conversation as resolved.
Show resolved Hide resolved
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
Loading