Local-first runtime cleanup and recovery hardening
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -113,6 +114,19 @@ var localMigrations = []localMigration{
|
||||
name: "Add line_no to local_configurations and backfill ordering",
|
||||
run: addLocalConfigurationLineNo,
|
||||
},
|
||||
{
|
||||
id: "2026_03_07_local_partnumber_book_catalog",
|
||||
name: "Convert local partnumber book cache to book membership + deduplicated PN catalog",
|
||||
run: migrateLocalPartnumberBookCatalog,
|
||||
},
|
||||
}
|
||||
|
||||
type localPartnumberCatalogRow struct {
|
||||
Partnumber string
|
||||
LotsJSON LocalPartnumberBookLots
|
||||
Description string
|
||||
CreatedAt time.Time
|
||||
ServerID int
|
||||
}
|
||||
|
||||
func runLocalMigrations(db *gorm.DB) error {
|
||||
@@ -865,3 +879,216 @@ WHERE id IN (SELECT id FROM ranked)
|
||||
return nil
|
||||
}
|
||||
|
||||
func migrateLocalPartnumberBookCatalog(tx *gorm.DB) error {
|
||||
type columnInfo struct {
|
||||
Name string `gorm:"column:name"`
|
||||
}
|
||||
|
||||
hasBooksTable := tx.Migrator().HasTable(&LocalPartnumberBook{})
|
||||
hasItemsTable := tx.Migrator().HasTable(&LocalPartnumberBookItem{})
|
||||
if !hasItemsTable {
|
||||
return nil
|
||||
}
|
||||
|
||||
if hasBooksTable {
|
||||
var bookCols []columnInfo
|
||||
if err := tx.Raw(`SELECT name FROM pragma_table_info('local_partnumber_books')`).Scan(&bookCols).Error; err != nil {
|
||||
return fmt.Errorf("load local_partnumber_books columns: %w", err)
|
||||
}
|
||||
hasPartnumbersJSON := false
|
||||
for _, c := range bookCols {
|
||||
if c.Name == "partnumbers_json" {
|
||||
hasPartnumbersJSON = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !hasPartnumbersJSON {
|
||||
if err := tx.Exec(`ALTER TABLE local_partnumber_books ADD COLUMN partnumbers_json TEXT NOT NULL DEFAULT '[]'`).Error; err != nil {
|
||||
return fmt.Errorf("add local_partnumber_books.partnumbers_json: %w", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var itemCols []columnInfo
|
||||
if err := tx.Raw(`SELECT name FROM pragma_table_info('local_partnumber_book_items')`).Scan(&itemCols).Error; err != nil {
|
||||
return fmt.Errorf("load local_partnumber_book_items columns: %w", err)
|
||||
}
|
||||
hasBookID := false
|
||||
hasLotName := false
|
||||
hasLotsJSON := false
|
||||
for _, c := range itemCols {
|
||||
if c.Name == "book_id" {
|
||||
hasBookID = true
|
||||
}
|
||||
if c.Name == "lot_name" {
|
||||
hasLotName = true
|
||||
}
|
||||
if c.Name == "lots_json" {
|
||||
hasLotsJSON = true
|
||||
}
|
||||
}
|
||||
if !hasBookID && !hasLotName && !hasLotsJSON {
|
||||
return nil
|
||||
}
|
||||
|
||||
type legacyRow struct {
|
||||
BookID uint
|
||||
Partnumber string
|
||||
LotName string
|
||||
Description string
|
||||
CreatedAt time.Time
|
||||
ServerID int
|
||||
}
|
||||
bookPNs := make(map[uint]map[string]struct{})
|
||||
catalog := make(map[string]*localPartnumberCatalogRow)
|
||||
|
||||
if hasBookID || hasLotName {
|
||||
var rows []legacyRow
|
||||
if err := tx.Raw(`
|
||||
SELECT
|
||||
i.book_id,
|
||||
i.partnumber,
|
||||
i.lot_name,
|
||||
COALESCE(i.description, '') AS description,
|
||||
b.created_at,
|
||||
b.server_id
|
||||
FROM local_partnumber_book_items i
|
||||
INNER JOIN local_partnumber_books b ON b.id = i.book_id
|
||||
ORDER BY b.created_at DESC, b.id DESC, i.partnumber ASC, i.id ASC
|
||||
`).Scan(&rows).Error; err != nil {
|
||||
return fmt.Errorf("load legacy local partnumber book items: %w", err)
|
||||
}
|
||||
|
||||
for _, row := range rows {
|
||||
if _, ok := bookPNs[row.BookID]; !ok {
|
||||
bookPNs[row.BookID] = make(map[string]struct{})
|
||||
}
|
||||
bookPNs[row.BookID][row.Partnumber] = struct{}{}
|
||||
|
||||
entry, ok := catalog[row.Partnumber]
|
||||
if !ok {
|
||||
entry = &localPartnumberCatalogRow{
|
||||
Partnumber: row.Partnumber,
|
||||
Description: row.Description,
|
||||
CreatedAt: row.CreatedAt,
|
||||
ServerID: row.ServerID,
|
||||
}
|
||||
catalog[row.Partnumber] = entry
|
||||
}
|
||||
if row.CreatedAt.After(entry.CreatedAt) || (row.CreatedAt.Equal(entry.CreatedAt) && row.ServerID >= entry.ServerID) {
|
||||
entry.Description = row.Description
|
||||
entry.CreatedAt = row.CreatedAt
|
||||
entry.ServerID = row.ServerID
|
||||
}
|
||||
found := false
|
||||
for i := range entry.LotsJSON {
|
||||
if entry.LotsJSON[i].LotName == row.LotName {
|
||||
entry.LotsJSON[i].Qty += 1
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found && row.LotName != "" {
|
||||
entry.LotsJSON = append(entry.LotsJSON, LocalPartnumberBookLot{LotName: row.LotName, Qty: 1})
|
||||
}
|
||||
}
|
||||
|
||||
var books []LocalPartnumberBook
|
||||
if err := tx.Find(&books).Error; err != nil {
|
||||
return fmt.Errorf("load local partnumber books: %w", err)
|
||||
}
|
||||
for _, book := range books {
|
||||
pnSet := bookPNs[book.ID]
|
||||
partnumbers := make([]string, 0, len(pnSet))
|
||||
for pn := range pnSet {
|
||||
partnumbers = append(partnumbers, pn)
|
||||
}
|
||||
sort.Strings(partnumbers)
|
||||
if err := tx.Model(&LocalPartnumberBook{}).
|
||||
Where("id = ?", book.ID).
|
||||
Update("partnumbers_json", LocalStringList(partnumbers)).Error; err != nil {
|
||||
return fmt.Errorf("update partnumbers_json for local book %d: %w", book.ID, err)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
var items []LocalPartnumberBookItem
|
||||
if err := tx.Order("id DESC").Find(&items).Error; err != nil {
|
||||
return fmt.Errorf("load canonical local partnumber book items: %w", err)
|
||||
}
|
||||
for _, item := range items {
|
||||
entry, ok := catalog[item.Partnumber]
|
||||
if !ok {
|
||||
copiedLots := append(LocalPartnumberBookLots(nil), item.LotsJSON...)
|
||||
catalog[item.Partnumber] = &localPartnumberCatalogRow{
|
||||
Partnumber: item.Partnumber,
|
||||
LotsJSON: copiedLots,
|
||||
Description: item.Description,
|
||||
}
|
||||
continue
|
||||
}
|
||||
if entry.Description == "" && item.Description != "" {
|
||||
entry.Description = item.Description
|
||||
}
|
||||
for _, lot := range item.LotsJSON {
|
||||
merged := false
|
||||
for i := range entry.LotsJSON {
|
||||
if entry.LotsJSON[i].LotName == lot.LotName {
|
||||
if lot.Qty > entry.LotsJSON[i].Qty {
|
||||
entry.LotsJSON[i].Qty = lot.Qty
|
||||
}
|
||||
merged = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !merged {
|
||||
entry.LotsJSON = append(entry.LotsJSON, lot)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return rebuildLocalPartnumberBookCatalog(tx, catalog)
|
||||
}
|
||||
|
||||
func rebuildLocalPartnumberBookCatalog(tx *gorm.DB, catalog map[string]*localPartnumberCatalogRow) error {
|
||||
if err := tx.Exec(`
|
||||
CREATE TABLE local_partnumber_book_items_new (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
partnumber TEXT NOT NULL UNIQUE,
|
||||
lots_json TEXT NOT NULL,
|
||||
description TEXT
|
||||
)
|
||||
`).Error; err != nil {
|
||||
return fmt.Errorf("create new local_partnumber_book_items table: %w", err)
|
||||
}
|
||||
|
||||
orderedPartnumbers := make([]string, 0, len(catalog))
|
||||
for pn := range catalog {
|
||||
orderedPartnumbers = append(orderedPartnumbers, pn)
|
||||
}
|
||||
sort.Strings(orderedPartnumbers)
|
||||
for _, pn := range orderedPartnumbers {
|
||||
row := catalog[pn]
|
||||
sort.Slice(row.LotsJSON, func(i, j int) bool {
|
||||
return row.LotsJSON[i].LotName < row.LotsJSON[j].LotName
|
||||
})
|
||||
if err := tx.Table("local_partnumber_book_items_new").Create(&LocalPartnumberBookItem{
|
||||
Partnumber: row.Partnumber,
|
||||
LotsJSON: row.LotsJSON,
|
||||
Description: row.Description,
|
||||
}).Error; err != nil {
|
||||
return fmt.Errorf("insert new local_partnumber_book_items row for %s: %w", pn, err)
|
||||
}
|
||||
}
|
||||
|
||||
if err := tx.Exec(`DROP TABLE local_partnumber_book_items`).Error; err != nil {
|
||||
return fmt.Errorf("drop legacy local_partnumber_book_items: %w", err)
|
||||
}
|
||||
if err := tx.Exec(`ALTER TABLE local_partnumber_book_items_new RENAME TO local_partnumber_book_items`).Error; err != nil {
|
||||
return fmt.Errorf("rename new local_partnumber_book_items table: %w", err)
|
||||
}
|
||||
if err := tx.Exec(`CREATE UNIQUE INDEX IF NOT EXISTS idx_local_partnumber_book_items_partnumber ON local_partnumber_book_items(partnumber)`).Error; err != nil {
|
||||
return fmt.Errorf("create local_partnumber_book_items partnumber index: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user