|
| 1 | +require "../spec_helper" |
| 2 | + |
| 3 | +struct TestMessageHandler |
| 4 | + include PG::Replication::Handler |
| 5 | + |
| 6 | + def received(msg : PG::Replication::Begin) |
| 7 | + pp begin: msg |
| 8 | + end |
| 9 | + |
| 10 | + def received(msg : PG::Replication::Message) |
| 11 | + pp message: msg |
| 12 | + end |
| 13 | + |
| 14 | + def received(msg : PG::Replication::Commit) |
| 15 | + pp commit: msg |
| 16 | + end |
| 17 | + |
| 18 | + def received(msg : PG::Replication::Origin) |
| 19 | + end |
| 20 | + |
| 21 | + def received(msg : PG::Replication::Relation) |
| 22 | + pp relation: msg |
| 23 | + end |
| 24 | + |
| 25 | + def received(msg : PG::Replication::Type) |
| 26 | + end |
| 27 | + |
| 28 | + def received(msg : PG::Replication::Insert) |
| 29 | + pp insert: msg |
| 30 | + end |
| 31 | + |
| 32 | + def received(msg : PG::Replication::Update) |
| 33 | + pp update: msg |
| 34 | + end |
| 35 | + |
| 36 | + def received(msg : PG::Replication::Delete) |
| 37 | + pp delete: msg |
| 38 | + end |
| 39 | + |
| 40 | + def received(msg : PG::Replication::Truncate) |
| 41 | + end |
| 42 | + |
| 43 | + def received(msg : PG::Replication::StreamStart) |
| 44 | + end |
| 45 | + |
| 46 | + def received(msg : PG::Replication::StreamStop) |
| 47 | + end |
| 48 | + |
| 49 | + def received(msg : PG::Replication::StreamCommit) |
| 50 | + end |
| 51 | + |
| 52 | + def received(msg : PG::Replication::StreamAbort) |
| 53 | + end |
| 54 | + |
| 55 | + def received(msg : PG::Replication::BeginPrepare) |
| 56 | + end |
| 57 | + |
| 58 | + def received(msg : PG::Replication::Prepare) |
| 59 | + end |
| 60 | + |
| 61 | + def received(msg : PG::Replication::CommitPrepared) |
| 62 | + end |
| 63 | + |
| 64 | + def received(msg : PG::Replication::RollbackPrepared) |
| 65 | + end |
| 66 | + |
| 67 | + def received(msg : PG::Replication::StreamPrepare) |
| 68 | + end |
| 69 | + |
| 70 | + def received(msg : PG::Replication::TupleData) |
| 71 | + end |
| 72 | +end |
| 73 | + |
| 74 | +describe PG::Replication do |
| 75 | + it "consumes the WAL" do |
| 76 | + publication_name = "test_publication_#{Random::Secure.hex}" |
| 77 | + slot_name = "test_slot_#{Random::Secure.hex}" |
| 78 | + table_name = "test_table_#{Random::Secure.hex}" |
| 79 | + handler = TestMessageHandler.new |
| 80 | + PG_DB.exec "CREATE PUBLICATION #{publication_name} FOR ALL TABLES" |
| 81 | + PG_DB.exec "SELECT pg_create_logical_replication_slot($1, 'pgoutput')", slot_name |
| 82 | + subscriber = PG.connect_replication(DB_URL, handler: handler, publication_name: publication_name, slot_name: slot_name) |
| 83 | + sleep 100.milliseconds |
| 84 | + PG_DB.exec "DROP TABLE IF EXISTS #{table_name}" |
| 85 | + PG_DB.exec <<-SQL |
| 86 | + CREATE TABLE #{table_name}( |
| 87 | + id UUID PRIMARY KEY DEFAULT gen_random_uuid(), |
| 88 | + string TEXT, |
| 89 | + number INT8 |
| 90 | + ) |
| 91 | + SQL |
| 92 | + id = PG_DB.query_one <<-SQL, as: UUID |
| 93 | + INSERT INTO #{table_name} (string, number) |
| 94 | + VALUES ('foo', 1) |
| 95 | + RETURNING id |
| 96 | + SQL |
| 97 | + |
| 98 | + PG_DB.exec "UPDATE #{table_name} SET string = 'bar' WHERE id = $1", id |
| 99 | + PG_DB.exec "UPDATE #{table_name} SET number = 2 WHERE id = $1", id |
| 100 | + |
| 101 | + sleep 100.milliseconds |
| 102 | + |
| 103 | + pp handler |
| 104 | + ensure |
| 105 | + subscriber.try &.close |
| 106 | + PG_DB.exec "SELECT pg_drop_replication_slot($1)", slot_name |
| 107 | + PG_DB.exec "DROP PUBLICATION IF EXISTS #{publication_name}" |
| 108 | + PG_DB.exec "DROP TABLE IF EXISTS #{table_name}" |
| 109 | + end |
| 110 | +end |
0 commit comments