| 1 | package DBIO::DuckDB::Deploy; | ||||||||||
| 2 | # ABSTRACT: Deploy and upgrade DuckDB schemas via test-deploy-and-compare | ||||||||||
| 3 | |||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 3 | ▶ | use base 'DBIO::Deploy::Base'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 3 | ▶ | use DBI; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 3 | ▶ | use DBIO::DuckDB::DDL; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 3 | ▶ | use DBIO::DuckDB::Introspect; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 3 | ▶ | use DBIO::DuckDB::Diff; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | |||||||||||
| 15 | 0 | ▶ | sub catalog { $_[0]->{catalog} } | ||||||||
|
Subroutine
catalog: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 16 | |||||||||||
| 17 | 0 | ▶ | sub _ddl_class { 'DBIO::DuckDB::DDL' } | ||||||||
|
Subroutine
_ddl_class: not called CC=1 SCAR=6.9
| |||||||||||
| 18 | 0 | ▶ | sub _introspect_class { 'DBIO::DuckDB::Introspect' } | ||||||||
|
Subroutine
_introspect_class: not called CC=1 SCAR=6.9
| |||||||||||
| 19 | 0 | ▶ | sub _diff_class { 'DBIO::DuckDB::Diff' } | ||||||||
|
Subroutine
_diff_class: not called CC=1 SCAR=6.9
| |||||||||||
| 20 | |||||||||||
| 21 | |||||||||||
| 22 | |||||||||||
| 23 | |||||||||||
| 24 | 0 | ▶ | sub _new_introspect { | ||||||||
|
Subroutine
_new_introspect: not called CC=2 SCAR=17.9
| |||||||||||
| 25 | 0 | my ($self, $dbh) = @_; | |||||||||
| 26 | 0 | my %opts = (dbh => $dbh); | |||||||||
| 27 | 0 | ▶ | $opts{catalog} = $self->catalog if defined $self->catalog; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 28 | 0 | return $self->_introspect_class->new(%opts); | |||||||||
| 29 | } | ||||||||||
| 30 | |||||||||||
| 31 | |||||||||||
| 32 | 0 | ▶ | sub _build_target_model { | ||||||||
|
Subroutine
_build_target_model: not called CC=1 SCAR=6.9
| |||||||||||
| 33 | 0 | my ($self) = @_; | |||||||||
| 34 | |||||||||||
| 35 | 0 | my $temp_dbh = DBI->connect('dbi:DuckDB:dbname=:memory:', '', '', { | |||||||||
| 36 | RaiseError => 1, PrintError => 0, AutoCommit => 1, | ||||||||||
| 37 | }); | ||||||||||
| 38 | |||||||||||
| 39 | 0 | $self->_execute_ddl($temp_dbh, $self->_install_ddl); | |||||||||
| 40 | |||||||||||
| 41 | 0 | my $target_model = $self->_introspect_class->new(dbh => $temp_dbh)->model; | |||||||||
| 42 | 0 | $temp_dbh->disconnect; | |||||||||
| 43 | |||||||||||
| 44 | 0 | return $target_model; | |||||||||
| 45 | } | ||||||||||
| 46 | |||||||||||
| 47 | |||||||||||
| 48 | 3 | 1; | |||||||||
| 49 | |||||||||||
| 50 | __END__ | ||||||||||
| 51 | |||||||||||
| 52 | =pod | ||||||||||
| 53 | |||||||||||
| 54 | =encoding UTF-8 | ||||||||||
| 55 | |||||||||||
| 56 | =head1 NAME | ||||||||||
| 57 | |||||||||||
| 58 | DBIO::DuckDB::Deploy - Deploy and upgrade DuckDB schemas via test-deploy-and-compare | ||||||||||
| 59 | |||||||||||
| 60 | =head1 VERSION | ||||||||||
| 61 | |||||||||||
| 62 | version 0.900001 | ||||||||||
| 63 | |||||||||||
| 64 | =head1 DESCRIPTION | ||||||||||
| 65 | |||||||||||
| 66 | C<DBIO::DuckDB::Deploy> orchestrates schema deployment and upgrades for | ||||||||||
| 67 | DuckDB using the test-deploy-and-compare strategy, parallel to | ||||||||||
| 68 | L<DBIO::SQLite::Deploy> and L<DBIO::PostgreSQL::Deploy>. | ||||||||||
| 69 | |||||||||||
| 70 | For upgrades it: | ||||||||||
| 71 | |||||||||||
| 72 | =over 4 | ||||||||||
| 73 | |||||||||||
| 74 | =item 1. Introspects the live database via C<information_schema> / C<duckdb_*> | ||||||||||
| 75 | |||||||||||
| 76 | =item 2. Connects to a fresh in-memory DuckDB database | ||||||||||
| 77 | |||||||||||
| 78 | =item 3. Deploys the desired schema (from DBIO classes) into that in-memory DB | ||||||||||
| 79 | |||||||||||
| 80 | =item 4. Introspects the in-memory database the same way | ||||||||||
| 81 | |||||||||||
| 82 | =item 5. Computes the diff between the two models using L<DBIO::DuckDB::Diff> | ||||||||||
| 83 | |||||||||||
| 84 | =back | ||||||||||
| 85 | |||||||||||
| 86 | DuckDB supports C<:memory:> so the throwaway DB is as cheap as with | ||||||||||
| 87 | SQLite -- no CREATE DATABASE dance like PostgreSQL. | ||||||||||
| 88 | |||||||||||
| 89 | my $deploy = DBIO::DuckDB::Deploy->new( | ||||||||||
| 90 | schema => MyApp::DB->connect("dbi:DuckDB:dbname=app.duckdb"), | ||||||||||
| 91 | ); | ||||||||||
| 92 | $deploy->install; # fresh | ||||||||||
| 93 | my $diff = $deploy->diff; # or step-by-step | ||||||||||
| 94 | $deploy->apply($diff) if $diff->has_changes; | ||||||||||
| 95 | $deploy->upgrade; # convenience | ||||||||||
| 96 | |||||||||||
| 97 | =head1 ATTRIBUTES | ||||||||||
| 98 | |||||||||||
| 99 | =head2 schema | ||||||||||
| 100 | |||||||||||
| 101 | A connected L<DBIO::Schema> instance using the L<DBIO::DuckDB> component. | ||||||||||
| 102 | Required. | ||||||||||
| 103 | |||||||||||
| 104 | =head2 catalog | ||||||||||
| 105 | |||||||||||
| 106 | Optional catalog name (e.g. a locally-attached file database). When set, | ||||||||||
| 107 | the source introspection is scoped to this catalog. Useful for multi-catalog | ||||||||||
| 108 | deployments and DuckLake. | ||||||||||
| 109 | |||||||||||
| 110 | B<Not applicable for Quack RPC remotes.> The remote catalog is opaque to | ||||||||||
| 111 | C<information_schema> -- deploy the schema on the B<server> process instead, | ||||||||||
| 112 | not against the client-side quack attach. Use C<PRAGMA table_info('remote.t')> | ||||||||||
| 113 | on the client to verify remote columns. | ||||||||||
| 114 | |||||||||||
| 115 | =head1 METHODS | ||||||||||
| 116 | |||||||||||
| 117 | =head2 install | ||||||||||
| 118 | |||||||||||
| 119 | $deploy->install; | ||||||||||
| 120 | |||||||||||
| 121 | Generates DDL via L<DBIO::DuckDB::DDL/install_ddl> and executes each | ||||||||||
| 122 | statement against the connected database. Suitable for fresh installs. | ||||||||||
| 123 | Inherited from L<DBIO::Deploy::Base>. | ||||||||||
| 124 | |||||||||||
| 125 | =head2 diff | ||||||||||
| 126 | |||||||||||
| 127 | my $diff = $deploy->diff; | ||||||||||
| 128 | |||||||||||
| 129 | Computes the difference between the live database and the desired state. | ||||||||||
| 130 | Spins up a throwaway in-memory DuckDB, deploys the desired schema there, | ||||||||||
| 131 | introspects both, and returns a L<DBIO::DuckDB::Diff> object. The | ||||||||||
| 132 | orchestration is inherited from L<DBIO::Deploy::Base>; only the | ||||||||||
| 133 | in-memory target build (L</_build_target_model>) and the catalog-aware | ||||||||||
| 134 | source introspection (L</_new_introspect>) are DuckDB-specific. | ||||||||||
| 135 | |||||||||||
| 136 | =head2 apply | ||||||||||
| 137 | |||||||||||
| 138 | $deploy->apply($diff); | ||||||||||
| 139 | |||||||||||
| 140 | Applies a L<DBIO::DuckDB::Diff> object by executing each statement from | ||||||||||
| 141 | C<< $diff->as_sql >>. No-op if the diff has no changes. Inherited from | ||||||||||
| 142 | L<DBIO::Deploy::Base>. | ||||||||||
| 143 | |||||||||||
| 144 | =head2 upgrade | ||||||||||
| 145 | |||||||||||
| 146 | my $diff = $deploy->upgrade; | ||||||||||
| 147 | |||||||||||
| 148 | Convenience: calls L</diff> then L</apply>. Returns the diff object if | ||||||||||
| 149 | changes were applied, or C<undef> if the database was already up to date. | ||||||||||
| 150 | Inherited from L<DBIO::Deploy::Base>. | ||||||||||
| 151 | |||||||||||
| 152 | =head2 _new_introspect | ||||||||||
| 153 | |||||||||||
| 154 | Factory for the source-side introspector. Overrides | ||||||||||
| 155 | L<DBIO::Deploy::Base/_new_introspect> to forward the C<catalog> attribute | ||||||||||
| 156 | (for locally-attached file / DuckLake catalogs) when it is set. | ||||||||||
| 157 | |||||||||||
| 158 | =head2 _build_target_model | ||||||||||
| 159 | |||||||||||
| 160 | Builds the desired-state model: connects a throwaway in-memory DuckDB, | ||||||||||
| 161 | deploys the install DDL there, and introspects it. DuckDB supports | ||||||||||
| 162 | C<:memory:>, so this is as cheap as with SQLite -- no temporary-database | ||||||||||
| 163 | dance like PostgreSQL. The temp DB is never scoped to C<catalog>. | ||||||||||
| 164 | |||||||||||
| 165 | =head1 SEE ALSO | ||||||||||
| 166 | |||||||||||
| 167 | =over 4 | ||||||||||
| 168 | |||||||||||
| 169 | =item * L<DBIO::DuckDB> - schema component | ||||||||||
| 170 | |||||||||||
| 171 | =item * L<DBIO::DuckDB::DDL> - generates DDL | ||||||||||
| 172 | |||||||||||
| 173 | =item * L<DBIO::DuckDB::Introspect> - reads live database state | ||||||||||
| 174 | |||||||||||
| 175 | =item * L<DBIO::DuckDB::Diff> - compares two introspected models | ||||||||||
| 176 | |||||||||||
| 177 | =item * L<DBIO::SQLite::Deploy> - sibling implementation | ||||||||||
| 178 | |||||||||||
| 179 | =back | ||||||||||
| 180 | |||||||||||
| 181 | =head1 AUTHOR | ||||||||||
| 182 | |||||||||||
| 183 | DBIO Authors | ||||||||||
| 184 | |||||||||||
| 185 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 186 | |||||||||||
| 187 | Copyright (C) 2026 DBIO Authors | ||||||||||
| 188 | |||||||||||
| 189 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 190 | the same terms as the Perl 5 programming language system itself. | ||||||||||
| 191 | |||||||||||
| 192 | =cut | ||||||||||