2022-11-15 23:30:37 +01:00
|
|
|
package BurguillosInfo::DB::Migrations;
|
|
|
|
|
|
|
|
use v5.34.1;
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
2023-08-08 19:39:13 +02:00
|
|
|
use utf8;
|
|
|
|
|
|
|
|
use feature 'signatures';
|
2022-11-15 23:30:37 +01:00
|
|
|
|
|
|
|
sub MIGRATIONS {
|
2023-05-07 18:10:55 +02:00
|
|
|
return (
|
|
|
|
'CREATE TABLE options (
|
|
|
|
name TEXT PRIMARY KEY,
|
|
|
|
value TEXT
|
|
|
|
)',
|
|
|
|
'CREATE EXTENSION IF NOT EXISTS "uuid-ossp"',
|
|
|
|
'CREATE TABLE paths (
|
|
|
|
path TEXT PRIMARY KEY,
|
|
|
|
first_seen timestamp DEFAULT NOW()
|
|
|
|
)',
|
|
|
|
'CREATE TABLE requests (
|
|
|
|
uuid UUID DEFAULT uuid_generate_v4(),
|
|
|
|
remote_address TEXT NOT NULL,
|
|
|
|
user_agent TEXT NOT NULL,
|
|
|
|
params JSON NOT NULL,
|
|
|
|
date timestamp DEFAULT NOW(),
|
|
|
|
path TEXT,
|
|
|
|
FOREIGN KEY (path) REFERENCES paths(path)
|
|
|
|
)',
|
2023-05-02 20:52:39 +02:00
|
|
|
'ALTER TABLE paths ADD column last_seen TIMESTAMP;',
|
|
|
|
'ALTER TABLE paths ALTER COLUMN last_seen SET DEFAULT NOW();',
|
|
|
|
'ALTER TABLE requests ADD PRIMARY KEY (uuid)',
|
2023-05-02 21:59:31 +02:00
|
|
|
'CREATE INDEX request_extra_index on requests (date, path);',
|
2023-05-07 18:10:55 +02:00
|
|
|
'ALTER TABLE requests ADD column referer text;',
|
|
|
|
'CREATE INDEX request_referer_index on requests (referer);',
|
2023-08-08 19:39:13 +02:00
|
|
|
'ALTER TABLE requests ADD COLUMN country TEXT;',
|
|
|
|
'CREATE INDEX request_country_index on requests (country);',
|
|
|
|
'ALTER TABLE requests ADD COLUMN subdivision TEXT;',
|
|
|
|
'CREATE INDEX request_subdivision_index on requests (subdivision);',
|
|
|
|
\&_populate_locations,
|
2023-08-08 19:46:41 +02:00
|
|
|
\&_populate_locations,
|
2023-08-08 20:03:55 +02:00
|
|
|
\&_populate_locations,
|
2023-05-07 18:10:55 +02:00
|
|
|
);
|
2022-11-15 23:30:37 +01:00
|
|
|
}
|
2023-08-08 19:39:13 +02:00
|
|
|
|
2023-08-08 19:59:20 +02:00
|
|
|
sub _populate_locations ($dbh) {
|
2023-08-08 19:39:13 +02:00
|
|
|
require BurguillosInfo;
|
|
|
|
require BurguillosInfo::Tracking;
|
2023-08-08 19:59:20 +02:00
|
|
|
my $tracking = BurguillosInfo::Tracking->new( BurguillosInfo->new );
|
|
|
|
my $page = 0;
|
|
|
|
while (1) {
|
2023-08-08 20:06:32 +02:00
|
|
|
my $data = $dbh->selectall_arrayref( <<"EOF", { Slice => {} } );
|
2023-08-08 19:39:13 +02:00
|
|
|
SELECT uuid, remote_address
|
|
|
|
FROM requests
|
2023-08-08 19:59:20 +02:00
|
|
|
WHERE date > NOW() - interval '2 months'
|
2023-08-08 20:08:10 +02:00
|
|
|
OFFSET $page
|
2023-08-08 20:07:29 +02:00
|
|
|
LIMIT 100;
|
2023-08-08 19:39:13 +02:00
|
|
|
EOF
|
2023-08-08 19:59:20 +02:00
|
|
|
if (!@$data) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for my $request (@$data) {
|
|
|
|
my ( $uuid, $remote_address ) =
|
|
|
|
$request->@{ 'uuid', 'remote_address' };
|
|
|
|
$tracking->update_country_and_subdivision( $dbh, $uuid,
|
|
|
|
$remote_address );
|
|
|
|
}
|
|
|
|
$page += 100;
|
2023-08-08 19:39:13 +02:00
|
|
|
}
|
|
|
|
}
|
2022-11-15 23:30:37 +01:00
|
|
|
1;
|