-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_database
More file actions
executable file
·47 lines (39 loc) · 1.06 KB
/
add_database
File metadata and controls
executable file
·47 lines (39 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env perl
use FindBin;
use lib "$FindBin::Bin/lib";
use lib "$FindBin::Bin/extlib/lib/perl5";
use DBIx::Custom;
my $database_file = shift // "$FindBin::Bin/data/gitprep.db";
# DBI
my %dbi_args = (
dsn => "dbi:SQLite:database=$database_file",
connector => 1,
option => {sqlite_unicode => 1, sqlite_use_immediate_transaction => 1}
);
my $dbi = DBIx::Custom->connect(%dbi_args);
# Database state
my $database_state;
if (!-f $database_file) {
$database_state = 'empty';
}
else {
# If project.user_id exists, that database is version 1
eval { $dbi->select('user_id', table => 'project', append => 'limit 0, 1') };
if ($@) {
$database_state = 'current';
}
else {
$database_state = 'v1';
}
}
# Need upgrade
if ($database_state eq 'v1') {
die "Can't setup database. you maybe need upgrade database";
}
my $project_columns = [
"maxsize varchar(60) not null default 5000000",
"regdate datetime default '0000-00-00 00:00:00'"
];
for my $column (@$project_columns) {
eval { $dbi->execute("alter table user add column $column") };
}