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; |
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+)/; |
15 | |
16 | use Exporter; |
17 | use Data::Dumper; |
18 | use SQL::Translator::Utils qw(debug normalize_name); |
19 | |
20 | use base qw(Exporter); |
21 | |
22 | @EXPORT_OK = qw(parse); |
23 | |
24 | # ------------------------------------------------------------------- |
25 | # parse($tr, $data) |
26 | # |
880a1a0c |
27 | # Note that $data, in the case of this parser, is not useful. |
b02b20b5 |
28 | # We're working with DBIx::Class Schemas, not data streams. |
29 | # ------------------------------------------------------------------- |
30 | sub parse { |
499adf63 |
31 | my ($tr, $data) = @_; |
32 | my $args = $tr->parser_args; |
33 | my $dbixschema = $args->{'DBIx::Schema'} || $data; |
34 | $dbixschema ||= $args->{'package'}; |
35 | my $limit_sources = $args->{'sources'}; |
b02b20b5 |
36 | |
37 | die 'No DBIx::Schema' unless ($dbixschema); |
38 | if (!ref $dbixschema) { |
39 | eval "use $dbixschema;"; |
40 | die "Can't load $dbixschema ($@)" if($@); |
41 | } |
42 | |
43 | my $schema = $tr->schema; |
44 | my $table_no = 0; |
45 | |
46 | # print Dumper($dbixschema->registered_classes); |
47 | |
0009fa49 |
48 | #foreach my $tableclass ($dbixschema->registered_classes) |
38e48163 |
49 | |
50 | my %seen_tables; |
51 | |
499adf63 |
52 | my @monikers = $dbixschema->sources; |
53 | if ($limit_sources) { |
54 | my $ref = ref $limit_sources || ''; |
5bdf1b3d |
55 | die "'sources' parameter must be an array or hash ref" unless $ref eq 'ARRAY' || ref eq 'HASH'; |
499adf63 |
56 | |
57 | # limit monikers to those specified in |
58 | my $sources; |
59 | if ($ref eq 'ARRAY') { |
60 | $sources->{$_} = 1 for (@$limit_sources); |
61 | } else { |
62 | $sources = $limit_sources; |
63 | } |
64 | @monikers = grep { $sources->{$_} } @monikers; |
65 | } |
66 | |
67 | |
454a5a42 |
68 | foreach my $moniker (sort @monikers) |
b02b20b5 |
69 | { |
0009fa49 |
70 | my $source = $dbixschema->source($moniker); |
b02b20b5 |
71 | |
38e48163 |
72 | next if $seen_tables{$source->name}++; |
73 | |
b02b20b5 |
74 | my $table = $schema->add_table( |
75 | name => $source->name, |
76 | type => 'TABLE', |
77 | ) || die $schema->error; |
78 | my $colcount = 0; |
79 | foreach my $col ($source->columns) |
80 | { |
d6c79cb3 |
81 | # assuming column_info in dbic is the same as DBI (?) |
b02b20b5 |
82 | # data_type is a number, column_type is text? |
83 | my %colinfo = ( |
84 | name => $col, |
b02b20b5 |
85 | size => 0, |
86 | is_auto_increment => 0, |
87 | is_foreign_key => 0, |
88 | is_nullable => 0, |
89 | %{$source->column_info($col)} |
90 | ); |
0009fa49 |
91 | if ($colinfo{is_nullable}) { |
92 | $colinfo{default} = '' unless exists $colinfo{default}; |
93 | } |
b02b20b5 |
94 | my $f = $table->add_field(%colinfo) || die $table->error; |
95 | } |
96 | $table->primary_key($source->primary_columns); |
97 | |
7b90bb13 |
98 | my @primary = $source->primary_columns; |
d25d7fc2 |
99 | foreach my $field (@primary) { |
100 | my $index = $table->add_index( |
101 | name => $field, |
102 | fields => [$field], |
103 | type => 'NORMAL', |
104 | ); |
105 | } |
7b90bb13 |
106 | my %unique_constraints = $source->unique_constraints; |
107 | foreach my $uniq (keys %unique_constraints) { |
de60a93d |
108 | if (!$source->compare_relationship_keys($unique_constraints{$uniq}, \@primary)) { |
7b90bb13 |
109 | $table->add_constraint( |
110 | type => 'unique', |
111 | name => "$uniq", |
112 | fields => $unique_constraints{$uniq} |
113 | ); |
d25d7fc2 |
114 | |
115 | my $index = $table->add_index( |
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 | |
499adf63 |
156 | # Make sure we dont create the same foreign key constraint twice |
157 | my $key_test = join("\x00", @keys); |
158 | |
637ca936 |
159 | #Decide if this is a foreign key based on whether the self |
160 | #items are our primary columns. |
161 | |
637ca936 |
162 | # If the sets are different, then we assume it's a foreign key from |
163 | # us to another table. |
3d618782 |
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} && |
a0024650 |
166 | ( exists $rel_info->{attrs}{is_foreign_key_constraint} ? |
167 | $rel_info->{attrs}{is_foreign_key_constraint} : |
3d618782 |
168 | !$source->compare_relationship_keys(\@keys, \@primary) |
a0024650 |
169 | ) |
3d618782 |
170 | ) |
171 | { |
499adf63 |
172 | $created_FK_rels{$rel_table}->{$key_test} = 1; |
0d865134 |
173 | if (scalar(@keys)) { |
174 | $table->add_constraint( |
175 | type => 'foreign_key', |
d25d7fc2 |
176 | name => $table->name . "_fk_$keys[0]", |
0d865134 |
177 | fields => \@keys, |
178 | reference_fields => \@refkeys, |
179 | reference_table => $rel_table, |
180 | on_delete => $on_delete, |
181 | on_update => $on_update |
182 | ); |
183 | |
184 | my $index = $table->add_index( |
d25d7fc2 |
185 | name => $keys[0], |
0d865134 |
186 | fields => \@keys, |
187 | type => 'NORMAL', |
188 | ); |
189 | } |
190 | |
637ca936 |
191 | } |
b02b20b5 |
192 | } |
193 | } |
aaf2403d |
194 | |
195 | if ($source->result_class->can('sqlt_deploy_hook')) { |
196 | $source->result_class->sqlt_deploy_hook($table); |
197 | } |
b02b20b5 |
198 | } |
d6c79cb3 |
199 | |
200 | if ($dbixschema->can('sqlt_deploy_hook')) { |
201 | $dbixschema->sqlt_deploy_hook($schema); |
202 | } |
203 | |
637ca936 |
204 | return 1; |
75d07914 |
205 | } |
b02b20b5 |
206 | |
207 | 1; |
de60a93d |
208 | |