upgrade.pl 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. );
  27. my $r_index = 0;
  28. my %old_releases_h = map {$_ => $r_index++ } @old_releases;
  29. my $eye_release = $old_releases[@old_releases - 1];
  30. $dbh=init_db();
  31. init_option($dbh);
  32. if (!$config_ref{version} and !$ARGV[0]) {
  33. print "Current version unknown! Skip upgrade!\n";
  34. exit 100;
  35. }
  36. if ($ARGV[0]) {
  37. if (exists($old_releases_h{$ARGV[0]})) { $config_ref{version}=$ARGV[0]; } else { print "Unknown version $ARGV[0]!\n"; }
  38. }
  39. if (!exists($old_releases_h{$config_ref{version}})) { print "Unknown version $config_ref{version}!\n"; exit 100; }
  40. if ($eye_release eq $config_ref{version}) { print "Already updated!\n"; exit; }
  41. print 'Current version: '.$config_ref{version}.' upgrade to: '.$eye_release."\n";
  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 = glob($dir_name.'/*.sql');
  65. if (@sql_patches and scalar @sql_patches) {
  66. foreach my $patch (@sql_patches) {
  67. next if (!$patch or ! -e $patch);
  68. next if ($patch=~/version.sql/);
  69. my @sql_cmd=read_file($patch);
  70. foreach my $sql (@sql_cmd) {
  71. my $sql_prep = $dbh->prepare($sql) or die "Unable to prepare $sql: " . $dbh->errstr."\n";
  72. my $sql_ref;
  73. my $rv = $sql_prep->execute();
  74. if (!$rv) { print "Unable to execute $sql: " . $dbh->errstr."\n"; }
  75. $sql_prep->finish();
  76. }
  77. }
  78. }
  79. #patch after change database schema
  80. @perl_patches = glob($dir_name.'/after*.pl');
  81. if (@perl_patches and scalar @perl_patches) {
  82. foreach my $patch (@perl_patches) {
  83. next if (!$patch or ! -e $patch);
  84. open(my $pipe, "-|", "perl $patch") or die "Error in apply upgrade script $patch! Ошибка: $!";
  85. while (my $line = <$pipe>) {
  86. if ($line =~ /::/) { print "\r"; $line =~s/\:\://; }
  87. print $line;
  88. }
  89. close($pipe);
  90. }
  91. }
  92. #change version
  93. do_sql($dbh,'UPDATE version SET `version`="'.$old_releases[$i].'"');
  94. }
  95. print "Done!\n";
  96. exit;