| 1 | package DBIO::SQLite::Deploy; | ||||||||||||
| 2 | # ABSTRACT: Deploy and upgrade SQLite schemas via test-deploy-and-compare | ||||||||||||
| 3 | |||||||||||||
| 4 | 194 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 194 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | |||||||||||||
| 7 | 194 | ▶ | use base 'DBIO::Deploy::Base'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | |||||||||||||
| 9 | 194 | ▶ | use DBI; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 194 | ▶ | use DBIO::SQLite::DDL; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 194 | ▶ | use DBIO::SQLite::Introspect; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | 194 | ▶ | use DBIO::SQLite::Diff; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | |||||||||||||
| 14 | |||||||||||||
| 15 | # --- hooks for Deploy::Base ------------------------------------------------- | ||||||||||||
| 16 | |||||||||||||
| 17 | 351 | ▶ | sub _ddl_class { 'DBIO::SQLite::DDL' } | ||||||||||
|
Subroutine
_ddl_class: called CC=1 SCAR=0.0
| |||||||||||||
| 18 | 20 | ▶ | sub _introspect_class { 'DBIO::SQLite::Introspect' } | ||||||||||
|
Subroutine
_introspect_class: called CC=1 SCAR=0.0
| |||||||||||||
| 19 | 10 | ▶ | sub _diff_class { 'DBIO::SQLite::Diff' } | ||||||||||
|
Subroutine
_diff_class: called CC=1 SCAR=0.0
| |||||||||||||
| 20 | |||||||||||||
| 21 | |||||||||||||
| 22 | 10 | ▶ | sub _build_target_model { | ||||||||||
|
Subroutine
_build_target_model: called CC=1 SCAR=0.0
| |||||||||||||
| 23 | 10 | my ($self) = @_; | |||||||||||
| 24 | |||||||||||||
| 25 | 10 | my $temp_dbh = DBI->connect('dbi:SQLite::memory:', '', '', { | |||||||||||
| 26 | RaiseError => 1, PrintError => 0, AutoCommit => 1, | ||||||||||||
| 27 | }); | ||||||||||||
| 28 | 10 | $temp_dbh->do('PRAGMA foreign_keys = ON'); | |||||||||||
| 29 | |||||||||||||
| 30 | 10 | $self->_execute_ddl($temp_dbh, $self->_install_ddl); | |||||||||||
| 31 | |||||||||||||
| 32 | 10 | my $model = $self->_new_introspect($temp_dbh)->model; | |||||||||||
| 33 | 10 | $temp_dbh->disconnect; | |||||||||||
| 34 | 10 | return $model; | |||||||||||
| 35 | } | ||||||||||||
| 36 | |||||||||||||
| 37 | |||||||||||||
| 38 | 5 | ▶ | sub apply { | ||||||||||
|
Subroutine
apply: called CC=5 SCAR=17.4
Pod
documented
| |||||||||||||
| 39 | 5 | my ($self, $diff) = @_; | |||||||||||
| 40 | 5 | my $ret = $self->SUPER::apply($diff); | |||||||||||
| 41 | 5 | ▶ | return $ret unless $ret; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 42 | |||||||||||||
| 43 | return $ret | ||||||||||||
| 44 | 4 | ▶ | unless grep { $_->isa('DBIO::SQLite::Diff::Rebuild') } @{ $diff->operations }; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 45 | |||||||||||||
| 46 | 2 | my $bad = $self->_dbh->selectall_arrayref('PRAGMA foreign_key_check'); | |||||||||||
| 47 | 2 | ▶ | if (@$bad) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 48 | 0 | ▶ | my %tables = map { ($_->[0] // '?') => 1 } @$bad; | ||||||||||
|
Condition0%0/2
$_->[0] // "?"
MC/DC0%0/1
$_->[0] // "?"
$_->[0]
Truth table0%0/2
A
A= $_->[0]
| |||||||||||||
| 49 | 0 | $self->schema->storage->throw_exception( | |||||||||||
| 50 | 'Foreign key violations after table rebuild: ' . join(', ', sort keys %tables) | ||||||||||||
| 51 | ); | ||||||||||||
| 52 | } | ||||||||||||
| 53 | |||||||||||||
| 54 | 2 | return $ret; | |||||||||||
| 55 | } | ||||||||||||
| 56 | |||||||||||||
| 57 | 194 | 1; | |||||||||||
| 58 | |||||||||||||
| 59 | __END__ | ||||||||||||
| 60 | |||||||||||||
| 61 | =pod | ||||||||||||
| 62 | |||||||||||||
| 63 | =encoding UTF-8 | ||||||||||||
| 64 | |||||||||||||
| 65 | =head1 NAME | ||||||||||||
| 66 | |||||||||||||
| 67 | DBIO::SQLite::Deploy - Deploy and upgrade SQLite schemas via test-deploy-and-compare | ||||||||||||
| 68 | |||||||||||||
| 69 | =head1 VERSION | ||||||||||||
| 70 | |||||||||||||
| 71 | version 0.900001 | ||||||||||||
| 72 | |||||||||||||
| 73 | =head1 DESCRIPTION | ||||||||||||
| 74 | |||||||||||||
| 75 | C<DBIO::SQLite::Deploy> orchestrates the deployment and upgrade of | ||||||||||||
| 76 | SQLite schemas using a test-deploy-and-compare strategy. | ||||||||||||
| 77 | |||||||||||||
| 78 | For upgrades, instead of computing diffs from abstract class | ||||||||||||
| 79 | representations, it: | ||||||||||||
| 80 | |||||||||||||
| 81 | =over 4 | ||||||||||||
| 82 | |||||||||||||
| 83 | =item 1. Introspects the live database via C<sqlite_master> and PRAGMAs | ||||||||||||
| 84 | |||||||||||||
| 85 | =item 2. Connects to a fresh in-memory SQLite database | ||||||||||||
| 86 | |||||||||||||
| 87 | =item 3. Deploys the desired schema (from DBIO classes) into the in-memory DB | ||||||||||||
| 88 | |||||||||||||
| 89 | =item 4. Introspects the in-memory database the same way | ||||||||||||
| 90 | |||||||||||||
| 91 | =item 5. Computes the diff between the two models using L<DBIO::SQLite::Diff> | ||||||||||||
| 92 | |||||||||||||
| 93 | =back | ||||||||||||
| 94 | |||||||||||||
| 95 | The temp DB is in-memory and goes away when the connection drops -- much | ||||||||||||
| 96 | simpler than the temp-database approach used for PostgreSQL. | ||||||||||||
| 97 | |||||||||||||
| 98 | The orchestration (C<install>, C<apply>, C<upgrade>) and the C<diff> | ||||||||||||
| 99 | shell come from L<DBIO::Deploy::Base>; only the engine-specific | ||||||||||||
| 100 | target-model build is overridden here. | ||||||||||||
| 101 | |||||||||||||
| 102 | my $deploy = DBIO::SQLite::Deploy->new( | ||||||||||||
| 103 | schema => MyApp::DB->connect("dbi:SQLite:dbname=app.db"), | ||||||||||||
| 104 | ); | ||||||||||||
| 105 | |||||||||||||
| 106 | # Fresh install | ||||||||||||
| 107 | $deploy->install; | ||||||||||||
| 108 | |||||||||||||
| 109 | # Upgrade | ||||||||||||
| 110 | $deploy->upgrade; | ||||||||||||
| 111 | |||||||||||||
| 112 | # Or in steps: | ||||||||||||
| 113 | my $diff = $deploy->diff; | ||||||||||||
| 114 | print $diff->summary; | ||||||||||||
| 115 | $deploy->apply($diff) if $diff->has_changes; | ||||||||||||
| 116 | |||||||||||||
| 117 | =head1 METHODS | ||||||||||||
| 118 | |||||||||||||
| 119 | =head2 _build_target_model | ||||||||||||
| 120 | |||||||||||||
| 121 | Connect to a throwaway in-memory SQLite database, deploy the desired | ||||||||||||
| 122 | schema into it, and return its introspected model. The in-memory | ||||||||||||
| 123 | connection goes out of scope at the end of this method (and is | ||||||||||||
| 124 | disconnected explicitly for predictability), so the target DB is gone | ||||||||||||
| 125 | before the diff object is built. | ||||||||||||
| 126 | |||||||||||||
| 127 | =head2 apply | ||||||||||||
| 128 | |||||||||||||
| 129 | Apply the diff, then -- if it contained a table rebuild -- verify cross-table | ||||||||||||
| 130 | foreign-key integrity. A rebuild runs with C<PRAGMA foreign_keys=OFF> so it | ||||||||||||
| 131 | can drop and rename a referenced table; once it is back in place this runs | ||||||||||||
| 132 | C<PRAGMA foreign_key_check> and throws if any dangling references remain. | ||||||||||||
| 133 | Non-rebuild upgrades are untouched (no extra check). | ||||||||||||
| 134 | |||||||||||||
| 135 | =over 4 | ||||||||||||
| 136 | |||||||||||||
| 137 | =item * L<DBIO::SQLite> - schema component | ||||||||||||
| 138 | |||||||||||||
| 139 | =item * L<DBIO::SQLite::DDL> - generates DDL used by C<install> and C<diff> | ||||||||||||
| 140 | |||||||||||||
| 141 | =item * L<DBIO::SQLite::Introspect> - reads live database state | ||||||||||||
| 142 | |||||||||||||
| 143 | =item * L<DBIO::SQLite::Diff> - compares two introspected models | ||||||||||||
| 144 | |||||||||||||
| 145 | =item * L<DBIO::Deploy::Base> - shared install/apply/upgrade orchestration | ||||||||||||
| 146 | |||||||||||||
| 147 | =item * L<DBIO::PostgreSQL::Deploy> - the PostgreSQL counterpart (temp-database variant) | ||||||||||||
| 148 | |||||||||||||
| 149 | =back | ||||||||||||
| 150 | |||||||||||||
| 151 | =head1 AUTHOR | ||||||||||||
| 152 | |||||||||||||
| 153 | DBIO & DBIx::Class Authors | ||||||||||||
| 154 | |||||||||||||
| 155 | =head1 COPYRIGHT AND LICENSE | ||||||||||||
| 156 | |||||||||||||
| 157 | Copyright (C) 2026 DBIO Authors | ||||||||||||
| 158 | Portions Copyright (C) 2005-2025 DBIx::Class Authors | ||||||||||||
| 159 | Based on DBIx::Class, heavily modified. | ||||||||||||
| 160 | |||||||||||||
| 161 | This is free software; you can redistribute it and/or modify it under | ||||||||||||
| 162 | the same terms as the Perl 5 programming language system itself. | ||||||||||||
| 163 | |||||||||||||
| 164 | =cut | ||||||||||||