forked from yuki-kimoto/gitprep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup_database
executable file
·324 lines (291 loc) · 7.8 KB
/
setup_database
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
#!/usr/bin/env perl
use FindBin;
use lib "$FindBin::Bin/lib";
use lib "$FindBin::Bin/extlib/lib/perl5";
use DBIx::Custom;
my $database_file = shift // "$FindBin::Bin/data/gitprep.db";
# DBI
my %dbi_args = (
dsn => "dbi:SQLite:database=$database_file",
connector => 1,
option => {sqlite_unicode => 1, sqlite_use_immediate_transaction => 1}
);
my $dbi = DBIx::Custom->connect(%dbi_args);
# Database state
my $database_state;
if (!-f $database_file) {
$database_state = 'empty';
}
else {
# If project.user_id exists, that database is version 1
eval { $dbi->select('user_id', table => 'project', append => 'limit 0, 1') };
if ($@) {
$database_state = 'current';
}
else {
$database_state = 'v1';
}
}
# Need upgrade
if ($database_state eq 'v1') {
die "Can't setup database. you maybe need upgrade database";
}
# Create database
else {
# Create user table
eval {
my $sql = <<"EOS";
create table user (
row_id integer primary key autoincrement,
id not null unique default '',
email not null unique default ''
);
EOS
$dbi->execute($sql);
};
# Create user columns
my $user_columns = [
"admin integer not null default 0",
"password not null default ''",
"salt not null default ''",
"name not null default ''"
];
for my $column (@$user_columns) {
eval { $dbi->execute("alter table user add column $column") };
}
# Check user table
eval { $dbi->select([qw/row_id id admin password salt email name/], table => 'user') };
if ($@) {
my $error = "Can't create user table properly: $@";
die $error;
}
# Create project table
eval {
my $sql = <<"EOS";
create table project (
row_id integer primary key autoincrement,
user integer not null default 0,
id not null,
unique(user, id)
);
EOS
$dbi->execute($sql);
};
# Create Project columns
my $project_columns = [
"default_branch not null default 'master'",
"original_project integer not null default 0",
"private integer not null default 0",
"ignore_space_change integer not null default 0",
"guess_encoding integer not null default ''",
"website_url not null default ''"
];
for my $column (@$project_columns) {
eval { $dbi->execute("alter table project add column $column") };
}
# Check project table
eval {
$dbi->select(
[qw/row_id user id default_branch original_project private ignore_space_change guess_encoding website_url/],
table => 'project'
);
};
if ($@) {
my $error = "Can't create project table properly: $@";
die $error;
}
# Create ssh_public_key table
eval {
my $sql = <<"EOS";
create table ssh_public_key (
row_id integer primary key autoincrement,
key not null unique default ''
);
EOS
$dbi->execute($sql);
};
# Create ssh_public_key columns
my $ssh_public_key_columns = [
"user integer not null default 0",
"title not null default ''"
];
for my $column (@$ssh_public_key_columns) {
eval { $dbi->execute("alter table ssh_public_key add column $column") };
}
# Check ssh_public_key table
eval { $dbi->select([qw/row_id user key title/], table => 'ssh_public_key') };
if ($@) {
my $error = "Can't create ssh_public_key table properly: $@";
die $error;
}
# Create collaboration table
eval {
my $sql = <<"EOS";
create table collaboration (
row_id integer primary key autoincrement,
project integer not null default 0,
user integer not null default 0,
unique(project, user)
);
EOS
$dbi->execute($sql);
};
# Check collaboration table
eval { $dbi->select([qw/row_id project user/], table => 'collaboration') };
if ($@) {
my $error = "Can't create collaboration table properly: $@";
die $error;
}
# Create issue table
eval {
my $sql = <<"EOS";
create table issue (
row_id integer primary key autoincrement,
project integer not null default 0,
number integer not null default 0,
unique(project, number)
);
EOS
$dbi->execute($sql);
};
# Create issue columns
my @issue_columns = (
"title not null default ''",
"open integer not null default 0",
"open_time integer not null default 0",
"open_user integer not null default 0",
"pull_request integer not null default 0",
);
for my $column (@issue_columns) {
eval { $dbi->execute("alter table issue add column $column") };
}
# Check issue table
eval { $dbi->select([qw/row_id title open open_time open_user pull_request project/], table => 'issue') };
if ($@) {
my $error = "Can't create issue table properly: $@";
die $error;
}
# Create issue_message table
eval {
my $sql = <<"EOS";
create table issue_message (
row_id integer primary key autoincrement,
issue integer not null default 0,
number integer not null default 0,
unique(issue, number)
);
EOS
$dbi->execute($sql);
};
# Create issue_message columns
my @issue_message_columns = (
"message not null default ''",
"create_time integer not null default 0",
"update_time integer not null default 0",
"user integer not null default 0"
);
for my $column (@issue_message_columns) {
eval { $dbi->execute("alter table issue_message add column $column") };
}
# Check issue_message table
eval { $dbi->select([qw/row_id issue number message create_time update_time user/], table => 'issue_message') };
if ($@) {
my $error = "Can't create issue_message table properly: $@";
die $error;
}
# Create pull_request table
eval {
my $sql = <<"EOS";
create table pull_request (
row_id integer primary key autoincrement,
base_project integer not null default 0,
base_branch not null default '',
target_project integer not null default 0,
target_branch not null default '',
unique(base_project, base_branch, target_project, target_branch)
);
EOS
$dbi->execute($sql);
};
# Create pull_request columns
my @pull_request_columns = ();
for my $column (@pull_request_columns) {
eval { $dbi->execute("alter table pull_request add column $column") };
}
# Check pull_request table
eval { $dbi->select([qw/row_id base_project base_branch target_project target_branch/], table => 'pull_request') };
if ($@) {
my $error = "Can't create pull_request table properly: $@";
die $error;
}
# Create label table
eval {
my $sql = <<"EOS";
create table label (
row_id integer primary key autoincrement,
project integer not null default 0,
id varchar(100) not null default '',
unique(project, id)
);
EOS
$dbi->execute($sql);
};
# Create label columns
my @label_columns = (
"color not null default ''"
);
for my $column (@label_columns) {
eval { $dbi->execute("alter table label add column $column") };
}
# Check label table
eval { $dbi->select([qw/row_id project id color/], table => 'label') };
if ($@) {
my $error = "Can't create label table properly: $@";
die $error;
}
# Create issue_label table
eval {
my $sql = <<"EOS";
create table issue_label (
row_id integer primary key autoincrement,
issue integer not null default 0,
label integer not null default 0,
unique(issue, label)
);
EOS
$dbi->execute($sql);
};
# Create issue_label columns
my @issue_label_columns = (
);
for my $column (@issue_label_columns) {
eval { $dbi->execute("alter table issue_label add column $column") };
}
# Check issue_label table
eval { $dbi->select([qw/row_id issue label/], table => 'issue_label') };
if ($@) {
my $error = "Can't create issue_label table properly: $@";
die $error;
}
# Create wiki table
eval {
my $sql = <<"EOS";
create table wiki (
row_id integer primary key autoincrement,
project integer not null default 0,
unique(project)
);
EOS
$dbi->execute($sql);
};
# Check wiki table
eval {
$dbi->select(
table => 'wiki'
);
};
if ($@) {
my $error = "Can't create wiki table properly: $@";
die $error;
}
}