use Digest::MD5 qw//;
require DBIx::Class;
-our $VERSION = '0.03999_01';
+our $VERSION = '0.04002';
__PACKAGE__->mk_ro_accessors(qw/
schema
debug
dump_directory
dump_overwrite
+ really_erase_my_files
db_schema
_tables
=head2 dump_overwrite
+Deprecated. See L</really_erase_my_files> below, which does *not* mean
+the same thing as the old C<dump_overwrite> setting from previous releases.
+
+=head2 really_erase_my_files
+
Default false. If true, Loader will unconditionally delete any existing
files before creating the new ones from scratch when dumping a schema to disk.
C<# DO NOT MODIFY THIS OR ANYTHING ABOVE!>
leaving any customizations you placed after that as they were.
-When C<dump_overwrite> is not set, if the output file already exists,
+When C<really_erase_my_files> is not set, if the output file already exists,
but the aforementioned final stanza is not found, or the checksum
contained there does not match the generated contents, Loader will
croak and not touch the file.
+You should really be using version control on your schema classes (and all
+of the rest of your code for that matter). Don't blame me if a bug in this
+code wipes something out when it shouldn't have, you've been warned.
+
=head1 METHODS
None of these methods are intended for direct invocation by regular
bless $self => $class;
- $self->{db_schema} ||= '';
$self->_ensure_arrayref(qw/additional_classes
additional_base_classes
left_base_classes
$self->{schema_class} ||= ( ref $self->{schema} || $self->{schema} );
$self->{schema} ||= $self->{schema_class};
+ croak "dump_overwrite is deprecated. Please read the"
+ . " DBIx::Class::Schema::Loader::Base documentation"
+ if $self->{dump_overwrite};
+
+ $self->{relbuilder} = DBIx::Class::Schema::Loader::RelBuilder->new(
+ $self->schema_class, $self->inflect_plural, $self->inflect_singular
+ ) if !$self->{skip_relationships};
+
$self;
}
$self->_ext_stmt($class, $_);
}
$self->_ext_stmt($class,
- q|# End of lines loaded from '$real_inc_path' |
+ qq|# End of lines loaded from '$real_inc_path' |
);
close($fh)
or croak "Failed to close $real_inc_path: $!";
sub load {
my $self = shift;
+ $self->_load_tables($self->_tables_list);
+}
+
+=head2 rescan
+
+Arguments: schema
+
+Rescan the database for newly added tables. Does
+not process drops or changes. Returns a list of
+the newly added table monikers.
+
+The schema argument should be the schema class
+or object to be affected. It should probably
+be derived from the original schema_class used
+during L</load>.
+
+=cut
+
+sub rescan {
+ my ($self, $schema) = @_;
+
+ $self->{schema} = $schema;
+
+ my @created;
+ my @current = $self->_tables_list;
+ foreach my $table ($self->_tables_list) {
+ if(!exists $self->{_tables}->{$table}) {
+ push(@created, $table);
+ }
+ }
+
+ my $loaded = $self->_load_tables(@created);
+
+ return map { $self->monikers->{$_} } @$loaded;
+}
+
+sub _load_tables {
+ my ($self, @tables) = @_;
+
# First, use _tables_list with constraint and exclude
# to get a list of tables to operate on
my $constraint = $self->constraint;
my $exclude = $self->exclude;
- my @tables = sort $self->_tables_list;
- if(!@tables) {
- warn "No tables found in database, nothing to load";
- }
- else {
- @tables = grep { /$constraint/ } @tables if $constraint;
- @tables = grep { ! /$exclude/ } @tables if $exclude;
+ @tables = grep { /$constraint/ } @tables if $constraint;
+ @tables = grep { ! /$exclude/ } @tables if $exclude;
- warn "All tables excluded by constraint/exclude, nothing to load"
- if !@tables;
+ # Save the new tables to the tables list
+ foreach (@tables) {
+ $self->{_tables}->{$_} = 1;
}
- # Save the tables list
- $self->{_tables} = \@tables;
-
# Set up classes/monikers
{
no warnings 'redefine';
$self->_setup_src_meta($_) for @tables;
- $self->_load_relationships if ! $self->skip_relationships;
+ if(!$self->skip_relationships) {
+ $self->_load_relationships($_) for @tables;
+ }
+
$self->_load_external($_)
- for ($self->schema_class, values %{$self->classes});
+ for map { $self->classes->{$_} } @tables;
$self->_dump_to_dir if $self->dump_directory;
# Drop temporary cache
delete $self->{_cache};
- 1;
+ return \@tables;
}
sub _get_dump_filename {
my $filename = $self->_get_dump_filename($class);
$self->_ensure_dump_subdirs($class);
- if (-f $filename && $self->dump_overwrite) {
+ if (-f $filename && $self->really_erase_my_files) {
warn "Deleting existing file '$filename' due to "
- . "'dump_overwrite' setting\n";
+ . "'really_erase_my_files' setting\n";
unlink($filename);
}
}
}
- croak "Cannot not overwrite '$filename' without 'dump_overwrite',"
+ croak "Cannot not overwrite '$filename' without 'really_erase_my_files',"
. " it does not appear to have been generated by Loader"
if !$found;
$self->_use ($table_class, @{$self->additional_classes});
$self->_inject($table_class, @{$self->additional_base_classes});
- $self->_dbic_stmt($table_class, 'load_components', @{$self->components}, qw/PK::Auto Core/);
+ $self->_dbic_stmt($table_class, 'load_components', @{$self->components}, 'Core');
$self->_dbic_stmt($table_class, 'load_resultset_components', @{$self->resultset_components})
if @{$self->resultset_components};
sub tables {
my $self = shift;
- return @{$self->_tables};
+ return keys %{$self->_tables};
}
# Make a moniker from a table
}
sub _load_relationships {
- my $self = shift;
+ my ($self, $table) = @_;
- # Construct the fk_info RelBuilder wants to see, by
- # translating table names to monikers in the _fk_info output
- my %fk_info;
- foreach my $table ($self->tables) {
- my $tbl_fk_info = $self->_table_fk_info($table);
- foreach my $fkdef (@$tbl_fk_info) {
- $fkdef->{remote_source} =
- $self->monikers->{delete $fkdef->{remote_table}};
- }
- my $moniker = $self->monikers->{$table};
- $fk_info{$moniker} = $tbl_fk_info;
+ my $tbl_fk_info = $self->_table_fk_info($table);
+ foreach my $fkdef (@$tbl_fk_info) {
+ $fkdef->{remote_source} =
+ $self->monikers->{delete $fkdef->{remote_table}};
}
- my $relbuilder = DBIx::Class::Schema::Loader::RelBuilder->new(
- $self->schema_class, \%fk_info, $self->inflect_plural,
- $self->inflect_singular
- );
+ my $local_moniker = $self->monikers->{$table};
+ my $rel_stmts = $self->{relbuilder}->generate_code($local_moniker, $tbl_fk_info);
- my $rel_stmts = $relbuilder->generate_code;
foreach my $src_class (sort keys %$rel_stmts) {
my $src_stmts = $rel_stmts->{$src_class};
foreach my $stmt (@$src_stmts) {