upgrade.pl 5.9 KB

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