... | ... |
@@ -43,6 +43,12 @@ Setup database. |
43 | 43 |
|
44 | 44 |
./setup_database |
45 | 45 |
|
46 |
+If you used Gitprep version 1 from now, you need upgrade database. |
|
47 |
+ |
|
48 |
+ mv data/gitprep.db data/gitprep_v1bak.db |
|
49 |
+ ./setup_database |
|
50 |
+ ./copy_database_v1_to_v2 data/gitprep_v1bak.db data/gitprep.db |
|
51 |
+ |
|
46 | 52 |
If you install git in your local directry, |
47 | 53 |
you must add the correct git command path to the **gitprep.conf** config file. |
48 | 54 |
|
... | ... |
@@ -397,6 +397,33 @@ sub rename_project { |
397 | 397 |
croak $error if $error; |
398 | 398 |
} |
399 | 399 |
|
400 |
+sub is_database_v1 { |
|
401 |
+ my $self = shift; |
|
402 |
+ |
|
403 |
+ # If project.user_id exists, that database is version 1 |
|
404 |
+ my $user_id; |
|
405 |
+ eval { $user_id = $self->app->dbi->model('project')->select('user_id', append => 'limit 0, 1') }; |
|
406 |
+ |
|
407 |
+ my $is_v1 = defined $user_id ? 1 : 0; |
|
408 |
+ |
|
409 |
+ return $is_v1; |
|
410 |
+} |
|
411 |
+ |
|
412 |
+=pod |
|
413 |
+ # If mail is empty, id is copied to mail for uniqueness |
|
414 |
+ my $user_ids = $dbi->select('id', table => 'user', where => {mail => ''})->values; |
|
415 |
+ for my $user_id (@$user_ids) { |
|
416 |
+ $dbi->update({mail => "$user_id\@gitprep.example"}, table => 'user', where => {id => $user_id}); |
|
417 |
+ } |
|
418 |
+ |
|
419 |
+ # add unique to mail |
|
420 |
+ eval { $dbi->execute("create unique index user__mail on user(mail)") }; |
|
421 |
+ my $created_user_mail_index = $dbi->execute("select * from sqlite_master where type = 'index' and name = 'user__mail'")->one; |
|
422 |
+ unless ($created_user_mail_index) { |
|
423 |
+ croak "Can't create user__mail index"; |
|
424 |
+ } |
|
425 |
+=cut |
|
426 |
+ |
|
400 | 427 |
sub setup_database { |
401 | 428 |
my $self = shift; |
402 | 429 |
|
... | ... |
@@ -409,6 +436,192 @@ create table user ( |
409 | 436 |
row_id integer primary key autoincrement, |
410 | 437 |
id not null unique default '' |
411 | 438 |
); |
439 |
+EOS |
|
440 |
+ $dbi->execute($sql); |
|
441 |
+ }; |
|
442 |
+ |
|
443 |
+ # Create user columns |
|
444 |
+ my $user_columns = [ |
|
445 |
+ "admin integer not null default 0", |
|
446 |
+ "password not null default ''", |
|
447 |
+ "salt not null default ''", |
|
448 |
+ "email not null default ''", |
|
449 |
+ "name not null default ''" |
|
450 |
+ ]; |
|
451 |
+ for my $column (@$user_columns) { |
|
452 |
+ eval { $dbi->execute("alter table user add column $column") }; |
|
453 |
+ } |
|
454 |
+ |
|
455 |
+ # Check user table |
|
456 |
+ eval { $dbi->select([qw/row_id id admin password salt email name/], table => 'user') }; |
|
457 |
+ if ($@) { |
|
458 |
+ my $error = "Can't create user table properly: $@"; |
|
459 |
+ $self->app->log->error($error); |
|
460 |
+ croak $error; |
|
461 |
+ } |
|
462 |
+ |
|
463 |
+ # Create project table |
|
464 |
+ eval { |
|
465 |
+ my $sql = <<"EOS"; |
|
466 |
+create table project ( |
|
467 |
+ row_id integer primary key autoincrement, |
|
468 |
+ user integer not null default 0, |
|
469 |
+ id not null, |
|
470 |
+ unique(user, id) |
|
471 |
+); |
|
472 |
+EOS |
|
473 |
+ $dbi->execute($sql); |
|
474 |
+ }; |
|
475 |
+ |
|
476 |
+ # Create Project columns |
|
477 |
+ my $project_columns = [ |
|
478 |
+ "default_branch not null default 'master'", |
|
479 |
+ "original_project integer not null default 0", |
|
480 |
+ "private integer not null default 0", |
|
481 |
+ "ignore_space_change integer not null default 0", |
|
482 |
+ "guess_encoding integer not null default ''" |
|
483 |
+ ]; |
|
484 |
+ for my $column (@$project_columns) { |
|
485 |
+ eval { $dbi->execute("alter table project add column $column") }; |
|
486 |
+ } |
|
487 |
+ |
|
488 |
+ # Check project table |
|
489 |
+ eval { |
|
490 |
+ $dbi->select( |
|
491 |
+ [qw/row_id user id default_branch original_project private ignore_space_change guess_encoding/], |
|
492 |
+ table => 'project' |
|
493 |
+ ); |
|
494 |
+ }; |
|
495 |
+ if ($@) { |
|
496 |
+ my $error = "Can't create project table properly: $@"; |
|
497 |
+ $self->app->log->error($error); |
|
498 |
+ croak $error; |
|
499 |
+ } |
|
500 |
+ |
|
501 |
+ # Create ssh_public_key table |
|
502 |
+ eval { |
|
503 |
+ my $sql = <<"EOS"; |
|
504 |
+create table ssh_public_key ( |
|
505 |
+ row_id integer primary key autoincrement, |
|
506 |
+ key not null unique default '' |
|
507 |
+); |
|
508 |
+EOS |
|
509 |
+ $dbi->execute($sql); |
|
510 |
+ }; |
|
511 |
+ |
|
512 |
+ # Create ssh_public_key columns |
|
513 |
+ my $ssh_public_key_columns = [ |
|
514 |
+ "user integer not null default 0", |
|
515 |
+ "title not null default ''" |
|
516 |
+ ]; |
|
517 |
+ for my $column (@$ssh_public_key_columns) { |
|
518 |
+ eval { $dbi->execute("alter table ssh_public_key add column $column") }; |
|
519 |
+ } |
|
520 |
+ |
|
521 |
+ # Check ssh_public_key table |
|
522 |
+ eval { $dbi->select([qw/row_id user key title/], table => 'ssh_public_key') }; |
|
523 |
+ if ($@) { |
|
524 |
+ my $error = "Can't create ssh_public_key table properly: $@"; |
|
525 |
+ $self->app->log->error($error); |
|
526 |
+ croak $error; |
|
527 |
+ } |
|
528 |
+ |
|
529 |
+ # Create collaboration table |
|
530 |
+ eval { |
|
531 |
+ my $sql = <<"EOS"; |
|
532 |
+create table collaboration ( |
|
533 |
+ row_id integer primary key autoincrement, |
|
534 |
+ project integer not null default 0, |
|
535 |
+ collaborator integer not null default 0, |
|
536 |
+ unique(project, collaborator) |
|
537 |
+); |
|
538 |
+EOS |
|
539 |
+ $dbi->execute($sql); |
|
540 |
+ }; |
|
541 |
+ |
|
542 |
+ # Check collaboration table |
|
543 |
+ eval { $dbi->select([qw/row_id project collaborator/], table => 'collaboration') }; |
|
544 |
+ if ($@) { |
|
545 |
+ my $error = "Can't create collaboration table properly: $@"; |
|
546 |
+ $self->app->log->error($error); |
|
547 |
+ croak $error; |
|
548 |
+ } |
|
549 |
+ |
|
550 |
+ # Create pull_request table |
|
551 |
+ eval { |
|
552 |
+ my $sql = <<"EOS"; |
|
553 |
+create table pull_request ( |
|
554 |
+ row_id integer primary key autoincrement, |
|
555 |
+ project integer not null default 0, |
|
556 |
+ branch1 not null default '', |
|
557 |
+ branch2 not null default '', |
|
558 |
+ unique(project, branch1, branch2) |
|
559 |
+); |
|
560 |
+EOS |
|
561 |
+ $dbi->execute($sql); |
|
562 |
+ }; |
|
563 |
+ |
|
564 |
+ # Create pull_request columns |
|
565 |
+ my @pull_request_columns = ( |
|
566 |
+ "title not null default ''", |
|
567 |
+ "message not null default ''", |
|
568 |
+ "open integer default 0", |
|
569 |
+ "open_time integer default 0", |
|
570 |
+ "open_user integer default 0" |
|
571 |
+ ); |
|
572 |
+ for my $column (@pull_request_columns) { |
|
573 |
+ eval { $dbi->execute("alter table pull_request add column $column") }; |
|
574 |
+ } |
|
575 |
+ |
|
576 |
+ # Check pull_request table |
|
577 |
+ eval { $dbi->select([qw/row_id project branch1 branch2 title message open open_time open_user/], table => 'pull_request') }; |
|
578 |
+ if ($@) { |
|
579 |
+ my $error = "Can't create pull_request table properly: $@"; |
|
580 |
+ $self->app->log->error($error); |
|
581 |
+ croak $error; |
|
582 |
+ } |
|
583 |
+ |
|
584 |
+ # Create number table |
|
585 |
+ eval { |
|
586 |
+ my $sql = <<"EOS"; |
|
587 |
+create table number ( |
|
588 |
+ row_id integer primary key autoincrement, |
|
589 |
+ key not null unique |
|
590 |
+); |
|
591 |
+EOS |
|
592 |
+ $dbi->execute($sql); |
|
593 |
+ }; |
|
594 |
+ |
|
595 |
+ # Create number columns |
|
596 |
+ my $number_columns = [ |
|
597 |
+ "value integer not null default '0'" |
|
598 |
+ ]; |
|
599 |
+ for my $column (@$number_columns) { |
|
600 |
+ eval { $dbi->execute("alter table number add column $column") }; |
|
601 |
+ } |
|
602 |
+ |
|
603 |
+ # Check number table |
|
604 |
+ eval { $dbi->select([qw/row_id key value/], table => 'number') }; |
|
605 |
+ if ($@) { |
|
606 |
+ my $error = "Can't create number table properly: $@"; |
|
607 |
+ $self->app->log->error($error); |
|
608 |
+ croak $error; |
|
609 |
+ } |
|
610 |
+} |
|
611 |
+ |
|
612 |
+=pod |
|
613 |
+sub setup_database_v1 { |
|
614 |
+ my $self = shift; |
|
615 |
+ |
|
616 |
+ my $dbi = $self->app->dbi; |
|
617 |
+ |
|
618 |
+ # Create user table |
|
619 |
+ eval { |
|
620 |
+ my $sql = <<"EOS"; |
|
621 |
+create table user ( |
|
622 |
+ row_id integer primary key autoincrement, |
|
623 |
+ id not null unique default '' |
|
624 |
+); |
|
412 | 625 |
EOS |
413 | 626 |
$dbi->execute($sql); |
414 | 627 |
}; |
... | ... |
@@ -616,7 +829,7 @@ EOS |
616 | 829 |
croak $error; |
617 | 830 |
} |
618 | 831 |
} |
619 |
- |
|
832 |
+=cut |
|
620 | 833 |
|
621 | 834 |
sub update_authorized_keys_file { |
622 | 835 |
my $self = shift; |
... | ... |
@@ -7,4 +7,10 @@ use lib "$FindBin::Bin/extlib/lib/perl5"; |
7 | 7 |
use Gitprep; |
8 | 8 |
|
9 | 9 |
my $app = Gitprep->new; |
10 |
-$app->manager->setup_database; |
|
10 |
+ |
|
11 |
+if ($app->manager->is_database_v1) { |
|
12 |
+ die "Can't setup database. you maybe need upgrade database"; |
|
13 |
+} |
|
14 |
+else { |
|
15 |
+ $app->manager->setup_database; |
|
16 |
+} |