upgrade.pl 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. init_option($dbh);
  29. if (!$config_ref{version} and !$ARGV[0]) {
  30. print "Current version unknown! Skip upgrade!\n";
  31. exit 100;
  32. }
  33. if ($ARGV[0]) {
  34. if (exists($old_releases_h{$ARGV[0]})) { $config_ref{version}=$ARGV[0]; } else { print "Unknown version $ARGV[0]!\n"; }
  35. }
  36. if (!exists($old_releases_h{$config_ref{version}})) { print "Unknown version $config_ref{version}!\n"; exit 100; }
  37. if ($eye_release eq $config_ref{version}) { print "Already updated!\n"; exit; }
  38. print 'Current version: '.$config_ref{version}.' upgrade to: '.$eye_release."\n";
  39. #1 - mysql
  40. #0 - pgsql
  41. my $db_type = ($config_ref{DBTYPE} eq 'mysql');
  42. my $old_version_index = $old_releases_h{$config_ref{version}} + 1;
  43. my $stage = 1;
  44. for (my $i=$old_version_index; $i < scalar @old_releases; $i++) {
  45. print "Stage $stage. Upgrade to $old_releases[$i]\n";
  46. $stage++;
  47. my $dir_name = $old_releases[$i];
  48. $dir_name =~s/\./-/g;
  49. next if (! -d $dir_name);
  50. #patch before change database schema
  51. my @perl_patches = glob($dir_name.'/before*.pl');
  52. if (@perl_patches and scalar @perl_patches) {
  53. foreach my $patch (@perl_patches) {
  54. next if (!$patch or ! -e $patch);
  55. open(my $pipe, "-|", "perl $patch") or die "Error in apply upgrade script $patch! Ошибка: $!";
  56. while (my $line = <$pipe>) {
  57. if ($line =~ /::/) { print "\r"; $line =~s/\:\://; }
  58. print $line;
  59. }
  60. close($pipe);
  61. }
  62. }
  63. #change database schema
  64. my @sql_patches;
  65. if ($db_type) {
  66. my @sql_patches1 = glob($dir_name.'/*.sql');
  67. my @sql_patches2 = glob($dir_name.'/*.msql');
  68. push(@sql_patches,@sql_patches1);
  69. push(@sql_patches,@sql_patches2);
  70. } else {
  71. @sql_patches = glob($dir_name.'/*.psql');
  72. }
  73. if (@sql_patches and scalar @sql_patches) {
  74. my @sorted_patches = sort @sql_patches;
  75. my $i = 0;
  76. while ($i < @sorted_patches) {
  77. my $patch = $sorted_patches[$i];
  78. $i++;
  79. eval {
  80. next if (!$patch or ! -e $patch);
  81. next if ($patch =~ /version\.sql/);
  82. my @sql_cmd = read_file($patch);
  83. my $j = 0;
  84. while ($j < @sql_cmd) {
  85. my $sql = $sql_cmd[$j];
  86. $j++;
  87. next if ($sql =~ /^(--|#)/);
  88. next if (!$sql);
  89. my $sql_prep = $dbh->prepare($sql);
  90. if (!$sql_prep) {
  91. warn "Unable to prepare SQL: $sql\nError: " . $dbh->errstr . "\n";
  92. next;
  93. }
  94. my $rv = $sql_prep->execute();
  95. if (!$rv) {
  96. warn "Unable to execute SQL: $sql\nError: " . $dbh->errstr . "\n";
  97. }
  98. $sql_prep->finish();
  99. }
  100. };
  101. if ($@) {
  102. chomp $@;
  103. print STDERR "Error processing patch '$patch': $@\n";
  104. }
  105. }
  106. }
  107. #patch after change database schema
  108. @perl_patches = glob($dir_name.'/after*.pl');
  109. if (@perl_patches and scalar @perl_patches) {
  110. foreach my $patch (@perl_patches) {
  111. next if (!$patch or ! -e $patch);
  112. open(my $pipe, "-|", "perl $patch") or die "Error in apply upgrade script $patch! Ошибка: $!";
  113. while (my $line = <$pipe>) {
  114. if ($line =~ /::/) { print "\r"; $line =~s/\:\://; }
  115. print $line;
  116. }
  117. close($pipe);
  118. }
  119. }
  120. #change version
  121. do_sql($dbh,'UPDATE version SET version="'.$old_releases[$i].'"');
  122. }
  123. print "Done!\n";
  124. exit;