blib/lib/AmberDB/Tools/Update.pm untested

filter: Statementstmt 0.0% 0 / 736 Branchbran 0.0% 0 / 270 Conditioncond 0.0% 0 / 281 MC/DCmcdc 0.0% 0 / 155 Subroutinesub 0.0% 0 / 43 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 1497 CC 136 SCAR 64.5
CC
136
Cov
0%
update_table
1560.0
update_storage
992.0
_inline_migrate_5_25_0
650.0
CRAP 632.0
1 package AmberDB::Tools::Update;
2  
3 use 5.016;
4 use warnings;
5 use strict;
6 use Carp qw(croak cluck);
7 use File::Spec;
8 use File::Path qw(make_path);
9 use File::Copy qw(move);
10 use Cwd qw(abs_path getcwd);
11 use version;
12  
13 our $VERSION = '5.26.6';
14 my $CREATED = '2018-10-08';
15  
16 sub new {
17  
18 my $class = shift;
19 my $self = {};
20  
21 require AmberDB;
22  
23 my ( $adb, %inputs );
24  
25 if ( ref( $_[0] ) ) {
26 $adb = shift;
27 %inputs = @_;
28 }
29 else {
30 %inputs = @_;
31 $adb = AmberDB->new(%inputs);
32 }
33  
34 $self->{_adb} = $adb;
35  
36 foreach my $in ( keys %inputs ) {
37 $self->{ uc($in) } = $inputs{$in};
38 }
39 $self->{say} = "";
40  
41 bless $self, $class;
42 return $self;
43 }
44  
45 # Helper to binary copy a file safely without external modules
46 sub _copy_file {
47 my ( $self, $src, $dst ) = @_;
48 return 0 unless -e $src;
49 open my $in, '<:raw', $src or return 0;
50 open my $out, '>:raw', $dst or do { close $in; return 0; };
51 my $buf;
52 while ( read( $in, $buf, 65536 ) ) {
53 print $out $buf;
54 }
55 close $in;
56 close $out;
57 return 1;
58 }
59  
60 # ---------------------------------------------------------------------
61 # update_table($tableid, %options)
62 # Scans an entire table record-by-record, detects any legacy formats (2003-2026),
63 # creates a timestamped backup with detected dominant version, and rewrites the table
64 # directly in ABR v1 format preserving original IDs.
65 # Rebuilds indexes using ($self->can('set_index') ? $self : do { require AmberDB::Tools::Index; AmberDB::Tools::Index->new($adb); })->set_index($tableid).
66 # Preserves companion data files ('del', 'aut').
67 # ---------------------------------------------------------------------
68 sub update_table {
69 my ( $self, $tableid, %opts ) = @_;
70 my $adb = $self->{_adb} or return;
71 $tableid or return;
72  
73 my $table_path = $adb->table_path($tableid);
74 my $ext = $adb->{db_ext} || "db";
75 my $file_path = "$table_path.$ext";
76  
77 # Use exist_table to verify primary table existence
78 return { status => 'not_found', table => $tableid }
79 unless $adb->exist_table( $tableid );
80  
81 # Format modification timestamp: YYYY-MMDD
82 my @mtime_parts = localtime( ( stat($file_path) )[9] || time() );
83 my $mtime_str = sprintf( "%04d-%02d%02d", $mtime_parts[5] + 1900, $mtime_parts[4] + 1, $mtime_parts[3] );
84  
85 # 1. Once mevcut tablodaki kayitlari tara
86 my %format_counts;
87 my $total = 0;
88 my $already_current = 0;
89 my $legacy_count = 0;
90 my @decoded_records;
91 my $recovered_from_backup = 0;
92  
93 $adb->recs_scan( $file_path, sub {
94 my ( $k, $v ) = @_;
95 $total++;
96 my $fmt = $adb->detect_tsv_format($v);
97 $format_counts{$fmt}++;
98  
99 my @fields;
100 if ( $fmt eq 'v5' ) {
101 @fields = $adb->db_decode($v);
102 $already_current++;
103 }
104 else {
105 @fields = $adb->tsv_decode( $v, $k );
106 $legacy_count++;
107 }
108 push @decoded_records, [ $k, @fields ];
109 } );
110 $adb->table_close($file_path);
111  
112 # Eger aktif dosya bos ise (ornegin onceki basarisiz bir calismada yedeklenip bos kalmissa):
113 # Dizin icindeki yedek dosyasini bulup kayitlari oradan kurtar
114 my $backup_file;
115 if ( $total == 0 ) {
116 my ( $pdir ) = $file_path =~ m{^(.*)[/\\]};
117 $pdir //= ".";
118 my @cand_backups;
119 if ( opendir( my $dh, $pdir ) ) {
120 while ( my $f = readdir($dh) ) {
121 next if $f eq '.' || $f eq '..';
122 if ( $f =~ /^\Q$tableid\E-.*?\.\Q$ext\E$/i ) {
123 push @cand_backups, "$pdir/$f";
124 }
125 }
126 closedir($dh);
127 }
128 @cand_backups = sort { ( -s $b ) <=> ( -s $a ) } @cand_backups;
129 for my $cb (@cand_backups) {
130 next unless -e $cb && -s $cb;
131 my @b_records;
132 my %b_counts;
133 my $b_legacy = 0;
134 $adb->recs_scan( $cb, sub {
135 my ( $k, $v ) = @_;
136 my $fmt = $adb->detect_tsv_format($v);
137 $b_counts{$fmt}++;
138 my @fields = ( $fmt eq 'v5' ) ? $adb->db_decode($v) : $adb->tsv_decode( $v, $k );
139 $b_legacy++ if $fmt ne 'v5';
140 push @b_records, [ $k, @fields ];
141 } );
142 $adb->table_close($cb);
143 if (@b_records) {
144 @decoded_records = @b_records;
145 %format_counts = %b_counts;
146 $total = scalar @b_records;
147 $legacy_count = $b_legacy;
148 $already_current = $total - $b_legacy;
149 $recovered_from_backup = 1;
150 $backup_file = $cb;
151 last;
152 }
153 }
154 }
155  
156 # 2. Eslikci veri dosyalarini ('del', 'aut') tara ve formatlarini tespit et
157 my %companion_info;
158 my $companion_legacy_total = 0;
159 for my $cext (qw(del aut)) {
160 next unless $adb->exist_table( $tableid, $cext );
161 my $cfile = "$table_path.$cext";
162 my $c_total = 0;
163 my $c_legacy = 0;
164 my $c_current = 0;
165 my @c_decoded;
166  
167 $adb->recs_scan( $cfile, sub {
168 my ( $ck, $cv ) = @_;
169 $c_total++;
170 my $cfmt = $adb->detect_tsv_format($cv);
171 if ( $cfmt eq 'v5' ) {
172 $c_current++;
173 push @c_decoded, [ $ck, $adb->db_decode($cv) ];
174 }
175 else {
176 $c_legacy++;
177 push @c_decoded, [ $ck, $adb->tsv_decode( $cv, $ck ) ];
178 }
179 } );
180 $adb->table_close($cfile);
181  
182 $companion_legacy_total += $c_legacy;
183 $companion_info{$cext} = {
184 exists => 1,
185 file => $cfile,
186 total => $c_total,
187 legacy => $c_legacy,
188 current => $c_current,
189 decoded => \@c_decoded,
190 };
191 }
192  
193 # Eksik indeks dosyalarini kontrol et
194 my $table_info = $adb->table_info($tableid);
195 my $missing_indexes = 0;
196 if ($table_info) {
197 if ( $table_info->{record_index} && !-e "$table_path.inx" ) {
198 $missing_indexes = 1;
199 }
200 if ( $table_info->{match_block} && !-e "$table_path.fld" ) {
201 $missing_indexes = 1;
202 }
203 if ( $table_info->{use_facet} && !-e "$table_path.fac" ) {
204 $missing_indexes = 1;
205 }
206 }
207  
208 # Eger ana tablo ve tum eslikci dosyalar zaten guncelse, eksik indeks yoksa ve force istenmemisse
209 if ( $legacy_count == 0 && $companion_legacy_total == 0 && !$opts{force} && !$recovered_from_backup && !$missing_indexes ) {
210 $self->{say} .= "Table '$tableid' is already up to date in ABR v1 format ($already_current records).\n";
211 my $res = {
212 status => 'already_current',
213 table => $tableid,
214 total => $total,
215 updated => 0,
216 already_current => $already_current,
217 };
218 for my $cext (qw(del aut)) {
219 if ( my $ci = $companion_info{$cext} ) {
220 $res->{"${cext}_status"} = ( $ci->{total} == 0 )
221 ? "0 records (empty)"
222 : "already ABR v1 ($ci->{total} records)";
223 }
224 }
225 return $res;
226 }
227  
228 # 3. En baskin eski formati tespit et
229 my $dom_ver = 'v4';
230 my $max_c = -1;
231 for my $v (qw(v4 v3 v2 v1)) {
232 if ( ( $format_counts{$v} // 0 ) > $max_c ) {
233 $max_c = $format_counts{$v} // 0;
234 $dom_ver = $v;
235 }
236 }
237  
238 # 4. Ana tablo donusumu (gerekliyse veya force ise)
239 if ( $legacy_count > 0 || $recovered_from_backup || $opts{force} ) {
240 # Eger yedekten kurtarilmadiysa, aktif dosyayi nihai yedek adiyla yedekle
241 if ( !$recovered_from_backup ) {
242 my $backup_base = "$table_path-$dom_ver-$mtime_str";
243 $backup_file = "$backup_base.$ext";
244 my $counter = 1;
245 while ( -e $backup_file ) {
246 $backup_file = "$backup_base-$counter.$ext";
247 $counter++;
248 }
249 rename( $file_path, $backup_file ) or do {
250 cluck "[DB_TOOL] Could not backup $file_path to $backup_file: $!\n";
251 return { status => 'error', table => $tableid, error => "Backup failed: $!" };
252 };
253 }
254  
255 # Yeni dosyayi sifir temiz yazma modunda ac
256 $adb->table_close($file_path);
257 $adb->clear_cache($tableid);
258 my $new_db = $adb->table_write($file_path);
259 unless ($new_db) {
260 return { status => 'error', table => $tableid, error => "Could not open $file_path for writing" };
261 }
262  
263 # Kayitlari yeni ikili ABR v1 formatinda tek tek yaz
264 $self->{say} .= " - Writing $total records in ABR v1 format...\n";
265 for my $rec (@decoded_records) {
266 my ( $k, @fields ) = @$rec;
267 my $v_new = $adb->db_encode(@fields);
268 my $k_enc = $adb->utf_encode("$k");
269 my $val_enc = $adb->utf_encode("$v_new");
270 $new_db->put( $k_enc, $val_enc );
271 }
272 $adb->table_close($file_path);
273  
274 # Eski turetilen indeks dosyalarini temizle
275 for my $iext (qw(inx src fld fac slg ajw ajn)) {
276 my $idx_f = "$table_path.$iext";
277 unlink $idx_f if -e $idx_f;
278 }
279 $adb->clear_cache($tableid);
280 }
281 else {
282 $backup_file //= "None (already ABR v1)";
283 $self->{say} .= " - Main table is already in ABR v1 format ($already_current records).\n";
284 }
285  
286 # 5. Eslikci veri dosyalarini ('del', 'aut') donustur (indeks insasindan once calisir)
287 my %companion_stats;
288 for my $cext (qw(del aut)) {
289 my $ci = $companion_info{$cext};
290 next unless $ci;
291 my $cfile = $ci->{file};
292  
293 if ( $ci->{legacy} > 0 || $opts{force} ) {
294 if ( $ci->{total} > 0 ) {
295 $self->{say} .= " - Migrating companion file ($cext: $ci->{total} records)...\n";
296 my $c_backup_base = "$table_path-$dom_ver-$mtime_str.$cext";
297 my $c_backup = $c_backup_base;
298 my $cnt = 1;
299 while ( -e $c_backup ) {
300 $c_backup = "$table_path-$dom_ver-$mtime_str-$cnt.$cext";
301 $cnt++;
302 }
303 rename( $cfile, $c_backup ) or next;
304  
305 my $new_cdb = $adb->table_write($cfile);
306 if ($new_cdb) {
307 for my $crec ( @{ $ci->{decoded} } ) {
308 my ( $ck, @cfields ) = @$crec;
309 my $cv_new = $adb->db_encode(@cfields);
310 $new_cdb->put( $adb->utf_encode("$ck"), $adb->utf_encode("$cv_new") );
311 }
312 $adb->table_close($cfile);
313 $companion_stats{"${cext}_migrated"} = $ci->{total};
314 $companion_stats{"${cext}_backup"} = $c_backup;
315 }
316 else {
317 rename( $c_backup, $cfile );
318 }
319 }
320 else {
321 $companion_stats{"${cext}_status"} = "0 records (empty)";
322 }
323 }
324 else {
325 $companion_stats{"${cext}_status"} = "already ABR v1 ($ci->{total} records)";
326 }
327 }
328  
329 # 6. Indeksleri insa et (gerekliyse veya eksikse)
330 my $is_simple = $adb->config('use_simple') || ( $table_info && $table_info->{use_simple} ) || ( $table_info && $table_info->{id_type} && $table_info->{id_type} eq 'ascii' );
331 if ( $is_simple ) {
332 $self->{say} .= " - Table '$tableid' is in simple mode, skipping index generation.\n";
333 }
334 elsif ( $legacy_count > 0 || $recovered_from_backup || $opts{force} ) {
335 $self->{say} .= " - Rebuilding indexes...\n";
336 eval {
337 ($self->can('set_index') ? $self : do { require AmberDB::Tools::Index; AmberDB::Tools::Index->new($adb); })->set_index( $tableid, @decoded_records );
338 1;
339 } or do {
340 warn " [ERROR in set_index]: $@\n";
341 };
342 }
343 elsif ($missing_indexes) {
344 $self->{say} .= " - Rebuilding missing indexes...\n";
345 eval {
346 ($self->can('set_index') ? $self : do { require AmberDB::Tools::Index; AmberDB::Tools::Index->new($adb); })->set_index( $tableid, @decoded_records );
347 1;
348 } or do {
349 warn " [ERROR in set_index]: $@\n";
350 };
351 }
352  
353 # 7. Eslikci sozluk ve sayac (.unq, .cnt) dosyalarini koru ve yedekle
354 my $has_unq = $adb->exist_table($tableid, 'unq');
355 my $unq_backup;
356 if ($has_unq) {
357 my $unq_file = "$table_path.unq";
358 my $u_backup_base = "$table_path-$dom_ver-$mtime_str.unq";
359 $unq_backup = $u_backup_base;
360 my $cnt = 1;
361 while ( -e $unq_backup ) {
362 $unq_backup = "$table_path-$dom_ver-$mtime_str-$cnt.unq";
363 $cnt++;
364 }
365 require File::Copy;
366 File::Copy::copy( $unq_file, $unq_backup );
367 }
368  
369 my $has_cnt = $adb->exist_table($tableid, 'cnt');
370 my $cnt_backup;
371 if ($has_cnt) {
372 my $cnt_file = "$table_path.cnt";
373 my $c_backup_base = "$table_path-$dom_ver-$mtime_str.cnt";
374 $cnt_backup = $c_backup_base;
375 my $cnt = 1;
376 while ( -e $cnt_backup ) {
377 $cnt_backup = "$table_path-$dom_ver-$mtime_str-$cnt.cnt";
378 $cnt++;
379 }
380 require File::Copy;
381 File::Copy::copy( $cnt_file, $cnt_backup );
382 }
383  
384 $self->{say} .= "Table '$tableid' updated to ABR v1: $total records migrated ($legacy_count converted, backup: $backup_file).\n";
385  
386 return {
387 status => ( ( $legacy_count > 0 || $companion_legacy_total > 0 || $recovered_from_backup || $opts{force} ) ? 'updated' : 'already_current' ),
388 table => $tableid,
389 total => $total,
390 updated => $legacy_count,
391 already_current => $already_current,
392 dominant_format => $dom_ver,
393 backup_file => $backup_file,
394 del_migrated => $companion_stats{del_migrated} // 0,
395 del_backup => $companion_stats{del_backup},
396 del_status => $companion_stats{del_status},
397 aut_migrated => $companion_stats{aut_migrated} // 0,
398 aut_backup => $companion_stats{aut_backup},
399 aut_status => $companion_stats{aut_status},
400 has_unq => $has_unq ? 1 : undef,
401 unq_backup => $unq_backup,
402 has_cnt => $has_cnt ? 1 : undef,
403 cnt_backup => $cnt_backup,
404 };
405 }
406  
407 # ---------------------------------------------------------------------
408 # update_all(%options)
409 # Iterates through all discovered tables and runs update_table on each.
410 # ---------------------------------------------------------------------
411 sub update_all {
412 my ( $self, %opts ) = @_;
413 my $adb = $self->{_adb} or return;
414  
415 my @tables = ($self->can('all_tables') ? $self->all_tables() : do { require AmberDB::Tools::Maintain; AmberDB::Tools::Maintain->new($self->{_adb})->all_tables(); });
416 my @results;
417  
418 foreach my $table (@tables) {
419 my $res = $self->update_table( $table, %opts );
420 push @results, $res if $res;
421 }
422  
423 return wantarray ? @results : \@results;
424 }
425  
426 # my $status = dbase_tableold
427 # ------------------------------------------------
428 sub replace_tablename {
429  
430 my ( $self, $find, $replace ) = @_;
431 my $adb = $self->{_adb} or return;
432  
433 my @tables;
434 my $dbase_dir = $adb->path('dbase_dir') || ".";
435 my $year_dir = $adb->path('year_dir') || "";
436  
437 if ( $adb->config('use_simple') ) {
438 @tables = glob "$dbase_dir/$find.*";
439 push @tables, ( glob "$dbase_dir/${find}_*" );
440 }
441 else {
442 @tables = glob "$dbase_dir/table/$find.*";
443 push @tables, ( glob "$dbase_dir/table/${find}_*" );
444 if ( $adb->config('use_section') ) {
445 my @sections = glob "$dbase_dir/section_*";
446 foreach my $sec_file (@sections) {
447 push @tables, ( glob "$sec_file/$find.*" );
448 push @tables, ( glob "$sec_file/${find}_*" );
449 }
450 }
451  
452 if ( $adb->config('use_year') && $year_dir ) {
453 @tables = glob "$dbase_dir/$year_dir/$find.*";
454 push @tables, ( glob "$dbase_dir/$year_dir/${find}_*" );
455 if ( $adb->config('use_section') ) {
456 my @sections = glob "$dbase_dir/$year_dir/section_*";
457 foreach my $sec_file (@sections) {
458 push @tables, ( glob "$sec_file/${find}.*" );
459 push @tables, ( glob "$sec_file/${find}_*" );
460 }
461 }
462 }
463 }
464  
465 foreach my $old_file (@tables) {
466 my $new_file = "$old_file";
467 $new_file =~ s/\/$find([\.\_])/$replace$1/;
468 rename( $old_file, $new_file );
469 }
470 $self->{say} .= " - database table $find renamed to $replace.\n";
471  
472 return 1;
473 }
474  
475 # my %files = (
476 # "dbase_table1" => 0,
477 # "dbase_table2" => 2,
478 # "dbase_table3" => { BLOCK => 1, START => 2, STARTBLOCK => 3 },
479 # );
480 # my %replace = (
481 # "fields1" => "new_fields1",
482 # "fields2" => "new_fields2",
483 # );
484 # my $status = $tools->replace_blockdata(\%files, \%replace);
485 # ------------------------------------------------
486 sub replace_blockdata {
487  
488 my ( $self, $files, $replace ) = @_;
489 my $adb = $self->{_adb} or return {};
490  
491 my $status = {};
492 ref($files) eq "HASH" or return $status;
493 ref($replace) eq "HASH" or return $status;
494  
495 while ( my ( $file, $block ) = each %{$files} ) {
496 my @datas = $adb->read_all($file);
497 foreach my $record (@datas) {
498 my $change = 0;
499 if ( $block =~ /^[0-9]+$/ ) {
500 if ( exists( $replace->{ $record->[$block] } ) ) {
501 $record->[$block] = $replace->{ $record->[$block] };
502 $change = 1;
503 }
504 }
505 elsif ( ref($block) eq "HASH" ) {
506 if ( $block->{BLOCK} ) {
507 my $b = $block->{BLOCK};
508 if ( exists( $replace->{ $record->[$b] } ) ) {
509 $record->[$b] = $replace->{ $record->[$b] };
510 $change = 1;
511 }
512 }
513  
514 if ( $block->{START} && $block->{STARTBLOCK} ) {
515 my $s = $block->{START};
516 my $b = $block->{STARTBLOCK};
517 foreach my $line ( @{$record}[ $s .. $#$record ] ) {
518 if ( exists( $replace->{ $line->[$b] } ) ) {
519 $line->[$b] = $replace->{ $line->[$b] };
520 $change = 1;
521 }
522 }
523 }
524 }
525 if ($change) {
526 $adb->modify_id( $file, @$record );
527 $status->{$file}->{ $record->[0] } = 1;
528 $self->{say} .= " * $file: $record->[0] ID updated.\n";
529 }
530 }
531 }
532  
533 return $status;
534 }
535  
536 # my $table_path = $tools->del_table($tableid);
537 # ------------------------------------------------
538  
539 sub db_simple {
540  
541 my ( $self, $dir, $ext ) = @_;
542  
543 $ext //= "db";
544 require AmberDB;
545 my $db_obje = AmberDB->new(
546 ext => { db => $ext },
547 path => { dbase_dir => $dir },
548 cfg => {
549 use_simple => 1,
550 }
551 );
552 return $db_obje;
553 }
554  
555 # Rebuilds and converts binary indexes for all tables in database directory.
556 # my $status = $tools->convert_tables();
557 # ------------------------------------------------
558 sub convert_tables {
559 my ($self) = @_;
560 my $adb = $self->{_adb} or return;
561  
562 my $db_dir = $adb->path('dbase_dir');
563 return unless $db_dir && -d $db_dir;
564  
565 my @dirs = ($db_dir);
566 push @dirs, File::Spec->catdir( $db_dir, 'table' ) if -d File::Spec->catdir( $db_dir, 'table' );
567  
568 my @db_files;
569 foreach my $d (@dirs) {
570 if ( opendir my $dh, $d ) {
571 push @db_files, map { File::Spec->catfile( $d, $_ ) }
572 grep { /\.\Q$adb->{db_ext}\E$/ }
573 readdir($dh);
574 closedir $dh;
575 }
576 }
577 my %converted;
578  
579 foreach my $file (@db_files) {
580 my ($tableid) = $file =~ /([^\\\/]+)\.\w+$/;
581 next unless $tableid;
582 next if $tableid =~ /^\_/;
583  
584 $self->{say} .= "Processing table $tableid...\n";
585 my $ok = ($self->can('set_index') ? $self : do { require AmberDB::Tools::Index; AmberDB::Tools::Index->new($adb); })->set_index($tableid);
586 if ($ok) {
587 $converted{$tableid} = 1;
588 $self->{say} .= " -> Rebuilt packed binary indexes (.inx, .fld, .src) for $tableid\n";
589 }
590 }
591  
592 return \%converted;
593 }
594  
595 # Creates a compressed, portable .amberdb archive containing table schemas,
596 # native data files (.db, .del, .aut, .cnt), and integrity manifest.
597 # my $archive = $tools->dump( [file => 'backup.amberdb'], [tables => ['t1', 't2']] );
598 # ---------------------------------------------------------------------
599  
600  
601 # ---------------------------------------------------------------------
602 # update_version(%opts)
603 # Checks MetaCPAN API for latest AmberDB release and optionally installs via cpanm.
604 # ---------------------------------------------------------------------
605 sub update_version {
606 my ( $self, %opts ) = @_;
607 my $opt_check = $opts{check} // 0;
608 my $opt_cpanm = $opts{cpanm} // '';
609  
610 print "=================================================================\n";
611 print " AmberDB Core Engine Update Utility \n";
612 print "=================================================================\n";
613 print "Current Engine Version : v$AmberDB::VERSION\n";
614  
615 my $api_url = "https://fastapi.metacpan.org/v1/release/AmberDB";
616 my $json_text;
617  
618 require HTTP::Tiny;
619 require JSON::PP;
620 require version;
621  
622 my $http = HTTP::Tiny->new( timeout => 5, verify_SSL => 1 );
623 my $res = $http->get($api_url);
624 if ( $res && $res->{success} ) {
625 $json_text = $res->{content};
626 }
627 else {
628 if ( $^O eq 'MSWin32' || $^O eq 'msys' || $^O eq 'cygwin' ) {
629 $json_text = `powershell -NoProfile -Command "try { (Invoke-WebRequest -Uri '$api_url' -UseBasicParsing).Content } catch {}"`;
630 }
631 else {
632 $json_text = `curl -s -L "$api_url" 2>/dev/null`;
633 }
634 }
635  
636 my $latest_version;
637 if ( $json_text ) {
638 my $data = eval { JSON::PP::decode_json($json_text) };
639 $latest_version = $data->{version} if $data && ref($data) eq 'HASH';
640 }
641  
642 if ( !defined $latest_version || $latest_version eq '' ) {
643 print "[WARNING] Could not retrieve release information from MetaCPAN (offline or API unavailable).\n";
644 print "You can manually verify or install via: cpanm AmberDB\n";
645 print "=================================================================\n";
646 return { status => 'warning', message => 'Could not retrieve release info from MetaCPAN' };
647 }
648  
649 print "Latest MetaCPAN Version: $latest_version\n";
650 print "-----------------------------------------------------------------\n";
651  
652 my $v_curr = eval { version->parse($AmberDB::VERSION) };
653 my $v_late = eval { version->parse($latest_version) };
654  
655 if ( $v_curr && $v_late && $v_curr >= $v_late ) {
656 print "[OK] AmberDB engine is up to date (v$AmberDB::VERSION).\n";
657 print "=================================================================\n";
658 return { status => 'up_to_date', version => $AmberDB::VERSION, latest => $latest_version };
659 }
660  
661 print "[UPDATE AVAILABLE] AmberDB can be updated from v$AmberDB::VERSION to v$latest_version.\n";
662  
663 if ( $opt_check ) {
664 print "[CHECK MODE] Skipping package installation.\n";
665 print "=================================================================\n";
666 return { status => 'update_available', current => $AmberDB::VERSION, latest => $latest_version, check_mode => 1 };
667 }
668  
669 my $cpanm_cmd = $opt_cpanm;
670 if ( !$cpanm_cmd ) {
671 my $has_cpanm = `where cpanm 2>nul` || `which cpanm 2>/dev/null`;
672 if ( $has_cpanm ) {
673 $cpanm_cmd = 'cpanm';
674 }
675 else {
676 my $has_cpan = `where cpan 2>nul` || `which cpan 2>/dev/null`;
677 $cpanm_cmd = 'cpan' if $has_cpan;
678 }
679 }
680  
681 if ( $cpanm_cmd ) {
682 print "Launching installer via '$cpanm_cmd AmberDB'...\n";
683 my $exit_code = system( $cpanm_cmd, "AmberDB" );
684 if ( $exit_code == 0 ) {
685 print "[SUCCESS] AmberDB engine updated successfully to latest CPAN release.\n";
686 print "=================================================================\n";
687 return { status => 'updated', version => $latest_version };
688 }
689 else {
690 print "[WARNING] Installer exited with status $exit_code. You can run '$cpanm_cmd AmberDB' manually.\n";
691 print "=================================================================\n";
692 return { status => 'error', exit_code => $exit_code };
693 }
694 }
695 else {
696 print "[INFO] Neither 'cpanm' nor 'cpan' executable was detected in PATH.\n";
697 print "Please run the following command to update AmberDB:\n";
698 print " cpanm AmberDB\n";
699 print "=================================================================\n";
700 return { status => 'manual_required', latest => $latest_version };
701 }
702 }
703  
704 # ---------------------------------------------------------------------
705 # update_storage(%opts)
706 # Migrates storage directory layout (scheme->schema, tables->table),
707 # upgrades table record formats to native ABR v5, rebuilds secondary indexes,
708 # and stamps config/storage_version.json.
709 # ---------------------------------------------------------------------
710 sub update_storage {
711 my ( $self, %opts ) = @_;
712 my $adb = $self->{_adb};
713  
714 my $target_dir = $opts{target_dir}
715 || ( $adb ? $adb->path('dbase_dir') : undef )
716 || $opts{dbase_dir}
717 || $opts{dbase};
718  
719 if ( !defined $target_dir || $target_dir eq '' ) {
720 my $cwd = eval { Cwd::abs_path(Cwd::getcwd()) } // '.';
721 if ( -d File::Spec->catdir( $cwd, "dbstore" ) ) {
722 $target_dir = File::Spec->catdir( $cwd, "dbstore" );
723 }
724 elsif ( -d File::Spec->catdir( $cwd, "dbase" ) ) {
725 $target_dir = File::Spec->catdir( $cwd, "dbase" );
726 }
727 elsif ( -d File::Spec->catdir( $cwd, "tables" ) ) {
728 $target_dir = $cwd;
729 }
730 else {
731 $target_dir = $cwd;
732 }
733 }
734 $target_dir = eval { Cwd::abs_path($target_dir) } // $target_dir;
735  
736 my $opt_force = $opts{force} // 0;
737 my $opt_check = $opts{check} // 0;
738 my $opt_no_backup = $opts{no_backup} // $opts{'no-backup'} // 0;
739 my $opt_tables = $opts{tables} // $opts{table} // '';
740 my $opt_manifest = $opts{manifest} // '';
741  
742 print "=================================================================\n";
743 print " AmberDB Storage, Directory & Compatibility Migration Engine \n";
744 print "=================================================================\n";
745 print "Database Directory : $target_dir\n";
746  
747 my $config_dir = File::Spec->catdir( $target_dir, "config" );
748 my $ver_file = File::Spec->catfile( $config_dir, "storage_version.json" );
749  
750 require JSON::PP;
751 require version;
752 require HTTP::Tiny;
753 require File::Path;
754 require File::Copy;
755  
756 # 1. Inspect existing storage version
757 my $current_storage_ver;
758 if ( -e $ver_file ) {
759 if ( open my $fh, '<', $ver_file ) {
760 local $/;
761 my $content = <$fh>;
762 close $fh;
763 my $data = eval { JSON::PP::decode_json($content) };
764 if ( $data && $data->{storage_version} ) {
765 $current_storage_ver = $data->{storage_version};
766 }
767 }
768 }
769  
770 if ( !defined $current_storage_ver || $current_storage_ver eq '' ) {
771 if ( -d File::Spec->catdir( $target_dir, "scheme" ) ) {
772 $current_storage_ver = "5.20.0";
773 }
774 elsif ( -d File::Spec->catdir( $target_dir, "tables" ) && !-d File::Spec->catdir( $target_dir, "table" ) ) {
775 $current_storage_ver = "5.21.0";
776 }
777 else {
778 $current_storage_ver = "5.21.0";
779 }
780 }
781  
782 print "Current Storage Version : v$current_storage_ver\n";
783  
784 # 2. Load Roadmap / Manifest (Online or Local Fallback)
785 my $target_storage_ver = "5.25.0";
786 my $manifest;
787  
788 my $manifest_url = $opt_manifest || "https://raw.githubusercontent.com/marufcetin/amberdb/main/migrations/manifest.json";
789 my $json_manifest;
790  
791 my $http = HTTP::Tiny->new( timeout => 5, verify_SSL => 1 );
792 my $m_res = $http->get($manifest_url);
793 if ( $m_res && $m_res->{success} ) {
794 $json_manifest = $m_res->{content};
795 }
796 elsif ( $^O eq 'MSWin32' || $^O eq 'msys' || $^O eq 'cygwin' ) {
797 $json_manifest = `powershell -NoProfile -Command "try { (Invoke-WebRequest -Uri '$manifest_url' -UseBasicParsing).Content } catch {}"`;
798 }
799 else {
800 $json_manifest = `curl -s -L "$manifest_url" 2>/dev/null`;
801 }
802  
803 if ( $json_manifest ) {
804 $manifest = eval { JSON::PP::decode_json($json_manifest) };
805 }
806  
807 if ( !$manifest || ref($manifest) ne 'HASH' || !$manifest->{current_storage_version} ) {
808 my $local_manifest_file = File::Spec->catfile( $target_dir, "migrations", "manifest.json" );
809 if ( !-e $local_manifest_file ) {
810 my ($vol, $dir, undef) = File::Spec->splitpath(__FILE__);
811 my $proj = File::Spec->catdir($dir, "..", "..", "..");
812 $local_manifest_file = File::Spec->catfile( $proj, "migrations", "manifest.json" );
813 }
814 if ( -e $local_manifest_file && open my $lfh, '<', $local_manifest_file ) {
815 local $/;
816 my $lcont = <$lfh>;
817 close $lfh;
818 $manifest = eval { JSON::PP::decode_json($lcont) };
819 }
820 }
821  
822 if ( $manifest && $manifest->{current_storage_version} ) {
823 $target_storage_ver = $manifest->{current_storage_version};
824 }
825  
826 print "Target Storage Version : v$target_storage_ver\n";
827 print "-----------------------------------------------------------------\n";
828  
829 my $v_curr = eval { version->parse($current_storage_ver) };
830 my $v_targ = eval { version->parse($target_storage_ver) };
831  
832 if ( $v_curr && $v_targ && $v_curr >= $v_targ && !$opt_force ) {
833 print "[OK] Storage format is already at latest version ($current_storage_ver).\n";
834 print " Use --force to rewrite and re-index existing tables.\n";
835 print "=================================================================\n";
836 return { status => 'already_current', current_version => $current_storage_ver };
837 }
838  
839 if ( $opt_check ) {
840 print "[CHECK MODE] Storage migration from v$current_storage_ver to v$target_storage_ver is pending.\n";
841 if ( $v_curr < version->parse('5.21.0') ) {
842 print " - [v5.21.0] Directory migration: Rename scheme/ -> schema/\n";
843 }
844 if ( $v_curr < version->parse('5.25.0') || $opt_force ) {
845 print " - [v5.25.0] Directory migration: Rename tables/ -> table/\n";
846 print " - [v5.25.0] Format migration: Convert legacy records to ABR v5 binary pack\n";
847 print " - [v5.25.0] Index migration: Rebuild all secondary indexes (.inx, .fld, .unq, .fac, .slg, .srt)\n";
848 }
849 print "=================================================================\n";
850 return { status => 'check', current_version => $current_storage_ver, target_version => $target_storage_ver };
851 }
852  
853 # 3. Pre-migration Safety Snapshot (unless --no-backup)
854 if ( !$opt_no_backup ) {
855 print "Taking pre-migration safety snapshot...\n";
856 my $backup_file = File::Spec->catfile( $target_dir, "backup_pre_storage_update_" . time() . ".amberdb" );
857 eval {
858 require AmberDB;
859 my $snap_adb = AmberDB->new( path => { dbase_dir => $target_dir } );
860 my $maintainer = $self->can('dump') ? $self : do {
861 require AmberDB::Tools::Maintain;
862 AmberDB::Tools::Maintain->new($snap_adb);
863 };
864 $maintainer->dump( file => $backup_file );
865 };
866 if ( -e $backup_file && -s $backup_file ) {
867 print " [+] Safety snapshot created: $backup_file\n";
868 }
869 else {
870 print " [INFO] Snapshot skipped or empty database.\n";
871 }
872 }
873  
874 # 4. Stage v5.21.0 Migration: Rename scheme -> schema
875 if ( $v_curr < version->parse('5.21.0') ) {
876 print "\n>>> [Stage v5.21.0 Migration] Scheme to Schema Directory Renaming...\n";
877 $self->_inline_migrate_5_21_0($target_dir);
878 }
879  
880 # 5. Stage v5.25.0 Migration: tables -> table, ABR v5 format & complete re-indexing
881 if ( $v_curr < version->parse('5.25.0') || $opt_force ) {
882 print "\n>>> [Stage v5.25.0 Migration] Tables to Table, ABR v5 Binary Pack & Index Rebuild...\n";
883 $self->_inline_migrate_5_25_0(
884 $target_dir,
885 force => $opt_force,
886 tables => $opt_tables,
887 );
888 }
889  
890 # 6. Synchronize standard directory layout
891 print "\nSynchronizing standard directory layout...\n";
892 my @dirs = (
893 File::Spec->catdir( $target_dir, "table" ),
894 File::Spec->catdir( $target_dir, "schema" ),
895 File::Spec->catdir( $target_dir, "journal" ),
896 File::Spec->catdir( $target_dir, "lock" ),
897 File::Spec->catdir( $target_dir, "session" ),
898 File::Spec->catdir( $target_dir, "config" ),
899 );
900 for my $d (@dirs) {
901 if ( !-d $d ) {
902 File::Path::make_path($d);
903 print " [+] Created $d\n";
904 }
905 }
906  
907 # 7. Stamp storage version
908 File::Path::make_path($config_dir) unless -d $config_dir;
909 if ( open my $fh, '>', $ver_file ) {
910 my $stamp = {
911 storage_version => $target_storage_ver,
912 amberdb_engine => $AmberDB::VERSION,
913 record_format => "abr_v5",
914 encoding => "utf-8",
915 last_updated => scalar localtime,
916 };
917 print $fh JSON::PP::encode_json($stamp);
918 close $fh;
919 print "\n[OK] Storage version stamped as v$target_storage_ver in $ver_file\n";
920 }
921  
922 print "=================================================================\n";
923 print " Storage migration completed successfully! \n";
924 print "=================================================================\n";
925 return { status => 'ok', target_version => $target_storage_ver };
926 }
927  
928 sub _inline_migrate_5_21_0 {
929 my ( $self, $tdir ) = @_;
930 my $scheme_dir = File::Spec->catdir($tdir, 'scheme');
931 my $schema_dir = File::Spec->catdir($tdir, 'schema');
932  
933 if (-d $scheme_dir) {
934 if (!-d $schema_dir) {
935 if (rename($scheme_dir, $schema_dir)) {
936 print " [v5.21.0] Renamed '$scheme_dir' -> '$schema_dir'\n";
937 }
938 else {
939 File::Path::make_path($schema_dir);
940 opendir(my $dh, $scheme_dir) or die "Cannot open $scheme_dir: $!";
941 my $moved = 0;
942 while (my $f = readdir($dh)) {
943 next if $f eq '.' || $f eq '..';
944 File::Copy::move(File::Spec->catfile($scheme_dir, $f), File::Spec->catfile($schema_dir, $f));
945 $moved++;
946 }
947 closedir($dh);
948 rmdir($scheme_dir);
949 print " [v5.21.0] Moved $moved file(s) from '$scheme_dir' -> '$schema_dir'\n";
950 }
951 }
952 else {
953 opendir(my $dh, $scheme_dir) or die "Cannot open $scheme_dir: $!";
954 my $moved = 0;
955 while (my $f = readdir($dh)) {
956 next if $f eq '.' || $f eq '..';
957 my $src = File::Spec->catfile($scheme_dir, $f);
958 my $dst = File::Spec->catfile($schema_dir, $f);
959 if (!-e $dst) {
960 File::Copy::move($src, $dst);
961 $moved++;
962 }
963 }
964 closedir($dh);
965 rmdir($scheme_dir);
966 print " [v5.21.0] Merged $moved file(s) from '$scheme_dir' into '$schema_dir'\n";
967 }
968 }
969 else {
970 File::Path::make_path($schema_dir) unless -d $schema_dir;
971 print " [v5.21.0] Verified schema directory '$schema_dir'\n";
972 }
973 }
974  
975 sub _inline_migrate_5_25_0 {
976 my ( $self, $tdir, %opts ) = @_;
977 my $tables_dir = File::Spec->catdir($tdir, 'tables');
978 my $table_dir = File::Spec->catdir($tdir, 'table');
979 my $opt_force = $opts{force} // 0;
980 my $opt_tables = $opts{tables} // '';
981  
982 # 1. Rename tables to table
983 if (-d $tables_dir) {
984 if (!-d $table_dir) {
985 if (rename($tables_dir, $table_dir)) {
986 print " [v5.25.0] Renamed directory: '$tables_dir' -> '$table_dir'\n";
987 }
988 else {
989 File::Path::make_path($table_dir);
990 opendir(my $dh, $tables_dir) or die "Cannot open $tables_dir: $!";
991 my $moved = 0;
992 while (my $f = readdir($dh)) {
993 next if $f eq '.' || $f eq '..';
994 File::Copy::move(File::Spec->catfile($tables_dir, $f), File::Spec->catfile($table_dir, $f));
995 $moved++;
996 }
997 closedir($dh);
998 rmdir($tables_dir);
999 print " [v5.25.0] Moved $moved file(s) from '$tables_dir' -> '$table_dir'\n";
1000 }
1001 }
1002 else {
1003 opendir(my $dh, $tables_dir) or die "Cannot open $tables_dir: $!";
1004 my $moved = 0;
1005 my $date_stamp = $opts{date_stamp};
1006 if (!$date_stamp) {
1007 my ($sec, $min, $hour, $mday, $mon, $year) = localtime();
1008 $date_stamp = sprintf("%04d-%02d-%02d", $year + 1900, $mon + 1, $mday);
1009 }
1010  
1011 while (my $f = readdir($dh)) {
1012 next if $f eq '.' || $f eq '..';
1013 my $src = File::Spec->catfile($tables_dir, $f);
1014 my $dst = File::Spec->catfile($table_dir, $f);
1015 if (!-e $dst) {
1016 if (File::Copy::move($src, $dst)) {
1017 $moved++;
1018 }
1019 else {
1020 warn " [!] Failed to move '$src' -> '$dst': $!\n";
1021 }
1022 }
1023 else {
1024 my ($base, $ext) = ( $f =~ /^(.*?)(\.[^.]+)$/ );
1025 my $stamped_name = (defined $base && length $base)
1026 ? "${base}_${date_stamp}${ext}"
1027 : "${f}_${date_stamp}";
1028  
1029 my $stamped_dst = File::Spec->catfile($table_dir, $stamped_name);
1030 if (-e $stamped_dst) {
1031 my $counter = 1;
1032 while (-e $stamped_dst) {
1033 my $suffixed = (defined $base && length $base)
1034 ? "${base}_${date_stamp}_${counter}${ext}"
1035 : "${f}_${date_stamp}_${counter}";
1036 $stamped_dst = File::Spec->catfile($table_dir, $suffixed);
1037 $counter++;
1038 }
1039 }
1040  
1041 if (File::Copy::move($src, $stamped_dst)) {
1042 $moved++;
1043 }
1044 else {
1045 warn " [!] Failed to move '$src' -> '$stamped_dst': $!\n";
1046 }
1047 }
1048 }
1049 closedir($dh);
1050 rmdir($tables_dir);
1051 print " [v5.25.0] Merged $moved file(s) from '$tables_dir' into '$table_dir'\n";
1052 }
1053 }
1054 else {
1055 File::Path::make_path($table_dir) unless -d $table_dir;
1056 print " [v5.25.0] Verified table directory '$table_dir'\n";
1057 }
1058  
1059 # 2. Convert legacy table records to ABR v5
1060 require AmberDB;
1061 my $fresh_adb = AmberDB->new( path => { dbase_dir => $tdir } );
1062 my $updater = $self->can('update_table') ? $self : do {
1063 require AmberDB::Tools::Update;
1064 AmberDB::Tools::Update->new($fresh_adb);
1065 };
1066  
1067 my @target_tables;
1068 if ($opt_tables) {
1069 @target_tables = ref($opt_tables) eq 'ARRAY' ? @$opt_tables : split /,/, $opt_tables;
1070 }
1071 else {
1072 my $maintainer = $self->can('all_tables') ? $self : do {
1073 require AmberDB::Tools::Maintain;
1074 AmberDB::Tools::Maintain->new($fresh_adb);
1075 };
1076 @target_tables = $maintainer->all_tables();
1077 }
1078  
1079 if (@target_tables) {
1080 print " [v5.25.0] Upgrading legacy table formats to native ABR v5...\n";
1081 for my $tbl (@target_tables) {
1082 $tbl =~ s/^\s+|\s+$//g;
1083 next unless $tbl;
1084  
1085 print " - Migrating table '$tbl' ... ";
1086 my $res = $updater->update_table( $tbl, force => $opt_force );
1087 if (!$res || $res->{status} eq 'error') {
1088 my $err = $res->{error} // 'Unknown error';
1089 print "FAILED! ($err)\n";
1090 }
1091 elsif ($res->{status} eq 'already_current') {
1092 print "ALREADY CURRENT ABR v5 (" . ($res->{already_current} // 0) . " records)\n";
1093 }
1094 elsif ($res->{status} eq 'updated') {
1095 print "Migrated! ($res->{total} records, format: $res->{dominant_format})\n";
1096 }
1097 else {
1098 print "OK\n";
1099 }
1100 }
1101  
1102 # 3. Rebuild all secondary binary indexes
1103 print " [v5.25.0] Rebuilding all derived secondary binary indexes...\n";
1104 my $indexer = $self->can('set_index') ? $self : do {
1105 require AmberDB::Tools::Index;
1106 AmberDB::Tools::Index->new($fresh_adb);
1107 };
1108 for my $tbl (@target_tables) {
1109 $tbl =~ s/^\s+|\s+$//g;
1110 next unless $tbl;
1111 $indexer->set_index($tbl);
1112 }
1113 }
1114 else {
1115 print " [v5.25.0] No tables found to migrate in '$tdir'.\n";
1116 }
1117 }
1118  
1119 1;