This repository has been archived by the owner on May 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathmysqlmigrate.js
411 lines (402 loc) · 12.6 KB
/
mysqlmigrate.js
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
import migrate from "https://raw.githubusercontent.com/txthinking/denolib/master/migrate.js";
import { encode as hexencode } from "https://deno.land/std@0.130.0/encoding/hex.ts";
import { randomBytes } from "https://deno.land/std@0.130.0/node/crypto.ts";
import { now } from "https://raw.githubusercontent.com/txthinking/denolib/master/f.js";
export default async function (db) {
var mg = await migrate(db);
await mg(
"create setting table",
`
CREATE TABLE setting (
id int(10) unsigned NOT NULL AUTO_INCREMENT,
k varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL default '',
v varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL default '',
PRIMARY KEY (id),
UNIQUE KEY (k)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
`
);
await mg(
"init key in setting table",
`
insert into setting values(null, 'key', '${new TextDecoder().decode(hexencode(randomBytes(16)))}')
`
);
await mg(
"init username in setting table",
`
insert into setting values(null, 'username', 'brook')
`
);
await mg(
"init password in setting table",
`
insert into setting values(null, 'password', 'brook')
`
);
await mg(
"init simulate_payment in setting table",
`
insert into setting values(null, 'simulate_payment', 'true')
`
);
await mg(
"init start_port in setting table",
`
insert into setting values(null, 'start_port', '10000')
`
);
await mg(
"init site_name in setting table",
`
insert into setting values(null, 'site_name', 'Your Site Name')
`
);
await mg(
"init last_port in setting table",
`
insert into setting values(null, 'last_port', '0')
`
);
await mg(
"init site_telegram in setting table",
`
insert into setting values(null, 'site_telegram', 'https://t.me/yourgroup')
`
);
await mg(
"init site_domain in setting table",
`
insert into setting values(null, 'site_domain', '')
`
);
await mg(
"init domainlist in setting table",
`
insert into setting values(null, 'domainlist', 'a.com\nb.com')
`
);
await mg(
"init cidr4list in setting table",
`
insert into setting values(null, 'cidr4list', '127.0.0.0/8\n10.0.0.0/8\n169.254.0.0/16\n172.16.0.0/12\n192.168.0.0/16\n224.0.0.0/4')
`
);
await mg(
"init cidr6list in setting table",
`
insert into setting values(null, 'cidr6list', '::1/128\n2400:da00::/32')
`
);
await mg(
"create vip table",
`
CREATE TABLE vip (
id int(10) unsigned NOT NULL AUTO_INCREMENT,
name varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL default '',
level int(10) NOT NULL default 0,
isdeleted int(1) NOT NULL default 0,
PRIMARY KEY (id),
UNIQUE KEY (level)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
`
);
await mg(
"init vip0 in vip table",
`
insert into vip values(1, 'Free(免费)', 0, 1)
`
);
await mg(
"init vip1 in vip table",
`
insert into vip values(2, 'Ordinary VIP(普通VIP)', 1, 1)
`
);
await mg(
"init vip2 in vip table",
`
insert into vip values(3, 'Premium VIP(高级VIP)', 2, 1)
`
);
await mg(
"create product table",
`
CREATE TABLE product (
id int(10) unsigned NOT NULL AUTO_INCREMENT,
vip_id int(10) NOT NULL default 0,
name varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL default '',
price_yuan int(10) NOT NULL default 0,
price_usd int(10) NOT NULL default 0,
duration int(10) NOT NULL default 0,
isdeleted int(1) NOT NULL default 0,
PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
`
);
await mg(
"init p11 in product table",
`
insert into product values(null, 2, 'Ordinary VIP 1 month(普通VIP1个月)', 990, 990, ${30 * 24 * 60 * 60}, 1)
`
);
await mg(
"init p12 in product table",
`
insert into product values(null, 2, 'Ordinary VIP 6 months(普通VIP6个月)', 5990, 5990, ${6 * 30 * 24 * 60 * 60}, 1)
`
);
await mg(
"init p13 in product table",
`
insert into product values(null, 2, 'Ordinary VIP 12 months(普通VIP12个月)', 11990, 11990, ${12 * 30 * 24 * 60 * 60}, 1)
`
);
await mg(
"init p21 in product table",
`
insert into product values(null, 3, 'Premium VIP 1 month(高级VIP1个月)', ${990 * 2},${990 * 2}, ${30 * 24 * 60 * 60}, 2)
`
);
await mg(
"init p22 in product table",
`
insert into product values(null, 3, 'Premium VIP 6 months(高级VIP6个月)', ${5990 * 2}, ${5990 * 2}, ${6 * 30 * 24 * 60 * 60}, 2)
`
);
await mg(
"init p23 in product table",
`
insert into product values(null, 3, 'Premium VIP 12 months(高级VIP12个月)', ${11990 * 2}, ${11990 * 2}, ${12 * 30 * 24 * 60 * 60}, 2)
`
);
await mg(
"create instance table",
`
CREATE TABLE instance (
id int(10) unsigned NOT NULL AUTO_INCREMENT,
vip_id int(10) NOT NULL default 0,
name varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL default '',
address varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL default '',
user varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL default '',
password varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL default '',
sshkey text,
kind int(10) NOT NULL default 0, -- 1 multi-port/multi-user 2 single port/multi-user
enable_brook_server int(10) NOT NULL default 0,
enable_brook_wsserver int(10) NOT NULL default 0,
enable_brook_wssserver int(10) NOT NULL default 0,
enable_brook_wssserver_withoutbrookprotocol int(10) NOT NULL default 0,
wssserver_kind int(10) NOT NULL default 0,
single_port int(10) NOT NULL default 0,
single_iswithoutbrookprotocol int(10) NOT NULL default 0,
single_kind int(10) NOT NULL default 0,
domain varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL default '',
cert text,
certkey text,
ca text,
isdeleted int(10) NOT NULL default 0, -- 1 no 2 yes
PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
`
);
await mg(
"create user table",
`
CREATE TABLE user (
id int(10) unsigned NOT NULL AUTO_INCREMENT,
username varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL default '',
password varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL default '',
port0 int(10) NOT NULL default 0,
port1 int(10) NOT NULL default 0,
port2 int(10) NOT NULL default 0,
port3 int(10) NOT NULL default 0,
transfer int(10) NOT NULL default 0,
PRIMARY KEY (id),
UNIQUE KEY (port0),
UNIQUE KEY (port1),
UNIQUE KEY (port2),
UNIQUE KEY (port3),
UNIQUE KEY (username)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
`
);
await mg(
"create payment table",
`
CREATE TABLE payment (
id int(10) unsigned NOT NULL AUTO_INCREMENT,
user_id int(10) NOT NULL default 0,
product_id int(10) NOT NULL default 0,
paymentid varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL default '',
paymentmethod varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL default '',
status int(10) NOT NULL default 0,
PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
`
);
await mg(
"create user_vip table",
`
CREATE TABLE user_vip (
id int(10) unsigned NOT NULL AUTO_INCREMENT,
user_id int(10) NOT NULL default 0,
vip_id int(10) NOT NULL default 0,
expiration int(10) NOT NULL default 0,
UNIQUE KEY (user_id, vip_id),
PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
`
);
await mg(
"create brook_link table",
`
CREATE TABLE brook_link (
id int(10) unsigned NOT NULL AUTO_INCREMENT,
vip_or_user int(10) NOT NULL default 0,
user_id int(10) NOT NULL default 0,
vip_id int(10) NOT NULL default 0,
brook_link text,
PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
`
);
await mg(
"create unmanaged_instance table",
`
CREATE TABLE unmanaged_instance (
id int(10) unsigned NOT NULL AUTO_INCREMENT,
name varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL default '',
address varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL default '',
user varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL default '',
password varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL default '',
sshkey text,
ports text,
PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
`
);
await mg(
"init txthinking_payments in setting table",
`
insert into setting values(null, 'txthinking_payments', 'false')
`
);
await mg(
"init txthinking_payments_key in setting table",
`
insert into setting values(null, 'txthinking_payments_key', '')
`
);
await mg(
"transfer bigint ",
`
alter table user change transfer transfer bigint not null default 0
`
);
await mg(
"init recaptcha in setting table",
`
insert into setting values(null, 'recaptcha', 'false')
`
);
await mg(
"init recaptcha_site_key in setting table",
`
insert into setting values(null, 'recaptcha_site_key', '')
`
);
await mg(
"init recaptcha_secret_key in setting table",
`
insert into setting values(null, 'recaptcha_secret_key', '')
`
);
await mg(
"init enable_signup in setting table",
`
insert into setting values(null, 'enable_signup', 'false')
`
);
// 1 not baned 2 banned
await mg(
"add baned in user table",
`
alter table user add column baned int(11) not null default 1
`
);
await mg(
"init announcement in setting table",
`
insert into setting values(null, 'announcement', '')
`
);
await mg(
"update setting v to text",
`
alter table setting change v v text
`
);
await mg(
"create task table",
`
CREATE TABLE task (
id int(10) unsigned NOT NULL AUTO_INCREMENT,
name varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL default '',
user_id int(10) NOT NULL default 0,
data text,
isdeleted int(1) NOT NULL default 1,
PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
`
);
await mg(
"add instance_ids in user table",
`
alter table user add column instance_ids text
`
);
await mg(
"init instance_ids in user table",
`
update user set instance_ids='todo' where baned=1
`
);
await mg(
"add user_ids in instance table",
`
alter table instance add column user_ids text
`
);
await mg(
"init user_ids in instance table",
`
update instance set user_ids='todo' where isdeleted=1
`
);
// compatibility
var rows = await db.query("select * from instance where isdeleted=1");
for(var i=0;i<rows.length;i++){
var row = await db.r('instance', rows[i].id);
if(row.user_ids != 'todo'){
continue;
}
var vip = await db.r("vip", row.vip_id);
var ids = [];
if(vip.level == 0){
ids = (await db.query("select * from user where baned=1")).map(v=>v.id);
}
if(vip.level != 0){
ids = (await db.query("select * from user_vip where vip_id=? and expiration>? and user_id in (select id from user where baned=1)", [vip.id, now()])).map(v=>v.user_id);
}
await db.u('instance', {id: row.id, user_ids: ids.join(':')});
}
var rows = await db.query("select * from user where baned=1");
for(var i=0;i<rows.length;i++){
var row = await db.r('user', rows[i].id);
if(row.instance_ids != 'todo'){
continue;
}
var ids = (await db.query("select * from instance where isdeleted=1 and (vip_id in (select vip_id from user_vip where expiration>? and user_id=?) or vip_id in (select id from vip where level=0))", [now(), row.id])).map(v=>v.id);
await db.u('user', {id: row.id, instance_ids: ids.join(':')});
}
}