Commit | Line | Data |
75d07914 |
1 | package # hide from PAUSE |
c0e7b4e5 |
2 | SQL::Translator::Parser::DBIx::Class; |
b02b20b5 |
3 | |
4 | # AUTHOR: Jess Robinson |
5 | |
1d996af5 |
6 | # Some mistakes the fault of Matt S Trout |
7 | |
c385ecea |
8 | # Others the fault of Ash Berlin |
9 | |
b02b20b5 |
10 | use strict; |
11 | use warnings; |
b7e303a8 |
12 | use vars qw($DEBUG @EXPORT_OK); |
b02b20b5 |
13 | $DEBUG = 0 unless defined $DEBUG; |
b02b20b5 |
14 | |
15 | use Exporter; |
16 | use Data::Dumper; |
17 | use SQL::Translator::Utils qw(debug normalize_name); |
18 | |
19 | use base qw(Exporter); |
20 | |
21 | @EXPORT_OK = qw(parse); |
22 | |
23 | # ------------------------------------------------------------------- |
24 | # parse($tr, $data) |
25 | # |
880a1a0c |
26 | # Note that $data, in the case of this parser, is not useful. |
b02b20b5 |
27 | # We're working with DBIx::Class Schemas, not data streams. |
28 | # ------------------------------------------------------------------- |
29 | sub parse { |
499adf63 |
30 | my ($tr, $data) = @_; |
31 | my $args = $tr->parser_args; |
b7e303a8 |
32 | my $dbicschema = $args->{'DBIx::Class::Schema'} || $args->{"DBIx::Schema"} ||$data; |
33 | $dbicschema ||= $args->{'package'}; |
499adf63 |
34 | my $limit_sources = $args->{'sources'}; |
b02b20b5 |
35 | |
b7e303a8 |
36 | die 'No DBIx::Class::Schema' unless ($dbicschema); |
37 | if (!ref $dbicschema) { |
38 | eval "use $dbicschema;"; |
39 | die "Can't load $dbicschema ($@)" if($@); |
b02b20b5 |
40 | } |
41 | |
42 | my $schema = $tr->schema; |
43 | my $table_no = 0; |
44 | |
b7e303a8 |
45 | $schema->name( ref($dbicschema) . " v" . ($dbicschema->VERSION || '1.x')) |
46 | unless ($schema->name); |
38e48163 |
47 | |
48 | my %seen_tables; |
49 | |
b7e303a8 |
50 | my @monikers = sort $dbicschema->sources; |
499adf63 |
51 | if ($limit_sources) { |
52 | my $ref = ref $limit_sources || ''; |
5bdf1b3d |
53 | die "'sources' parameter must be an array or hash ref" unless $ref eq 'ARRAY' || ref eq 'HASH'; |
499adf63 |
54 | |
55 | # limit monikers to those specified in |
56 | my $sources; |
57 | if ($ref eq 'ARRAY') { |
58 | $sources->{$_} = 1 for (@$limit_sources); |
59 | } else { |
60 | $sources = $limit_sources; |
61 | } |
62 | @monikers = grep { $sources->{$_} } @monikers; |
63 | } |
64 | |
65 | |
454a5a42 |
66 | foreach my $moniker (sort @monikers) |
b02b20b5 |
67 | { |
b7e303a8 |
68 | my $source = $dbicschema->source($moniker); |
b02b20b5 |
69 | |
b7e303a8 |
70 | # Its possible to have multiple DBIC source using same table |
38e48163 |
71 | next if $seen_tables{$source->name}++; |
72 | |
b02b20b5 |
73 | my $table = $schema->add_table( |
74 | name => $source->name, |
75 | type => 'TABLE', |
76 | ) || die $schema->error; |
77 | my $colcount = 0; |
78 | foreach my $col ($source->columns) |
79 | { |
d6c79cb3 |
80 | # assuming column_info in dbic is the same as DBI (?) |
b02b20b5 |
81 | # data_type is a number, column_type is text? |
82 | my %colinfo = ( |
83 | name => $col, |
b02b20b5 |
84 | size => 0, |
85 | is_auto_increment => 0, |
86 | is_foreign_key => 0, |
87 | is_nullable => 0, |
88 | %{$source->column_info($col)} |
89 | ); |
0009fa49 |
90 | if ($colinfo{is_nullable}) { |
91 | $colinfo{default} = '' unless exists $colinfo{default}; |
92 | } |
b02b20b5 |
93 | my $f = $table->add_field(%colinfo) || die $table->error; |
94 | } |
95 | $table->primary_key($source->primary_columns); |
96 | |
7b90bb13 |
97 | my @primary = $source->primary_columns; |
d25d7fc2 |
98 | foreach my $field (@primary) { |
99 | my $index = $table->add_index( |
100 | name => $field, |
101 | fields => [$field], |
102 | type => 'NORMAL', |
103 | ); |
104 | } |
7b90bb13 |
105 | my %unique_constraints = $source->unique_constraints; |
b7e303a8 |
106 | foreach my $uniq (sort keys %unique_constraints) { |
de60a93d |
107 | if (!$source->compare_relationship_keys($unique_constraints{$uniq}, \@primary)) { |
7b90bb13 |
108 | $table->add_constraint( |
109 | type => 'unique', |
110 | name => "$uniq", |
111 | fields => $unique_constraints{$uniq} |
112 | ); |
d25d7fc2 |
113 | |
114 | my $index = $table->add_index( |
b7e303a8 |
115 | # TODO: Pick a better than that wont conflict |
d25d7fc2 |
116 | name => $unique_constraints{$uniq}->[0], |
117 | fields => $unique_constraints{$uniq}, |
118 | type => 'NORMAL', |
119 | ); |
120 | |
7b90bb13 |
121 | } |
122 | } |
123 | |
b02b20b5 |
124 | my @rels = $source->relationships(); |
499adf63 |
125 | |
126 | my %created_FK_rels; |
127 | |
454a5a42 |
128 | foreach my $rel (sort @rels) |
b02b20b5 |
129 | { |
130 | my $rel_info = $source->relationship_info($rel); |
637ca936 |
131 | |
637ca936 |
132 | # Ignore any rel cond that isn't a straight hash |
133 | next unless ref $rel_info->{cond} eq 'HASH'; |
134 | |
de60a93d |
135 | my $othertable = $source->related_source($rel); |
136 | my $rel_table = $othertable->name; |
137 | |
637ca936 |
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; |
142 | |
143 | if($rel_table) |
75d07914 |
144 | { |
de60a93d |
145 | my $reverse_rels = $source->reverse_relationship_info($rel); |
146 | my ($otherrelname, $otherrelationship) = each %{$reverse_rels}; |
147 | |
148 | my $on_delete = ''; |
149 | my $on_update = ''; |
150 | |
151 | if (defined $otherrelationship) { |
152 | $on_delete = $otherrelationship->{'attrs'}->{cascade_delete} ? 'CASCADE' : ''; |
153 | $on_update = $otherrelationship->{'attrs'}->{cascade_copy} ? 'CASCADE' : ''; |
154 | } |
155 | |
13de943d |
156 | my $is_deferrable = $rel_info->{attrs}{is_deferrable} || 0; |
157 | |
499adf63 |
158 | # Make sure we dont create the same foreign key constraint twice |
159 | my $key_test = join("\x00", @keys); |
160 | |
637ca936 |
161 | #Decide if this is a foreign key based on whether the self |
162 | #items are our primary columns. |
163 | |
637ca936 |
164 | # If the sets are different, then we assume it's a foreign key from |
165 | # us to another table. |
3d618782 |
166 | # OR: If is_foreign_key_constraint attr is explicity set (or set to false) on the relation |
7b5d0b84 |
167 | next if ( exists $created_FK_rels{$rel_table}->{$key_test} ); |
168 | if ( exists $rel_info->{attrs}{is_foreign_key_constraint}) { |
169 | # not is this attr set to 0 but definitely if set to 1 |
170 | next unless ($rel_info->{attrs}{is_foreign_key_constraint}); |
171 | } else { |
172 | # not if might have |
173 | # next if ($rel_info->{attrs}{accessor} eq 'single' && exists $rel_info->{attrs}{join_type} && uc($rel_info->{attrs}{join_type}) eq 'LEFT'); |
174 | # not sure about this one |
175 | next if $source->compare_relationship_keys(\@keys, \@primary); |
176 | } |
177 | |
178 | $created_FK_rels{$rel_table}->{$key_test} = 1; |
179 | if (scalar(@keys)) { |
180 | $table->add_constraint( |
0d865134 |
181 | type => 'foreign_key', |
d25d7fc2 |
182 | name => $table->name . "_fk_$keys[0]", |
0d865134 |
183 | fields => \@keys, |
184 | reference_fields => \@refkeys, |
185 | reference_table => $rel_table, |
186 | on_delete => $on_delete, |
187 | on_update => $on_update |
7b5d0b84 |
188 | ); |
0d865134 |
189 | |
7b5d0b84 |
190 | my $index = $table->add_index( |
191 | name => join('_', @keys), |
0d865134 |
192 | fields => \@keys, |
193 | type => 'NORMAL', |
7b5d0b84 |
194 | ); |
637ca936 |
195 | } |
b02b20b5 |
196 | } |
197 | } |
aaf2403d |
198 | |
199 | if ($source->result_class->can('sqlt_deploy_hook')) { |
200 | $source->result_class->sqlt_deploy_hook($table); |
201 | } |
b02b20b5 |
202 | } |
d6c79cb3 |
203 | |
b7e303a8 |
204 | if ($dbicschema->can('sqlt_deploy_hook')) { |
205 | $dbicschema->sqlt_deploy_hook($schema); |
d6c79cb3 |
206 | } |
207 | |
637ca936 |
208 | return 1; |
75d07914 |
209 | } |
b02b20b5 |
210 | |
211 | 1; |
de60a93d |
212 | |