upgrade.pl 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 strict;
  15. use warnings;
  16. my @old_releases = (
  17. '2.6.1',
  18. '2.6.2',
  19. '2.6.3',
  20. '2.7.0',
  21. '2.7.1',
  22. '2.7.2',
  23. '2.7.3',
  24. '2.7.4',
  25. '2.7.5',
  26. '2.7.6',
  27. '2.7.7',
  28. '2.7.8',
  29. );
  30. my $r_index = 0;
  31. my %old_releases_h = map {$_ => $r_index++ } @old_releases;
  32. my $eye_release = $old_releases[@old_releases - 1];
  33. $dbh=init_db();
  34. init_option($dbh);
  35. if (!$config_ref{version} and !$ARGV[0]) {
  36. print "Current version unknown! Skip upgrade!\n";
  37. exit 100;
  38. }
  39. if ($ARGV[0]) {
  40. if (exists($old_releases_h{$ARGV[0]})) { $config_ref{version}=$ARGV[0]; } else { print "Unknown version $ARGV[0]!\n"; }
  41. }
  42. if (!exists($old_releases_h{$config_ref{version}})) { print "Unknown version $config_ref{version}!\n"; exit 100; }
  43. if ($eye_release eq $config_ref{version}) { print "Already updated!\n"; exit; }
  44. print 'Current version: '.$config_ref{version}.' upgrade to: '.$eye_release."\n";
  45. my $old_version_index = $old_releases_h{$config_ref{version}} + 1;
  46. my $stage = 1;
  47. for (my $i=$old_version_index; $i < scalar @old_releases; $i++) {
  48. print "Stage $stage. Upgrade to $old_releases[$i]\n";
  49. $stage++;
  50. my $dir_name = $old_releases[$i];
  51. $dir_name =~s/\./-/g;
  52. next if (! -d $dir_name);
  53. #patch before change database schema
  54. my @perl_patches = glob($dir_name.'/before*.pl');
  55. if (@perl_patches and scalar @perl_patches) {
  56. foreach my $patch (@perl_patches) {
  57. next if (!$patch or ! -e $patch);
  58. open(my $pipe, "-|", "perl $patch") or die "Error in apply upgrade script $patch! Ошибка: $!";
  59. while (my $line = <$pipe>) {
  60. if ($line =~ /::/) { print "\r"; $line =~s/\:\://; }
  61. print $line;
  62. }
  63. close($pipe);
  64. }
  65. }
  66. #change database schema
  67. my @sql_patches = glob($dir_name.'/*.sql');
  68. if (@sql_patches and scalar @sql_patches) {
  69. foreach my $patch (@sql_patches) {
  70. next if (!$patch or ! -e $patch);
  71. next if ($patch=~/version.sql/);
  72. my @sql_cmd=read_file($patch);
  73. foreach my $sql (@sql_cmd) {
  74. my $sql_prep = $dbh->prepare($sql) or die "Unable to prepare $sql: " . $dbh->errstr."\n";
  75. my $sql_ref;
  76. my $rv = $sql_prep->execute();
  77. if (!$rv) { print "Unable to execute $sql: " . $dbh->errstr."\n"; }
  78. $sql_prep->finish();
  79. }
  80. }
  81. }
  82. #patch after change database schema
  83. @perl_patches = glob($dir_name.'/after*.pl');
  84. if (@perl_patches and scalar @perl_patches) {
  85. foreach my $patch (@perl_patches) {
  86. next if (!$patch or ! -e $patch);
  87. open(my $pipe, "-|", "perl $patch") or die "Error in apply upgrade script $patch! Ошибка: $!";
  88. while (my $line = <$pipe>) {
  89. if ($line =~ /::/) { print "\r"; $line =~s/\:\://; }
  90. print $line;
  91. }
  92. close($pipe);
  93. }
  94. }
  95. #change version
  96. do_sql($dbh,'UPDATE version SET `version`="'.$old_releases[$i].'"');
  97. }
  98. print "Done!\n";
  99. exit;