upgrade.pl 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. );
  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. init_option($dbh);
  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. my $old_version_index = $old_releases_h{$config_ref{version}} + 1;
  42. my $stage = 1;
  43. for (my $i=$old_version_index; $i < scalar @old_releases; $i++) {
  44. print "Stage $stage. Upgrade to $old_releases[$i]\n";
  45. $stage++;
  46. my $dir_name = $old_releases[$i];
  47. $dir_name =~s/\./-/g;
  48. next if (! -d $dir_name);
  49. #patch before change database schema
  50. my @perl_patches = glob($dir_name.'/before*.pl');
  51. if (@perl_patches and scalar @perl_patches) {
  52. foreach my $patch (@perl_patches) {
  53. next if (!$patch or ! -e $patch);
  54. open(my $pipe, "-|", "perl $patch") or die "Error in apply upgrade script $patch! Ошибка: $!";
  55. while (my $line = <$pipe>) {
  56. if ($line =~ /::/) { print "\r"; $line =~s/\:\://; }
  57. print $line;
  58. }
  59. close($pipe);
  60. }
  61. }
  62. #change database schema
  63. my @sql_patches = glob($dir_name.'/*.sql');
  64. if (@sql_patches and scalar @sql_patches) {
  65. foreach my $patch (@sql_patches) {
  66. next if (!$patch or ! -e $patch);
  67. next if ($patch=~/version.sql/);
  68. my @sql_cmd=read_file($patch);
  69. foreach my $sql (@sql_cmd) {
  70. my $sql_prep = $dbh->prepare($sql) or die "Unable to prepare $sql: " . $dbh->errstr."\n";
  71. my $sql_ref;
  72. my $rv = $sql_prep->execute();
  73. if (!$rv) { print "Unable to execute $sql: " . $dbh->errstr."\n"; }
  74. $sql_prep->finish();
  75. }
  76. }
  77. }
  78. #patch after change database schema
  79. @perl_patches = glob($dir_name.'/after*.pl');
  80. if (@perl_patches and scalar @perl_patches) {
  81. foreach my $patch (@perl_patches) {
  82. next if (!$patch or ! -e $patch);
  83. open(my $pipe, "-|", "perl $patch") or die "Error in apply upgrade script $patch! Ошибка: $!";
  84. while (my $line = <$pipe>) {
  85. if ($line =~ /::/) { print "\r"; $line =~s/\:\://; }
  86. print $line;
  87. }
  88. close($pipe);
  89. }
  90. }
  91. #change version
  92. do_sql($dbh,'UPDATE version SET `version`="'.$old_releases[$i].'"');
  93. }
  94. print "Done!\n";
  95. exit;