initial shot at "dev version" functionality dev-schema
Arthur Axel 'fREW' Schmidt [Thu, 23 Feb 2012 03:46:03 +0000 (21:46 -0600)]
lib/DBIx/Class/DeploymentHandler.pm

index d3f483e..74602ed 100644 (file)
@@ -59,6 +59,85 @@ sub prepare_install {
 sub BUILD { $_[0]->version_storage }
 __PACKAGE__->meta->make_immutable;
 
+sub dev_version_instaled { shift->database_version eq 'DEV' }
+
+use SQL::Translator;
+sub install_dev_version {
+   my ($self, $extra) = @_;
+
+   die 'roll back existing dev version before installing a new dev version'
+      if $self->dev_version_installed;
+
+   my $from_schema = do {
+      my $t = SQL::Translator->new({
+         debug => 0,
+         trace => 0,
+         parser => 'SQL::Translator::Parser::YAML',
+      });
+      $t->translate($self->deploy_method->_ddl_protoschema_produce_filename(
+         $self->database_version, $self->script_directory
+      ) or die $t->error;
+      $t->schema;
+   };
+
+   my $to_schema = do {
+      my $t = SQL::Translator->new({
+         parser => 'SQL::Translator::Parser::DBIx::Class',
+         parser_args => {
+            package => $self->schema,
+         },
+      });
+      $t->translate or die $t->error;
+      $t->schema;
+   };
+
+   my $db = $self->deploy_method->storage->sqlt_type;
+
+   my @up = [SQL::Translator::Diff::schema_diff(
+      $source_schema, $db,
+      $dest_schema,   $db,
+      $sqltargs
+   )];
+
+   my @down = [SQL::Translator::Diff::schema_diff(
+      $dest_schema,   $db,
+      $source_schema, $db,
+      $sqltargs
+   )];
+
+   $self->deploy_method->_run_sql_array(\@up);
+
+   $self->version_storage->version_rs->create({
+      version       => 'DEV',
+      upgrade_sql   => \@up,
+      downgrade_sql => \@down,
+      extra         => $extra,
+   });
+}
+
+sub remove_dev_version {
+   my ($self, $extra) = @_;
+
+   die 'no dev version installed to remove'
+      unless $self->dev_version_installed;
+
+   my ($dev_data, $to_version) = $self->version_storage
+      ->version_rs
+      ->search(undef, {
+         order_by => { -desc => 'id' },
+         rows => 2,
+      })->all;
+
+   $self->deploy_method->_run_sql_array(\@sql_to_run);
+
+   $self->version_storage->version_rs->create({
+      version => $to_version->version,
+      upgrade_sql => \@sql_to_run,
+      downgrade_sql => [], # not meant to be "reverted"
+      extra         => $extra
+   })
+}
+
 1;
 
 #vim: ts=2 sw=2 expandtab