|
| 1 | +#!/usr/bin/perl |
| 2 | +use lib 'lib'; |
| 3 | + |
| 4 | +BEGIN { |
| 5 | + |
| 6 | +package OpenNewsWire::DB::Script; |
| 7 | + |
| 8 | +use OpenNewsWire::DB; |
| 9 | +use DBIx::Class::DeploymentHandler; |
| 10 | + |
| 11 | +use Moose; |
| 12 | +use MooseX::AttributeShortcuts; |
| 13 | + |
| 14 | +with 'MooseX::Getopt'; |
| 15 | + |
| 16 | +has connection_name => ( |
| 17 | + traits => [ 'Getopt' ], |
| 18 | + is => 'ro', |
| 19 | + isa => 'Str', |
| 20 | + required => 1, |
| 21 | + cmd_aliases => 'c', |
| 22 | + default => sub { 'ONW_DB' }, |
| 23 | +); |
| 24 | + |
| 25 | +has force => ( |
| 26 | + is => 'ro', isa => 'Bool', default => sub { 0 } |
| 27 | +); |
| 28 | + |
| 29 | +has _dh => (is => 'lazy'); |
| 30 | + |
| 31 | +sub _build__dh { |
| 32 | + my ($self) = @_; |
| 33 | + print $self->connection_name; |
| 34 | + DBIx::Class::DeploymentHandler->new({ |
| 35 | + schema => OpenNewsWire::DB->connect($self->connection_name), |
| 36 | + force_overwrite => $self->force, |
| 37 | + script_directory => 'share/ddl', |
| 38 | + databases => [ 'PostgreSQL' ], |
| 39 | + }); |
| 40 | +} |
| 41 | + |
| 42 | +sub cmd_write_ddl { |
| 43 | + my ($self) = @_; |
| 44 | + |
| 45 | + $self->_dh->prepare_install; |
| 46 | + |
| 47 | + my $v = $self->_dh->schema_version; |
| 48 | + |
| 49 | + if ($v > 1) { |
| 50 | + $self->_dh->prepare_upgrade({ from_version => $v-1, to_version => $v}); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +sub cmd_install { |
| 55 | + my ($self) = @_; |
| 56 | + |
| 57 | + $self->_dh->install; |
| 58 | + |
| 59 | + $self->_dh->schema->install_defaults; |
| 60 | +} |
| 61 | + |
| 62 | +sub cmd_upgrade { |
| 63 | + shift->_dh->upgrade; |
| 64 | +} |
| 65 | + |
| 66 | +sub run { |
| 67 | + my ($self) = @_; |
| 68 | + |
| 69 | + my ($cmd, @what) = @{$self->extra_argv}; |
| 70 | + |
| 71 | + die "Must supply a command\n" unless $cmd; |
| 72 | + die "Extra argv detected - command only please\n" if @what; |
| 73 | + die "No such command ${cmd}\n" unless $self->can("cmd_${cmd}"); |
| 74 | + |
| 75 | + $self->${\"cmd_${cmd}"}; |
| 76 | +} |
| 77 | + |
| 78 | +} # end of BEGIN block |
| 79 | + |
| 80 | +OpenNewsWire::DB::Script->new_with_options->run; |
| 81 | + |
| 82 | +=head1 NOTES |
| 83 | +
|
| 84 | +
|
| 85 | +
|
| 86 | +
|
| 87 | +=cut |
0 commit comments