Skip to content

Commit

Permalink
Clean up user tables before Combining in Upgrade
Browse files Browse the repository at this point in the history
For #1128
  • Loading branch information
eSilverStrike committed Jun 9, 2022
1 parent a789995 commit 47c4b82
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
10 changes: 10 additions & 0 deletions sql/updates/mysql_2.2.1_to_2.2.2.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,16 @@ function update_CombineUserTables222()

// Insert dummy data for the guest user beforehand, to prevent column values becoming NULL
DB_query($sql2);

// Clean user tables that may have orphan records
$sql = "DELETE uc FROM {$_TABLES['usercomment']} uc WHERE uid NOT IN (SELECT uid FROM {$_TABLES['users']} u WHERE uc.uid = u.uid)";
DB_query($sql);
$sql = "DELETE uc FROM {$_TABLES['userindex']} uc WHERE uid NOT IN (SELECT uid FROM {$_TABLES['users']} u WHERE uc.uid = u.uid)";
DB_query($sql);
$sql = "DELETE uc FROM {$_TABLES['userinfo']} uc WHERE uid NOT IN (SELECT uid FROM {$_TABLES['users']} u WHERE uc.uid = u.uid)";
DB_query($sql);
$sql = "DELETE uc FROM {$_TABLES['userprefs']} uc WHERE uid NOT IN (SELECT uid FROM {$_TABLES['users']} u WHERE uc.uid = u.uid)";
DB_query($sql);

// Collect data from old tables
$result = DB_query($sql3);
Expand Down
10 changes: 10 additions & 0 deletions sql/updates/pgsql_2.2.1_to_2.2.2.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,16 @@ function update_CombineUserTables222()
LEFT JOIN {$_TABLES['userinfo']} AS f ON c.uid = f.uid
LEFT JOIN {$_TABLES['userprefs']} AS p ON c.uid = p.uid
SQL;

// Clean user tables that may have orphan records
$sql = "DELETE uc FROM {$_TABLES['usercomment']} uc WHERE uid NOT IN (SELECT uid FROM {$_TABLES['users']} u WHERE uc.uid = u.uid)";
DB_query($sql);
$sql = "DELETE uc FROM {$_TABLES['userindex']} uc WHERE uid NOT IN (SELECT uid FROM {$_TABLES['users']} u WHERE uc.uid = u.uid)";
DB_query($sql);
$sql = "DELETE uc FROM {$_TABLES['userinfo']} uc WHERE uid NOT IN (SELECT uid FROM {$_TABLES['users']} u WHERE uc.uid = u.uid)";
DB_query($sql);
$sql = "DELETE uc FROM {$_TABLES['userprefs']} uc WHERE uid NOT IN (SELECT uid FROM {$_TABLES['users']} u WHERE uc.uid = u.uid)";
DB_query($sql);

// With PostgreSQL, we can use transaction
if (DB_beginTransaction()) {
Expand Down

0 comments on commit 47c4b82

Please sign in to comment.