Bug 25040: monkeypatch Schema::Loader for recent MariaDB
[koha.git] / misc / devel / update_dbix_class_files.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Copyright (C) 2012 ByWater Solutions
6 # Copyright (C) 2013 Equinox Software, Inc.
7 #
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20
21 use Modern::Perl;
22 use DBIx::Class::Schema::Loader qw/ make_schema_at /;
23
24 use Getopt::Long;
25 use Pod::Usage;
26
27 my %db_defaults = (
28     driver => 'mysql',
29     host => 'localhost',
30     port => '3306',
31 );
32
33 my $path = "./";
34 my $db_driver;
35 my $db_host;
36 my $db_port;
37 my $db_name;
38 my $db_user;
39 my $db_passwd;
40 my $koha_conf;
41 my $help;
42
43 GetOptions(
44     "path=s"      => \$path,
45     "db_driver=s" => \$db_driver,
46     "db_host=s"   => \$db_host,
47     "db_port=s"   => \$db_port,
48     "db_name=s"   => \$db_name,
49     "db_user=s"   => \$db_user,
50     "db_passwd=s" => \$db_passwd,
51     "koha-conf:s" => \$koha_conf,
52     "h|help"      => \$help
53 );
54
55 # If we were asked for usage instructions, do it
56 pod2usage(1) if defined $help;
57
58 if (defined $koha_conf) {
59     if ($koha_conf eq '' and not defined $ENV{KOHA_CONF}) {
60         print STDERR "Error: KOHA_CONF is not defined\n";
61         exit(1);
62     }
63
64     $koha_conf ||= $ENV{KOHA_CONF};
65     unless (-r $koha_conf) {
66         print STDERR "Error: File $koha_conf does not exist or is not readable\n";
67         exit(1);
68     }
69
70     require C4::Context;
71     my $context = C4::Context->new($koha_conf);
72     unless ($context) {
73         print STDERR "Error: Koha context creation failed. Please check that $koha_conf is correct\n";
74         exit(1);
75     }
76
77     $context->set_context;
78     $db_defaults{driver} = $context->config('db_scheme');
79     $db_defaults{host} = $context->config('hostname');
80     $db_defaults{port} = $context->config('port');
81     $db_defaults{name} = $context->config('database');
82     $db_defaults{user} = $context->config('user');
83     $db_defaults{passwd} = $context->config('pass');
84 }
85
86 $db_driver //= $db_defaults{driver};
87 $db_host //= $db_defaults{host};
88 $db_port //= $db_defaults{port};
89 $db_name //= $db_defaults{name};
90 $db_user //= $db_defaults{user};
91 $db_passwd //= $db_defaults{passwd};
92
93 if (! defined $db_name ) {
94     print "Error: \'db_name\' parameter is mandatory.\n";
95     pod2usage(1);
96 } else {
97
98     make_schema_at(
99         "Koha::Schema",
100         { debug => 1, dump_directory => $path, preserve_case => 1 },
101         [
102             "DBI:$db_driver:dbname=$db_name;host=$db_host;port=$db_port",
103             $db_user,
104             $db_passwd,
105             { loader_class => 'Koha::Schema::Loader::mysql' }
106         ]
107     );
108 }
109
110 1;
111
112 =head1 NAME
113
114 misc/devel/update_dbix_class_files.pl
115
116 =head1 SYNOPSIS
117
118  update_dbix_class_files.pl [--koha-conf <path>] --db_name=db-name \
119                             --db_user=db-user --db_passwd=db-pass ...
120
121 The command in usually called from the root directory for the Koha source tree.
122 If you are running from another directory, use the --path switch to specify
123 a different path.
124
125 =head1 OPTIONS
126
127 =over 8
128
129 =item B<--koha-conf> <path>
130
131 Path to koha-conf.xml from which DB connection params will be retrieved.
132
133 <path> is optional and defaults to the value of environment variable KOHA_CONF,
134 if set. It is an error to omit the <path> if KOHA_CONF is not set.
135
136 Any B<--db_*> options will override values retrieved from <path>.
137
138 =item B<--db_name>
139
140 DB name. (mandatory)
141
142 =item B<--db_user>
143
144 DB user name.
145
146 =item B<--db_passwd>
147
148 DB password.
149
150 =item B<--db_driver>
151
152 DB driver to be used. (defaults to 'mysql')
153
154 =item B<--db_host>
155
156 hostname for the DB server. (defaults to 'localhost')
157
158 =item B<--db_port>
159
160 port number for the DB server. (defaults to '3306')
161
162 =item B<--path>
163
164 path into which create the schema files. (defaults to './')
165
166 =item B<-h|--help>
167
168 prints this help text
169
170 =back