You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -131,105 +129,13 @@ public function save_component_order()
131
129
132
130
publicfunctionmigrate_post_stats_to_v2()
133
131
{
134
-
@set_time_limit(0);
135
-
136
-
/** @var \wpdb $wpdb */
137
-
global$wpdb;
138
-
139
-
do {
140
-
// Select all rows with a post ID but no path ID
141
-
// Note: there is no need for an OFFSET here because we are updating rows as we go
142
-
$results = $wpdb->get_results("SELECT DISTINCT(post_id) FROM {$wpdb->prefix}koko_analytics_post_stats WHERE post_id IS NOT NULL AND path_id IS NULL LIMIT 1000");
$wpdb->query("INSERT INTO {$wpdb->prefix}koko_analytics_post_stats(date, path_id, post_id, visitors, pageviews) SELECT date, path_id, post_id, SUM(visitors), SUM(pageviews) FROM {$wpdb->prefix}koko_analytics_post_stats_old GROUP BY date, path_id");
179
-
180
-
$wpdb->query("DROP TABLE IF EXISTS {$wpdb->prefix}koko_analytics_post_stats_old");
* Between version 2.0 and 2.0.10, there was an issue with the migration script above which would result in incorrect path ID's being returned when bulk inserting new paths.
185
-
* This fixes every entry in the post_stats table by checking each path whether it is correct
186
-
*/
187
136
publicfunctionfix_post_paths_after_v2()
188
137
{
189
-
@set_time_limit(0);
190
-
191
-
/** @var \wpdb $wpdb */
192
-
global$wpdb;
193
-
194
-
$offset = 0;
195
-
$limit = 1000;
196
-
$upserter = newUpserter('paths', 'path');
197
-
198
-
do {
199
-
$results = $wpdb->get_results($wpdb->prepare("SELECT post_id, path_id, p.path FROM {$wpdb->prefix}koko_analytics_post_stats s LEFT JOIN {$wpdb->prefix}koko_analytics_paths p ON p.id = s.path_id WHERE post_id IS NOT NULL AND post_id != 0 GROUP BY post_id LIMIT %d OFFSET %d", [$limit, $offset]));
Copy file name to clipboardExpand all lines: src/Admin/Controller.php
+1Lines changed: 1 addition & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -130,6 +130,7 @@ public function action_admin_notices(): void
130
130
<?phpesc_html_e('Click the button below to proceed with the database migration, this can take some time if you have a large site.', 'koko-analytics'); ?>
* Handles migrating post_stats data from the old (post_id based) format
9
+
* to the new (path_id based) format.
10
+
*
11
+
* This class contains no HTTP concerns (nonces, redirects, etc.)
12
+
* and can be safely called from WP-CLI, cron, migration scripts, or admin actions.
13
+
*/
14
+
15
+
namespaceKokoAnalytics;
16
+
17
+
useKokoAnalytics\Normalizers\Path;
18
+
19
+
class Post_Stats_Migrator
20
+
{
21
+
publicfunctionmigrate_to_v2(): void
22
+
{
23
+
@set_time_limit(0);
24
+
25
+
/** @var \wpdb $wpdb */
26
+
global$wpdb;
27
+
28
+
do {
29
+
// Select all rows with a post ID but no path ID
30
+
// Note: there is no need for an OFFSET here because we are updating rows as we go
31
+
$results = $wpdb->get_results("SELECT DISTINCT(post_id) FROM {$wpdb->prefix}koko_analytics_post_stats WHERE post_id IS NOT NULL AND path_id IS NULL LIMIT 1000");
$wpdb->query("INSERT INTO {$wpdb->prefix}koko_analytics_post_stats(date, path_id, post_id, visitors, pageviews) SELECT date, path_id, post_id, SUM(visitors), SUM(pageviews) FROM {$wpdb->prefix}koko_analytics_post_stats_old GROUP BY date, path_id");
68
+
69
+
$wpdb->query("DROP TABLE IF EXISTS {$wpdb->prefix}koko_analytics_post_stats_old");
70
+
}
71
+
72
+
/**
73
+
* Between version 2.0 and 2.0.10, there was an issue with the migration script above which would result in incorrect path ID's being returned when bulk inserting new paths.
74
+
* This fixes every entry in the post_stats table by checking each path whether it is correct
75
+
*/
76
+
publicfunctionfix_paths(): void
77
+
{
78
+
@set_time_limit(0);
79
+
80
+
/** @var \wpdb $wpdb */
81
+
global$wpdb;
82
+
83
+
$offset = 0;
84
+
$limit = 1000;
85
+
$upserter = newUpserter('paths', 'path');
86
+
87
+
do {
88
+
$results = $wpdb->get_results($wpdb->prepare("SELECT post_id, path_id, p.path FROM {$wpdb->prefix}koko_analytics_post_stats s LEFT JOIN {$wpdb->prefix}koko_analytics_paths p ON p.id = s.path_id WHERE post_id IS NOT NULL AND post_id != 0 GROUP BY post_id LIMIT %d OFFSET %d", [$limit, $offset]));
0 commit comments