Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Rename build/build to up_to_date/auto_update

More consistent and hopefully more clear.
  • Loading branch information...
commit d7bb70e3eafde52498f78c991101a7fbeda3dfdf 1 parent 7ad106b
@rwstauner authored
Showing with 53 additions and 49 deletions.
  1. +41 −37 lib/DBIx/Schema/UpToDate.pm
  2. +7 −7 t/methods.t
  3. +5 −5 xt/author/sqlite.t
View
78 lib/DBIx/Schema/UpToDate.pm
@@ -13,24 +13,24 @@ Options used by the base module:
=for :list
* C<dbh> - A B<d>ataB<b>ase B<h>andle (as returned from C<< DBI->connect >>)
Database commands will be executed against this handle.
-* C<build> - Boolean
-By default L</build> is called at initialization (after being blessed).
+* C<auto_update> - Boolean
+By default L</up_to_date> is called at initialization (just after being blessed).
Set this value to false to disable this if you need to do something else
-before building. You will have to call L</build> yourself.
+before updating. You will have to call L</up_to_date> yourself.
=cut
sub new {
my $class = shift;
my $self = {
- build => 1,
+ auto_update => 1,
@_ == 1 ? %{$_[0]} : @_
};
bless $self, $class;
# make sure the database schema is current
- $self->build()
- if $self->{build};
+ $self->up_to_date()
+ if $self->{auto_update};
return $self;
}
@@ -46,33 +46,6 @@ sub dbh {
return $self->{dbh};
}
-=item build
-
-Builds the database from L</current_version> to L</latest_version>
-performing whatever tasks may be necessary to bring the schema up to date.
-
-=cut
-
-sub build {
- my ($self) = @_;
- my $dbh = $self->dbh;
-
- my $current = $self->current_version;
- if( !defined($current) ){
- $self->initialize_version_table;
- $current = $self->current_version;
- die("Unable to initialize version table\n")
- if !defined($current);
- }
-
- my $latest = $self->latest_version;
-
- # execute each update required to go from current to latest version
- # (starting with next version, obviously (don't redo current))
- $self->update_to_version($_)
- foreach ($current + 1) .. $latest;
-}
-
=item current_version
Determine the current version of the database schema.
@@ -144,8 +117,8 @@ sub set_version {
=item updates
Returns an arrayref of subs (coderefs)
-that can be used to rebuild the database from one version to the next.
-This is used by L</build> to replay a recorded database history
+that can be used to update the database from one version to the next.
+This is used by L</up_to_date> to replay a recorded database history
on the L</dbh> until the database schema is up to date.
=cut
@@ -180,6 +153,35 @@ sub update_to_version {
$dbh->commit();
}
+=item up_to_date
+
+Ensures that the database is up to date.
+If it is not it will apply updates
+after L</current_version> up to L</latest_version>
+to bring the schema up to date.
+
+=cut
+
+sub up_to_date {
+ my ($self) = @_;
+ my $dbh = $self->dbh;
+
+ my $current = $self->current_version;
+ if( !defined($current) ){
+ $self->initialize_version_table;
+ $current = $self->current_version;
+ die("Unable to initialize version table\n")
+ if !defined($current);
+ }
+
+ my $latest = $self->latest_version;
+
+ # execute each update required to go from current to latest version
+ # (starting with next version, obviously (don't redo current))
+ $self->update_to_version($_)
+ foreach ($current + 1) .. $latest;
+}
+
=item version_table_name
The name to use the for the schema version metadata.
@@ -248,11 +250,13 @@ to bring the schema up to date.
The rest of the methods are small in the hopes that you
can overwrite the ones you need to get the customization you require.
-The updates can be run individually (outside of L</build>)
+The updates can be run individually (outside of L</up_to_date>)
for testing your subs...
my $dbh = DBI->connect(@in_memory_database);
- my $schema = DBIx::Schema::UpToDate->new(dbh => $dbh, build => 0);
+ my $schema = DBIx::Schema::UpToDate->new(dbh => $dbh, auto_update => 0);
+
+ # don't forget this:
$schema->initialize_version_table;
$schema->update_to_version(1);
View
14 t/methods.t
@@ -19,7 +19,7 @@ my $dbh = Test::MockObject->new()
->mock(selectcol_arrayref => sub { [$db_ver] })
->mock(table_info => sub { $sth });
-my $schema = new_ok($mod, [dbh => $dbh, build => 0]);
+my $schema = new_ok($mod, [dbh => $dbh, auto_update => 0]);
# current_version
$table_info = [];
@@ -35,30 +35,30 @@ delete $schema->{updates};
# this one's a little silly
is($schema->latest_version, @{ $schema->updates }, 'latest version');
-# build
+# up_to_date()
my $updated = 0;
$db_ver = 0;
$schema->{updates} = [sub { $updated++ }, sub { $updated++ }];
$sth->set_series('fetchall_arrayref', [], [1]);
-$schema->build;
+$schema->up_to_date();
is($updated, 2, 'correct number of updates');
$sth->mock('fetchall_arrayref', sub { [1] });
$updated = 0;
$db_ver = 1;
-$schema->build;
+$schema->up_to_date();
is($updated, 1, 'correct number of updates');
$updated = 0;
$db_ver = 2;
-$schema->build;
+$schema->up_to_date();
is($updated, 0, 'correct number of updates');
$sth->set_series('fetchall_arrayref', [], []);
-is(eval { $schema->build; }, undef, 'build dies w/o current version');
-like($@, qr/version table/, 'build died w/o version table');
+is(eval { $schema->up_to_date(); }, undef, 'up_to_date() dies w/o current version');
+like($@, qr/version table/, 'up_to_date() died w/o version table');
# update_to_version
my $inst = [0, 0];
View
10 xt/author/sqlite.t
@@ -11,7 +11,7 @@ my $mod = 'DBIx::Schema::UpToDate';
eval "require $mod" or die $@;
my $dbh = DBI->connect('dbi:SQLite:dbname=:memory:', undef, undef, {FetchHashKeyName => 'NAME_lc'});
-my $schema = new_ok($mod, [dbh => $dbh, build => 0]);
+my $schema = new_ok($mod, [dbh => $dbh, auto_update => 0]);
# current_version
is($schema->current_version, undef, 'not built');
@@ -22,7 +22,7 @@ is($schema->current_version, 57, 'lying about version');
$schema->dbh->do('DROP TABLE schema_version');
is($schema->current_version, undef, 'not built');
-# build
+# up_to_date()
my $updated = 0;
$schema->{updates} = [
sub {
@@ -36,7 +36,7 @@ $schema->{updates} = [
},
];
-$schema->build;
+$schema->up_to_date();
is($updated, 2, 'correct number of updates');
is($schema->current_version, 2, 'correct current version');
is(@{$schema->dbh->table_info('%', '%', 'goober')->fetchall_arrayref}, 1, 'table created');
@@ -52,7 +52,7 @@ push(@{$schema->{updates}},
}
);
-$schema->build;
+$schema->up_to_date();
is($updated, 1, 'correct number of updates');
is($schema->current_version, 3, 'correct current version');
is_deeply($schema->dbh->selectall_arrayref('SELECT * FROM goober', {Slice => {}}),
@@ -61,7 +61,7 @@ is_deeply($schema->dbh->selectall_arrayref('SELECT * FROM goober', {Slice => {}}
# reset and try again
$schema->dbh->do("DROP TABLE $_") for qw(schema_version goober nut);
$updated = 0;
-$schema->build;
+$schema->up_to_date();
is($updated, 3, 'correct number of updates');
is($schema->current_version, 3, 'correct current version');
is_deeply($schema->dbh->selectall_arrayref('SELECT * FROM goober', {Slice => {}}),
Please sign in to comment.
Something went wrong with that request. Please try again.