root/trunk/midgard/tools/midrepository/database-upgrade.in

Revision 7101, 1.4 kB (checked in by bergius, 5 years ago)

Initial commit of the MidRepository? workflow extension
Issue number:
Obtained from:
Submitted by:
Reviewed by:

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
Line 
1 #!/usr/bin/perl
2
3 use DBI;
4 use Getopt::Std;
5
6 getopts('td:u:p:U:P:l');
7
8 $database = $opt_d ? $opt_d : "@STAGING_DB@";
9 $user = $opt_u ? $opt_u : "@DB_USER@";
10 $password = $opt_p ? $opt_p : "@DB_USER_PASSWORD@";
11 $root = $opt_U ? $opt_U : '@DB_ADMIN_USER@';
12 $rootpw = $opt_P ? $opt_P : '@DB_ADMIN_PASSWORD@';
13
14 ######
15
16 $driver=0;
17 foreach (DBI->available_drivers) {
18    ($_ eq 'mysql') && ($driver = 1) && last;
19 }
20 if (!$driver) { fail("The MySQL DBD driver is not installed"); }
21
22 $dsn = "DBI:mysql:database=$database";
23 ($dbh = DBI->connect($dsn, $user, $password)) || fail("Failed to connect as $user");
24 ($rdbh = DBI->connect($dsn, $root, $rootpw)) || fail("Failed to connect as $root");
25
26
27 ############ UPGRADE ##############################
28
29 if (!tableexists('article_i')) {
30   `mysql $database < i18n.sql`;
31   `repligard -m -c convertdb.conf`;
32 }
33
34 if (!columnexists('repligard', 'locked')) {
35   `mysql $database < vc.sql`;
36 }
37
38 exit;
39
40
41
42 ################ UTIL ################################
43
44 sub tableexists
45 {
46 my $table = shift;
47
48    foreach ($dbh->tables) {
49       if ($_ eq $table) {
50          return 1;
51       }
52    }
53
54    return 0;
55 }
56
57 sub columnexists
58 {
59 my $table = shift;
60 my $column = shift;
61 my $colname;
62
63 my $sth = $rdbh->prepare("describe $table");
64    ($sth && $sth->execute()) || return 0;
65
66    while (($colname) = $sth->fetchrow_array) {
67       if ($colname eq $column) { return 1; }
68    }
69
70    return 0;
71 }
72
73 sub fail
74 {
75    print STDERR $_[0], "\n";
76    exit($_[1] ? $_[1] : 1);
77 }
Note: See TracBrowser for help on using the browser.