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: $@"; |
|
68 |
$self->app->log->error($error); |
|
69 |
croak $error; |
|
70 |
} |
|
71 |
|
|
72 |
# Create project table |
|
73 |
eval { |
|
74 |
my $sql = <<"EOS"; |
|
75 |
create table project ( |
|
76 |
row_id integer primary key autoincrement, |
|
77 |
user integer not null default 0, |
|
78 |
id not null, |
|
79 |
unique(user, id) |
|
80 |
); |
|
81 |
EOS |
|
82 |
$dbi->execute($sql); |
|
83 |
}; |
|
84 |
|
|
85 |
# Create Project columns |
|
86 |
my $project_columns = [ |
|
87 |
"default_branch not null default 'master'", |
|
88 |
"original_project integer not null default 0", |
|
89 |
"private integer not null default 0", |
|
90 |
"ignore_space_change integer not null default 0", |
|
91 |
"guess_encoding integer not null default ''" |
|
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( |
|
100 |
[qw/row_id user id default_branch original_project private ignore_space_change guess_encoding/], |
|
101 |
table => 'project' |
|
102 |
); |
|
103 |
}; |
|
104 |
if ($@) { |
|
105 |
my $error = "Can't create project table properly: $@"; |
|
106 |
$self->app->log->error($error); |
|
107 |
croak $error; |
|
108 |
} |
|
109 | ||
110 |
# Create ssh_public_key table |
|
111 |
eval { |
|
112 |
my $sql = <<"EOS"; |
|
113 |
create table ssh_public_key ( |
|
114 |
row_id integer primary key autoincrement, |
|
115 |
key not null unique default '' |
|
116 |
); |
|
117 |
EOS |
|
118 |
$dbi->execute($sql); |
|
119 |
}; |
|
120 | ||
121 |
# Create ssh_public_key columns |
|
122 |
my $ssh_public_key_columns = [ |
|
123 |
"user integer not null default 0", |
|
124 |
"title not null default ''" |
|
125 |
]; |
|
126 |
for my $column (@$ssh_public_key_columns) { |
|
127 |
eval { $dbi->execute("alter table ssh_public_key add column $column") }; |
|
128 |
} |
|
129 |
|
|
130 |
# Check ssh_public_key table |
|
131 |
eval { $dbi->select([qw/row_id user key title/], table => 'ssh_public_key') }; |
|
132 |
if ($@) { |
|
133 |
my $error = "Can't create ssh_public_key table properly: $@"; |
|
134 |
$self->app->log->error($error); |
|
135 |
croak $error; |
|
136 |
} |
|
137 | ||
138 |
# Create collaboration table |
|
139 |
eval { |
|
140 |
my $sql = <<"EOS"; |
|
141 |
create table collaboration ( |
|
142 |
row_id integer primary key autoincrement, |
|
143 |
project integer not null default 0, |
|
144 |
collaborator integer not null default 0, |
|
145 |
unique(project, collaborator) |
|
146 |
); |
|
147 |
EOS |
|
148 |
$dbi->execute($sql); |
|
149 |
}; |
|
150 |
|
|
151 |
# Check collaboration table |
|
152 |
eval { $dbi->select([qw/row_id project collaborator/], table => 'collaboration') }; |
|
153 |
if ($@) { |
|
154 |
my $error = "Can't create collaboration table properly: $@"; |
|
155 |
$self->app->log->error($error); |
|
156 |
croak $error; |
|
157 |
} |
|
158 | ||
159 |
# Create pull_request table |
|
160 |
eval { |
|
161 |
my $sql = <<"EOS"; |
|
162 |
create table pull_request ( |
|
163 |
row_id integer primary key autoincrement, |
|
164 |
project integer not null default 0, |
|
165 |
branch1 not null default '', |
|
166 |
branch2 not null default '', |
|
167 |
unique(project, branch1, branch2) |
|
168 |
); |
|
169 |
EOS |
|
170 |
$dbi->execute($sql); |
|
171 |
}; |
|
172 |
|
|
173 |
# Create pull_request columns |
|
174 |
my @pull_request_columns = ( |
|
175 |
"title not null default ''", |
|
176 |
"open integer default 0", |
|
177 |
"open_time integer default 0", |
|
178 |
"open_user integer default 0" |
|
179 |
); |
|
180 |
for my $column (@pull_request_columns) { |
|
181 |
eval { $dbi->execute("alter table pull_request add column $column") }; |
|
182 |
} |
|
183 | ||
184 |
# Check pull_request table |
|
185 |
eval { $dbi->select([qw/row_id project branch1 branch2 title open open_time open_user/], table => 'pull_request') }; |
|
186 |
if ($@) { |
|
187 |
my $error = "Can't create pull_request table properly: $@"; |
|
188 |
$self->app->log->error($error); |
|
189 |
croak $error; |
|
190 |
} |
|
create version 2.0 setup scr...
|
191 |
} |