Parcourir la source

rewrited upgrade script

root il y a 11 mois
Parent
commit
13ccdfa354
2 fichiers modifiés avec 70 ajouts et 44 suppressions
  1. 30 18
      scripts/eyelib/database.pm
  2. 40 26
      scripts/updates/2-8-0/after_sql.pl

+ 30 - 18
scripts/eyelib/database.pm

@@ -237,28 +237,40 @@ File::Temp::cleanup();
 
 #---------------------------------------------------------------------------------------------------------------
 
+#my $new_id = do_sql($dbh, 'INSERT INTO User_list (login) VALUES (?)', 'Ivan');
 sub do_sql {
-my $db=shift;
-my $sql=shift;
-return if (!$db);
-return if (!$sql);
-if ($sql!~/^select /i) { log_debug($sql); }
-my $sql_prep = $db->prepare($sql) or die "Unable to prepare $sql: " . $db->errstr;
-my $sql_ref;
-my $rv = $sql_prep->execute() or die "Unable to execute $sql: " . $db->errstr;
-if ($sql=~/^insert/i) {
-    if ($config_ref{DBTYPE} eq 'mysql') {
-        $sql_ref = $sql_prep->{mysql_insertid};
-	} else {
-        ($sql_ref) = $db->selectrow_array("SELECT lastval()");
-	}
+    my ($db, $sql, @bind_values) = @_;
+    return unless $db;
+    return unless $sql;
+    # Логируем не-SELECT-запросы
+    log_debug( $sql . (@bind_values ? ' | bind: [' . join(', ', map { defined $_ ? $_ : 'undef' } @bind_values) . ']' : '')) unless $sql =~ /^select /i;
+
+    # Подготовка запроса
+    my $sth = $db->prepare($sql) or die "Unable to prepare SQL [$sql]: " . $db->errstr;
+    # Выполнение запроса с подстановкой параметров, если есть
+    my $rv;
+    if (@bind_values) {
+        $rv = $sth->execute(@bind_values) or die "Unable to execute SQL [$sql] with bind: [" . join(', ', map { defined $_ ? $_ : 'undef' } @bind_values) . "]: " . $sth->errstr;
+    } else {
+        $rv = $sth->execute() or die "Unable to execute SQL [$sql]: " . $sth->errstr;
     }
-if ($sql=~/^select /i) { $sql_ref = $sql_prep->fetchall_arrayref() or die "Unable to select $sql: " . $db->errstr; };
-$sql_prep->finish();
-return $sql_ref;
+    my $sql_ref;
+    # Возврат ID при insert
+    if ($sql =~ /^insert/i) {
+        if ($config_ref{DBTYPE} and $config_ref{DBTYPE} eq 'mysql') {
+            $sql_ref = $sth->{mysql_insertid};
+        } else {
+            ($sql_ref) = $db->selectrow_array("SELECT lastval()");
+        }
+    }
+    # Обработка SELECT
+    elsif ($sql =~ /^select /i) {
+        $sql_ref = $sth->fetchall_arrayref({}) or die "Unable to fetch data for SQL [$sql]: " . $sth->errstr;
+    }
+    $sth->finish();
+    return $sql_ref;
 }
 
-
 #---------------------------------------------------------------------------------------------------------------
 
 sub get_first_line {

+ 40 - 26
scripts/updates/2-8-0/after_sql.pl

@@ -15,6 +15,7 @@ use eyelib::main;
 use eyelib::database;
 use strict;
 use warnings;
+use Data::Dumper;
 
 STDOUT->autoflush(1);
 
@@ -24,14 +25,17 @@ my $this_release = '2.8.0';
 $dbh=init_db();
 init_option($dbh);
 
+my $force = 0;
+if ($ARGV[0] and $ARGV[0] eq 'force') { $force = 1; }
+
 if (!$config_ref{version}) {
     print "Current version unknown! Skip upgrade!\n";
     exit 100;
     }
 
-if ($this_release eq $config_ref{version}) { print "Already updated!\n"; exit; }
+if (!$force and $this_release eq $config_ref{version}) { print "Already updated!\n"; exit; }
 
-if ($upgrade_from ne $config_ref{version}) { print "Illegal version. Needed $upgrade_from!\n"; exit; }
+if (!$force and $upgrade_from ne $config_ref{version}) { print "Illegal version. Needed $upgrade_from!\n"; exit; }
 
 print 'Apply patch for version: '.$config_ref{version}.' upgrade to: '.$this_release."\n";
 
@@ -43,35 +47,45 @@ print "Stage 1: Fix dns name fields\n";
 
 my $i = 0;
 foreach my $row (@authlist_ref) {
-my $new;
-$i++;
-my $dns_name = trim($row->{dns_name});
-if ($dns_name and $dns_name =~ /\.\Q$domain_name\E$/i) {
-    $dns_name =~ s/\.\Q$domain_name\E$//i;
-    $dns_name =~s/\.$//g;
-    $dns_name =~s/_/-/g;
-    $dns_name =~s/ /-/g;
-    $dns_name =~s/-$//g;
-    $dns_name = trim($dns_name);
-    if ($dns_name) { $new->{dns_name}=$dns_name; }
-    } else {
-    $dns_name =~s/_/-/g;
-    $dns_name =~s/ /-/g;
-    $dns_name =~s/-$//g;
+    $i++;
+    my $percent = int(($i / $total) * 100);
+    print "\r::Progress: [$percent%] ";
+
+    my $dns_name = trim($row->{dns_name});
+    next unless $dns_name;
+    my $original_name = $dns_name;
+
+    $dns_name =~ s/\.$//g;
+    $dns_name =~ s/_/-/g;
+    $dns_name =~ s/ /-/g;
+    $dns_name =~ s/-$//g;
     $dns_name = trim($dns_name);
-    if ($dns_name and $dns_name=~/\./) {
-        $dns_name = $dns_name.".";
-        $new->{dns_name}=$dns_name;
-        }
-    }
 
-my $percent = int(($i / $total) * 100);
+    my $new;
 
-if (exists $new->{dns_name} and $new->{dns_name}) {
-    update_record($dbh,'User_auth',$new,'id='.$row->{id});
+    # --- Если имя заканчивается на домен, убираем его
+    if ($dns_name =~ /\.\Q$domain_name\E$/i) {
+        $dns_name =~ s/\.\Q$domain_name\E$//i;
+        $dns_name = trim($dns_name);
+        $new->{dns_name} = $dns_name if $dns_name;
     }
 
-print "\r::Progress: [$percent%] ";
+    # --- Если домен не указан в конце (возможно, уже очищен), обрабатываем точки
+    if ($dns_name !~ /\.\Q$domain_name\E$/i) {
+        $dns_name =~ s/\.\.$//g;
+        my $dot_count = ($dns_name =~ tr/.//);
+        if ($dot_count > 1) {
+            $dns_name .= "." unless $dns_name =~ /\.$/;
+        } else {
+            $dns_name =~ s/\.$//g;
+        }
+        $new->{dns_name} = $dns_name if $dns_name;
+    }
+
+    # --- Обновляем, только если имя изменилось
+    if (exists $new->{dns_name} && $new->{dns_name} ne $original_name) {
+        do_sql($dbh, 'UPDATE User_auth SET dns_name = ? WHERE id = ?', $new->{dns_name}, $row->{id});
+    }
 }
 
 print "Done!\n";