Showing 2 changed files with 66 additions and 25 deletions
+2 -25
lib/Gitprep.pm
... ...
@@ -72,32 +72,9 @@ sub startup {
72 72
     option => {sqlite_unicode => 1}
73 73
   );
74 74
   $self->dbi($dbi);
75
-
76
-  # Create user table
77
-  eval {
78
-    my $sql = <<"EOS";
79
-create table user (
80
-  row_id integer primary key autoincrement,
81
-  id not null unique,
82
-  config not null
83
-);
84
-EOS
85
-    $dbi->execute($sql);
86
-  };
87 75
   
88
-  # Create project table
89
-  eval {
90
-    my $sql = <<"EOS";
91
-create table project (
92
-  row_id integer primary key autoincrement,
93
-  user_id not null,
94
-  name not null,
95
-  config not null,
96
-  unique(user_id, name)
97
-);
98
-EOS
99
-    $dbi->execute($sql);
100
-  };
76
+  # Setup database
77
+  $self->manager->setup_database;
101 78
   
102 79
   # Model
103 80
   my $models = [
+64
lib/Gitprep/RepManager.pm
... ...
@@ -234,6 +234,70 @@ sub rename_project {
234 234
   return 1;
235 235
 }
236 236
 
237
+sub setup_database {
238
+  my $self = shift;
239
+  
240
+  my $dbi = $self->app->dbi;
241
+  
242
+  # Create user table
243
+  eval {
244
+    my $sql = <<"EOS";
245
+create table user (
246
+  row_id integer primary key autoincrement,
247
+  id not null unique,
248
+);
249
+EOS
250
+    $dbi->execute($sql);
251
+  };
252
+
253
+  # Create usert columns
254
+  my $user_columns = [
255
+    "config not null default ''",
256
+    "tab_index not null default ''"
257
+  ];
258
+  for my $column (@$user_columns) {
259
+    eval { $dbi->execute("alter table user add column $column") };
260
+  }
261
+  
262
+  # Check user table
263
+  eval { $dbi->select(['config', 'tab_index'], table => 'user') };
264
+  if ($@) {
265
+    my $error = "Can't create user table properly";
266
+    $self->app->log->error($error);
267
+    croak $error;
268
+  }
269
+  
270
+  # Create project table
271
+  eval {
272
+    my $sql = <<"EOS";
273
+create table project (
274
+  row_id integer primary key autoincrement,
275
+  user_id not null,
276
+  name not null,
277
+  unique(user_id, name)
278
+);
279
+EOS
280
+    $dbi->execute($sql);
281
+  };
282
+  
283
+  # Create Project columns
284
+  my $project_columns = [
285
+    "config not null default ''",
286
+    "tab_index not null default ''"
287
+  ];
288
+  for my $column (@$project_columns) {
289
+    eval { $dbi->execute("alter table project add column $column") };
290
+  }
291
+
292
+  # Check project table
293
+  eval { $dbi->select(['config', 'tab_index'], table => 'project') };
294
+  if ($@) {
295
+    my $error = "Can't create project table properly";
296
+    $self->app->log->error($error);
297
+    croak $error;
298
+  }
299
+}
300
+
237 301
 sub _create_project {
238 302
   my ($self, $user, $project, $new_config) = @_;
239 303
   $new_config ||= {};