upgrade.pl 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. open(my $pipe, "-|", "$^X $patch") or die "Error applying upgrade script $patch: $!";
  58. while (my $line = <$pipe>) {
  59. chomp $line;
  60. if ($line =~ s/^:://) {
  61. # Строка начинается с "::" → выводим с \r и без перевода строки
  62. printf "\r%-80s", $line; # дополняем пробелами до 80 символов, чтобы затереть старый текст
  63. $| = 1; # flush
  64. } else {
  65. print "$line\n";
  66. }
  67. }
  68. close($pipe);
  69. print "\n" if $pipe;
  70. }
  71. }
  72. @perl_patches=();
  73. #change database schema
  74. # === Apply SQL patches ===
  75. my @sql_patches;
  76. if ($db_type) {
  77. push @sql_patches, glob("$dir_name/*.sql"), glob("$dir_name/*.msql");
  78. } else {
  79. @sql_patches = glob("$dir_name/*.psql");
  80. }
  81. if (@sql_patches) {
  82. my @sorted_patches = sort @sql_patches;
  83. for my $patch (@sorted_patches) {
  84. next if !$patch || !-e $patch;
  85. next if $patch =~ /version\.sql$/;
  86. print " → Applying SQL patch: $patch\n";
  87. my @sql_lines = read_file($patch);
  88. my $stmt_num = 0;
  89. for my $raw_line (@sql_lines) {
  90. # Убираем комментарии и пустые строки
  91. my $sql = $raw_line;
  92. $sql =~ s/\s+$//; # trim
  93. next if $sql eq '' || $sql =~ /^(--|#)/;
  94. $stmt_num++;
  95. # Логируем команду
  96. print " [$stmt_num] Executing: $sql\n";
  97. eval {
  98. my $sth = $dbh->prepare($sql);
  99. if (!$sth) {
  100. die "Prepare failed: " . $dbh->errstr;
  101. }
  102. my $rv = $sth->execute();
  103. if (!defined $rv) {
  104. die "Execute failed: " . $dbh->errstr;
  105. }
  106. # Показываем результат (если есть)
  107. if ($sql =~ /^\s*(INSERT|UPDATE|DELETE|TRUNCATE)/i) {
  108. my $rows = $sth->rows;
  109. print " → Affected rows: $rows\n";
  110. } elsif ($sql =~ /^\s*SELECT/i) {
  111. my $rows = $sth->fetchall_arrayref({});
  112. my $count = @$rows;
  113. print " → Selected $count row(s)\n";
  114. } else {
  115. print " → Command executed successfully\n";
  116. }
  117. $sth->finish();
  118. 1;
  119. } or do {
  120. my $err = $@;
  121. chomp $err;
  122. print " ❌ ERROR: $err\n";
  123. # Не прерываем — продолжаем, как в оригинале
  124. };
  125. }
  126. print " → Patch $patch applied.\n\n";
  127. }
  128. }
  129. # patch after change database schema
  130. @perl_patches = glob("$dir_name/after*.pl");
  131. if (@perl_patches) {
  132. foreach my $patch (@perl_patches) {
  133. next unless $patch && -e $patch;
  134. open(my $pipe, "-|", "$^X $patch") or die "Error applying upgrade script $patch: $!";
  135. while (my $line = <$pipe>) {
  136. chomp $line;
  137. if ($line =~ s/^:://) {
  138. # Строка начинается с "::" → выводим с \r и без перевода строки
  139. printf "\r%-80s", $line; # дополняем пробелами до 80 символов, чтобы затереть старый текст
  140. $| = 1; # flush
  141. } else {
  142. print "$line\n";
  143. }
  144. }
  145. close($pipe);
  146. print "\n" if $pipe;
  147. }
  148. }
  149. #change version
  150. do_sql($dbh,'UPDATE version SET version="'.$old_releases[$i].'"');
  151. }
  152. print "Done!\n";
  153. exit;