| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Anego::Task::SchemaLoader; | 
| 2 | 1 |  |  | 1 |  | 4 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 32 |  | 
| 3 | 1 |  |  | 1 |  | 3 | use warnings; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 23 |  | 
| 4 | 1 |  |  | 1 |  | 4 | use utf8; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 6 |  | 
| 5 | 1 |  |  | 1 |  | 58 | use Digest::MD5 qw/ md5_hex /; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 67 |  | 
| 6 | 1 |  |  | 1 |  | 5 | use Module::Load; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 8 |  | 
| 7 | 1 |  |  | 1 |  | 848 | use SQL::Translator; | 
|  | 1 |  |  |  |  | 214939 |  | 
|  | 1 |  |  |  |  | 33 |  | 
| 8 |  |  |  |  |  |  |  | 
| 9 | 1 |  |  | 1 |  | 8 | use Anego::Config; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 22 |  | 
| 10 | 1 |  |  | 1 |  | 432 | use Anego::Git; | 
|  | 1 |  |  |  |  | 4 |  | 
|  | 1 |  |  |  |  | 605 |  | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | sub target { | 
| 13 | 0 |  |  | 0 | 0 |  | my $class  = shift; | 
| 14 | 0 |  | 0 |  |  |  | my $method = shift || 'latest'; | 
| 15 | 0 |  |  |  |  |  | my @args   = @_; | 
| 16 |  |  |  |  |  |  |  | 
| 17 | 0 | 0 |  |  |  |  | unless ($class->can($method)) { | 
| 18 | 0 |  |  |  |  |  | errorf("Could not find subcommand: %s\n", $method); | 
| 19 |  |  |  |  |  |  | } | 
| 20 |  |  |  |  |  |  |  | 
| 21 | 0 |  |  |  |  |  | return $class->$method(@args); | 
| 22 |  |  |  |  |  |  | } | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | sub revision { | 
| 25 | 0 |  |  | 0 | 0 |  | my ($class, $revision) = @_; | 
| 26 | 0 |  |  |  |  |  | my $config = Anego::Config->load; | 
| 27 |  |  |  |  |  |  |  | 
| 28 | 0 |  |  |  |  |  | my $schema_class = $config->schema_class; | 
| 29 | 0 |  |  |  |  |  | my $schema_str   = git_cat_file(sprintf('%s:%s', $revision, $config->schema_path)); | 
| 30 | 0 |  |  |  |  |  | $schema_str =~ s/package\s+$schema_class;?//; | 
| 31 |  |  |  |  |  |  |  | 
| 32 | 0 |  |  |  |  |  | my $klass = sprintf('Anego::__ANON__::%s::%s', $revision, md5_hex(int rand 65535)); | 
| 33 | 0 |  |  |  |  |  | eval sprintf <<'__SRC__', $klass, $schema_str; | 
| 34 |  |  |  |  |  |  | package %s { | 
| 35 |  |  |  |  |  |  | %s | 
| 36 |  |  |  |  |  |  | } | 
| 37 |  |  |  |  |  |  | __SRC__ | 
| 38 |  |  |  |  |  |  |  | 
| 39 | 0 |  |  |  |  |  | my $schema = SQL::Translator->new( | 
| 40 |  |  |  |  |  |  | parser => $config->rdbms, | 
| 41 |  |  |  |  |  |  | data   => \$klass->output, | 
| 42 |  |  |  |  |  |  | )->translate; | 
| 43 | 0 |  |  |  |  |  | return _filter($schema); | 
| 44 |  |  |  |  |  |  | } | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | sub latest { | 
| 47 | 0 |  |  | 0 | 0 |  | my ($class) = @_; | 
| 48 | 0 |  |  |  |  |  | my $config = Anego::Config->load; | 
| 49 |  |  |  |  |  |  |  | 
| 50 | 0 |  |  |  |  |  | my $schema_class = $config->schema_class; | 
| 51 | 0 |  |  |  |  |  | Module::Load::load $schema_class; | 
| 52 |  |  |  |  |  |  |  | 
| 53 | 0 |  |  |  |  |  | my $schema = SQL::Translator->new( | 
| 54 |  |  |  |  |  |  | parser => $config->rdbms, | 
| 55 |  |  |  |  |  |  | data   => \$schema_class->output, | 
| 56 |  |  |  |  |  |  | )->translate; | 
| 57 | 0 |  |  |  |  |  | return _filter($schema); | 
| 58 |  |  |  |  |  |  | } | 
| 59 |  |  |  |  |  |  |  | 
| 60 |  |  |  |  |  |  | sub database { | 
| 61 | 0 |  |  | 0 | 0 |  | my ($class) = @_; | 
| 62 | 0 |  |  |  |  |  | my $config = Anego::Config->load; | 
| 63 |  |  |  |  |  |  |  | 
| 64 | 0 |  |  |  |  |  | my $schema = SQL::Translator->new( | 
| 65 |  |  |  |  |  |  | parser      => 'DBI', | 
| 66 |  |  |  |  |  |  | parser_args => { dbh => $config->dbh }, | 
| 67 |  |  |  |  |  |  | )->translate; | 
| 68 | 0 |  |  |  |  |  | return _filter($schema); | 
| 69 |  |  |  |  |  |  | } | 
| 70 |  |  |  |  |  |  |  | 
| 71 |  |  |  |  |  |  | sub _filter { | 
| 72 | 0 |  |  | 0 |  |  | my ($schema) = @_; | 
| 73 | 0 |  |  |  |  |  | my $config = Anego::Config->load; | 
| 74 |  |  |  |  |  |  |  | 
| 75 | 0 | 0 |  |  |  |  | if ($config->rdbms eq 'MySQL') { | 
| 76 | 0 |  |  |  |  |  | for my $table ($schema->get_tables) { | 
| 77 | 0 |  |  |  |  |  | my $options = $table->options; | 
| 78 | 0 | 0 |  |  |  |  | if (my ($idx) = grep { $options->[$_]->{AUTO_INCREMENT} } 0..$#{$options}) { | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
| 79 | 0 |  |  |  |  |  | splice @{ $options }, $idx, 1; | 
|  | 0 |  |  |  |  |  |  | 
| 80 |  |  |  |  |  |  | } | 
| 81 | 0 |  |  |  |  |  | for my $field ($table->get_fields) { | 
| 82 | 0 | 0 | 0 |  |  |  | delete $field->{default_value} if $field->{is_nullable} && exists $field->{default_value} && $field->{default_value} eq 'NULL'; | 
|  |  |  | 0 |  |  |  |  | 
| 83 |  |  |  |  |  |  | } | 
| 84 |  |  |  |  |  |  | } | 
| 85 |  |  |  |  |  |  | } | 
| 86 | 0 |  |  |  |  |  | return $schema; | 
| 87 |  |  |  |  |  |  | } | 
| 88 |  |  |  |  |  |  |  | 
| 89 |  |  |  |  |  |  | 1; |