separete database creating l...
|
1 |
#!/usr/bin/env perl |
2 | ||
3 |
use FindBin; |
|
4 |
use lib "$FindBin::Bin/lib"; |
|
5 |
use lib "$FindBin::Bin/extlib/lib/perl5"; |
|
fix some tests
|
6 |
use DBIx::Custom; |
separete database creating l...
|
7 | |
fix some tests
|
8 |
my $database_file = shift // "$FindBin::Bin/data/gitprep.db"; |
separete database creating l...
|
9 | |
fix some tests
|
10 |
# DBI |
11 |
my %dbi_args = ( |
|
12 |
dsn => "dbi:SQLite:database=$database_file", |
|
13 |
connector => 1, |
|
14 |
option => {sqlite_unicode => 1, sqlite_use_immediate_transaction => 1} |
|
15 |
); |
|
16 |
my $dbi = DBIx::Custom->connect(%dbi_args); |
|
create version 2.0 setup scr...
|
17 | |
fix some tests
|
18 |
# Database state |
19 |
my $database_state; |
|
20 |
if (!-f $database_file) { |
|
21 |
$database_state = 'empty'; |
|
22 |
} |
|
23 |
else { |
|
24 |
# If project.user_id exists, that database is version 1 |
|
25 |
eval { $dbi->select('user_id', table => 'project', append => 'limit 0, 1') }; |
|
26 |
|
|
27 |
if ($@) { |
|
28 |
$database_state = 'current'; |
|
29 |
} |
|
30 |
else { |
|
31 |
$database_state = 'v1'; |
|
32 |
} |
|
33 |
} |
|
34 | ||
35 |
# Need upgrade |
|
36 |
if ($database_state eq 'v1') { |
|
create version 2.0 setup scr...
|
37 |
die "Can't setup database. you maybe need upgrade database"; |
38 |
} |
|
fix some tests
|
39 |
# Create database |
create version 2.0 setup scr...
|
40 |
else { |
fix some tests
|
41 |
# Create user table |
42 |
eval { |
|
43 |
my $sql = <<"EOS"; |
|
44 |
create table user ( |
|
45 |
row_id integer primary key autoincrement, |
|
46 |
id not null unique default '', |
|
47 |
email not null unique default '' |
|
48 |
); |
|
49 |
EOS |
|
50 |
$dbi->execute($sql); |
|
51 |
}; |
|
52 | ||
53 |
# Create user columns |
|
54 |
my $user_columns = [ |
|
55 |
"admin integer not null default 0", |
|
56 |
"password not null default ''", |
|
57 |
"salt not null default ''", |
|
58 |
"name not null default ''" |
|
59 |
]; |
|
60 |
for my $column (@$user_columns) { |
|
61 |
eval { $dbi->execute("alter table user add column $column") }; |
|
62 |
} |
|
63 | ||
64 |
# Check user table |
|
65 |
eval { $dbi->select([qw/row_id id admin password salt email name/], table => 'user') }; |
|
66 |
if ($@) { |
|
67 |
my $error = "Can't create user table properly: $@"; |
|
fix collaboration
|
68 |
die $error; |
fix some tests
|
69 |
} |
70 |
|
|
71 |
# Create project table |
|
72 |
eval { |
|
73 |
my $sql = <<"EOS"; |
|
74 |
create table project ( |
|
75 |
row_id integer primary key autoincrement, |
|
76 |
user integer not null default 0, |
|
77 |
id not null, |
|
78 |
unique(user, id) |
|
79 |
); |
|
80 |
EOS |
|
81 |
$dbi->execute($sql); |
|
82 |
}; |
|
83 |
|
|
84 |
# Create Project columns |
|
85 |
my $project_columns = [ |
|
86 |
"default_branch not null default 'master'", |
|
87 |
"original_project integer not null default 0", |
|
88 |
"private integer not null default 0", |
|
89 |
"ignore_space_change integer not null default 0", |
|
add website URL settings. yo...
|
90 |
"guess_encoding integer not null default ''", |
91 |
"website_url not null default ''" |
|
fix some tests
|
92 |
]; |
93 |
for my $column (@$project_columns) { |
|
94 |
eval { $dbi->execute("alter table project add column $column") }; |
|
95 |
} |
|
96 | ||
97 |
# Check project table |
|
98 |
eval { |
|
99 |
$dbi->select( |
|
add website URL settings. yo...
|
100 |
[qw/row_id user id default_branch original_project private ignore_space_change guess_encoding website_url/], |
fix some tests
|
101 |
table => 'project' |
102 |
); |
|
103 |
}; |
|
104 |
if ($@) { |
|
105 |
my $error = "Can't create project table properly: $@"; |
|
fix collaboration
|
106 |
die $error; |
fix some tests
|
107 |
} |
108 | ||
109 |
# Create ssh_public_key table |
|
110 |
eval { |
|
111 |
my $sql = <<"EOS"; |
|
112 |
create table ssh_public_key ( |
|
113 |
row_id integer primary key autoincrement, |
|
114 |
key not null unique default '' |
|
115 |
); |
|
116 |
EOS |
|
117 |
$dbi->execute($sql); |
|
118 |
}; |
|
119 | ||
120 |
# Create ssh_public_key columns |
|
121 |
my $ssh_public_key_columns = [ |
|
122 |
"user integer not null default 0", |
|
123 |
"title not null default ''" |
|
124 |
]; |
|
125 |
for my $column (@$ssh_public_key_columns) { |
|
126 |
eval { $dbi->execute("alter table ssh_public_key add column $column") }; |
|
127 |
} |
|
128 |
|
|
129 |
# Check ssh_public_key table |
|
130 |
eval { $dbi->select([qw/row_id user key title/], table => 'ssh_public_key') }; |
|
131 |
if ($@) { |
|
132 |
my $error = "Can't create ssh_public_key table properly: $@"; |
|
fix collaboration
|
133 |
die $error; |
fix some tests
|
134 |
} |
135 | ||
136 |
# Create collaboration table |
|
137 |
eval { |
|
138 |
my $sql = <<"EOS"; |
|
139 |
create table collaboration ( |
|
140 |
row_id integer primary key autoincrement, |
|
141 |
project integer not null default 0, |
|
fix collaboration
|
142 |
user integer not null default 0, |
143 |
unique(project, user) |
|
fix some tests
|
144 |
); |
145 |
EOS |
|
146 |
$dbi->execute($sql); |
|
147 |
}; |
|
148 |
|
|
149 |
# Check collaboration table |
|
fix collaboration
|
150 |
eval { $dbi->select([qw/row_id project user/], table => 'collaboration') }; |
fix some tests
|
151 |
if ($@) { |
152 |
my $error = "Can't create collaboration table properly: $@"; |
|
fix collaboration
|
153 |
die $error; |
fix some tests
|
154 |
} |
155 | ||
fix setup_database, add issu...
|
156 |
# Create issue table |
Revert "cleanup"
|
157 |
eval { |
158 |
my $sql = <<"EOS"; |
|
fix setup_database, add issu...
|
159 |
create table issue ( |
issue belong to project
|
160 |
row_id integer primary key autoincrement, |
161 |
project integer not null default 0, |
|
162 |
number integer not null default 0, |
|
163 |
unique(project, number) |
|
Revert "cleanup"
|
164 |
); |
165 |
EOS |
|
166 |
$dbi->execute($sql); |
|
167 |
}; |
|
168 |
|
|
fix setup_database, add issu...
|
169 |
# Create issue columns |
170 |
my @issue_columns = ( |
|
Revert "cleanup"
|
171 |
"title not null default ''", |
issue belong to project
|
172 |
"open integer not null default 0", |
173 |
"open_time integer not null default 0", |
|
174 |
"open_user integer not null default 0", |
|
175 |
"pull_request integer not null default 0", |
|
Revert "cleanup"
|
176 |
); |
fix setup_database, add issu...
|
177 |
for my $column (@issue_columns) { |
178 |
eval { $dbi->execute("alter table issue add column $column") }; |
|
Revert "cleanup"
|
179 |
} |
180 | ||
fix setup_database, add issu...
|
181 |
# Check issue table |
issue belong to project
|
182 |
eval { $dbi->select([qw/row_id title open open_time open_user pull_request project/], table => 'issue') }; |
Revert "cleanup"
|
183 |
if ($@) { |
fix setup_database, add issu...
|
184 |
my $error = "Can't create issue table properly: $@"; |
Revert "cleanup"
|
185 |
die $error; |
186 |
} |
|
fix setup_database, add issu...
|
187 |
|
188 |
# Create issue_message table |
|
add pull_request_message tab...
|
189 |
eval { |
190 |
my $sql = <<"EOS"; |
|
fix setup_database, add issu...
|
191 |
create table issue_message ( |
add pull_request_message tab...
|
192 |
row_id integer primary key autoincrement, |
fix setup_database, add issu...
|
193 |
issue integer not null default 0, |
add pull_request_message tab...
|
194 |
number integer not null default 0, |
fix setup_database, add issu...
|
195 |
unique(issue, number) |
add pull_request_message tab...
|
196 |
); |
197 |
EOS |
|
198 |
$dbi->execute($sql); |
|
199 |
}; |
|
200 |
|
|
fix setup_database, add issu...
|
201 |
# Create issue_message columns |
202 |
my @issue_message_columns = ( |
|
add pull_request_message tab...
|
203 |
"message not null default ''", |
issue belong to project
|
204 |
"create_time integer not null default 0", |
205 |
"update_time integer not null default 0", |
|
add pull request message log...
|
206 |
"user integer not null default 0" |
add pull_request_message tab...
|
207 |
); |
fix setup_database, add issu...
|
208 |
for my $column (@issue_message_columns) { |
209 |
eval { $dbi->execute("alter table issue_message add column $column") }; |
|
210 |
} |
|
211 | ||
212 |
# Check issue_message table |
|
213 |
eval { $dbi->select([qw/row_id issue number message create_time update_time user/], table => 'issue_message') }; |
|
214 |
if ($@) { |
|
215 |
my $error = "Can't create issue_message table properly: $@"; |
|
216 |
die $error; |
|
217 |
} |
|
218 | ||
219 |
# Create pull_request table |
|
220 |
eval { |
|
221 |
my $sql = <<"EOS"; |
|
222 |
create table pull_request ( |
|
223 |
row_id integer primary key autoincrement, |
|
224 |
base_project integer not null default 0, |
|
225 |
base_branch not null default '', |
|
226 |
target_project integer not null default 0, |
|
227 |
target_branch not null default '', |
|
228 |
unique(base_project, base_branch, target_project, target_branch) |
|
229 |
); |
|
230 |
EOS |
|
231 |
$dbi->execute($sql); |
|
232 |
}; |
|
233 |
|
|
234 |
# Create pull_request columns |
|
235 |
my @pull_request_columns = (); |
|
236 |
for my $column (@pull_request_columns) { |
|
237 |
eval { $dbi->execute("alter table pull_request add column $column") }; |
|
add pull_request_message tab...
|
238 |
} |
239 | ||
fix setup_database, add issu...
|
240 |
# Check pull_request table |
241 |
eval { $dbi->select([qw/row_id base_project base_branch target_project target_branch/], table => 'pull_request') }; |
|
add pull_request_message tab...
|
242 |
if ($@) { |
fix setup_database, add issu...
|
243 |
my $error = "Can't create pull_request table properly: $@"; |
add pull_request_message tab...
|
244 |
die $error; |
245 |
} |
|
add label table
|
246 | |
247 |
# Create label table |
|
248 |
eval { |
|
249 |
my $sql = <<"EOS"; |
|
250 |
create table label ( |
|
251 |
row_id integer primary key autoincrement, |
|
252 |
project integer not null default 0, |
|
253 |
id varchar(100) not null default '', |
|
254 |
unique(project, id) |
|
255 |
); |
|
256 |
EOS |
|
257 |
$dbi->execute($sql); |
|
258 |
}; |
|
259 |
|
|
260 |
# Create label columns |
|
261 |
my @label_columns = ( |
|
262 |
"color not null default ''" |
|
263 |
); |
|
264 |
for my $column (@label_columns) { |
|
265 |
eval { $dbi->execute("alter table label add column $column") }; |
|
266 |
} |
|
267 | ||
268 |
# Check label table |
|
269 |
eval { $dbi->select([qw/row_id project id color/], table => 'label') }; |
|
270 |
if ($@) { |
|
271 |
my $error = "Can't create label table properly: $@"; |
|
272 |
die $error; |
|
273 |
} |
|
add issue_label table
|
274 | |
275 |
# Create issue_label table |
|
276 |
eval { |
|
277 |
my $sql = <<"EOS"; |
|
278 |
create table issue_label ( |
|
279 |
row_id integer primary key autoincrement, |
|
280 |
issue integer not null default 0, |
|
281 |
label integer not null default 0, |
|
282 |
unique(issue, label) |
|
283 |
); |
|
284 |
EOS |
|
285 |
$dbi->execute($sql); |
|
286 |
}; |
|
287 |
|
|
288 |
# Create issue_label columns |
|
289 |
my @issue_label_columns = ( |
|
290 |
); |
|
291 |
for my $column (@issue_label_columns) { |
|
292 |
eval { $dbi->execute("alter table issue_label add column $column") }; |
|
293 |
} |
|
294 | ||
295 |
# Check issue_label table |
|
296 |
eval { $dbi->select([qw/row_id issue label/], table => 'issue_label') }; |
|
297 |
if ($@) { |
|
298 |
my $error = "Can't create issue_label table properly: $@"; |
|
299 |
die $error; |
|
300 |
} |
|
create version 2.0 setup scr...
|
301 |
} |