Avoid logs in Postgres

This commit is contained in:
Till Faelligen 2022-09-15 08:08:12 +02:00
parent a5f8c07184
commit 36128f6294
No known key found for this signature in database
GPG key ID: 3DF82D8AB9211D4E

View file

@ -146,19 +146,18 @@ func (m *Migrator) ExecutedMigrations(ctx context.Context) (map[string]struct{},
// inserts a migration given their name to the database. // inserts a migration given their name to the database.
// This should only be used when manually inserting migrations. // This should only be used when manually inserting migrations.
func InsertMigration(ctx context.Context, db *sql.DB, migrationName string) error { func InsertMigration(ctx context.Context, db *sql.DB, migrationName string) error {
_, err := db.ExecContext(ctx, createDBMigrationsSQL) m := NewMigrator(db)
existingMigrations, err := m.ExecutedMigrations(ctx)
if err != nil { if err != nil {
return fmt.Errorf("unable to create db_migrations: %w", err) return err
} }
_, err = db.ExecContext(ctx, insertVersionSQL, if _, ok := existingMigrations[migrationName]; ok {
return nil
}
_, err = m.db.ExecContext(ctx, insertVersionSQL,
migrationName, migrationName,
time.Now().Format(time.RFC3339), time.Now().Format(time.RFC3339),
internal.VersionString(), internal.VersionString(),
) )
// If the migration was already executed, we'll get a unique constraint error,
// return nil instead, to avoid unnecessary logging.
if IsUniqueConstraintViolationErr(err) {
return nil
}
return err return err
} }