-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #372 from uoregon-libraries/fix/batchname
Store canonical batch name in the database
- Loading branch information
Showing
16 changed files
with
122 additions
and
36 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
### Fixed | ||
|
||
- Batch names are now stored in the database when a batch is built, rather than | ||
computed as needed. This fixes weird datestamp inconsistency when a batch is | ||
generated on a server with one timezone, and links to staging/production are | ||
generated in a different timezone. This also prevents us from having massive | ||
problems if we make a major change to the batch name generation algorithm. | ||
|
||
### Migration | ||
|
||
- Run database migrations: | ||
- `make && ./bin/migrate-database -c ./settings up` |
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
5 changes: 5 additions & 0 deletions
5
src/cmd/migrate-database/_sql/20241220071500_batch_permaname.sql
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,5 @@ | ||
-- +goose Up | ||
ALTER TABLE `batches` ADD `full_name` TINYTEXT COLLATE utf8_bin; | ||
|
||
-- +goose Down | ||
ALTER TABLE `batches` DROP COLUMN `full_name`; |
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
46 changes: 46 additions & 0 deletions
46
src/cmd/migrate-database/migrations/20241220073000_batch_permaname.go
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,46 @@ | ||
package migrations | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
|
||
"github.com/pressly/goose/v3" | ||
"github.com/uoregon-libraries/newspaper-curation-app/src/models" | ||
) | ||
|
||
func init() { | ||
goose.AddMigration(upAddBatchPermaname, noop) | ||
} | ||
|
||
func upAddBatchPermaname(tx *sql.Tx) error { | ||
var stmt, err = tx.Prepare("UPDATE batches SET full_name = ? WHERE id = ? AND (full_name IS NULL OR full_name = '')") | ||
if err != nil { | ||
return fmt.Errorf("preparing SQL: %w", err) | ||
} | ||
|
||
var batches []*models.Batch | ||
batches, err = models.ActionableBatches() | ||
if err != nil { | ||
return fmt.Errorf("reading batches from db: %w", err) | ||
} | ||
|
||
for _, b := range batches { | ||
if b.FullName == "" { | ||
b.GenerateFullName() | ||
var r, err = stmt.Exec(b.FullName, b.ID) | ||
if err != nil { | ||
return fmt.Errorf("saving batch %q: %w", b.FullName, err) | ||
} | ||
var n int64 | ||
n, err = r.RowsAffected() | ||
if err != nil { | ||
return fmt.Errorf("counting rows affected by batch naming: %w", err) | ||
} | ||
if n != 1 { | ||
return fmt.Errorf("saving batch %q: %d rows affected instead of 1", b.FullName, n) | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} |
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,9 @@ | ||
package migrations | ||
|
||
import "database/sql" | ||
|
||
// noop is for migrations that don't do anything, such as when a column is | ||
// deleted in an SQL migration, so there's no need for a logic reversal. | ||
func noop(_ *sql.Tx) error { | ||
return nil | ||
} |
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
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