From e9e97e466f85eecc1591572cb51eeba4c25e9670 Mon Sep 17 00:00:00 2001
From: Lars Dieckow <ldieckow@sipwise.com>
Date: Tue, 9 Apr 2013 12:22:43 +0200
Subject: [PATCH] allow discarding changes in generated code from the
 command-line, docs for this

---
 Build.PL             |  2 ++
 bin/ngcp-dump-schema | 67 +++++++++++++++++++++++++++++++++++++++++---
 2 files changed, 65 insertions(+), 4 deletions(-)

diff --git a/Build.PL b/Build.PL
index 40517e96..9ad44d50 100644
--- a/Build.PL
+++ b/Build.PL
@@ -8,10 +8,12 @@ my $builder = Module::Build->new(
         'aliased'                     => 0,
         'DBIx::Class::Schema::Loader' => 0,
         'File::Path'                  => 0,
+        'Getopt::Long'                => 0,
         'MooseX::FileAttribute'       => 0,
         'MooseX::NonMoose'            => 0,
         'namespace::sweep'            => 0,
         'Quantum::Superpositions'     => 0,
+        'Pod::Usage'                  => 0,
         'Regexp::Common'              => 0,
         'Regexp::IPv6'                => 0,
         'Sipwise::Base'               => 0,
diff --git a/bin/ngcp-dump-schema b/bin/ngcp-dump-schema
index 2447d764..825bacb1 100644
--- a/bin/ngcp-dump-schema
+++ b/bin/ngcp-dump-schema
@@ -2,17 +2,30 @@
 use Sipwise::Base;
 use DBIx::Class::Schema::Loader qw(make_schema_at);
 use File::Path qw(make_path);
+use Getopt::Long qw(GetOptions);
 use Quantum::Superpositions qw(any);
+use Pod::Usage qw(pod2usage);
 
-my $dump_dir = 'lib';
-make_path $dump_dir;
+my %opt = (
+    dump_directory => 'lib',
+);
+
+GetOptions(\%opt, 'dump_directory:s', 'overwrite_modifications', 'version=s', 'help|?', 'man')
+    or die 'could not process command-line options';
+
+pod2usage(-exitval => 1) if $opt{help};
+pod2usage(-exitval => 0, -verbose => 2) if $opt{man};
+pod2usage unless $opt{version};
+
+make_path $opt{dump_directory};
 
 for my $db (qw(accounting billing carrier kamailio ngcp provisioning sipstats)) {
     make_schema_at(
         "NGCP::Schema::$db",
         {
+            overwrite_modifications => $opt{overwrite_modifications},
             col_collision_map => 'column_%s',
-            dump_directory => $dump_dir,
+            dump_directory => $opt{dump_directory},
             filter_generated_code => sub {
                 my ($type, $class, $text) = @_;
                 my (@source, @pod);
@@ -44,7 +57,7 @@ for my $db (qw(accounting billing carrier kamailio ngcp provisioning sipstats))
                     $package,
                     "use Sipwise::Base;\n",
                     ($package =~ /::Result::/ ? "use MooseX::NonMoose;\n" : ()),
-                    "our \$VERSION = '1.000';\n",
+                    "our \$VERSION = '$opt{version}';\n",
                     @source,
                 );
                 @pod = ("=encoding UTF-8\n\n", @pod, "=cut\n") if @pod;
@@ -59,3 +72,49 @@ for my $db (qw(accounting billing carrier kamailio ngcp provisioning sipstats))
         ],
     );
 }
+
+__END__
+
+=encoding UTF-8
+
+=head1 NAME
+
+ncgp-dump-schema - recreate schema files from live database definition
+
+=head1 SYNOPSIS
+
+    perl bin/ncgp-dump-schema --version='1.000'
+
+=head2 Options
+
+    --dump_directory            output location, default 'lib'
+    --overwrite_modifications   whether to dicard changes in generated code
+    --version                   (required) version for dumped schema classes
+    --help                      brief help message
+    --man                       full documentation
+
+=head1 OPTIONS
+
+=head2 C<--dump_directory>
+
+Directory name for the output, default is F<lib>.
+
+=head2 C<--overwrite_modifications>
+
+(boolean) See L<DBIx::Class::Schema::Loader::Base/overwrite_modifications>,
+default is false. Use with care, since this is a potentially destructive
+operation, only apply to a working copy in a known safe fall-back state.
+
+=head2 C<--version>
+
+(required) Version for the dumped classes. Increase the version when
+incompatible changes are done, e.g. adding a table. See L<Perl::Version> for
+the notion and L<Sipwise::CodingStandards/"versions"> for guidance.
+
+=head2 C<--help>
+
+Print a brief help message and exits.
+
+=head2 C<--man>
+
+Prints the manual page and exits.