| 1 | # ----------------------------------------------------------------------------- | ||||||||||
| 2 | |||||||||||
| 3 | =encoding utf8 | ||||||||||
| 4 | |||||||||||
| 5 | =head1 NAME | ||||||||||
| 6 | |||||||||||
| 7 | Quiq::Database::Patch - Definiere Patches für eine Datenbank und wende sie an (Basisklasse) | ||||||||||
| 8 | |||||||||||
| 9 | =head1 BASE CLASS | ||||||||||
| 10 | |||||||||||
| 11 | L<Quiq::Hash> | ||||||||||
| 12 | |||||||||||
| 13 | =head1 SYNOPSIS | ||||||||||
| 14 | |||||||||||
| 15 | Abgeleitete Patch-Klasse mit Patch-Methoden patch1() ... | ||||||||||
| 16 | patchN() definieren: | ||||||||||
| 17 | |||||||||||
| 18 | package MyPatchClass; | ||||||||||
| 19 | use base qw/Quiq::Database::Patch/; | ||||||||||
| 20 | |||||||||||
| 21 | sub patch1 { | ||||||||||
| 22 | my ($self,$db) = @_; | ||||||||||
| 23 | |||||||||||
| 24 | # Code Patch 1 | ||||||||||
| 25 | |||||||||||
| 26 | return; | ||||||||||
| 27 | } | ||||||||||
| 28 | |||||||||||
| 29 | sub patch2 { | ||||||||||
| 30 | my ($self,$db) = @_; | ||||||||||
| 31 | |||||||||||
| 32 | # Code Patch 2 | ||||||||||
| 33 | |||||||||||
| 34 | return; | ||||||||||
| 35 | } | ||||||||||
| 36 | |||||||||||
| 37 | # ... | ||||||||||
| 38 | |||||||||||
| 39 | Ein oder mehrere Patches auf eine Datenbank anwenden: | ||||||||||
| 40 | |||||||||||
| 41 | $db = Quiq::Database::Connection->new($udl); | ||||||||||
| 42 | $pat = MyPatchClass->new($db); | ||||||||||
| 43 | $pat->apply($n); | ||||||||||
| 44 | |||||||||||
| 45 | =head1 DESCRIPTION | ||||||||||
| 46 | |||||||||||
| 47 | Wir entwickeln eine Datenbank, indem wir fortgesetzt Patches auf | ||||||||||
| 48 | sie anwenden. Die Patches können Schema- oder Datenänderungen | ||||||||||
| 49 | betreffen. Die Patches werden fortschreitend in einer einzigen | ||||||||||
| 50 | Klasse definiert. Die Patchklasse ist von der Klasse Quiq::Database::Patch | ||||||||||
| 51 | abgeleitet. Jeder Patch wird durch eine Methode mit dem Namen | ||||||||||
| 52 | C<patch>I<N> realisiert. Hierbei ist I<N> der Patchlevel. Wir | ||||||||||
| 53 | heben die Datenbank auf Patchlevel $n, indem wir die Methode | ||||||||||
| 54 | C<< $pat->apply($n) >> aufrufen. Alle Patches vom aktuellen | ||||||||||
| 55 | Patchlevel+1 bis $n werden dabei nacheinander auf die Datenbank | ||||||||||
| 56 | angewandt. Ist der aktuelle Patchlevel gleich oder größer dem | ||||||||||
| 57 | angeforderten Patchlevel $n, wird kein Patch angewandt. Auf einen | ||||||||||
| 58 | früheren Patchlevel als den aktuellen Patchlevel kann nicht | ||||||||||
| 59 | zurückgegangen werden. Soll ein Patch zurückgenommen werden, ist | ||||||||||
| 60 | ein weiterer Patch zu schreiben, der diesen rückgängig macht. | ||||||||||
| 61 | Jeder Patch wird einzeln committet. Der aktuelle Patchlevel ist in | ||||||||||
| 62 | der Tabelle C<PATCHLEVEL> festgehalten. Diese wird beim ersten | ||||||||||
| 63 | Aufruf der Methode C<< $class->new($db) >> automatisch angelegt. | ||||||||||
| 64 | |||||||||||
| 65 | =cut | ||||||||||
| 66 | |||||||||||
| 67 | # ----------------------------------------------------------------------------- | ||||||||||
| 68 | |||||||||||
| 69 | package Quiq::Database::Patch; | ||||||||||
| 70 | 1 | ▶ | use base qw/Quiq::Hash/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 71 | |||||||||||
| 72 | 1 | ▶ | use v5.10; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 73 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 74 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 75 | |||||||||||
| 76 | 1 | our $VERSION = '1.240'; | |||||||||
| 77 | |||||||||||
| 78 | # ----------------------------------------------------------------------------- | ||||||||||
| 79 | |||||||||||
| 80 | =head1 METHODS | ||||||||||
| 81 | |||||||||||
| 82 | =head2 Konstruktor | ||||||||||
| 83 | |||||||||||
| 84 | =head3 new() - Instantiiere Objekt | ||||||||||
| 85 | |||||||||||
| 86 | =head4 Synopsis | ||||||||||
| 87 | |||||||||||
| 88 | $pat = $class->new($db); | ||||||||||
| 89 | |||||||||||
| 90 | =head4 Arguments | ||||||||||
| 91 | |||||||||||
| 92 | =over 4 | ||||||||||
| 93 | |||||||||||
| 94 | =item $db | ||||||||||
| 95 | |||||||||||
| 96 | (Object) Datenbankverbindung | ||||||||||
| 97 | |||||||||||
| 98 | =back | ||||||||||
| 99 | |||||||||||
| 100 | =head4 Returns | ||||||||||
| 101 | |||||||||||
| 102 | Patch-Object | ||||||||||
| 103 | |||||||||||
| 104 | =head4 Description | ||||||||||
| 105 | |||||||||||
| 106 | Instantiiere eine Objekt der Klasse und liefere eine Referenz auf | ||||||||||
| 107 | dieses Objekt zurück. | ||||||||||
| 108 | |||||||||||
| 109 | =cut | ||||||||||
| 110 | |||||||||||
| 111 | # ----------------------------------------------------------------------------- | ||||||||||
| 112 | |||||||||||
| 113 | 1 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 114 | 1 | my ($class,$db) = @_; | |||||||||
| 115 | |||||||||||
| 116 | # Patchlevel-Tabelle anlegen, falls sie nicht existiert | ||||||||||
| 117 | |||||||||||
| 118 | 1 | ▶ | if (!$db->tableExists('patchlevel')) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 119 | 1 | $db->createTable('patchlevel', | |||||||||
| 120 | ['pat_id',type=>'INTEGER',notNull=>1], | ||||||||||
| 121 | ); | ||||||||||
| 122 | 1 | $db->insert('patchlevel',pat_id=>0); | |||||||||
| 123 | } | ||||||||||
| 124 | |||||||||||
| 125 | # Instantiiere Objekt | ||||||||||
| 126 | |||||||||||
| 127 | 1 | return $class->SUPER::new( | |||||||||
| 128 | db => $db, | ||||||||||
| 129 | ); | ||||||||||
| 130 | } | ||||||||||
| 131 | |||||||||||
| 132 | # ----------------------------------------------------------------------------- | ||||||||||
| 133 | |||||||||||
| 134 | =head2 Klassenmethoden | ||||||||||
| 135 | |||||||||||
| 136 | =head3 maxLevel() - Liefere den höchsten möglichen Patchlevel | ||||||||||
| 137 | |||||||||||
| 138 | =head4 Synopsis | ||||||||||
| 139 | |||||||||||
| 140 | $level = this->maxLevel; | ||||||||||
| 141 | |||||||||||
| 142 | =head4 Returns | ||||||||||
| 143 | |||||||||||
| 144 | (Integer) Patchlevel | ||||||||||
| 145 | |||||||||||
| 146 | =head4 Description | ||||||||||
| 147 | |||||||||||
| 148 | Ermittele den höchsten möglichen Patchlevel und liefere diesen zurück. | ||||||||||
| 149 | |||||||||||
| 150 | =cut | ||||||||||
| 151 | |||||||||||
| 152 | # ----------------------------------------------------------------------------- | ||||||||||
| 153 | |||||||||||
| 154 | 1 | ▶ | sub maxLevel { | ||||||||
|
Subroutine
maxLevel: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 155 | 1 | my $this = shift; | |||||||||
| 156 | |||||||||||
| 157 | 1 | my $level = 0; | |||||||||
| 158 | |||||||||||
| 159 | 1 | while (1) { | |||||||||
| 160 | 3 | my $method = sprintf 'patch%d',$level+1; | |||||||||
| 161 | 3 | ▶ | if (!$this->can($method)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 162 | 1 | last; | |||||||||
| 163 | } | ||||||||||
| 164 | 2 | $level++; | |||||||||
| 165 | } | ||||||||||
| 166 | |||||||||||
| 167 | 1 | return $level; | |||||||||
| 168 | } | ||||||||||
| 169 | |||||||||||
| 170 | # ----------------------------------------------------------------------------- | ||||||||||
| 171 | |||||||||||
| 172 | =head2 Objektmethoden | ||||||||||
| 173 | |||||||||||
| 174 | =head3 apply() - Wende Patch(es) an | ||||||||||
| 175 | |||||||||||
| 176 | =head4 Synopsis | ||||||||||
| 177 | |||||||||||
| 178 | $pat->apply($level); | ||||||||||
| 179 | |||||||||||
| 180 | =head4 Arguments | ||||||||||
| 181 | |||||||||||
| 182 | =over 4 | ||||||||||
| 183 | |||||||||||
| 184 | =item $level | ||||||||||
| 185 | |||||||||||
| 186 | (Integer) Patchlevel | ||||||||||
| 187 | |||||||||||
| 188 | =back | ||||||||||
| 189 | |||||||||||
| 190 | =head4 Description | ||||||||||
| 191 | |||||||||||
| 192 | Wende alle Patches an, bis Patchlevel $level erreicht ist. | ||||||||||
| 193 | |||||||||||
| 194 | =cut | ||||||||||
| 195 | |||||||||||
| 196 | # ----------------------------------------------------------------------------- | ||||||||||
| 197 | |||||||||||
| 198 | 1 | ▶ | sub apply { | ||||||||
|
Subroutine
apply: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 199 | 1 | my ($self,$level) = @_; | |||||||||
| 200 | |||||||||||
| 201 | 1 | my $db = $self->db; | |||||||||
| 202 | |||||||||||
| 203 | # Aktuellen Patchlevel ermitteln | ||||||||||
| 204 | 1 | my $currLevel = $self->currentLevel; | |||||||||
| 205 | |||||||||||
| 206 | # Überprüfe die Existenz der erforderlichen Patch-Methoden | ||||||||||
| 207 | |||||||||||
| 208 | 1 | ▶ | for (my $i = $currLevel+1; $i <= $level; $i++) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 209 | 2 | my $method = sprintf 'patch%d',$i; | |||||||||
| 210 | 2 | ▶ | if (!$self->can($method)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 211 | 0 | $self->throw( | |||||||||
| 212 | 'PATCH-00001: Patch method does not exist', | ||||||||||
| 213 | Patch => $method, | ||||||||||
| 214 | ); | ||||||||||
| 215 | } | ||||||||||
| 216 | } | ||||||||||
| 217 | |||||||||||
| 218 | # Wende Patch-Methoden an | ||||||||||
| 219 | |||||||||||
| 220 | 1 | ▶ | for (my $i = $currLevel+1; $i <= $level; $i++) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 221 | 2 | my $method = sprintf 'patch%d',$i; | |||||||||
| 222 | 2 | say "Applying $method()..."; | |||||||||
| 223 | 2 | $db->begin; | |||||||||
| 224 | 2 | $self->$method($db); | |||||||||
| 225 | 2 | $db->update('patchlevel',pat_id=>$i); | |||||||||
| 226 | 2 | $db->commit; | |||||||||
| 227 | } | ||||||||||
| 228 | |||||||||||
| 229 | 1 | return; | |||||||||
| 230 | } | ||||||||||
| 231 | |||||||||||
| 232 | # ----------------------------------------------------------------------------- | ||||||||||
| 233 | |||||||||||
| 234 | =head3 currentLevel() - Liefere aktuellen Patchlevel | ||||||||||
| 235 | |||||||||||
| 236 | =head4 Synopsis | ||||||||||
| 237 | |||||||||||
| 238 | $level = $pat->currentLevel; | ||||||||||
| 239 | |||||||||||
| 240 | =head4 Returns | ||||||||||
| 241 | |||||||||||
| 242 | (Integer) Patchlevel | ||||||||||
| 243 | |||||||||||
| 244 | =head4 Description | ||||||||||
| 245 | |||||||||||
| 246 | Ermittele den aktuellen Patchlevel und liefere diesen zurück. | ||||||||||
| 247 | |||||||||||
| 248 | =cut | ||||||||||
| 249 | |||||||||||
| 250 | # ----------------------------------------------------------------------------- | ||||||||||
| 251 | |||||||||||
| 252 | 3 | ▶ | sub currentLevel { | ||||||||
|
Subroutine
currentLevel: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 253 | 3 | return shift->db->value('patchlevel','pat_id'); | |||||||||
| 254 | } | ||||||||||
| 255 | |||||||||||
| 256 | # ----------------------------------------------------------------------------- | ||||||||||
| 257 | |||||||||||
| 258 | =head1 VERSION | ||||||||||
| 259 | |||||||||||
| 260 | 1.240 | ||||||||||
| 261 | |||||||||||
| 262 | =head1 AUTHOR | ||||||||||
| 263 | |||||||||||
| 264 | Frank Seitz, L<http://fseitz.de/> | ||||||||||
| 265 | |||||||||||
| 266 | =head1 COPYRIGHT | ||||||||||
| 267 | |||||||||||
| 268 | Copyright (C) 2026 Frank Seitz | ||||||||||
| 269 | |||||||||||
| 270 | =head1 LICENSE | ||||||||||
| 271 | |||||||||||
| 272 | This code is free software; you can redistribute it and/or modify | ||||||||||
| 273 | it under the same terms as Perl itself. | ||||||||||
| 274 | |||||||||||
| 275 | =cut | ||||||||||
| 276 | |||||||||||
| 277 | # ----------------------------------------------------------------------------- | ||||||||||
| 278 | |||||||||||
| 279 | 1 | 1; | |||||||||
| 280 | |||||||||||
| 281 | # eof | ||||||||||