gitprep / old / copy_database_v1_to_v2 /
Newer Older
202 lines | 5.507kb
fix setup_database
Yuki Kimoto authored on 2016-04-21
1
#!/usr/bin/env perl
2

            
add user copy logic
Yuki Kimoto authored on 2016-04-21
3
use strict;
4
use warnings;
5

            
fix setup_database
Yuki Kimoto authored on 2016-04-21
6
use FindBin;
7
use lib "$FindBin::Bin/../extlib/lib/perl5";
8
use DBIx::Custom;
9

            
10
my ($old_database_file, $new_database_file) = @ARGV;
11

            
12
# Old DBI
13
my %old_dbi_args = (
14
  dsn => "dbi:SQLite:database=$old_database_file",
15
  connector => 1,
16
  option => {sqlite_unicode => 1, sqlite_use_immediate_transaction => 1}
17
);
18
my $old_dbi = DBIx::Custom->connect(%old_dbi_args);
19

            
20
# New DBI
21
my %new_dbi_args = (
22
  dsn => "dbi:SQLite:database=$new_database_file",
23
  connector => 1,
24
  option => {sqlite_unicode => 1, sqlite_use_immediate_transaction => 1}
25
);
26
my $new_dbi = DBIx::Custom->connect(%new_dbi_args);
27

            
add user copy logic
Yuki Kimoto authored on 2016-04-21
28
# Copy user data
29
my $old_users = $old_dbi->select(table => 'user')->all;
30
my $new_user_count = $new_dbi->select('count(*)', table => 'user')->value;
31
unless ($new_user_count) {
32
  for my $old_user (@$old_users) {
33
    
34
    my @new_user_columns = qw(
35
      row_id
36
      id
37
      email
38
      admin
39
      password
40
      salt
41
      name
42
    );
43
    
44
    my $new_user = {};
45
    for my $new_user_column (@new_user_columns) {
46
      if (exists $old_user->{$new_user_column}) {
47
        $new_user->{$new_user_column} = $old_user->{$new_user_column};
48
      }
49
    }
50
    
51
    unless (exists $new_user->{email}) {
52
      $new_user->{email} = $old_user->{id} . '@gitprep.example';
53
    }
54
    
55
    $new_dbi->insert($new_user, table => 'user');
56
  }
fix setup_database
Yuki Kimoto authored on 2016-04-21
57
}
58

            
add project copy logic
Yuki Kimoto authored on 2016-04-21
59
# Copy project data
60
my $old_projects = $old_dbi->select(table => 'project')->all;
61
my $new_project_count = $new_dbi->select('count(*)', table => 'project')->value;
62
unless ($new_project_count) {
63
  for my $old_project (@$old_projects) {
64
    
65
    my @new_project_columns = qw(
66
      row_id
67
      id
68
      default_branch
69
      private integer
70
      ignore_space_change
71
      guess_encoding
72
    );
73
    
74
    my $new_project = {};
75
    for my $new_project_column (@new_project_columns) {
76
      if (exists $old_project->{$new_project_column}) {
77
        $new_project->{$new_project_column} = $old_project->{$new_project_column};
78
      }
79
    }
80
    
81
    # name is copied to id
82
    if (exists $old_project->{name}) {
83
      $new_project->{id} = $old_project->{name};
84
    }
85
    
86
    # original_pid is copied to original_project, which is converted to project.row_id
87
    if (exists $old_project->{original_pid}) {
88
      my $project_row_id = $old_dbi->select(
89
        'row_id',
90
        table => 'project',
91
        where => {
92
          original_pid => $old_project->{original_pid}
93
        },
94
        append => 'order by row_id'
95
      )->value;
96
      if ($project_row_id && $project_row_id ne $new_project->{row_id}) {
97
        $new_project->{original_project} = $project_row_id;
98
      }
99
    }
100
    
101
    # user_id is copied to user, which is converted to user.row_id
102
    my $user_row_id = $old_dbi->select(
103
      'row_id',
104
      table => 'user',
105
      where => {id => $old_project->{user_id}}
106
    )->value;
107
    if (defined $user_row_id) {
108
      $new_project->{user} = $user_row_id;
109
    }
110
    else {
111
      next;
112
    }
113
    
114
    $new_dbi->insert($new_project, table => 'project');
115
  }
116
}
117

            
add ssh_public_key table cop...
Yuki Kimoto authored on 2016-04-21
118
# Copy ssh_public_key data
119
my $old_ssh_public_keys = $old_dbi->select(table => 'ssh_public_key')->all;
120
my $new_ssh_public_key_count = $new_dbi->select('count(*)', table => 'ssh_public_key')->value;
121
unless ($new_ssh_public_key_count) {
122
  for my $old_ssh_public_key (@$old_ssh_public_keys) {
123
    
124
    my @new_ssh_public_key_columns = qw(
125
      row_id
126
      key
127
      title
128
    );
129
    
130
    my $new_ssh_public_key = {};
131
    for my $new_ssh_public_key_column (@new_ssh_public_key_columns) {
132
      if (exists $old_ssh_public_key->{$new_ssh_public_key_column}) {
133
        $new_ssh_public_key->{$new_ssh_public_key_column} = $old_ssh_public_key->{$new_ssh_public_key_column};
134
      }
135
    }
136

            
137
    # user_id is copied to user, which is converted to user.row_id
138
    my $user_row_id = $old_dbi->select(
139
      'row_id',
140
      table => 'user',
141
      where => {id => $old_ssh_public_key->{user_id}}
142
    )->value;
143
    if (defined $user_row_id) {
144
      $new_ssh_public_key->{user} = $user_row_id;
145
    }
146
    else {
147
      next;
148
    }
149

            
150
    $new_dbi->insert($new_ssh_public_key, table => 'ssh_public_key');
151
  }
152
}
153

            
add collaborator copy logic
Yuki Kimoto authored on 2016-04-21
154
# Copy collaboration data
155
my $old_collaborations = $old_dbi->select(table => 'collaboration')->all;
156
my $new_collaboration_count = $new_dbi->select('count(*)', table => 'collaboration')->value;
157
unless ($new_collaboration_count) {
158
  for my $old_collaboration (@$old_collaborations) {
159
    
160
    my @new_collaboration_columns = qw(
161
      row_id
162
    );
163
    
164
    my $new_collaboration = {};
165
    for my $new_collaboration_column (@new_collaboration_columns) {
166
      if (exists $old_collaboration->{$new_collaboration_column}) {
167
        $new_collaboration->{$new_collaboration_column} = $old_collaboration->{$new_collaboration_column};
168
      }
169
    }
170

            
171
    # collaborator_id is copied to collaborator, which is converted to user.row_id
172
    my $user_row_id = $old_dbi->select(
173
      'row_id',
174
      table => 'user',
175
      where => {id => $old_collaboration->{collaborator_id}}
176
    )->value;
177
    if (defined $user_row_id) {
fix collaboration
Yuki Kimoto authored on 2016-04-22
178
      $new_collaboration->{user} = $user_row_id;
add collaborator copy logic
Yuki Kimoto authored on 2016-04-21
179
    }
180
    else {
181
      next;
182
    }
183
    
184
    my $project_row_id = $old_dbi->select(
185
      'row_id',
186
      table => 'project',
187
      where => {name => $old_collaboration->{project_name}}
188
    )->value;
189
    if (defined $project_row_id) {
190
      $new_collaboration->{project} = $project_row_id;
191
    }
192
    else {
193
      next;
194
    }
195
    
196
    $new_dbi->insert($new_collaboration, table => 'collaboration');
197
  }
198
}
199

            
fix setup_database
Yuki Kimoto authored on 2016-04-21
200
=pod
201
collaboration
202
=cut