Skip to content

Commit

Permalink
Renamed this package Module::Install::DiffCheck. It's working! woot!
Browse files Browse the repository at this point in the history
  • Loading branch information
jhannah committed Oct 9, 2009
1 parent 9a1a437 commit 2200fe1
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 78 deletions.
2 changes: 1 addition & 1 deletion Changes
@@ -1,4 +1,4 @@
Revision history for Module-Install-SchemaCheck
Revision history for Module-Install-DiffCheck

0.01 Date/time
First version, released on an unsuspecting world.
Expand Down
8 changes: 4 additions & 4 deletions Makefile.PL
Expand Up @@ -3,16 +3,16 @@ use warnings;
use ExtUtils::MakeMaker;

WriteMakefile(
NAME => 'Module::Install::SchemaCheck',
NAME => 'Module::Install::DiffCheck',
AUTHOR => 'Jay Hannah <jay@jays.net>',
VERSION_FROM => 'lib/Module/Install/SchemaCheck.pm',
ABSTRACT_FROM => 'lib/Module/Install/SchemaCheck.pm',
VERSION_FROM => 'lib/Module/Install/DiffCheck.pm',
ABSTRACT_FROM => 'lib/Module/Install/DiffCheck.pm',
PL_FILES => {},
PREREQ_PM => {
'Test::More' => 0,
'Module::Install' => 0,
'Text::Diff::Parser' => 0,
},
dist => { COMPRESS => 'gzip -9f', SUFFIX => 'gz', },
clean => { FILES => 'Module-Install-SchemaCheck-*' },
clean => { FILES => 'Module-Install-DiffCheck-*' },
);
12 changes: 6 additions & 6 deletions README
@@ -1,4 +1,4 @@
Module-Install-SchemaCheck
Module-Install-DiffCheck

Checks your actual database schema against your expected meta-data.

Expand All @@ -18,21 +18,21 @@ SUPPORT AND DOCUMENTATION
After installing, you can find documentation for this module with the
perldoc command.

perldoc Module::Install::SchemaCheck
perldoc Module::Install::DiffCheck

You can also look for information at:

RT, CPAN's request tracker
http://rt.cpan.org/NoAuth/Bugs.html?Dist=Module-Install-SchemaCheck
http://rt.cpan.org/NoAuth/Bugs.html?Dist=Module-Install-DiffCheck

AnnoCPAN, Annotated CPAN documentation
http://annocpan.org/dist/Module-Install-SchemaCheck
http://annocpan.org/dist/Module-Install-DiffCheck

CPAN Ratings
http://cpanratings.perl.org/d/Module-Install-SchemaCheck
http://cpanratings.perl.org/d/Module-Install-DiffCheck

Search CPAN
http://search.cpan.org/dist/Module-Install-SchemaCheck
http://search.cpan.org/dist/Module-Install-DiffCheck

Version control
http://github.com/jhannah/module-install-schemacheck
Expand Down
144 changes: 80 additions & 64 deletions lib/Module/Install/SchemaCheck.pm → lib/Module/Install/DiffCheck.pm
@@ -1,20 +1,31 @@
package Module::Install::SchemaCheck;
package Module::Install::DiffCheck;

=head1 NAME
Module::Install::SchemaCheck - Verify that a database schema meets expectations
Module::Install::DiffCheck - Run diff commands for potential trouble
=head1 SYNOPSIS
Add statements like these to your Module::Install generated Makefile.PL:
schemacheck(
refresher => 'Model/omnihub/refresh_Schema.pl',
diff_cmd => 'svn diff Model/omnihub/Schema',
diffcheck(
before_diff_commands => [
'Model/refresh_Schema.pl',
'Model/mysqldump.pl root SuperSecret',
],
diff_commands => [
'svn diff Model',
],
ignore_lines => [
qr/ *#/, # Ignore comments
qr/^\-\-/, # Ignore comments
qr/AUTO_INCREMENT/, # These change all the time
],
);
That's it. C<refresher> is executed, then C<diff_cmd>
is executed and any non-benign changes found cause a fatal error.
That's it. Each C<before_diff_commands> is executed, then each C<diff_commands>
is executed and any lines that don't match an C<ignore_lines> regex cause
a fatal error.
=head1 DESCRIPTION
Expand Down Expand Up @@ -77,16 +88,16 @@ require Module::Install::Base;
our $VERSION = '0.01';


=head2 schemacheck
=head2 diffheck
See SYNOPSIS above.
=cut

sub schemacheck {
sub diffcheck {
my ($self, %args) = @_;
print <<EOF;
*** Module::Install::SchemaCheck
*** Module::Install::DiffCheck
EOF

unless ($args{diff_cmd}) {
Expand All @@ -95,77 +106,82 @@ EOF

my $fatal = 0;
if ($args{refresher}) {
$fatal += $self->_run_refresher(\%args);
$fatal += $self->_run_before_diff_commands(\%args);
}
$fatal += $self->_run_diff(\%args);
$fatal += $self->_run_diff_commands(\%args);

if ($fatal) {
print "*** Module::Install::SchemaCheck FATAL ERRORS\n";
print "*** Module::Install::DiffCheck FATAL ERRORS\n";
exit $fatal;
}

print <<EOF;
*** Module::Install::SchemaCheck finished.
*** Module::Install::DiffCheck finished.
EOF

return 1; # Does Module::Install care?
}


sub _run_refresher {
sub _run_before_diff_commands {
my ($self, $args) = @_;

my $fatal = 0;
my $cmd = $args->{refresher};
print "running '$cmd'\n";
open(my $in, "$cmd 2>&1 |");
while (<$in>) {
chomp;
print " $_\n";
# $fatal++; # hmm...
foreach my $cmd (@{$args->{before_diff_commands}}) {
print "running '$cmd'\n";
open(my $in, "$cmd 2>&1 |");
while (<$in>) {
chomp;
print " $_\n";
# $fatal++; # hmm...
}
close $in;
}
close $in;
return $fatal;
}


sub _run_diff {
sub _run_diff_commands {
my ($self, $args) = @_;

my $cmd = $args->{diff_cmd};
print "running '$cmd'\n";
my $diff = `$cmd`;

my $parser = Text::Diff::Parser->new(
Simplify => 1,
Diff => $diff,
Verbose => 1,
);

my $fatal = 0;
foreach my $change ( $parser->changes ) {
next unless ($change->type); # How do blanks get in here?
my $msg = sprintf(
" SCHEMA CHANGE DETECTED! %s %s %s line(s) at lines %s/%s:\n",
$change->filename1,
$change->type,
$change->size,
$change->line1,
$change->line2,

foreach my $cmd (@{$args->{diff_commands}}) {
print "running '$cmd'\n";
my $diff = `$cmd`;

my $parser = Text::Diff::Parser->new(
Simplify => 1,
Diff => $diff,
# Verbose => 1,
);
my $size = $change->size;
my $show_change = 0;

foreach my $line ( 0..($size-1) ) {
# Huh... Only the new is available. Not the old?
$msg .= sprintf(" [%s]\n", $change->text( $line ));
next if ($change->text( $line ) =~ / *#/); # Ignore comment changes
$show_change = 1;
$fatal = 1;
}
if ($show_change) {
# Hmm... It would be nice if we could just kick out the unidiff here. I emailed the author.
print $msg;
my $fatal = 0;
foreach my $change ( $parser->changes ) {
next unless ($change->type); # How do blanks get in here?
my $msg = sprintf(
" SCHEMA CHANGE DETECTED! %s %s %s line(s) at lines %s/%s:\n",
$change->filename1,
$change->type,
$change->size,
$change->line1,
$change->line2,
);
my $size = $change->size;
my $show_change = 0;

LINE:
foreach my $line ( 0..($size-1) ) {
# Huh... Only the new is available. Not the old?
foreach my $i (@{$args->{ignore_lines}}) {
next LINE if ($change->text( $line ) =~ $i);
}
$msg .= sprintf(" [%s]\n", $change->text( $line ));
$show_change = 1;
$fatal = 1;
}
if ($show_change) {
# Hmm... It would be nice if we could just kick out the unidiff here. I emailed the author.
print $msg;
}
}
}
return $fatal;
Expand All @@ -186,34 +202,34 @@ That bug stops C<mysqldump> diffs from being processed currectly.
So, for now I'm only using this against L<DBIx::Class::Schema::Loader> schemas.
Please report any bugs or feature requests to C<bug-module-install-schemacheck at rt.cpan.org>, or through
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Module-Install-SchemaCheck>. I will be notified, and then you'll
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Module-Install-DiffCheck>. I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.
=head1 SUPPORT
You can find documentation for this module with the perldoc command.
perldoc Module::Install::SchemaCheck
perldoc Module::Install::DiffCheck
You can also look for information at:
=over 4
=item * RT: CPAN's request tracker
L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Module-Install-SchemaCheck>
L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Module-Install-DiffCheck>
=item * AnnoCPAN: Annotated CPAN documentation
L<http://annocpan.org/dist/Module-Install-SchemaCheck>
L<http://annocpan.org/dist/Module-Install-DiffCheck>
=item * CPAN Ratings
L<http://cpanratings.perl.org/d/Module-Install-SchemaCheck>
L<http://cpanratings.perl.org/d/Module-Install-DiffCheck>
=item * Search CPAN
L<http://search.cpan.org/dist/Module-Install-SchemaCheck>
L<http://search.cpan.org/dist/Module-Install-DiffCheck>
=item * Version control
Expand All @@ -228,5 +244,5 @@ Copyright 2009 Jay Hannah, all rights reserved.
=cut

1; # End of Module::Install::SchemaCheck
1; # End of Module::Install::DiffCheck

4 changes: 2 additions & 2 deletions t/00-load.t
Expand Up @@ -3,7 +3,7 @@
use Test::More tests => 1;

BEGIN {
use_ok( 'Module::Install::SchemaCheck' );
use_ok( 'Module::Install::DiffCheck' );
}

diag( "Testing Module::Install::SchemaCheck $Module::Install::SchemaCheck::VERSION, Perl $], $^X" );
diag( "Testing Module::Install::DiffCheck $Module::Install::DiffCheck::VERSION, Perl $], $^X" );
2 changes: 1 addition & 1 deletion t/boilerplate.t
Expand Up @@ -48,7 +48,7 @@ TODO: {
"placeholder date/time" => qr(Date/time)
);

module_boilerplate_ok('lib/Module/Install/SchemaCheck.pm');
module_boilerplate_ok('lib/Module/Install/DiffCheck.pm');


}
Expand Down

0 comments on commit 2200fe1

Please sign in to comment.