gitprep / setup_database /
Newer Older
300 lines | 7.555kb
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",
90
    "guess_encoding integer not null default ''"
91
  ];
92
  for my $column (@$project_columns) {
93
    eval { $dbi->execute("alter table project add column $column") };
94
  }
95

            
96
  # Check project table
97
  eval {
98
    $dbi->select(
99
      [qw/row_id user id default_branch original_project private ignore_space_change guess_encoding/],
100
      table => 'project'
101
    );
102
  };
103
  if ($@) {
104
    my $error = "Can't create project table properly: $@";
fix collaboration
Yuki Kimoto authored on 2016-04-22
105
    die $error;
fix some tests
Yuki Kimoto authored on 2016-04-21
106
  }
107

            
108
  # Create ssh_public_key table
109
  eval {
110
    my $sql = <<"EOS";
111
create table ssh_public_key (
112
  row_id integer primary key autoincrement,
113
  key not null unique default ''
114
);
115
EOS
116
    $dbi->execute($sql);
117
  };
118

            
119
  # Create ssh_public_key columns
120
  my $ssh_public_key_columns = [
121
    "user integer not null default 0",
122
    "title not null default ''"
123
  ];
124
  for my $column (@$ssh_public_key_columns) {
125
    eval { $dbi->execute("alter table ssh_public_key add column $column") };
126
  }
127
  
128
  # Check ssh_public_key table
129
  eval { $dbi->select([qw/row_id user key title/], table => 'ssh_public_key') };
130
  if ($@) {
131
    my $error = "Can't create ssh_public_key table properly: $@";
fix collaboration
Yuki Kimoto authored on 2016-04-22
132
    die $error;
fix some tests
Yuki Kimoto authored on 2016-04-21
133
  }
134

            
135
  # Create collaboration table
136
  eval {
137
    my $sql = <<"EOS";
138
create table collaboration (
139
  row_id integer primary key autoincrement,
140
  project integer not null default 0,
fix collaboration
Yuki Kimoto authored on 2016-04-22
141
  user integer not null default 0,
142
  unique(project, user)
fix some tests
Yuki Kimoto authored on 2016-04-21
143
);
144
EOS
145
    $dbi->execute($sql);
146
  };
147
  
148
  # Check collaboration table
fix collaboration
Yuki Kimoto authored on 2016-04-22
149
  eval { $dbi->select([qw/row_id project user/], table => 'collaboration') };
fix some tests
Yuki Kimoto authored on 2016-04-21
150
  if ($@) {
151
    my $error = "Can't create collaboration table properly: $@";
fix collaboration
Yuki Kimoto authored on 2016-04-22
152
    die $error;
fix some tests
Yuki Kimoto authored on 2016-04-21
153
  }
154

            
fix setup_database, add issu...
Yuki Kimoto authored on 2016-06-08
155
  # Create issue table
Revert "cleanup"
Yuki Kimoto authored on 2016-06-08
156
  eval {
157
    my $sql = <<"EOS";
fix setup_database, add issu...
Yuki Kimoto authored on 2016-06-08
158
create table issue (
issue belong to project
Yuki Kimoto authored on 2016-06-11
159
  row_id integer primary key autoincrement,
160
  project integer not null default 0,
161
  number integer not null default 0,
162
  unique(project, number)
Revert "cleanup"
Yuki Kimoto authored on 2016-06-08
163
);
164
EOS
165
    $dbi->execute($sql);
166
  };
167
  
fix setup_database, add issu...
Yuki Kimoto authored on 2016-06-08
168
  # Create issue columns
169
  my @issue_columns = (
Revert "cleanup"
Yuki Kimoto authored on 2016-06-08
170
    "title not null default ''",
issue belong to project
Yuki Kimoto authored on 2016-06-11
171
    "open integer not null default 0",
172
    "open_time integer not null default 0",
173
    "open_user integer not null default 0",
174
    "pull_request integer  not null default 0",
Revert "cleanup"
Yuki Kimoto authored on 2016-06-08
175
  );
fix setup_database, add issu...
Yuki Kimoto authored on 2016-06-08
176
  for my $column (@issue_columns) {
177
    eval { $dbi->execute("alter table issue add column $column") };
Revert "cleanup"
Yuki Kimoto authored on 2016-06-08
178
  }
179

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

            
211
  # Check issue_message table
212
  eval { $dbi->select([qw/row_id issue number message create_time update_time user/], table => 'issue_message') };
213
  if ($@) {
214
    my $error = "Can't create issue_message table properly: $@";
215
    die $error;
216
  }
217

            
218
  # Create pull_request table
219
  eval {
220
    my $sql = <<"EOS";
221
create table pull_request (
222
  row_id integer primary key autoincrement,
223
  base_project integer not null default 0,
224
  base_branch not null default '',
225
  target_project integer not null default 0,
226
  target_branch not null default '',
227
  unique(base_project, base_branch, target_project, target_branch)
228
);
229
EOS
230
    $dbi->execute($sql);
231
  };
232
  
233
  # Create pull_request columns
234
  my @pull_request_columns = ();
235
  for my $column (@pull_request_columns) {
236
    eval { $dbi->execute("alter table pull_request add column $column") };
add pull_request_message tab...
Yuki Kimoto authored on 2016-04-23
237
  }
238

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

            
246
  # Create label table
247
  eval {
248
    my $sql = <<"EOS";
249
create table label (
250
  row_id integer primary key autoincrement,
251
  project integer not null default 0,
252
  id varchar(100) not null default '',
253
  unique(project, id)
254
);
255
EOS
256
    $dbi->execute($sql);
257
  };
258
  
259
  # Create label columns
260
  my @label_columns = (
261
    "color not null default ''"
262
  );
263
  for my $column (@label_columns) {
264
    eval { $dbi->execute("alter table label add column $column") };
265
  }
266

            
267
  # Check label table
268
  eval { $dbi->select([qw/row_id project id color/], table => 'label') };
269
  if ($@) {
270
    my $error = "Can't create label table properly: $@";
271
    die $error;
272
  }
add issue_label table
Yuki Kimoto authored on 2016-07-30
273

            
274
  # Create issue_label table
275
  eval {
276
    my $sql = <<"EOS";
277
create table issue_label (
278
  row_id integer primary key autoincrement,
279
  issue integer not null default 0,
280
  label integer not null default 0,
281
  unique(issue, label)
282
);
283
EOS
284
    $dbi->execute($sql);
285
  };
286
  
287
  # Create issue_label columns
288
  my @issue_label_columns = (
289
  );
290
  for my $column (@issue_label_columns) {
291
    eval { $dbi->execute("alter table issue_label add column $column") };
292
  }
293

            
294
  # Check issue_label table
295
  eval { $dbi->select([qw/row_id issue label/], table => 'issue_label') };
296
  if ($@) {
297
    my $error = "Can't create issue_label table properly: $@";
298
    die $error;
299
  }
create version 2.0 setup scr...
Yuki Kimoto authored on 2016-04-21
300
}