Skip to content

Commit 44f98be

Browse files
Nemo157Joshua Nelson
authored and
Joshua Nelson
committed
Offset primary key sequences during testing to test referencing incorrect tables
1 parent 15e702d commit 44f98be

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/test/mod.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,29 @@ impl TestDatabase {
239239
))?;
240240
crate::db::migrate(None, &conn)?;
241241

242+
// Move all sequence start positions 10000 apart to avoid overlapping primary keys
243+
let query: String = conn
244+
.query(
245+
"
246+
SELECT relname
247+
FROM pg_class
248+
INNER JOIN pg_namespace
249+
ON pg_class.relnamespace = pg_namespace.oid
250+
WHERE pg_class.relkind = 'S'
251+
AND pg_namespace.nspname = $1
252+
",
253+
&[&schema],
254+
)?
255+
.into_iter()
256+
.map(|row| row.get(0))
257+
.enumerate()
258+
.map(|(i, sequence): (_, String)| {
259+
let offset = (i + 1) * 10000;
260+
format!(r#"ALTER SEQUENCE "{}" RESTART WITH {};"#, sequence, offset)
261+
})
262+
.collect();
263+
conn.batch_execute(&query)?;
264+
242265
Ok(TestDatabase {
243266
pool: Pool::new_with_schema(config, &schema)?,
244267
schema,

0 commit comments

Comments
 (0)