upgrade.pl 3.2 KB

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