blob: c57d388fe5d744e6914847a8cadb34cac08a9adf (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe AfterCommitQueue do
describe '#run_after_commit' do
it 'runs after record is saved' do
called = false
test_proc = proc { called = true }
project = build(:project)
project.run_after_commit(&test_proc)
expect(called).to be false
# save! is run in its own transaction
project.save!
expect(called).to be true
end
it 'runs after transaction is committed' do
called = false
test_proc = proc { called = true }
project = build(:project)
Project.transaction do
project.run_after_commit(&test_proc)
project.save!
expect(called).to be false
end
expect(called).to be true
end
end
describe '#run_after_commit_or_now' do
it 'runs immediately if not within a transction' do
called = false
test_proc = proc { called = true }
project = build(:project)
project.run_after_commit_or_now(&test_proc)
expect(called).to be true
end
it 'runs after transaction has completed' do
called = false
test_proc = proc { called = true }
project = build(:project)
Project.transaction do
# Add this record to the current transaction so that after commit hooks
# are called
Project.connection.add_transaction_record(project)
project.run_after_commit_or_now(&test_proc)
project.save!
expect(called).to be false
end
expect(called).to be true
end
context 'multiple databases - Ci::ApplicationRecord models' do
before do
skip_if_multiple_databases_not_setup(:ci)
table_sql = <<~SQL
CREATE TABLE _test_gitlab_ci_after_commit_queue (
id serial NOT NULL PRIMARY KEY);
SQL
::Ci::ApplicationRecord.connection.execute(table_sql)
end
let(:ci_klass) do
Class.new(Ci::ApplicationRecord) do
self.table_name = '_test_gitlab_ci_after_commit_queue'
include AfterCommitQueue
def self.name
'TestCiAfterCommitQueue'
end
end
end
let(:ci_record) { ci_klass.new }
it 'runs immediately if not within a transaction' do
called = false
test_proc = proc { called = true }
ci_record.run_after_commit_or_now(&test_proc)
expect(called).to be true
end
it 'runs after transaction has completed' do
called = false
test_proc = proc { called = true }
Ci::ApplicationRecord.transaction do
# Add this record to the current transaction so that after commit hooks
# are called
Ci::ApplicationRecord.connection.add_transaction_record(ci_record)
ci_record.run_after_commit_or_now(&test_proc)
ci_record.save!
expect(called).to be false
end
expect(called).to be true
end
end
end
end
|