blib/lib/DBIx/Auto/Migrate.pm

filter: Statementstmt 85.3% 76 / 89 Branchbran 56.2% 9 / 16 Conditioncond 50.0% 2 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 89.4% 17 / 19 Podpod n/a totaltotal 80.0% 104 / 130 CC 11 SCAR 7.9
CC
11
Cov
80%
_migrations_finish
4.1
_migrate
3.4
_apply_migration
3.1
CRAP 2.2
« Build ↑ Summary
1 package DBIx::Auto::Migrate;
2  
3 2 our $VERSION = "0.8";
4  
5 2 ▶use v5.16.3;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 sub _check_defined_sub {
12 4 ▶ my ( $caller, $sub ) = @_;
Subroutine _check_defined_sub: called CC=2 SCAR=9.2
13 4 ▶ if ( !$caller->can($sub) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $caller->can($sub)) 0 4 4
14 0 die
15 0 "To import '@{[__PACKAGE__]}' in '$caller' you must implement the '$sub' subroutine";
16 }
17 }
18  
19 sub import {
20 3 ▶ my $caller = caller;
Subroutine import: called CC=1 SCAR=0.0
21 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
22 3 *{"${caller}::finish_auto_migrate"} = sub {
23 1 ▶ _migrations_finish($caller);
Subroutine __ANON__: called CC=1 SCAR=0.0
24 3 };
25 }
26  
27 sub _migrations_finish {
28 1 ▶ my ($caller) = @_;
Subroutine _migrations_finish: called CC=4 SCAR=14.1
29 1 _check_defined_sub( $caller, 'migrations' );
30 1 _check_defined_sub( $caller, 'dsn' );
31 1 _check_defined_sub( $caller, 'user' );
32 1 _check_defined_sub( $caller, 'pass' );
33 1 my $extra;
34 1 ▶ if ( defined( my $extra_sub = $caller->can('extra') ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $extra_sub = $caller->can("extra"))) 0 1 1
35 0 $extra = $extra_sub->();
36 }
37 1 ▶ $extra //= {};
Condition50%1/2
$extra //= {}
l!l
01
MC/DC0%0/1
$extra //= {}
$extra
Truth table50%1/2
A
A= $extra
Aresult
11
00
38 1 my ( $dsn, $user, $pass ) = ( $caller->dsn, $caller->user, $caller->pass );
39  
40 1 ▶ if ( 'HASH' ne ref $extra ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("HASH" ne ref $extra) 0 1 1
41 0 die "${caller}::extra should return a hashref or undef";
42 }
43 {
44 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
45 2 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
46 1 *{"${caller}::connect"} = sub {
47 1 ▶ _connect_wrapper( $caller, 'connect', $dsn, $user, $pass, $extra );
Subroutine __ANON__: called CC=1 SCAR=0.0
48 1 };
49 1 *{"${caller}::connect_cached"} = sub {
50 0 ▶ _connect_wrapper( $caller, 'connect_cached', $dsn, $user, $pass,
Subroutine __ANON__: not called CC=1 SCAR=6.9
51 $extra );
52 1 };
53  
54 }
55 }
56  
57 sub _connect_wrapper {
58 1 ▶ my ( $caller, $sub, $dsn, $user, $pass, $extra ) = @_;
Subroutine _connect_wrapper: called CC=1 SCAR=0.8
59 my $dbh = DBI->can($sub)->(
60 'DBI', $dsn, $user, $pass,
61 {
62 RaiseError => 1,
63 Callbacks => {
64 connected => sub {
65 0 ▶ eval {shift->do('set timezone = UTC')};
Subroutine __ANON__: not called CC=1 SCAR=6.9
66 0 return;
67 }
68 },
69 1 %$extra,
70 },
71 );
72 1 _migrate( $caller, $dbh );
73 1 return $dbh;
74 }
75  
76 sub _migrate {
77 1 ▶ my ( $caller, $dbh ) = @_;
Subroutine _migrate: called CC=3 SCAR=12.3
78 1 local $dbh->{RaiseError} = 0;
79 1 local $dbh->{PrintError} = 0;
80 1 my @migrations = $caller->can('migrations')->();
81 1 ▶ if ( _get_current_migration($dbh) > @migrations ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_get_current_migration($dbh) > @migrations) 0 1 1
82 0 warn "Something happened there, wrong migration number.";
83 }
84 1 ▶ if ( _get_current_migration($dbh) >= @migrations ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_get_current_migration($dbh) >= @migrations) 0 1 1
85 0 say STDERR "Migrations already applied.";
86 0 return;
87 }
88 1 _apply_migrations( $dbh, \@migrations );
89  
90 }
91  
92 sub _apply_migrations {
93 1 ▶ my $dbh = shift;
Subroutine _apply_migrations: called CC=2 SCAR=6.9
94 1 my $migrations = shift;
95 1 ▶ for ( my $i = _get_current_migration($dbh) ; $i < @$migrations ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$migrations) 3 1 4
96 3 local $dbh->{RaiseError} = 1;
97 3 my $current_migration = $migrations->[$i];
98 3 my $migration_number = $i + 1;
99 3 _apply_migration( $dbh, $current_migration, $migration_number );
100 }
101 }
102  
103 sub _get_current_migration {
104 3 ▶ my $dbh = shift;
Subroutine _get_current_migration: called CC=2 SCAR=7.1
105 3 my $result = $dbh->selectrow_hashref( <<'EOF', undef, 'current_migration' );
106 SELECT value FROM options WHERE name = ?;
107 EOF
108 3 ▶ return int( $result->{value} // 0 );
Condition50%1/2
$result->{"value"} // 0
l!l
03
MC/DC0%0/1
$result->{"value"} // 0
$result->{"value"}
Truth table50%1/2
A
A= $result->{"value"}
Aresult
11
00
109 }
110  
111 sub _apply_migration {
112 3 ▶ my $dbh = shift;
Subroutine _apply_migration: called CC=3 SCAR=11.4
113 3 my $current_migration = shift;
114 3 my $migration_number = shift;
115 {
116 3 eval { $dbh->do($current_migration); };
117 3 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 3 3
118 0 die "$current_migration\n failed with: $@";
119 }
120 }
121 3 my $success = eval {
122 3 $dbh->do( <<'EOF', undef, 'current_migration', $migration_number );
123 INSERT INTO options (name, value)
124 VALUES (?, ?)
125 EOF
126 3 1;
127 };
128 3 ▶ if (!$success) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $success) 0 3 3
129 0 $dbh->do( <<'EOF', undef, $migration_number, 'current_migration' );
130 UPDATE options
131 SET value = ?
132 WHERE name = ?
133 EOF
134 }
135 }
136 2 1;
137  
138 =pod
139  
140 =encoding utf-8
141  
142 =head1 NAME
143  
144 DBIx::Auto::Migrate - Wrap your database connections and automatically apply db migrations.
145  
146 =head1 SYNOPSIS
147  
148 package MyCompany::DB;
149
150 use v5.16.3;
151 use strict;
152 use warnings;
153  
154 use DBIx::Auto::Migrate;
155  
156 finish_auto_migrate;
157  
158 sub create_index {
159 my ($table, $column) = @_;
160 if (!$table) {
161 die 'Index requires table';
162 }
163 if (!$column) {
164 die 'Index requires column';
165 }
166 return "CREATE INDEX index_${table}_${column} ON $table ($column)";
167 }
168  
169 sub migrations {
170 return (
171 'CREATE TABLE options (
172 id BIGSERIAL PRIMARY KEY,
173 name TEXT,
174 value TEXT,
175 UNIQUE (name)
176 )',
177 create_index(qw/options name/),
178 'CREATE TABLE users (
179 id BIGSERIAL PRIMARY KEY,
180 uuid TEXT NOT NULL,
181 username TEXT NOT NULL,
182 name TEXT NOT NULL,
183 surname TEXT NOT NULL,
184 UNIQUE(username)
185 )',
186 create_index(qw/users uuid/),
187 create_index(qw/users username/),
188 );
189 }
190  
191 sub dsn {
192 return 'dbi:Pg:dbname=my_fancy_app_db';
193 }
194  
195 sub user {
196 return 'user';
197 }
198  
199 sub pass {
200 return 'supertopsecretdbpass';
201 }
202  
203 sub extra {
204 {
205 PrintError => 1,
206 }
207 }
208  
209 And elsewhere:
210  
211 my $dbh = MyCompany::DB->connect;
212 my $dbh = MyCompany::DB->connect_cached;
213  
214 =head1 DESCRIPTION
215  
216 Sometimes is convenient to be able to make server or desktop programs that
217 use a database with the ability to be automatically have their database
218 upgraded in runtime.
219  
220 This module comes from a snippet of code I was copying all the time between
221 different projects with different database engines such as PostgreSQL and SQLite,
222 it is time to stop copying logic like this between projects and make public
223 my way to apply database migrations defined in code in a extensible way.
224  
225 It is only possible to migrate forward so be careful.
226  
227 To check an example project that uses this code you can check L<https://github.com/sergiotarxz/Perl-App-RSS-Social>
228  
229 =head1 SUBS TO IMPLEMENT IN YOUR OWN DATABASE WRAPPER
230  
231 =head2 migrations
232  
233 sub migrations {
234 return (
235 'CREATE TABLE options (
236 id BIGSERIAL PRIMARY KEY,
237 name TEXT,
238 value TEXT,
239 UNIQUE (name)
240 )',
241 'CREATE TABLE users (
242 id BIGSERIAL PRIMARY KEY,
243 uuid TEXT NOT NULL,
244 username TEXT NOT NULL,
245 name TEXT NOT NULL,
246 surname TEXT NOT NULL,
247 UNIQUE(username)
248 )',
249 );
250 }
251  
252 Returns a list of migrations, creating a options table in the first migration is
253 obligatory since it is internally used to keep track of the current migration number.
254  
255 =head2 dsn
256  
257 sub dsn {
258 return 'dbi:Pg:dbname=my_fancy_app_db';
259 }
260  
261 Returns a valid DSN for L<DBI>, you can use any logic to return this, even reading a database config file.
262  
263 =head2 user
264  
265 sub user { 'mydbuser' }
266  
267 Returns a valid user for L<DBI>, you can use any logic to return this, even reading a database config file.
268  
269 =head2 pass
270  
271 sub pass { 'mypass' }
272  
273 Returns a valid password for L<DBI>, you can use any logic to return this, even reading a database config file.
274  
275 =head2 extra
276  
277 sub extra {
278 {
279 PrintError => 1,
280 }
281 }
282  
283 You can optionally implement this method to pass extra options to L<DBI>, the
284 return must be a hashref or undef.
285  
286 =head1 FINALIZING THE DATABASE WRAPPER CLASS
287  
288 finish_auto_migrate();
289  
290 Calling this method will ensure your class is completely ready to be used,
291 you can do it at any point if every prerequisite is available.
292  
293 =head1 METHODS AUTOMATICALLY AVAILABLE IN YOUR WRAPPER
294  
295 =head2 connect
296  
297 my $dbh = MyCompany::DB->connect;
298  
299 Same as L<DBI>::C<connect> but without taking any argument.
300  
301 =head2 connect_cached
302  
303 my $dbh = MyCompany::DB->connect_cached;
304  
305 Same as L<DBI>::C<connect_cached> but without taking any argument.
306  
307 =head1 BUGS AND LIMITATIONS
308  
309 Tries to be database independent, but I cannot really ensure it.
310  
311 More testing is needed.
312  
313 =head1 AUTHOR
314  
315 SERGIOXZ - Sergio Iglesias
316  
317 =head1 CONTRIBUTORS
318  
319 SERGIOXZ - Sergio Iglesias
320  
321 =head1 COPYRIGHT
322  
323 Copyright © Sergio Iglesias (2025)
324  
325 =head1 LICENSE
326  
327 This library is free software and may be distributed under the same terms
328 as perl itself. See L<https://dev.perl.org/licenses/>.
329  
330 =cut
« Build ↑ Summary