gitprep / setup_database /
Newer Older
301 lines | 7.606kb
separete database creating l...
Yuki Kimoto authored on 2016-04-06
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
Yuki Kimoto authored on 2016-04-21
6
use DBIx::Custom;
separete database creating l...
Yuki Kimoto authored on 2016-04-06
7

            
fix some tests
Yuki Kimoto authored on 2016-04-21
8
my $database_file = shift // "$FindBin::Bin/data/gitprep.db";
separete database creating l...
Yuki Kimoto authored on 2016-04-06
9

            
fix some tests
Yuki Kimoto authored on 2016-04-21
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...
Yuki Kimoto authored on 2016-04-21
17

            
fix some tests
Yuki Kimoto authored on 2016-04-21
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...
Yuki Kimoto authored on 2016-04-21
37
  die "Can't setup database. you maybe need upgrade database";
38
}
fix some tests
Yuki Kimoto authored on 2016-04-21
39
# Create database
create version 2.0 setup scr...
Yuki Kimoto authored on 2016-04-21
40
else {
fix some tests
Yuki Kimoto authored on 2016-04-21
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
Yuki Kimoto authored on 2016-04-22
68
    die $error;
fix some tests
Yuki Kimoto authored on 2016-04-21
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...
Yuki Kimoto authored on 2016-08-17
90
    "guess_encoding integer not null default ''",
91
    "website_url not null default ''"
fix some tests
Yuki Kimoto authored on 2016-04-21
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...
Yuki Kimoto authored on 2016-08-17
100
      [qw/row_id user id default_branch original_project private ignore_space_change guess_encoding website_url/],
fix some tests
Yuki Kimoto authored on 2016-04-21
101
      table => 'project'
102
    );
103
  };
104
  if ($@) {
105
    my $error = "Can't create project table properly: $@";
fix collaboration
Yuki Kimoto authored on 2016-04-22
106
    die $error;
fix some tests
Yuki Kimoto authored on 2016-04-21
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
Yuki Kimoto authored on 2016-04-22
133
    die $error;
fix some tests
Yuki Kimoto authored on 2016-04-21
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
Yuki Kimoto authored on 2016-04-22
142
  user integer not null default 0,
143
  unique(project, user)
fix some tests
Yuki Kimoto authored on 2016-04-21
144
);
145
EOS
146
    $dbi->execute($sql);
147
  };
148
  
149
  # Check collaboration table
fix collaboration
Yuki Kimoto authored on 2016-04-22
150
  eval { $dbi->select([qw/row_id project user/], table => 'collaboration') };
fix some tests
Yuki Kimoto authored on 2016-04-21
151
  if ($@) {
152
    my $error = "Can't create collaboration table properly: $@";
fix collaboration
Yuki Kimoto authored on 2016-04-22
153
    die $error;
fix some tests
Yuki Kimoto authored on 2016-04-21
154
  }
155

            
fix setup_database, add issu...
Yuki Kimoto authored on 2016-06-08
156
  # Create issue table
Revert "cleanup"
Yuki Kimoto authored on 2016-06-08
157
  eval {
158
    my $sql = <<"EOS";
fix setup_database, add issu...
Yuki Kimoto authored on 2016-06-08
159
create table issue (
issue belong to project
Yuki Kimoto authored on 2016-06-11
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"
Yuki Kimoto authored on 2016-06-08
164
);
165
EOS
166
    $dbi->execute($sql);
167
  };
168
  
fix setup_database, add issu...
Yuki Kimoto authored on 2016-06-08
169
  # Create issue columns
170
  my @issue_columns = (
Revert "cleanup"
Yuki Kimoto authored on 2016-06-08
171
    "title not null default ''",
issue belong to project
Yuki Kimoto authored on 2016-06-11
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"
Yuki Kimoto authored on 2016-06-08
176
  );
fix setup_database, add issu...
Yuki Kimoto authored on 2016-06-08
177
  for my $column (@issue_columns) {
178
    eval { $dbi->execute("alter table issue add column $column") };
Revert "cleanup"
Yuki Kimoto authored on 2016-06-08
179
  }
180

            
fix setup_database, add issu...
Yuki Kimoto authored on 2016-06-08
181
  # Check issue table
issue belong to project
Yuki Kimoto authored on 2016-06-11
182
  eval { $dbi->select([qw/row_id title open open_time open_user pull_request project/], table => 'issue') };
Revert "cleanup"
Yuki Kimoto authored on 2016-06-08
183
  if ($@) {
fix setup_database, add issu...
Yuki Kimoto authored on 2016-06-08
184
    my $error = "Can't create issue table properly: $@";
Revert "cleanup"
Yuki Kimoto authored on 2016-06-08
185
    die $error;
186
  }
fix setup_database, add issu...
Yuki Kimoto authored on 2016-06-08
187
  
188
  # Create issue_message table
add pull_request_message tab...
Yuki Kimoto authored on 2016-04-23
189
  eval {
190
    my $sql = <<"EOS";
fix setup_database, add issu...
Yuki Kimoto authored on 2016-06-08
191
create table issue_message (
add pull_request_message tab...
Yuki Kimoto authored on 2016-04-23
192
  row_id integer primary key autoincrement,
fix setup_database, add issu...
Yuki Kimoto authored on 2016-06-08
193
  issue integer not null default 0,
add pull_request_message tab...
Yuki Kimoto authored on 2016-04-23
194
  number integer not null default 0,
fix setup_database, add issu...
Yuki Kimoto authored on 2016-06-08
195
  unique(issue, number)
add pull_request_message tab...
Yuki Kimoto authored on 2016-04-23
196
);
197
EOS
198
    $dbi->execute($sql);
199
  };
200
  
fix setup_database, add issu...
Yuki Kimoto authored on 2016-06-08
201
  # Create issue_message columns
202
  my @issue_message_columns = (
add pull_request_message tab...
Yuki Kimoto authored on 2016-04-23
203
    "message not null default ''",
issue belong to project
Yuki Kimoto authored on 2016-06-11
204
    "create_time integer not null default 0",
205
    "update_time integer not null default 0",
add pull request message log...
Yuki Kimoto authored on 2016-04-23
206
    "user integer not null default 0"
add pull_request_message tab...
Yuki Kimoto authored on 2016-04-23
207
  );
fix setup_database, add issu...
Yuki Kimoto authored on 2016-06-08
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...
Yuki Kimoto authored on 2016-04-23
238
  }
239

            
fix setup_database, add issu...
Yuki Kimoto authored on 2016-06-08
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...
Yuki Kimoto authored on 2016-04-23
242
  if ($@) {
fix setup_database, add issu...
Yuki Kimoto authored on 2016-06-08
243
    my $error = "Can't create pull_request table properly: $@";
add pull_request_message tab...
Yuki Kimoto authored on 2016-04-23
244
    die $error;
245
  }
add label table
Yuki Kimoto authored on 2016-07-26
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
Yuki Kimoto authored on 2016-07-30
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...
Yuki Kimoto authored on 2016-04-21
301
}