1 package # hide from PAUSE
2 SQL::Translator::Parser::DBIx::Class;
4 # AUTHOR: Jess Robinson
6 # Some mistakes the fault of Matt S Trout
8 # Others the fault of Ash Berlin
12 use vars qw($DEBUG $VERSION @EXPORT_OK);
13 $DEBUG = 0 unless defined $DEBUG;
14 $VERSION = sprintf "%d.%02d", q$Revision 1.0$ =~ /(\d+)\.(\d+)/;
18 use SQL::Translator::Utils qw(debug normalize_name);
20 use base qw(Exporter);
22 @EXPORT_OK = qw(parse);
24 # -------------------------------------------------------------------
27 # Note that $data, in the case of this parser, is not useful.
28 # We're working with DBIx::Class Schemas, not data streams.
29 # -------------------------------------------------------------------
32 my $args = $tr->parser_args;
33 my $dbixschema = $args->{'DBIx::Schema'} || $data;
34 $dbixschema ||= $args->{'package'};
35 my $limit_sources = $args->{'sources'};
37 die 'No DBIx::Schema' unless ($dbixschema);
38 if (!ref $dbixschema) {
39 eval "use $dbixschema;";
40 die "Can't load $dbixschema ($@)" if($@);
43 my $schema = $tr->schema;
46 # print Dumper($dbixschema->registered_classes);
48 #foreach my $tableclass ($dbixschema->registered_classes)
52 my @monikers = $dbixschema->sources;
54 my $ref = ref $limit_sources || '';
55 die "'sources' parameter must be an array or hash ref" unless $ref eq 'ARRAY' || ref eq 'HASH';
57 # limit monikers to those specified in
59 if ($ref eq 'ARRAY') {
60 $sources->{$_} = 1 for (@$limit_sources);
62 $sources = $limit_sources;
64 @monikers = grep { $sources->{$_} } @monikers;
68 foreach my $moniker (sort @monikers)
70 my $source = $dbixschema->source($moniker);
72 next if $seen_tables{$source->name}++;
74 my $table = $schema->add_table(
75 name => $source->name,
77 ) || die $schema->error;
79 foreach my $col ($source->columns)
81 # assuming column_info in dbic is the same as DBI (?)
82 # data_type is a number, column_type is text?
86 is_auto_increment => 0,
89 %{$source->column_info($col)}
91 if ($colinfo{is_nullable}) {
92 $colinfo{default} = '' unless exists $colinfo{default};
94 my $f = $table->add_field(%colinfo) || die $table->error;
96 $table->primary_key($source->primary_columns);
98 my @primary = $source->primary_columns;
99 my %unique_constraints = $source->unique_constraints;
100 foreach my $uniq (keys %unique_constraints) {
101 if (!$source->compare_relationship_keys($unique_constraints{$uniq}, \@primary)) {
102 $table->add_constraint(
105 fields => $unique_constraints{$uniq}
110 my @rels = $source->relationships();
114 foreach my $rel (sort @rels)
116 my $rel_info = $source->relationship_info($rel);
118 # Ignore any rel cond that isn't a straight hash
119 next unless ref $rel_info->{cond} eq 'HASH';
121 my $othertable = $source->related_source($rel);
122 my $rel_table = $othertable->name;
124 # Get the key information, mapping off the foreign/self markers
125 my @cond = keys(%{$rel_info->{cond}});
126 my @refkeys = map {/^\w+\.(\w+)$/} @cond;
127 my @keys = map {$rel_info->{cond}->{$_} =~ /^\w+\.(\w+)$/} @cond;
131 my $reverse_rels = $source->reverse_relationship_info($rel);
132 my ($otherrelname, $otherrelationship) = each %{$reverse_rels};
137 if (defined $otherrelationship) {
138 $on_delete = $otherrelationship->{'attrs'}->{cascade_delete} ? 'CASCADE' : '';
139 $on_update = $otherrelationship->{'attrs'}->{cascade_copy} ? 'CASCADE' : '';
142 my $is_deferrable = $rel_info->{attrs}{is_deferrable} || 0;
144 # Make sure we dont create the same foreign key constraint twice
145 my $key_test = join("\x00", @keys);
147 #Decide if this is a foreign key based on whether the self
148 #items are our primary columns.
150 # If the sets are different, then we assume it's a foreign key from
151 # us to another table.
152 # OR: If is_foreign_key_constraint attr is explicity set (or set to false) on the relation
153 if ( ! exists $created_FK_rels{$rel_table}->{$key_test} &&
154 ( exists $rel_info->{attrs}{is_foreign_key_constraint} ?
155 $rel_info->{attrs}{is_foreign_key_constraint} :
156 !$source->compare_relationship_keys(\@keys, \@primary)
160 $created_FK_rels{$rel_table}->{$key_test} = 1;
161 $table->add_constraint(
162 type => 'foreign_key',
163 name => "fk_$keys[0]",
165 reference_fields => \@refkeys,
166 reference_table => $rel_table,
167 on_delete => $on_delete,
168 on_update => $on_update,
169 deferrable => $is_deferrable,
175 if ($source->result_class->can('sqlt_deploy_hook')) {
176 $source->result_class->sqlt_deploy_hook($table);
180 if ($dbixschema->can('sqlt_deploy_hook')) {
181 $dbixschema->sqlt_deploy_hook($schema);