Remove add_index and replace it with sqlt_deploy_hook
[dbsrgits/DBIx-Class.git] / lib / SQL / Translator / Parser / DBIx / Class.pm
CommitLineData
75d07914 1package # 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 10use strict;
11use warnings;
12use vars qw($DEBUG $VERSION @EXPORT_OK);
13$DEBUG = 0 unless defined $DEBUG;
14$VERSION = sprintf "%d.%02d", q$Revision 1.0$ =~ /(\d+)\.(\d+)/;
15
16use Exporter;
17use Data::Dumper;
18use SQL::Translator::Utils qw(debug normalize_name);
19
20use 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# -------------------------------------------------------------------
30sub 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
68 foreach my $moniker (@monikers)
b02b20b5 69 {
0009fa49 70 #eval "use $tableclass";
71 #print("Can't load $tableclass"), next if($@);
72 my $source = $dbixschema->source($moniker);
b02b20b5 73
38e48163 74 next if $seen_tables{$source->name}++;
75
b02b20b5 76 my $table = $schema->add_table(
77 name => $source->name,
78 type => 'TABLE',
79 ) || die $schema->error;
80 my $colcount = 0;
81 foreach my $col ($source->columns)
82 {
83 # assuming column_info in dbix is the same as DBI (?)
84 # data_type is a number, column_type is text?
85 my %colinfo = (
86 name => $col,
b02b20b5 87 size => 0,
88 is_auto_increment => 0,
89 is_foreign_key => 0,
90 is_nullable => 0,
91 %{$source->column_info($col)}
92 );
0009fa49 93 if ($colinfo{is_nullable}) {
94 $colinfo{default} = '' unless exists $colinfo{default};
95 }
b02b20b5 96 my $f = $table->add_field(%colinfo) || die $table->error;
97 }
98 $table->primary_key($source->primary_columns);
99
7b90bb13 100 my @primary = $source->primary_columns;
101 my %unique_constraints = $source->unique_constraints;
102 foreach my $uniq (keys %unique_constraints) {
de60a93d 103 if (!$source->compare_relationship_keys($unique_constraints{$uniq}, \@primary)) {
7b90bb13 104 $table->add_constraint(
105 type => 'unique',
106 name => "$uniq",
107 fields => $unique_constraints{$uniq}
108 );
109 }
110 }
111
b02b20b5 112 my @rels = $source->relationships();
499adf63 113
114 my %created_FK_rels;
115
b02b20b5 116 foreach my $rel (@rels)
117 {
118 my $rel_info = $source->relationship_info($rel);
637ca936 119
637ca936 120 # Ignore any rel cond that isn't a straight hash
121 next unless ref $rel_info->{cond} eq 'HASH';
122
de60a93d 123 my $othertable = $source->related_source($rel);
124 my $rel_table = $othertable->name;
125
637ca936 126 # Get the key information, mapping off the foreign/self markers
127 my @cond = keys(%{$rel_info->{cond}});
128 my @refkeys = map {/^\w+\.(\w+)$/} @cond;
129 my @keys = map {$rel_info->{cond}->{$_} =~ /^\w+\.(\w+)$/} @cond;
130
131 if($rel_table)
75d07914 132 {
de60a93d 133 my $reverse_rels = $source->reverse_relationship_info($rel);
134 my ($otherrelname, $otherrelationship) = each %{$reverse_rels};
135
136 my $on_delete = '';
137 my $on_update = '';
138
139 if (defined $otherrelationship) {
140 $on_delete = $otherrelationship->{'attrs'}->{cascade_delete} ? 'CASCADE' : '';
141 $on_update = $otherrelationship->{'attrs'}->{cascade_copy} ? 'CASCADE' : '';
142 }
143
499adf63 144 # Make sure we dont create the same foreign key constraint twice
145 my $key_test = join("\x00", @keys);
146
637ca936 147 #Decide if this is a foreign key based on whether the self
148 #items are our primary columns.
149
637ca936 150 # If the sets are different, then we assume it's a foreign key from
151 # us to another table.
3d618782 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)
157 )
158 )
159 {
499adf63 160 $created_FK_rels{$rel_table}->{$key_test} = 1;
637ca936 161 $table->add_constraint(
162 type => 'foreign_key',
163 name => "fk_$keys[0]",
164 fields => \@keys,
165 reference_fields => \@refkeys,
166 reference_table => $rel_table,
de60a93d 167 on_delete => $on_delete,
168 on_update => $on_update
637ca936 169 );
170 }
b02b20b5 171 }
172 }
aaf2403d 173
174 if ($source->result_class->can('sqlt_deploy_hook')) {
175 $source->result_class->sqlt_deploy_hook($table);
176 }
b02b20b5 177 }
637ca936 178 return 1;
75d07914 179}
b02b20b5 180
1811;
de60a93d 182