lib/DBIx/EAV/Schema.pm

filter: Statementstmt 99.2% 131 / 132 Branchbran 84.6% 44 / 52 Conditioncond 37.5% 3 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 100% 23 / 23 Podpod 50.0% 6 / 12 totaltotal 89.2% 207 / 232 SCAR 36.5
CC
38
Cov
93%
_build_sqlt_schema
22.1
deploy
5.0
dbh_do
3.1
CRAP 38.6
« lib/DBIx/EAV/ResultSet.pm ↑ Summary lib/DBIx/EAV/Table.pm »
1 package DBIx::EAV::Schema;
2  
3 10 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use DBIx::EAV::Table;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use SQL::Translator;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 use constant {
9 SQL_DEBUG => $ENV{DBIX_EAV_TRACE}
10 10 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 10 our $SCHEMA_VERSION = 1;
13  
14  
15 10 my %driver_to_producer = (
16 mysql => 'MySQL'
17 );
18  
19  
20 10 has 'dbh', is => 'ro', required => 1;
21  
22 10 has 'database_cascade_delete', is => 'ro', default => 1;
23 10 has 'table_prefix', is => 'ro', default => 'eav_';
24 10 has 'tenant_id', is => 'ro';
25 12 ▶has 'data_types', is => 'ro', default => sub { [qw/ int decimal varchar text datetime bool /] };
Subroutine __ANON__: called CC=1 SCAR=0.0
26 9 ▶has 'static_attributes', is => 'ro', default => sub { [] };
Subroutine __ANON__: called CC=1 SCAR=0.0
27 10 has 'id_type', is => 'ro', default => 'int';
28  
29 10 has 'translator', is => 'ro', init_arg => undef, lazy => 1, builder => 1;
30 12 ▶has '_tables', is => 'ro', init_arg => undef, default => sub { {} };
Subroutine __ANON__: called CC=1 SCAR=0.0
31  
32  
33 sub BUILD {
34 12 ▶ my $self = shift;
Subroutine BUILD: called CC=2 SCAR=7.2
Pod undocumented
35  
36 # enable sqlite fk for cascade delete to work
37 12 ▶ $self->dbh_do("PRAGMA foreign_keys = ON;")
Branch: 1 branch
BranchTrueFalseTotal
if $self->db_driver_name eq "SQLite" 12 0 12
38 if $self->db_driver_name eq 'SQLite';
39 }
40  
41  
42 sub _build_translator {
43 12 ▶ my $self = shift;
Subroutine _build_translator: called CC=1 SCAR=0.0
44  
45 12 my $sqlt = SQL::Translator->new;
46 12 $self->_build_sqlt_schema($sqlt->schema);
47  
48 12 $sqlt;
49 }
50  
51 sub _build_sqlt_schema {
52 12 ▶ my ($self, $schema) = @_;
Subroutine _build_sqlt_schema: called CC=22 SCAR=31.0
53  
54 my @schema = (
55  
56 entity_types => {
57 columns => ['id', $self->tenant_id ? 'tenant_id' : (), 'name:varchar:255'],
58 index => [$self->tenant_id ? 'tenant_id' : ()],
59 unique => {
60 name => [$self->tenant_id ? 'tenant_id' : (),'name']
61 }
62 },
63  
64 entities => {
65 12 columns => [qw/ id entity_type_id /, @{ $self->static_attributes } ],
66 fk => { entity_type_id => 'entity_types' }
67 },
68  
69 attributes => {
70 columns => [qw/ id entity_type_id name:varchar:255 data_type:varchar:64 /],
71 fk => { entity_type_id => 'entity_types' }
72 },
73  
74 relationships => {
75 columns => [qw/ id left_entity_type_id right_entity_type_id name:varchar:255 incoming_name:varchar:255 is_has_one:bool::0 is_has_many:bool::0 is_many_to_many:bool::0 /],
76 fk => { left_entity_type_id => 'entity_types', right_entity_type_id => 'entity_types' },
77 unique => {
78 name => ['left_entity_type_id','name']
79 }
80 },
81  
82 entity_relationships => {
83 columns => [qw/ relationship_id left_entity_id right_entity_id /],
84 pk => [qw/ relationship_id left_entity_id right_entity_id /],
85 fk => {
86 relationship_id => 'relationships',
87 left_entity_id => { table => 'entities', cascade_delete => $self->database_cascade_delete },
88 right_entity_id => { table => 'entities', cascade_delete => $self->database_cascade_delete },
89 }
90 },
91  
92 type_hierarchy => {
93 columns => [qw/ parent_type_id child_type_id /],
94 pk => [qw/ parent_type_id child_type_id /],
95 fk => {
96 parent_type_id => { table => 'entity_types', cascade_delete => $self->database_cascade_delete },
97 child_type_id => { table => 'entity_types', cascade_delete => $self->database_cascade_delete },
98 }
99 },
100  
101 map {
102 72 ("value_$_" => {
103 columns => [qw/ entity_id attribute_id /, 'value:'.$_],
104 fk => {
105 entity_id => { table => 'entities', cascade_delete => $self->database_cascade_delete },
106 attribute_id => 'attributes'
107 }
108 })
109 12 ▶ } @{ $self->data_types }
Branch: 3 branches
BranchTrueFalseTotal
$self->tenant_id ? : 11 1 12
$self->tenant_id ? : 11 1 12
$self->tenant_id ? : 11 1 12
110 );
111  
112 12 ▶ for (my $i = 0; $i < @schema; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @schema) 144 12 156
113  
114 # add table
115 144 my $table_name = $schema[$i];
116 144 my $table_schema = $schema[$i+1];
117 144 ▶ my $table = $schema->add_table( name => $self->table_prefix . $table_name )
Branch: 1 branch
BranchTrueFalseTotal
my $table = $schema->add_table("name", $self->table_prefix . $table_name) or die $schema->error 0 144 144
118 or die $schema->error;
119  
120 # add columns
121 144 foreach my $col ( @{ $table_schema->{columns} }) {
122  
123 488 ▶ my $field_params = ref $col ? $col : do {
Branch: 1 branch
BranchTrueFalseTotal
ref $col ? : 0 488 488
124  
125 488 my ($name, $type, $size, $default) = split ':', $col;
126 +{
127 488 name => $name,
128 data_type => $type,
129 size => $size,
130 default_value => $default
131 }
132 };
133  
134 $field_params->{data_type} = $self->id_type
135 488 ▶ if $field_params->{name} =~ /(?:^id$|_id$)/;
Branch: 1 branch
BranchTrueFalseTotal
if $field_params->{"name"} =~ /(?:^id$|_id$)/ 311 177 488
136  
137 $field_params->{is_auto_increment} = 1
138 488 ▶ if $field_params->{name} eq 'id';
Branch: 1 branch
BranchTrueFalseTotal
if $field_params->{"name"} eq "id" 48 440 488
139  
140 488 ▶ $field_params->{is_nullable} //= 0;
Condition50%1/2
$field_params->{"is_nullable"} //= 0
l!l
0488
MC/DC0%0/1
$field_params->{"is_nullable"} //= 0
$field_params->{"is_nullable"}
Truth table50%1/2
A
A= $field_params->{"is_nullable"}
Aresult
11
00
141  
142 488 ▶ $table->add_field(%$field_params)
Branch: 1 branch
BranchTrueFalseTotal
$table->add_field(%$field_params) or die $table->error 0 488 488
143 or die $table->error;
144 }
145  
146 # # primary key
147 144 ▶ my $pk = $table->get_field('id') ? 'id' : $table_schema->{pk};
Branch: 1 branch
BranchTrueFalseTotal
$table->get_field('id') ? : 48 96 144
148 144 ▶ $table->primary_key($pk) if $pk;
Branch: 1 branch
BranchTrueFalseTotal
if $pk 72 72 144
149  
150 # # foreign keys
151 144 ▶ foreach my $fk_column (keys %{ $table_schema->{fk} || {} }) {
Branch: 1 branch
BranchTrueFalseTotal
$table_schema->{"fk"} or {} 12 132 144
152  
153 252 my $params = $table_schema->{fk}->{$fk_column};
154 252 ▶ $params = { table => $params } unless ref $params;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $params 132 120 252
155  
156 $table->add_constraint(
157 name => join('_', 'fk', $table_name, $fk_column, $params->{table}),
158 type => 'foreign_key',
159 fields => $fk_column,
160 reference_fields => 'id',
161 reference_table => $self->table_prefix . $params->{table},
162 252 ▶ on_delete => $params->{cascade_delete} ? 'CASCADE' : 'NO ACTION'
Branch: 1 branch
BranchTrueFalseTotal
$params->{'cascade_delete'} ? : 120 132 252
163 );
164 }
165  
166 # # unique constraints
167 144 ▶ foreach my $name (keys %{ $table_schema->{unique} || {} }) {
Branch: 1 branch
BranchTrueFalseTotal
$table_schema->{"unique"} or {} 120 24 144
168  
169 $table->add_index(
170 name => join('_', 'unique', $table_name, $name),
171 type => 'unique',
172 24 fields => $table_schema->{unique}{$name},
173 );
174 }
175  
176 # # index
177 144 ▶ foreach my $colname (@{ $table_schema->{index} || [] }) {
Branch: 1 branch
BranchTrueFalseTotal
$table_schema->{"index"} or [] 132 12 144
178  
179 11 $table->add_index(
180 name => join('_', 'idx', $table_name, $colname),
181 type => 'normal',
182 fields => $colname,
183 );
184 }
185 }
186  
187 12 return 1;
188 }
189  
190  
191 26 ▶sub version { $SCHEMA_VERSION }
Subroutine version: called CC=1 SCAR=0.0
Pod undocumented
192  
193 sub get_ddl {
194 13 ▶ my ($self, $producer) = @_;
Subroutine get_ddl: called CC=3 SCAR=11.2
Pod documented
195  
196 13 ▶ unless ($producer) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($producer) 12 1 13
197  
198 12 my $driver = $self->dbh->{Driver}{Name};
199 12 ▶ $producer = $driver_to_producer{$driver} || $driver;
Condition33%1/3
$driver_to_producer{$driver} || $driver
l!l&&r!l&&!r
0120
MC/DC0%0/2
$driver_to_producer{$driver} || $driver
$driver_to_producer{$driver} $driver
Truth table33%1/3
A || B
A= $driver_to_producer{$driver}B= $driver
ABresult
1X1
011
000
200 }
201  
202 13 $self->translator->producer($producer);
203 13 $self->translator->translate;
204 }
205  
206 sub version_table {
207 25 ▶ my $self = shift;
Subroutine version_table: called CC=1 SCAR=0.0
Pod undocumented
208  
209 25 DBIx::EAV::Table->new(
210 dbh => $self->dbh,
211 name => $self->table_prefix . 'schema_versions',
212 columns => [qw/ id version ddl /]
213 );
214 }
215  
216 sub version_table_is_installed {
217 14 ▶ my $self = shift;
Subroutine version_table_is_installed: called CC=1 SCAR=0.0
Pod undocumented
218  
219 14 my $success = 0;
220  
221 14 eval {
222 14 $self->dbh_do(sprintf 'SELECT COUNT(*) FROM %s', $self->table_prefix . 'schema_versions');
223 3 $success = 1;
224 };
225  
226 14 $success;
227 }
228  
229 sub install_version_table {
230 10 ▶ my $self = shift;
Subroutine install_version_table: called CC=3 SCAR=11.1
Pod undocumented
231  
232 10 my $sqlt = SQL::Translator->new;
233 10 my $table = $sqlt->schema->add_table( name => $self->version_table->name );
234  
235 10 $table->add_field(
236 name => 'id',
237 data_type => 'INTEGER',
238 is_auto_increment => 1
239 );
240  
241 10 $table->add_field(
242 name => 'version',
243 data_type => 'INTEGER'
244 );
245  
246 10 $table->add_field(
247 name => 'ddl',
248 data_type => 'TEXT'
249 );
250  
251 10 $table->primary_key('id');
252  
253 # execute ddl
254 10 my $driver = $self->dbh->{Driver}{Name};
255 10 ▶ $sqlt->producer($driver_to_producer{$driver} || $driver);
Condition33%1/3
$driver_to_producer{$driver} || $driver
l!l&&r!l&&!r
0100
MC/DC0%0/2
$driver_to_producer{$driver} || $driver
$driver_to_producer{$driver} $driver
Truth table33%1/3
A || B
A= $driver_to_producer{$driver}B= $driver
ABresult
1X1
011
000
256  
257 $self->dbh_do($_)
258 10 for grep { /\w/ } split ';', $sqlt->translate;
259  
260 }
261  
262 sub installed_version {
263 2 ▶ my $self = shift;
Subroutine installed_version: called CC=2 SCAR=7.0
Pod undocumented
264 2 my $table = $self->version_table;
265 2 my $row;
266 2 eval {
267 2 my ($rv, $sth) = $self->dbh_do(sprintf 'SELECT * FROM %s ORDER BY id DESC', $table->name);
268 2 $row = $sth->fetchrow_hashref;
269 };
270 2 ▶ return unless $row;
Branch: 1 branch
BranchTrueFalseTotal
unless $row 0 2 2
271 2 $row->{version};
272 }
273  
274 sub deploy {
275 12 ▶ my $self = shift;
Subroutine deploy: called CC=5 SCAR=16.1
Pod documented
276 12 my %options = ( @_, no_comments => 1 );
277  
278 $self->translator->$_($options{$_})
279 12 for keys %options;
280  
281 # deploy version table
282 12 ▶ $self->install_version_table
Branch: 1 branch
BranchTrueFalseTotal
unless $self->version_table_is_installed 10 2 12
283 unless $self->version_table_is_installed;
284  
285 # check we already installed this version
286 12 my $version_table = $self->version_table;
287 12 ▶ return if $version_table->select_one({ version => $self->version });
Branch: 1 branch
BranchTrueFalseTotal
if $version_table->select_one({"version", $self->version}) 1 11 12
288  
289 # deploy ddl
290 11 my $ddl = $self->get_ddl;
291 $self->dbh_do($_)
292 11 for grep { /\w/ } split ';', $ddl;
293  
294 # create version record
295 11 $version_table->insert({
296 version => $self->version,
297 ddl => 'DDL'
298 });
299 }
300  
301  
302 sub dbh_do {
303 368 ▶ my ($self, $stmt, $bind) = @_;
Subroutine dbh_do: called CC=3 SCAR=11.2
Pod documented
304  
305 368 if (SQL_DEBUG) {
306 my $i = 0;
307 print STDERR "$stmt";
308 print STDERR $bind ? sprintf(": %s\n", join(' ', map { $i++.'='.$_ } @{ $bind || [] }))
309 : ";\n";
310 }
311  
312 368 my $sth = $self->dbh->prepare($stmt);
313 368 ▶ my $rv = $sth->execute(ref $bind eq 'ARRAY' ? @$bind : ());
Branch: 1 branch
BranchTrueFalseTotal
ref $bind eq 'ARRAY' ? : 0 368 368
314 357 ▶ die $sth->errstr unless defined $rv;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rv 0 357 357
315  
316 357 return ($rv, $sth);
317 }
318  
319 sub table {
320 1208 ▶ my ($self, $name) = @_;
Subroutine table: called CC=3 SCAR=11.0
Pod documented
321  
322 return $self->_tables->{$name}
323 1208 ▶ if exists $self->_tables->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->_tables->{$name} 1126 82 1208
324  
325 82 my $table_schema = $self->translator->schema->get_table($self->table_prefix . $name);
326  
327 82 ▶ croak "Table '$name' does not exist."
Branch: 1 branch
BranchTrueFalseTotal
unless $table_schema 0 82 82
328 unless $table_schema;
329  
330 82 $self->_tables->{$name} = DBIx::EAV::Table->new(
331 dbh => $self->dbh,
332 tenant_id => $self->tenant_id,
333 name => $table_schema->name,
334 columns => [ $table_schema->field_names ]
335 );
336 }
337  
338 sub has_data_type {
339 113 ▶ my ($self, $name) = @_;
Subroutine has_data_type: called CC=3 SCAR=11.1
Pod documented
340 113 foreach (@{$self->data_types}) {
341 337 ▶ return 1 if $_ eq $name;
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq $name 113 224 337
342 }
343 0 0;
344 }
345  
346 sub db_driver_name {
347 26 ▶ shift->dbh->{Driver}{Name};
Subroutine db_driver_name: called CC=1 SCAR=0.0
Pod documented
348 }
349  
350  
351 10 1;
352  
353  
354 __END__
« lib/DBIx/EAV/ResultSet.pm ↑ Summary lib/DBIx/EAV/Table.pm »