-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path7_generate_order_lines.sql
150 lines (127 loc) · 3.7 KB
/
7_generate_order_lines.sql
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
with reservation_to_update as (
select *, floor(random()*(300-5)+5) as count from reservation where state = 'FINISHED'
)
insert into order_line(public_id,membership_id, reservation_id, product_id, count, total, invoice_id, created)
select
uuid_generate_v4(),
rtu.membership_id,
rtu.id,
1 as product_id,
rtu.count as count,
5 * rtu.count,
null,
rtu.planned_end
from reservation_to_update rtu;
with reservation_to_update as (
select * from reservation where state = 'FINISHED'
)
insert into order_line(public_id, membership_id, reservation_id, product_id, count, total, invoice_id, created)
select
uuid_generate_v4(),
rtu.membership_id,
rtu.id,
2 as product_id,
1,
10.00,
null,
rtu.planned_end
from reservation_to_update rtu;
with reservation_to_update as (
select * from reservation where state = 'FINISHED' order by random() limit 200000
)
insert into order_line(public_id, membership_id, reservation_id, product_id, count, total, invoice_id, created)
select
uuid_generate_v4(),
rtu.membership_id,
rtu.id,
3 as product_id,
1,
100.00,
null,
rtu.planned_end
from reservation_to_update rtu;
with reservation_to_update as (
select *, floor(random()*(5-1)+1) as count from reservation where state = 'FINISHED' order by random() limit 800000
)
insert into order_line(public_id, membership_id, reservation_id, product_id, count, total, invoice_id, created)
select
uuid_generate_v4(),
rtu.membership_id,
rtu.id,
5 as product_id,
rtu.count,
50 * rtu.count,
null,
rtu.planned_end
from reservation_to_update rtu;
with reservation_to_update as (
select * from reservation where state = 'FINISHED' order by random() limit 200000
)
insert into order_line(public_id, membership_id, reservation_id, product_id, count, total, invoice_id, created)
select
uuid_generate_v4(),
rtu.membership_id,
rtu.id,
6 as product_id,
1,
100.00,
null,
rtu.planned_end
from reservation_to_update rtu;
with reservation_to_update as (
select * from reservation where state = 'FINISHED' order by random() limit 300000
)
insert into order_line(public_id, membership_id, reservation_id, product_id, count, total, invoice_id, created)
select
uuid_generate_v4(),
rtu.membership_id,
rtu.id,
7 as product_id,
1,
500.00,
null,
rtu.planned_end
from reservation_to_update rtu;
with reservation_to_update as (
select * from reservation where state = 'FINISHED' order by random() limit 400000
)
insert into order_line(public_id, membership_id, reservation_id, product_id, count, total, invoice_id, created)
select
uuid_generate_v4(),
rtu.membership_id,
rtu.id,
8 as product_id,
1,
100.00,
null,
rtu.planned_end
from reservation_to_update rtu;
with reservation_to_update as (
select * from reservation where state = 'FINISHED' order by random() limit 300000
)
insert into order_line(public_id, membership_id, reservation_id, product_id, count, total, invoice_id, created)
select
uuid_generate_v4(),
rtu.membership_id,
rtu.id,
9 as product_id,
1,
200.00,
null,
rtu.planned_end
from reservation_to_update rtu;
insert into order_line(public_id, membership_id, reservation_id, product_id, count, total, invoice_id, created)
select
uuid_generate_v4(),
m.id,
null,
4 as product_id,
1,
500.00,
null,
m.created_ts + ((a.period * 6)::varchar || ' months')::interval
from membership m
cross join lateral (
select generate_series(1, extract(year from age(now(), m.created_ts)) * 2 +
extract(month from age(now(), m.created_ts)) / 6::integer) as period
) a;