-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(block-explorer): make the block-explorer creation more reliable (#…
…977)
- Loading branch information
1 parent
4d5c530
commit fb69d9d
Showing
8 changed files
with
200 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package postgresql | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
) | ||
|
||
func isMigrationApplied(db *sql.DB, filename string) (bool, error) { | ||
var count int | ||
err := db.QueryRow("SELECT COUNT(*) FROM applied_migrations WHERE filename = $1", filename). | ||
Scan(&count) | ||
if err != nil { | ||
return false, err | ||
} | ||
return count > 0, nil | ||
} | ||
|
||
func ApplyMigration(db *sql.DB, filename string, content []byte) error { | ||
applied, err := isMigrationApplied(db, filename) | ||
if err != nil { | ||
return fmt.Errorf("failed to check if migration is applied: %w", err) | ||
} | ||
if applied { | ||
fmt.Printf("Migration %s already applied, skipping\n", filename) | ||
return nil | ||
} | ||
|
||
_, err = db.Exec(string(content)) | ||
if err != nil { | ||
return fmt.Errorf("failed to execute migration %s: %w", filename, err) | ||
} | ||
|
||
_, err = db.Exec("INSERT INTO applied_migrations (filename) VALUES ($1)", filename) | ||
if err != nil { | ||
return fmt.Errorf("failed to record applied migration %s: %w", filename, err) | ||
} | ||
|
||
fmt.Printf("Migration %s applied successfully\n", filename) | ||
return nil | ||
} |
Oops, something went wrong.