-
Notifications
You must be signed in to change notification settings - Fork 84
/
db_migrate.pl
72 lines (61 loc) · 2.52 KB
/
db_migrate.pl
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/perl -w
# Reality schema migration utility
# forked from Bliss by ayan4m1, updated by Thevisad
use Getopt::Long;
use File::Basename;
use DBIx::Migration::Directories;
use DBIx::Transaction;
use DBI;
our %args;
GetOptions(
\%args,
'hostname|host|h=s',
'username|user|u=s',
'password|pass|p=s',
'database|name|dbname|d=s',
'port=s',
'schema|s=s',
'version|v=s',
'check',
'help'
);
my %db = (
'host' => $args{'hostname'} ? $args{'hostname'} : 'localhost',
'user' => $args{'username'} ? $args{'username'} : 'dayz',
'pass' => $args{'password'} ? $args{'password'} : 'dayz',
'name' => $args{'database'} ? $args{'database'} : 'dayz',
'port' => $args{'port'} ? $args{'port'} : '3306'
);
if ($args{'help'}) {
print "usage: db_migrate.pl [--host <hostname>] [--user <username>] [--pass <password>] [--name <dbname>] [--port <port>] [--schema <schema>] [--version <version>]\n";
print "\n";
print "Use --schema to specify an optional schema (the name should be a directory in schema\\) which has custom schema changes to apply instead of the default \"Reality\" schema.\n";
print "If you specify --schema, you must specify a --version for that schema as well. The starting schema version is always 0.01 for official optional schemas.\n";
exit;
}
die "FATAL: Schema version must be specified for non-standard schema!\n" if ($args{'schema'} && !defined $args{'version'});
my $schema = $args{'schema'} ? $args{'schema'} : "Reality";
my $version = $args{'version'} ? $args{'version'} : "0.46";
print "INFO: Trying to connect to $db{'host'}, database $db{'name'} as $db{'user'}\n";
my $dbh = DBIx::Transaction->connect(
"dbi:mysql:$db{'name'}:$db{'host'}:$db{'port'}",
$db{'user'},
$db{'pass'}
) or die "FATAL: Could not connect to MySQL - " . DBI->errstr . "\n";
my $m = DBIx::Migration::Directories->new(
base => dirname(__FILE__) . '/schema',
schema => $schema,
desired_version => $version,
dbh => $dbh
);
my $cur_version = $m->get_current_version();
if ($cur_version) {
printf("INFO: Current $schema version is %.2f\n", $cur_version);
} else {
print "INFO: Did not find an existing schema for $schema\n";
}
print "INFO: Latest schema version is $version\n" if (defined $args{'check'});
die "INFO: Exiting\n" if (defined $args{'check'});
print "INFO: Attempting migration to $version\n";
$m->migrate or die "FATAL: Database migration failed!\n";
printf("INFO: Completed the migration from %.2f to version %.2f\n", (($cur_version) ? $cur_version : 0), $m->get_current_version());