/
Sqitch.pm
333 lines (237 loc) · 7.75 KB
/
Sqitch.pm
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package App::Sqitch;
use v5.10;
use strict;
use warnings;
use utf8;
use Getopt::Long;
use Hash::Merge qw(merge);
use Path::Class;
use Config;
use App::Sqitch::Config;
use App::Sqitch::Command;
use Moose;
use Moose::Util::TypeConstraints;
use namespace::autoclean;
our $VERSION = '0.11';
has plan_file => (is => 'ro', required => 1, default => sub {
file 'sqitch.plan';
});
has _engine => (is => 'ro', isa => enum [qw(pg mysql sqlite)]);
has engine => (is => 'ro', isa => 'Maybe[App::Sqitch::Engine]', lazy => 1, default => sub {
my $self = shift;
my $name = $self->_engine or return;
require App::Sqitch::Engine;
App::Sqitch::Engine->load({sqitch => $self, engine => $name});
});
has client => (is => 'ro', isa => 'Str');
has db_name => (is => 'ro', isa => 'Str', required => 1, lazy => 1, default => sub {
$ENV{SQITCH_DBNAME} // shift->username
});
has username => (is => 'ro', isa => 'Str', required => 1, lazy => 1, default => sub {
$ENV{SQITCH_USER} // $ENV{USER}
});
has host => (is => 'ro', isa => 'Maybe[Str]', lazy => 1, default => sub {
$ENV{SQITCH_HOST};
});
has port => (is => 'ro', isa => 'Maybe[Int]', lazy => 1, default => sub {
$ENV{SQITCH_PORT};
});
has sql_dir => (is => 'ro', required => 1, lazy => 1, default => sub { dir 'sql' });
has deploy_dir => (is => 'ro', required => 1, lazy => 1, default => sub {
shift->sql_dir->subdir('deploy');
});
has revert_dir => (is => 'ro', required => 1, lazy => 1, default => sub {
shift->sql_dir->subdir('revert');
});
has test_dir => (is => 'ro', required => 1, lazy => 1, default => sub {
shift->sql_dir->subdir('test');
});
has extension => (is => 'ro', isa => 'Str', default => 'sql');
has dry_run => (is => 'ro', isa => 'Bool', required => 1, default => 0);
has verbosity => (is => 'ro', required => 1, default => 1);
has config => (is => 'ro', isa => 'App::Sqitch::Config', lazy => 1, default => sub {
App::Sqitch::Config->new
});
has editor => (is => 'ro', lazy => 1, default => sub {
return $ENV{SQITCH_EDITOR} || $ENV{EDITOR} || (
$^O eq 'MSWin32' ? 'notepad.exe' : 'vi'
);
});
sub go {
my $class = shift;
# 1. Split command and options.
my ($core_args, $cmd, $cmd_args) = $class->_split_args(@ARGV);
# 2. Parse core options.
my $core_opts = $class->_parse_core_opts($core_args);
# 3. Load config.
my $config = App::Sqitch::Config->new;
# 4. Instantiate Sqitch.
my $params = merge $core_opts, $config->get_section(section => 'core');
$params->{_engine} = delete $params->{engine};
my $sqitch = $class->new($params);
$sqitch->{config} = $config;
# 5. Instantiate the command object.
my $command = App::Sqitch::Command->load({
sqitch => $sqitch,
command => $cmd,
config => $config,
args => $cmd_args,
});
# 6. Execute command.
return $command->execute(@{ $cmd_args }) ? 0 : 2;
}
sub _core_opts {
return qw(
plan-file=s
engine=s
client=s
db-name|d=s
username|user|u=s
host=s
port=i
sql-dir=s
deploy-dir=s
revert-dir=s
test-dir=s
extension=s
dry-run
quiet
verbose+
help
man
version
);
}
sub _split_args {
my ($self, @args) = @_;
my $cmd_at = 0;
my $add_one = sub { $cmd_at++ };
my $add_two = sub { $cmd_at += 2 };
Getopt::Long::Configure(qw(bundling));
Getopt::Long::GetOptionsFromArray(
[@args],
# Halt processing on on first non-option, which will be the command.
'<>' => sub { die '!FINISH' },
# Count how many args we've processed until we die.
map { $_ => m/=/ ? $add_two : $add_one } $self->_core_opts
) or $self->_pod2usage;
# Splice the command and its options out of the arguments.
my ($cmd, @cmd_opts) = splice @args, $cmd_at;
return \@args, $cmd, \@cmd_opts;
}
sub _parse_core_opts {
my ($self, $args) = @_;
my %opts;
Getopt::Long::Configure(qw(bundling pass_through));
Getopt::Long::GetOptionsFromArray($args, map {
(my $k = $_) =~ s/[|=+:!].*//;
$k =~ s/-/_/g;
$_ => \$opts{$k};
} $self->_core_opts) or $self->_pod2usage;
# Handle documentation requests.
$self->_pod2usage('-exitval' => 0, '-verbose' => 2) if delete $opts{man};
$self->_pod2usage('-exitval' => 0 ) if delete $opts{help};
# Handle version request.
if (delete $opts{version}) {
require File::Basename;
my $fn = File::Basename::basename($0);
print $fn, ' (', __PACKAGE__, ') ', __PACKAGE__->VERSION, $/;
exit;
}
# Normalize the options (remove undefs) and return.
$opts{verbosity} = delete $opts{verbose};
delete $opts{$_} for grep { !defined $opts{$_} } keys %opts;
return \%opts;
}
sub _pod2usage {
shift;
require Pod::Usage;
Pod::Usage::pod2usage(
'-verbose' => 99,
'-sections' => '(?i:(Synopsis|Usage|Options))',
'-exitval' => 2,
@_
);
}
__PACKAGE__->meta->make_immutable;
no Moose;
__END__
=head1 Name
App::Sqitch - VCS-powered SQL change management
=head1 Synopsis
user App::Sqitch;
exit App::Sqitch->go;
=head1 Description
This module provides the implementation for L<sqitch>. You probably want to
read L<its documentation|sqitch>, or L<the tutorial|sqitchtutorial>. Unless
you want to hack on Sqitch itself, or provide support for a new engine or
L<command|Sqitch::App::Command>. In which case, you will find this API
documentation useful.
=head1 Interface
=head2 Class Methods
=head3 C<go>
App::Sqitch->go;
Called from C<sqitch>, this class method parses command-line options and
arguments in C<@ARGV>, parses the configuration file, constructs an
App::Sqitch object, constructs a command object, and runs it.
=head2 Constructor
=head3 C<new>
my $sqitch = App::Sqitch->new(\%params);
Constructs and returns a new Sqitch object. The supported parameters include:
=over
=item C<plan_file>
=item C<engine>
=item C<client>
=item C<db_name>
=item C<username>
=item C<host>
=item C<port>
=item C<sql_dir>
=item C<deploy_dir>
=item C<revert_dir>
=item C<test_dir>
=item C<extension>
=item C<dry_run>
=item C<editor>
=item C<verbosity>
=back
=head2 Accessors
=head3 C<plan_file>
=head3 C<engine>
=head3 C<client>
=head3 C<db_name>
=head3 C<username>
=head3 C<host>
=head3 C<port>
=head3 C<sql_dir>
=head3 C<deploy_dir>
=head3 C<revert_dir>
=head3 C<test_dir>
=head3 C<extension>
=head3 C<dry_run>
=head3 C<editor>
=head3 C<config>
my $config = $sqitch->config;
Returns the full configuration, combined from the project, user, and system
configuration files.
=head3 C<verbosity>
=head1 Author
David E. Wheeler <david@justatheory.com>
=head1 License
Copyright (c) 2012 iovation Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
=cut