gitprep / setup_database /
Newer Older
186 lines | 4.576kb
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

            
155
  # Create pull_request table
156
  eval {
157
    my $sql = <<"EOS";
158
create table pull_request (
159
  row_id integer primary key autoincrement,
160
  project integer not null default 0,
161
  branch1 not null default '',
162
  branch2 not null default '',
163
  unique(project, branch1, branch2)
164
);
165
EOS
166
    $dbi->execute($sql);
167
  };
168
  
169
  # Create pull_request columns
170
  my @pull_request_columns = (
171
    "title not null default ''",
172
    "open integer default 0",
173
    "open_time integer default 0",
174
    "open_user integer default 0"
175
  );
176
  for my $column (@pull_request_columns) {
177
    eval { $dbi->execute("alter table pull_request add column $column") };
178
  }
179

            
180
  # Check pull_request table
181
  eval { $dbi->select([qw/row_id project branch1 branch2 title open open_time open_user/], table => 'pull_request') };
182
  if ($@) {
183
    my $error = "Can't create pull_request table properly: $@";
fix collaboration
Yuki Kimoto authored on 2016-04-22
184
    die $error;
fix some tests
Yuki Kimoto authored on 2016-04-21
185
  }
create version 2.0 setup scr...
Yuki Kimoto authored on 2016-04-21
186
}