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 @EXPORT_OK);
13 $DEBUG = 0 unless defined $DEBUG;
17 use SQL::Translator::Utils qw(debug normalize_name);
19 use base qw(Exporter);
21 @EXPORT_OK = qw(parse);
23 # -------------------------------------------------------------------
26 # Note that $data, in the case of this parser, is not useful.
27 # We're working with DBIx::Class Schemas, not data streams.
28 # -------------------------------------------------------------------
31 my $args = $tr->parser_args;
32 my $dbicschema = $args->{'DBIx::Class::Schema'} || $args->{"DBIx::Schema"} ||$data;
33 $dbicschema ||= $args->{'package'};
34 my $limit_sources = $args->{'sources'};
36 die 'No DBIx::Class::Schema' unless ($dbicschema);
37 if (!ref $dbicschema) {
38 eval "use $dbicschema;";
39 die "Can't load $dbicschema ($@)" if($@);
42 my $schema = $tr->schema;
45 $schema->name( ref($dbicschema) . " v" . ($dbicschema->VERSION || '1.x'))
46 unless ($schema->name);
50 my @monikers = sort $dbicschema->sources;
52 my $ref = ref $limit_sources || '';
53 die "'sources' parameter must be an array or hash ref" unless $ref eq 'ARRAY' || ref eq 'HASH';
55 # limit monikers to those specified in
57 if ($ref eq 'ARRAY') {
58 $sources->{$_} = 1 for (@$limit_sources);
60 $sources = $limit_sources;
62 @monikers = grep { $sources->{$_} } @monikers;
66 foreach my $moniker (sort @monikers)
68 my $source = $dbicschema->source($moniker);
70 # Its possible to have multiple DBIC source using same table
71 next if $seen_tables{$source->name}++;
73 my $table = $schema->add_table(
74 name => $source->name,
76 ) || die $schema->error;
78 foreach my $col ($source->columns)
80 # assuming column_info in dbic is the same as DBI (?)
81 # data_type is a number, column_type is text?
85 is_auto_increment => 0,
88 %{$source->column_info($col)}
90 if ($colinfo{is_nullable}) {
91 $colinfo{default} = '' unless exists $colinfo{default};
93 my $f = $table->add_field(%colinfo) || die $table->error;
95 $table->primary_key($source->primary_columns);
97 my @primary = $source->primary_columns;
98 foreach my $field (@primary) {
99 my $index = $table->add_index(
105 my %unique_constraints = $source->unique_constraints;
106 foreach my $uniq (sort keys %unique_constraints) {
107 if (!$source->compare_relationship_keys($unique_constraints{$uniq}, \@primary)) {
108 $table->add_constraint(
111 fields => $unique_constraints{$uniq}
114 my $index = $table->add_index(
115 # TODO: Pick a better than that wont conflict
116 name => $unique_constraints{$uniq}->[0],
117 fields => $unique_constraints{$uniq},
124 my @rels = $source->relationships();
128 foreach my $rel (sort @rels)
130 my $rel_info = $source->relationship_info($rel);
132 # Ignore any rel cond that isn't a straight hash
133 next unless ref $rel_info->{cond} eq 'HASH';
135 my $othertable = $source->related_source($rel);
136 my $rel_table = $othertable->name;
138 # Get the key information, mapping off the foreign/self markers
139 my @cond = keys(%{$rel_info->{cond}});
140 my @refkeys = map {/^\w+\.(\w+)$/} @cond;
141 my @keys = map {$rel_info->{cond}->{$_} =~ /^\w+\.(\w+)$/} @cond;
145 my $reverse_rels = $source->reverse_relationship_info($rel);
146 my ($otherrelname, $otherrelationship) = each %{$reverse_rels};
151 if (defined $otherrelationship) {
152 $on_delete = $otherrelationship->{'attrs'}->{cascade_delete} ? 'CASCADE' : '';
153 $on_update = $otherrelationship->{'attrs'}->{cascade_copy} ? 'CASCADE' : '';
156 # Make sure we dont create the same foreign key constraint twice
157 my $key_test = join("\x00", @keys);
159 #Decide if this is a foreign key based on whether the self
160 #items are our primary columns.
162 # If the sets are different, then we assume it's a foreign key from
163 # us to another table.
164 # OR: If is_foreign_key_constraint attr is explicity set (or set to false) on the relation
165 if ( ! exists $created_FK_rels{$rel_table}->{$key_test} &&
166 ( exists $rel_info->{attrs}{is_foreign_key_constraint} ?
167 $rel_info->{attrs}{is_foreign_key_constraint} :
168 !$source->compare_relationship_keys(\@keys, \@primary)
172 $created_FK_rels{$rel_table}->{$key_test} = 1;
174 $table->add_constraint(
175 type => 'foreign_key',
176 name => $table->name . "_fk_$keys[0]",
178 reference_fields => \@refkeys,
179 reference_table => $rel_table,
180 on_delete => $on_delete,
181 on_update => $on_update
184 my $index = $table->add_index(
195 if ($source->result_class->can('sqlt_deploy_hook')) {
196 $source->result_class->sqlt_deploy_hook($table);
200 if ($dbicschema->can('sqlt_deploy_hook')) {
201 $dbicschema->sqlt_deploy_hook($schema);