Commit | Line | Data |
a2c29633 |
1 | #!/usr/bin/env perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
6 | use Module::Runtime 'use_module'; |
7 | use SQL::Translator; |
8 | use Path::Class 'file'; |
9 | use Getopt::Long; |
10 | my $getopt = Getopt::Long::Parser->new( |
11 | config => [qw/gnu_getopt bundling_override no_ignore_case/] |
12 | ); |
13 | my $args = {}; |
14 | $getopt->getoptions($args, qw/ |
15 | ddl-out=s@ |
16 | schema-class=s@ |
6635c6d2 |
17 | deploy-to=s@ |
a2c29633 |
18 | /); |
19 | |
20 | die "You need to specify one DDL output filename via --ddl-out\n" |
21 | if @{$args->{'ddl-out'}||[]} != 1; |
22 | |
23 | die "You need to specify one DBIC schema class via --schema-class\n" |
24 | if @{$args->{'schema-class'}||[]} != 1; |
25 | |
6635c6d2 |
26 | die "You may not specify more than one deploy path via --deploy-to\n" |
27 | if @{$args->{'deploy-to'}||[]} > 1; |
a2c29633 |
28 | |
6635c6d2 |
29 | my $schema = use_module( $args->{'schema-class'}[0] )->connect( |
30 | $args->{'deploy-to'} |
31 | ? ( "DBI:SQLite:$args->{'deploy-to'}[0]", undef, undef, { on_connect_do => "PRAGMA synchronous = OFF" } ) |
32 | : () |
33 | ); |
34 | |
35 | if ($args->{'deploy-to'}) { |
36 | file($args->{'deploy-to'}[0])->dir->mkpath; |
37 | $schema->deploy({ add_drop_table => 1 }); |
38 | } |
a2c29633 |
39 | |
40 | my $ddl_fh; |
41 | if ($args->{'ddl-out'}[0] eq '-') { |
42 | $ddl_fh = *STDOUT; |
43 | } |
44 | else { |
45 | my $fn = file($args->{'ddl-out'}[0]); |
46 | $fn->dir->mkpath; |
47 | open $ddl_fh, '>', $fn |
48 | or die "Unable to open $fn: $!\n"; |
49 | } |
50 | binmode $ddl_fh; # avoid win32 \n crapfest |
51 | |
52 | print $ddl_fh scalar $schema->deployment_statements( |
53 | 'SQLite', |
54 | undef, |
55 | undef, |
56 | { |
57 | producer_args => { no_transaction => 1 }, |
58 | quote_identifiers => 1, |
59 | no_comments => 1, |
60 | }, |
61 | ); |