Skip to content

Commit d3a9c4c

Browse files
authored
Testing: Update postgres sequences (#9304)
1 parent 2d8a895 commit d3a9c4c

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

models/test_fixtures.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,37 @@ func LoadFixtures() error {
3535
if err != nil {
3636
fmt.Printf("LoadFixtures failed after retries: %v\n", err)
3737
}
38+
// Now if we're running postgres we need to tell it to update the sequences
39+
if x.Dialect().DriverName() == "postgres" {
40+
results, err := x.QueryString(`SELECT 'SELECT SETVAL(' ||
41+
quote_literal(quote_ident(PGT.schemaname) || '.' || quote_ident(S.relname)) ||
42+
', COALESCE(MAX(' ||quote_ident(C.attname)|| '), 1) ) FROM ' ||
43+
quote_ident(PGT.schemaname)|| '.'||quote_ident(T.relname)|| ';'
44+
FROM pg_class AS S,
45+
pg_depend AS D,
46+
pg_class AS T,
47+
pg_attribute AS C,
48+
pg_tables AS PGT
49+
WHERE S.relkind = 'S'
50+
AND S.oid = D.objid
51+
AND D.refobjid = T.oid
52+
AND D.refobjid = C.attrelid
53+
AND D.refobjsubid = C.attnum
54+
AND T.relname = PGT.tablename
55+
ORDER BY S.relname;`)
56+
if err != nil {
57+
fmt.Printf("Failed to generate sequence update: %v\n", err)
58+
return err
59+
}
60+
for _, r := range results {
61+
for _, value := range r {
62+
_, err = x.Exec(value)
63+
if err != nil {
64+
fmt.Printf("Failed to update sequence: %s Error: %v\n", value, err)
65+
return err
66+
}
67+
}
68+
}
69+
}
3870
return err
3971
}

0 commit comments

Comments
 (0)