spread TODOs out
[dbsrgits/DBIx-Class-DeploymentHandler.git] / lib / DBIx / Class / DeploymentHandler / DeployMethod / SQL / Translator.pm
index 1082ab2..101ae2e 100644 (file)
@@ -22,7 +22,6 @@ has schema => (
   isa      => 'DBIx::Class::Schema',
   is       => 'ro',
   required => 1,
-  handles => [qw( schema_version )],
 );
 
 has storage => (
@@ -62,6 +61,13 @@ has txn_wrap => (
   default => 1,
 );
 
+has schema_version => (
+  is => 'ro',
+  lazy_build => 1,
+);
+
+method _build_schema_version { $self->schema->schema_version }
+
 method __ddl_consume_with_prefix($type, $versions, $prefix) {
   my $base_dir = $self->upgrade_directory;
 
@@ -184,21 +190,22 @@ method _run_sql_and_perl($filenames) {
 
 sub deploy {
   my $self = shift;
+  my $version = shift || $self->schema_version;
 
   return $self->_run_sql_and_perl($self->_ddl_schema_consume_filenames(
     $self->storage->sqlt_type,
-    $self->schema_version
+    $version,
   ));
 }
 
 sub _prepare_install {
-  my $self = shift;
+  my $self      = shift;
   my $sqltargs  = { %{$self->sqltargs}, %{shift @_} };
   my $to_file   = shift;
   my $schema    = $self->schema;
   my $databases = $self->databases;
   my $dir       = $self->upgrade_directory;
-  my $version = $schema->schema_version;
+  my $version   = $self->schema_version;
 
   my $sqlt = SQL::Translator->new({
     add_drop_table          => 1,
@@ -269,7 +276,7 @@ sub prepare_resultsource_install {
     }, $filename);
 }
 
-sub prepare_install {
+sub prepare_deploy {
   my $self = shift;
   $self->_prepare_install({}, '_ddl_schema_produce_filename');
 }
@@ -291,7 +298,7 @@ method _prepare_changegrade($from_version, $to_version, $version_set, $direction
   my $dir       = $self->upgrade_directory;
   my $sqltargs  = $self->sqltargs;
 
-  my $schema_version = $schema->schema_version;
+  my $schema_version = $self->schema_version;
 
   $sqltargs = {
     add_drop_table => 1,
@@ -442,7 +449,91 @@ documented here is extra fun stuff or private methods.
 
 =head1 DIRECTORY LAYOUT
 
-It's heavily based upon L<DBIx::Migration::Directories>.
+Arguably this is the best feature of L<DBIx::Class::DeploymentHandler>.  It's
+heavily based upon L<DBIx::Migration::Directories>, but has some extensions and
+modifications, so even if you are familiar with it, please read this.  I feel
+like the best way to describe the layout is with the following example:
+
+ $sql_migration_dir
+ |- SQLite
+ |  |- down
+ |  |  `- 1-2
+ |  |     `- 001-auto.sql
+ |  |- schema
+ |  |  `- 1
+ |  |     `- 001-auto.sql
+ |  `- up
+ |     |- 1-2
+ |     |  `- 001-auto.sql
+ |     `- 2-3
+ |        `- 001-auto.sql
+ |- _common
+ |  |- down
+ |  |  `- 1-2
+ |  |     `- 002-remove-customers.pl
+ |  `- up
+ |     `- 1-2
+ |        `- 002-generate-customers.pl
+ |- _generic
+ |  |- down
+ |  |  `- 1-2
+ |  |     `- 001-auto.sql
+ |  |- schema
+ |  |  `- 1
+ |  |     `- 001-auto.sql
+ |  `- up
+ |     `- 1-2
+ |        |- 001-auto.sql
+ |        `- 002-create-stored-procedures.sql
+ `- MySQL
+    |- down
+    |  `- 1-2
+    |     `- 001-auto.sql
+    |- schema
+    |  `- 1
+    |     `- 001-auto.sql
+    `- up
+       `- 1-2
+          `- 001-auto.sql
+
+So basically, the code
+
+ $dm->deploy(1)
+
+on an C<SQLite> database that would simply run
+C<$sql_migration_dir/SQLite/schema/1/001-auto.sql>.  Next,
+
+ $dm->upgrade_single_step([1,2])
+
+would run C<$sql_migration_dir/SQLite/up/1-2/001-auto.sql> followed by
+C<$sql_migration_dir/_common/up/1-2/002-generate-customers.pl>.
+
+Now, a C<.pl> file doesn't have to be in the C<_common> directory, but most of
+the time it probably should be, since perl scripts will mostly be database
+independent.
+
+C<_generic> exists for when you for some reason are sure that your SQL is
+generic enough to run on all databases.  Good luck with that one.
+
+=head1 PERL SCRIPTS
+
+A perl script for this tool is very simple.  It merely needs to contain a
+sub called C<run> that takes a L<DBIx::Class::Schema> as it's only argument.
+A very basic perl script might look like:
+
+ #!perl
+
+ use strict;
+ use warnings;
+
+ sub run {
+   my $schema = shift;
+
+   $schema->resultset('Users')->create({
+     name => 'root',
+     password => 'root',
+   })
+ }
 
 =attr schema
 
@@ -456,7 +547,8 @@ and generate the DDL.  This is automatically created with L</_build_storage>.
 
 =attr sqltargs
 
-#rename
+TODO
+# rename
 
 =attr upgrade_directory
 
@@ -472,6 +564,11 @@ generate files for
 Set to true (which is the default) to wrap all upgrades and deploys in a single
 transaction.
 
+=attr schema_version
+
+The version the schema on your harddrive is at.  Defaults to
+C<< $self->schema->schema_version >>.
+
 =method __ddl_consume_with_prefix
 
  $dm->__ddl_consume_with_prefix( 'SQLite', [qw( 1.00 1.01 )], 'up' )