|
25 | 25 | require 'sequel_postgresql_triggers'
|
26 | 26 | else
|
27 | 27 | puts "Running specs with extension"
|
28 |
| - DB.extension :pg_triggers |
| 28 | + DB.extension :pg_triggers |
29 | 29 | end
|
30 | 30 | DB.extension :pg_array
|
31 | 31 |
|
|
59 | 59 |
|
60 | 60 | DB[:entries].insert(:id=>2, :account_id=>1)
|
61 | 61 | DB[:accounts].order(:id).select_map(:num_entries).must_equal [2, 0]
|
62 |
| - |
| 62 | + |
63 | 63 | DB[:entries].insert(:id=>3, :account_id=>nil)
|
64 | 64 | DB[:accounts].order(:id).select_map(:num_entries).must_equal [2, 0]
|
65 |
| - |
| 65 | + |
66 | 66 | DB[:entries].where(:id=>3).update(:account_id=>2)
|
67 | 67 | DB[:accounts].order(:id).select_map(:num_entries).must_equal [2, 1]
|
68 |
| - |
| 68 | + |
69 | 69 | DB[:entries].where(:id=>2).update(:account_id=>2)
|
70 | 70 | DB[:accounts].order(:id).select_map(:num_entries).must_equal [1, 2]
|
71 |
| - |
| 71 | + |
72 | 72 | DB[:entries].where(:id=>2).update(:account_id=>nil)
|
73 | 73 | DB[:accounts].order(:id).select_map(:num_entries).must_equal [1, 1]
|
74 |
| - |
| 74 | + |
75 | 75 | DB[:entries].where(:id=>2).update(:id=>4)
|
76 | 76 | DB[:accounts].order(:id).select_map(:num_entries).must_equal [1, 1]
|
77 |
| - |
| 77 | + |
78 | 78 | DB[:entries].where(:id=>4).update(:account_id=>2)
|
79 | 79 | DB[:accounts].order(:id).select_map(:num_entries).must_equal [1, 2]
|
80 |
| - |
| 80 | + |
81 | 81 | DB[:entries].where(:id=>4).update(:account_id=>nil)
|
82 | 82 | DB[:accounts].order(:id).select_map(:num_entries).must_equal [1, 1]
|
83 |
| - |
| 83 | + |
84 | 84 | DB[:entries].filter(:id=>4).delete
|
85 | 85 | DB[:accounts].order(:id).select_map(:num_entries).must_equal [1, 1]
|
86 |
| - |
| 86 | + |
87 | 87 | DB[:entries].delete
|
88 | 88 | DB[:accounts].order(:id).select_map(:num_entries).must_equal [0, 0]
|
89 | 89 | end
|
|
195 | 195 |
|
196 | 196 | DB[:entries].insert(:id=>2, :account_id=>1, :amount=>200)
|
197 | 197 | DB[:accounts].order(:id).select_map(:balance).must_equal [300, 0]
|
198 |
| - |
| 198 | + |
199 | 199 | DB[:entries].insert(:id=>3, :account_id=>nil, :amount=>500)
|
200 | 200 | DB[:accounts].order(:id).select_map(:balance).must_equal [300, 0]
|
201 |
| - |
| 201 | + |
202 | 202 | DB[:entries].where(:id=>3).update(:account_id=>2)
|
203 | 203 | DB[:accounts].order(:id).select_map(:balance).must_equal [300, 500]
|
204 |
| - |
| 204 | + |
205 | 205 | DB[:entries].exclude(:id=>2).update(:amount=>Sequel.*(:amount, 2))
|
206 | 206 | DB[:accounts].order(:id).select_map(:balance).must_equal [400, 1000]
|
207 |
| - |
| 207 | + |
208 | 208 | DB[:entries].where(:id=>2).update(:account_id=>2)
|
209 | 209 | DB[:accounts].order(:id).select_map(:balance).must_equal [200, 1200]
|
210 |
| - |
| 210 | + |
211 | 211 | DB[:entries].where(:id=>2).update(:account_id=>nil)
|
212 | 212 | DB[:accounts].order(:id).select_map(:balance).must_equal [200, 1000]
|
213 |
| - |
| 213 | + |
214 | 214 | DB[:entries].where(:id=>2).update(:id=>4)
|
215 | 215 | DB[:accounts].order(:id).select_map(:balance).must_equal [200, 1000]
|
216 |
| - |
| 216 | + |
217 | 217 | DB[:entries].where(:id=>4).update(:account_id=>2)
|
218 | 218 | DB[:accounts].order(:id).select_map(:balance).must_equal [200, 1200]
|
219 |
| - |
| 219 | + |
220 | 220 | DB[:entries].where(:id=>4).update(:account_id=>nil)
|
221 | 221 | DB[:accounts].order(:id).select_map(:balance).must_equal [200, 1000]
|
222 |
| - |
| 222 | + |
223 | 223 | DB[:entries].filter(:id=>4).delete
|
224 | 224 | DB[:accounts].order(:id).select_map(:balance).must_equal [200, 1000]
|
225 |
| - |
| 225 | + |
226 | 226 | DB[:entries].delete
|
227 | 227 | DB[:accounts].order(:id).select_map(:balance).must_equal [0, 0]
|
228 | 228 | end
|
|
748 | 748 | end
|
749 | 749 | end
|
750 | 750 |
|
751 |
| - |
752 | 751 | describe "PostgreSQL JSON Audit Logging" do
|
753 | 752 | before do
|
754 | 753 | DB.extension :pg_json
|
|
788 | 787 | h.must_equal(:schema=>"public", :table=>"accounts", :action=>"DELETE", :prior=>{"a"=>3, "id"=>2})
|
789 | 788 | end
|
790 | 789 | end if DB.server_version >= 90400
|
| 790 | + |
| 791 | +describe "Basic PostgreSQL Transactional Outbox" do |
| 792 | + before do |
| 793 | + DB.extension :pg_json |
| 794 | + DB.create_table(:accounts){integer :id; String :s} |
| 795 | + function_name = DB.pgt_outbox_setup(:accounts, :function_name=>:spgt_outbox_events) |
| 796 | + DB.pgt_outbox_events(:accounts, function_name) |
| 797 | + @logs = DB[:accounts_outbox].reverse(:created) |
| 798 | + end |
| 799 | + |
| 800 | + after do |
| 801 | + DB.drop_table(:accounts, :accounts_outbox) |
| 802 | + DB.drop_function(:spgt_outbox_events) |
| 803 | + end |
| 804 | + |
| 805 | + it "should store outbox events for writes on main table" do |
| 806 | + @logs.first.must_be_nil |
| 807 | + |
| 808 | + ds = DB[:accounts] |
| 809 | + ds.insert(id: 1, s: 'string') |
| 810 | + ds.all.must_equal [{id: 1, s: 'string'}] |
| 811 | + h = @logs.first |
| 812 | + h.delete(:created).to_i.must_be_close_to(10, DB.get(Sequel::CURRENT_TIMESTAMP).to_i) |
| 813 | + h.delete(:updated).to_i.must_be_close_to(10, DB.get(Sequel::CURRENT_TIMESTAMP).to_i) |
| 814 | + h.must_equal(id: 1, attempts: 0, attempted: nil, completed: nil, event_type: "accounts_created", last_error: nil, data_before: nil, data_after: {"s" => "string", "id" => 1}, metadata: nil) |
| 815 | + |
| 816 | + ds.where(id: 1).update(s: 'string2') |
| 817 | + ds.all.must_equal [{id: 1, s: 'string2'}] |
| 818 | + h = @logs.first |
| 819 | + h.delete(:created).to_i.must_be_close_to(10, DB.get(Sequel::CURRENT_TIMESTAMP).to_i) |
| 820 | + h.delete(:updated).to_i.must_be_close_to(10, DB.get(Sequel::CURRENT_TIMESTAMP).to_i) |
| 821 | + h.must_equal(id: 2, attempts: 0, attempted: nil, completed: nil, event_type: "accounts_updated", last_error: nil, data_before: {"s" => "string", "id" => 1}, data_after: {"s" => "string2", "id" => 1}, metadata: nil) |
| 822 | + |
| 823 | + ds.delete |
| 824 | + ds.all.must_equal [] |
| 825 | + h = @logs.first |
| 826 | + h.delete(:created).to_i.must_be_close_to(10, DB.get(Sequel::CURRENT_TIMESTAMP).to_i) |
| 827 | + h.delete(:updated).to_i.must_be_close_to(10, DB.get(Sequel::CURRENT_TIMESTAMP).to_i) |
| 828 | + h.must_equal(id: 3, attempts: 0, attempted: nil, completed: nil, event_type: "accounts_deleted", last_error: nil, data_before: {"s" => "string2", "id" => 1}, data_after: nil, metadata: nil) |
| 829 | + end |
| 830 | +end if DB.server_version >= 90400 |
| 831 | + |
| 832 | +describe "PostgreSQL Transactional Outbox With UUID Pkey" do |
| 833 | + before do |
| 834 | + DB.extension :pg_json |
| 835 | + DB.create_table(:accounts){integer :id; String :s} |
| 836 | + function_name = DB.pgt_outbox_setup(:accounts, uuid_primary_key: true, function_name: :spgt_outbox_events) |
| 837 | + DB.pgt_outbox_events(:accounts, function_name) |
| 838 | + @logs = DB[:accounts_outbox].reverse(:created) |
| 839 | + end |
| 840 | + |
| 841 | + after do |
| 842 | + DB.drop_table(:accounts, :accounts_outbox) |
| 843 | + DB.drop_function(:spgt_outbox_events) |
| 844 | + end |
| 845 | + |
| 846 | + it "should store outbox events for writes on main table" do |
| 847 | + @logs.first.must_be_nil |
| 848 | + |
| 849 | + ds = DB[:accounts] |
| 850 | + ds.insert(id: 1, s: 'string') |
| 851 | + ds.all.must_equal [{id: 1, s: 'string'}] |
| 852 | + h = @logs.first |
| 853 | + h.delete(:created).to_i.must_be_close_to(10, DB.get(Sequel::CURRENT_TIMESTAMP).to_i) |
| 854 | + h.delete(:updated).to_i.must_be_close_to(10, DB.get(Sequel::CURRENT_TIMESTAMP).to_i) |
| 855 | + id = h.delete(:id) |
| 856 | + id.must_match(/\A\h{8}-\h{4}-\h{4}-\h{4}-\h{12}\z/) |
| 857 | + h.must_equal(attempts: 0, attempted: nil, completed: nil, event_type: "accounts_created", last_error: nil, data_before: nil, data_after: {"s" => "string", "id" => 1}, metadata: nil) |
| 858 | + |
| 859 | + ds.where(id: 1).update(s: 'string2') |
| 860 | + ds.all.must_equal [{id: 1, s: 'string2'}] |
| 861 | + h = @logs.first |
| 862 | + h.delete(:created).to_i.must_be_close_to(10, DB.get(Sequel::CURRENT_TIMESTAMP).to_i) |
| 863 | + h.delete(:updated).to_i.must_be_close_to(10, DB.get(Sequel::CURRENT_TIMESTAMP).to_i) |
| 864 | + id = h.delete(:id) |
| 865 | + id.must_match(/\A\h{8}-\h{4}-\h{4}-\h{4}-\h{12}\z/) |
| 866 | + h.must_equal(attempts: 0, attempted: nil, completed: nil, event_type: "accounts_updated", last_error: nil, data_before: {"s" => "string", "id" => 1}, data_after: {"s" => "string2", "id" => 1}, metadata: nil) |
| 867 | + |
| 868 | + ds.delete |
| 869 | + ds.all.must_equal [] |
| 870 | + h = @logs.first |
| 871 | + h.delete(:created).to_i.must_be_close_to(10, DB.get(Sequel::CURRENT_TIMESTAMP).to_i) |
| 872 | + h.delete(:updated).to_i.must_be_close_to(10, DB.get(Sequel::CURRENT_TIMESTAMP).to_i) |
| 873 | + id = h.delete(:id) |
| 874 | + id.must_match(/\A\h{8}-\h{4}-\h{4}-\h{4}-\h{12}\z/) |
| 875 | + h.must_equal(attempts: 0, attempted: nil, completed: nil, event_type: "accounts_deleted", last_error: nil, data_before: {"s" => "string2", "id" => 1}, data_after: nil, metadata: nil) |
| 876 | + end |
| 877 | +end if DB.server_version >= 90400 |
| 878 | + |
| 879 | +describe "PostgreSQL Transactional Outbox With UUID Pkey" do |
| 880 | + before do |
| 881 | + DB.extension :pg_json |
| 882 | + DB.create_table(:accounts){integer :id; String :s} |
| 883 | + function_name = DB.pgt_outbox_setup(:accounts, uuid_primary_key: true, boolean_completed_column: true, function_name: :spgt_outbox_events) |
| 884 | + DB.pgt_outbox_events(:accounts, function_name) |
| 885 | + @logs = DB[:accounts_outbox].reverse(:created) |
| 886 | + end |
| 887 | + |
| 888 | + after do |
| 889 | + DB.drop_table(:accounts, :accounts_outbox) |
| 890 | + DB.drop_function(:spgt_outbox_events) |
| 891 | + end |
| 892 | + |
| 893 | + it "should store outbox events for writes on main table" do |
| 894 | + @logs.first.must_be_nil |
| 895 | + |
| 896 | + ds = DB[:accounts] |
| 897 | + ds.insert(id: 1, s: 'string') |
| 898 | + ds.all.must_equal [{id: 1, s: 'string'}] |
| 899 | + h = @logs.first |
| 900 | + h.delete(:created).to_i.must_be_close_to(10, DB.get(Sequel::CURRENT_TIMESTAMP).to_i) |
| 901 | + h.delete(:updated).to_i.must_be_close_to(10, DB.get(Sequel::CURRENT_TIMESTAMP).to_i) |
| 902 | + id = h.delete(:id) |
| 903 | + id.must_match(/\A\h{8}-\h{4}-\h{4}-\h{4}-\h{12}\z/) |
| 904 | + h.must_equal(attempts: 0, attempted: nil, completed: false, event_type: "accounts_created", last_error: nil, data_before: nil, data_after: {"s" => "string", "id" => 1}, metadata: nil) |
| 905 | + |
| 906 | + ds.where(id: 1).update(s: 'string2') |
| 907 | + ds.all.must_equal [{id: 1, s: 'string2'}] |
| 908 | + h = @logs.first |
| 909 | + h.delete(:created).to_i.must_be_close_to(10, DB.get(Sequel::CURRENT_TIMESTAMP).to_i) |
| 910 | + h.delete(:updated).to_i.must_be_close_to(10, DB.get(Sequel::CURRENT_TIMESTAMP).to_i) |
| 911 | + id = h.delete(:id) |
| 912 | + id.must_match(/\A\h{8}-\h{4}-\h{4}-\h{4}-\h{12}\z/) |
| 913 | + h.must_equal(attempts: 0, attempted: nil, completed: false, event_type: "accounts_updated", last_error: nil, data_before: {"s" => "string", "id" => 1}, data_after: {"s" => "string2", "id" => 1}, metadata: nil) |
| 914 | + |
| 915 | + ds.delete |
| 916 | + ds.all.must_equal [] |
| 917 | + h = @logs.first |
| 918 | + h.delete(:created).to_i.must_be_close_to(10, DB.get(Sequel::CURRENT_TIMESTAMP).to_i) |
| 919 | + h.delete(:updated).to_i.must_be_close_to(10, DB.get(Sequel::CURRENT_TIMESTAMP).to_i) |
| 920 | + id = h.delete(:id) |
| 921 | + id.must_match(/\A\h{8}-\h{4}-\h{4}-\h{4}-\h{12}\z/) |
| 922 | + h.must_equal(attempts: 0, attempted: nil, completed: false, event_type: "accounts_deleted", last_error: nil, data_before: {"s" => "string2", "id" => 1}, data_after: nil, metadata: nil) |
| 923 | + end |
| 924 | +end if DB.server_version >= 90400 |
0 commit comments