|
| 1 | +use rust::setup::*; |
| 2 | +use sqlx::Executor; |
| 3 | + |
| 4 | +#[tokio::test] |
| 5 | +async fn test_omni_only_pk_rewrite() { |
| 6 | + let shard_0 = connection_sqlx_direct_db("shard_0").await; |
| 7 | + let shard_1 = connection_sqlx_direct_db("shard_1").await; |
| 8 | + |
| 9 | + for (shard, pool) in [&shard_0, &shard_1].iter().enumerate() { |
| 10 | + pool.execute( |
| 11 | + "DROP TABLE IF EXISTS public.test_omni_rewrite_pk_omni, test_omni_rewrite_pk_sharded CASCADE", |
| 12 | + ) |
| 13 | + .await |
| 14 | + .unwrap(); |
| 15 | + |
| 16 | + pool.execute("CREATE TABLE IF NOT EXISTS public.test_omni_rewrite_pk_omni(id BIGSERIAL PRIMARY KEY, value TEXT NOT NULL)").await.unwrap(); |
| 17 | + pool.execute("CREATE TABLE IF NOT EXISTS public.test_omni_rewrite_pk_sharded(id BIGSERIAL PRIMARY KEY, customer_id BIGINT NOT NULL, value TEXT NOT NULL)").await.unwrap(); |
| 18 | + |
| 19 | + pool.execute( |
| 20 | + "SELECT pgdog.install_sharded_sequence('public', 'test_omni_rewrite_pk_sharded', 'id')", |
| 21 | + ) |
| 22 | + .await |
| 23 | + .unwrap(); |
| 24 | + |
| 25 | + // Configure sharding. |
| 26 | + let mut t = pool.begin().await.unwrap(); |
| 27 | + t.execute("DELETE FROM pgdog.config").await.unwrap(); |
| 28 | + t.execute( |
| 29 | + format!( |
| 30 | + "INSERT INTO pgdog.config (shard, shards) VALUES ({}, 2)", |
| 31 | + shard |
| 32 | + ) |
| 33 | + .as_str(), |
| 34 | + ) |
| 35 | + .await |
| 36 | + .unwrap(); |
| 37 | + t.commit().await.unwrap(); |
| 38 | + } |
| 39 | + |
| 40 | + let sharded = connections_sqlx().await.pop().unwrap(); |
| 41 | + let admin = admin_sqlx().await; |
| 42 | + |
| 43 | + // This will reload the schema as well. |
| 44 | + admin |
| 45 | + .execute("SET rewrite_primary_key TO 'rewrite_omni'") |
| 46 | + .await |
| 47 | + .unwrap(); |
| 48 | + |
| 49 | + let starting_id: (i64,) = sqlx::query_as("SELECT pgdog.unique_id()") |
| 50 | + .fetch_one(&sharded) |
| 51 | + .await |
| 52 | + .unwrap(); |
| 53 | + |
| 54 | + for run in 0..25 { |
| 55 | + let omni_id: (i64,) = sqlx::query_as( |
| 56 | + "INSERT INTO public.test_omni_rewrite_pk_omni (value) VALUES ($1) RETURNING id", |
| 57 | + ) |
| 58 | + .bind(format!("test_{}", run)) |
| 59 | + .fetch_one(&sharded) |
| 60 | + .await |
| 61 | + .unwrap(); |
| 62 | + |
| 63 | + assert!( |
| 64 | + omni_id.0 > starting_id.0, |
| 65 | + "omni ID should be unique_id, but got {}", |
| 66 | + omni_id.0 |
| 67 | + ); |
| 68 | + |
| 69 | + let sharded_id: (i64,) = sqlx::query_as( |
| 70 | + "INSERT INTO public.test_omni_rewrite_pk_sharded (customer_id, value) VALUES ($1, $2) RETURNING id", |
| 71 | + ) |
| 72 | + .bind(run as i64) |
| 73 | + .bind(format!("test_{}", run)) |
| 74 | + .fetch_one(&sharded) |
| 75 | + .await |
| 76 | + .unwrap(); |
| 77 | + |
| 78 | + assert!( |
| 79 | + sharded_id.0 < omni_id.0, |
| 80 | + "sharded ID should not be unique_id, but got {}", |
| 81 | + sharded_id.0 |
| 82 | + ); |
| 83 | + } |
| 84 | + |
| 85 | + sharded.close().await; |
| 86 | + let sharded = connections_sqlx().await.pop().unwrap(); |
| 87 | + |
| 88 | + // Re-enable sharded unique ID. |
| 89 | + admin |
| 90 | + .execute("SET rewrite_primary_key TO 'rewrite'") |
| 91 | + .await |
| 92 | + .unwrap(); |
| 93 | + |
| 94 | + // The rewrite is cached in prepared statements |
| 95 | + // and the query cache, so we need to be careful to rest it |
| 96 | + // _after_ the client disconnected. In production, changing this setting |
| 97 | + // definitely requires a restart. |
| 98 | + admin.execute("RESET QUERY_CACHE").await.unwrap(); |
| 99 | + admin.execute("RESET PREPARED").await.unwrap(); |
| 100 | + |
| 101 | + for run in 25..50 { |
| 102 | + let omni_id: (i64,) = sqlx::query_as( |
| 103 | + "INSERT INTO public.test_omni_rewrite_pk_omni (value) VALUES ($1) RETURNING id", |
| 104 | + ) |
| 105 | + .bind(format!("test_{}", run)) |
| 106 | + .fetch_one(&sharded) |
| 107 | + .await |
| 108 | + .unwrap(); |
| 109 | + |
| 110 | + assert!( |
| 111 | + omni_id.0 > starting_id.0, |
| 112 | + "omni ID should be unique_id, but got {}", |
| 113 | + omni_id.0 |
| 114 | + ); |
| 115 | + |
| 116 | + let sharded_id: (i64,) = sqlx::query_as( |
| 117 | + "INSERT INTO public.test_omni_rewrite_pk_sharded (customer_id, value) VALUES ($1, $2) RETURNING id", |
| 118 | + ) |
| 119 | + .bind(run as i64) |
| 120 | + .bind(format!("test_{}", run)) |
| 121 | + .fetch_one(&sharded) |
| 122 | + .await |
| 123 | + .unwrap(); |
| 124 | + |
| 125 | + assert!( |
| 126 | + sharded_id.0 > omni_id.0, |
| 127 | + "sharded ID should be unique_id, but got {}", |
| 128 | + sharded_id.0 |
| 129 | + ); |
| 130 | + } |
| 131 | +} |
0 commit comments