Skip to content

Commit

Permalink
Improving migration script to avoid large amount of data and run just…
Browse files Browse the repository at this point in the history
… one time (#405)

Signed-off-by: Nathan Martins <[email protected]>
  • Loading branch information
nathanmartinszup committed Oct 19, 2021
1 parent f73adad commit 98929d1
Showing 1 changed file with 42 additions and 3 deletions.
45 changes: 42 additions & 3 deletions api/cmd/migration/hash/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package hash

import (
"fmt"
"regexp"
"strings"

Expand All @@ -28,6 +29,9 @@ import (
"github.com/ZupIT/horusec-devkit/pkg/utils/logger"
)

const MigrationName = "20211019_hash_migration"
const MigrationTable = "horusec_migrations"

type Migration struct {
databaseConnection *database.Connection
}
Expand All @@ -47,7 +51,29 @@ func StartMigration() {
databaseConnection: databaseConnection,
}

if migration.alreadyMigrated() {
return
}

migration.migrateHashesToRemoveVulnID()
migration.setMigrationAsApplied()
}

func (m *Migration) alreadyMigrated() bool {
var migrations []string

err := m.databaseConnection.Read.Find(&migrations, map[string]interface{}{}, MigrationTable).GetErrorExceptNotFound()
if err != nil {
logger.LogError("HASH MIGRATION - failed to check if migration is already applied", err)
}

for _, v := range migrations {
if v == MigrationName {
return true
}
}

return false
}

func (m *Migration) migrateHashesToRemoveVulnID() {
Expand Down Expand Up @@ -82,6 +108,9 @@ func (m *Migration) querySelectLatestVulnerabilities() string {
ORDER BY an.repository_id, an.created_at DESC
) AS latest_analysis
ON latest_analysis.analysis_id = av.analysis_id
WHERE vuln.details LIKE ANY (array['%HS-JAVA%', '%HS-JS%', '%HS-CSHARP%',
'%HS-DART%', '%HS-JVM%', '%HS-KOTLIN%', '%HS-KUBERNETES%', '%HS-LEAKS%', '%HS-NGINX%',
'%HS-JAVASCRIPT%', '%HS-SWIFT%'])
`
}

Expand All @@ -91,20 +120,23 @@ func (m *Migration) updateVulnerability(vuln *Vulnerability) {
if vuln.VulnHash != expectHash {
correctVulnerability, err := m.getVulnerabilityByHash(expectHash)
if err != nil {
logger.LogWarn("HASH MIGRATION - failed to get vulnerability by hash")
return
}

vulnerabilityNToN, err := m.getVulnerabilityNToN(vuln.AnalysisID, vuln.VulnerabilityID)
if err != nil {
logger.LogWarn("HASH MIGRATION - failed to get vulnerability n to n")
return
}

vulnerabilityNToN.VulnerabilityID = correctVulnerability.VulnerabilityID
err = m.databaseConnection.Write.Update(vulnerabilityNToN,
m.filterManyToManyByID(vuln.AnalysisID, vuln.VulnerabilityID), vulnerabilityNToN.GetTable()).GetError()
logger.LogError("HASH MIGRATION - failed to update vulnerability n to n", err)
if err != nil {
logger.LogError("HASH MIGRATION - failed to update vulnerability n to n", err)
return
}

logger.LogInfo(fmt.Sprintf("HASH MIGRATION - hash %s migrated", vuln.VulnHash))
}
}

Expand Down Expand Up @@ -150,3 +182,10 @@ func (m *Migration) removeHorusecIDFromDetails(details string) string {

return regexp.MustCompile(horusecIDRegex).ReplaceAllString(details, "")
}

func (m *Migration) setMigrationAsApplied() {
err := m.databaseConnection.Write.Create(map[string]interface{}{"name": MigrationName}, MigrationTable).GetError()
if err != nil {
logger.LogPanic("HASH MIGRATION- failed to set that migration was applied on database", err)
}
}

0 comments on commit 98929d1

Please sign in to comment.