product variant id
This commit is contained in:
39
main.go
39
main.go
@@ -133,6 +133,45 @@ func main() {
|
||||
log.Println("productlocations.publishedat added and backfilled (one time)")
|
||||
}
|
||||
|
||||
// A key generator for productvariants.variantid.
|
||||
//
|
||||
// The column is NOT NULL with no default and no identity, unlike
|
||||
// products.productid next door which is an identity column. So every insert
|
||||
// had to supply the id by hand, and GORM does not — it sent nothing and
|
||||
// Postgres refused the row with a not-null violation. That is why variants
|
||||
// could never be attached to a product: the write could not land at all.
|
||||
//
|
||||
// Guarded on the absence of a default rather than tracked as a migration
|
||||
// version, matching the checks above: the question is answerable from the
|
||||
// schema itself. The sequence starts above whatever ids are already there,
|
||||
// so the two rows on production keep theirs.
|
||||
var variantKeyed int64
|
||||
if err := db.DB.Raw(`
|
||||
SELECT COUNT(1) FROM information_schema.columns
|
||||
WHERE table_name = 'productvariants' AND column_name = 'variantid'
|
||||
AND (column_default IS NOT NULL OR is_identity = 'YES')`).
|
||||
Scan(&variantKeyed).Error; err != nil {
|
||||
log.Fatal("could not check productvariants.variantid:", err)
|
||||
}
|
||||
if variantKeyed == 0 {
|
||||
if err := db.DB.Exec(`
|
||||
CREATE SEQUENCE IF NOT EXISTS productvariants_variantid_seq
|
||||
START WITH 1 OWNED BY productvariants.variantid`).Error; err != nil {
|
||||
log.Fatal("could not create productvariants_variantid_seq:", err)
|
||||
}
|
||||
if err := db.DB.Exec(`
|
||||
SELECT setval('productvariants_variantid_seq',
|
||||
COALESCE((SELECT MAX(variantid) FROM productvariants), 0) + 1, false)`).Error; err != nil {
|
||||
log.Fatal("could not position productvariants_variantid_seq:", err)
|
||||
}
|
||||
if err := db.DB.Exec(`
|
||||
ALTER TABLE productvariants
|
||||
ALTER COLUMN variantid SET DEFAULT nextval('productvariants_variantid_seq')`).Error; err != nil {
|
||||
log.Fatal("could not default productvariants.variantid:", err)
|
||||
}
|
||||
log.Println("productvariants.variantid given a key generator (one time)")
|
||||
}
|
||||
|
||||
// The catalogue's own stable key for an imported product.
|
||||
//
|
||||
// `catalogueid` was never able to be this. The catalogue is rebuilt by
|
||||
|
||||
Reference in New Issue
Block a user