upgrade.pl 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #!/usr/bin/perl
  2. #
  3. # Copyright (C) Roman Dmitiriev, rnd@rajven.ru
  4. #
  5. use utf8;
  6. use strict;
  7. use warnings;
  8. use Encode;
  9. use open qw(:std :encoding(UTF-8));
  10. no warnings 'utf8';
  11. use FindBin '$Bin';
  12. use lib "/opt/Eye/scripts";
  13. use eyelib::config;
  14. use eyelib::main;
  15. use eyelib::database;
  16. use eyelib::common;
  17. use Data::Dumper;
  18. use strict;
  19. use warnings;
  20. STDOUT->autoflush(1);
  21. my $update_dir = '/opt/Eye/scripts/updates';
  22. opendir(my $dh, $update_dir) or die "Eror listing for $update_dir: $!";
  23. my @old_releases = sort grep { -d "$update_dir/$_" && !/^\.\.?$/ && /^\d/ } readdir($dh);
  24. closedir $dh;
  25. s/-/./g for @old_releases;
  26. my $r_index = 0;
  27. my %old_releases_h = map {$_ => $r_index++ } @old_releases;
  28. my $eye_release = $old_releases[@old_releases - 1];
  29. $dbh=init_db();
  30. $config_ref{version}='';
  31. my $version_record = get_record_sql($dbh,"SELECT version FROM version WHERE version is NOT NULL");
  32. if ($version_record) { $config_ref{version}=$version_record->{version}; }
  33. if (!$config_ref{version} and !$ARGV[0]) {
  34. print "Current version unknown! Skip upgrade!\n";
  35. exit 100;
  36. }
  37. if ($ARGV[0]) {
  38. if (exists($old_releases_h{$ARGV[0]})) { $config_ref{version}=$ARGV[0]; } else { print "Unknown version $ARGV[0]!\n"; }
  39. }
  40. if (!exists($old_releases_h{$config_ref{version}})) { print "Unknown version $config_ref{version}!\n"; exit 100; }
  41. if ($eye_release eq $config_ref{version}) { print "Already updated!\n"; exit; }
  42. print 'Current version: '.$config_ref{version}.' upgrade to: '.$eye_release."\n";
  43. #1 - mysql
  44. #0 - pgsql
  45. my $db_type = ($config_ref{DBTYPE} eq 'mysql');
  46. my $old_version_index = $old_releases_h{$config_ref{version}} + 1;
  47. my $stage = 1;
  48. for (my $i=$old_version_index; $i < scalar @old_releases; $i++) {
  49. print "Stage $stage. Upgrade to $old_releases[$i]\n";
  50. $stage++;
  51. my $dir_name = $update_dir."/".$old_releases[$i];
  52. $dir_name =~s/\./-/g;
  53. next if (! -d $dir_name);
  54. # patch before change database schema
  55. my @perl_patches = glob("$dir_name/before*.pl");
  56. if (@perl_patches) {
  57. foreach my $patch (@perl_patches) {
  58. next unless $patch && -e $patch;
  59. # Выводим полный путь к патчу
  60. print " → Applying Perl patch: $patch\n";
  61. open(my $pipe, "-|", "$^X $patch") or die "Error applying upgrade script $patch: $!";
  62. while (my $line = <$pipe>) {
  63. chomp $line;
  64. if ($line =~ s/^:://) {
  65. printf "\r%-80s", $line;
  66. $| = 1;
  67. } else {
  68. print "$line\n";
  69. }
  70. }
  71. close($pipe);
  72. print "\n";
  73. }
  74. }
  75. @perl_patches = ();
  76. #change database schema
  77. # === Apply SQL patches ===
  78. my @sql_patches;
  79. if ($db_type) {
  80. push @sql_patches, glob("$dir_name/*.sql"), glob("$dir_name/*.msql");
  81. } else {
  82. @sql_patches = glob("$dir_name/*.psql");
  83. }
  84. if (@sql_patches) {
  85. my @sorted_patches = sort @sql_patches;
  86. for my $patch (@sorted_patches) {
  87. next if !$patch || !-e $patch;
  88. next if $patch =~ /version\.sql$/;
  89. print " → Applying SQL patch: $patch\n";
  90. my @sql_lines = read_file($patch);
  91. my $stmt_num = 0;
  92. for my $raw_line (@sql_lines) {
  93. # Убираем комментарии и пустые строки
  94. my $sql = $raw_line;
  95. $sql =~ s/\s+$//; # trim
  96. next if $sql eq '' || $sql =~ /^(--|#)/;
  97. $stmt_num++;
  98. # Логируем команду
  99. print " [$stmt_num] Executing: $sql\n";
  100. eval {
  101. my $sth = $dbh->prepare($sql);
  102. if (!$sth) {
  103. die "Prepare failed: " . $dbh->errstr;
  104. }
  105. my $rv = $sth->execute();
  106. if (!defined $rv) {
  107. die "Execute failed: " . $dbh->errstr;
  108. }
  109. # Показываем результат (если есть)
  110. if ($sql =~ /^\s*(INSERT|UPDATE|DELETE|TRUNCATE)/i) {
  111. my $rows = $sth->rows;
  112. print " → Affected rows: $rows\n";
  113. } elsif ($sql =~ /^\s*SELECT/i) {
  114. my $rows = $sth->fetchall_arrayref({});
  115. my $count = @$rows;
  116. print " → Selected $count row(s)\n";
  117. } else {
  118. print " → Command executed successfully\n";
  119. }
  120. $sth->finish();
  121. 1;
  122. } or do {
  123. my $err = $@;
  124. chomp $err;
  125. print " ❌ ERROR: $err\n";
  126. # Не прерываем — продолжаем, как в оригинале
  127. };
  128. }
  129. print " → Patch $patch applied.\n\n";
  130. }
  131. }
  132. # patch after change database schema
  133. @perl_patches = glob("$dir_name/after*.pl");
  134. if (@perl_patches) {
  135. foreach my $patch (@perl_patches) {
  136. next unless $patch && -e $patch;
  137. # Выводим полный путь к патчу
  138. print " → Applying Perl patch: $patch\n";
  139. open(my $pipe, "-|", "$^X $patch") or die "Error applying upgrade script $patch: $!";
  140. while (my $line = <$pipe>) {
  141. chomp $line;
  142. if ($line =~ s/^:://) {
  143. printf "\r%-80s", $line;
  144. $| = 1;
  145. } else {
  146. print "$line\n";
  147. }
  148. }
  149. close($pipe);
  150. print "\n";
  151. }
  152. }
  153. @perl_patches = ();
  154. #change version
  155. do_sql($dbh,'UPDATE version SET version="'.$old_releases[$i].'"');
  156. }
  157. print "Done!\n";
  158. exit;