burguillos.info/lib/BurguillosInfo/DB/Migrations.pm

122 lines
4.5 KiB
Perl
Raw Normal View History

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';
sub MIGRATIONS {
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)
)',
2024-01-13 01:17:57 +01:00
'ALTER TABLE paths ADD COLUMN last_seen TIMESTAMP;',
2023-05-02 20:52:39 +02:00
'ALTER TABLE paths ALTER COLUMN last_seen SET DEFAULT NOW();',
'ALTER TABLE requests ADD PRIMARY KEY (uuid)',
'CREATE INDEX request_extra_index on requests (date, path);',
2024-01-13 01:17:57 +01: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);',
2023-08-09 11:23:45 +02:00
\&_populate_locations,
2023-08-09 11:26:31 +02:00
\&_populate_locations,
2023-08-09 11:23:45 +02:00
\&_populate_locations,
\&_populate_locations,
\&_populate_locations,
2023-09-07 18:12:28 +02:00
'CREATE TABLE farmacia_guardia (
uuid UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
date timestamp NOT NULL,
id_farmacia TEXT NOT NULL
);',
'CREATE INDEX farmacia_guardia_index on farmacia_guardia (date, id_farmacia, uuid);',
2023-11-17 23:16:54 +01:00
'CREATE TABLE conquer_user (
uuid UUID NOT NULL PRIMARY KEY,
username TEXT NOT NULL UNIQUE,
encrypted_password TEXT NOT NULL,
2023-11-19 23:14:02 +01:00
last_activity TIMESTAMP NOT NULL DEFAULT NOW(),
is_admin BOOLEAN NOT NULL DEFAULT false,
registration_date TIMESTAMP NOT NULL DEFAULT NOW()
2023-11-17 23:16:54 +01:00
);',
2023-12-31 20:43:53 +01:00
'CREATE TABLE conquer_node (
uuid UUID NOT NULL PRIMARY KEY,
name TEXT NOT NULL,
type TEXT NOT NULL,
coordinate_1 REAL NOT NULL,
coordinate_2 REAL NOT NULL,
description TEXT NOT NULL
);',
'CREATE INDEX index_conquer_node_coordinate_1 on conquer_node (coordinate_1);',
'CREATE INDEX index_conquer_node_coordinate_2 on conquer_node (coordinate_2);',
'ALTER TABLE conquer_user ADD COLUMN last_coordinate_1 REAL NOT NULL DEFAULT 0;',
'ALTER TABLE conquer_user ALTER COLUMN last_coordinate_1 DROP DEFAULT;',
'ALTER TABLE conquer_user ADD COLUMN last_coordinate_2 REAL NOT NULL DEFAULT 0;',
'ALTER TABLE conquer_user ALTER COLUMN last_coordinate_2 DROP DEFAULT;',
2024-01-13 01:17:57 +01:00
'CREATE TABLE conquer_teams (
uuid UUID NOT NULL PRIMARY KEY,
name TEXT NOT NULL,
description TEXT NOT NULL DEFAULT \'\',
points INTEGER NOT NULL DEFAULT 0
);',
'ALTER TABLE conquer_user ADD COLUMN team UUID REFERENCES conquer_teams (uuid);',
'ALTER TABLE conquer_node ADD COLUMN team UUID REFERENCES conquer_teams (uuid);',
'ALTER TABLE conquer_teams ADD COLUMN color TEXT NOT NULL DEFAULT \'#000\';',
'ALTER TABLE conquer_teams ALTER COLUMN color SET DEFAULT \'#555\';',
'ALTER TABLE conquer_teams ALTER COLUMN color SET DEFAULT \'#aaa\';',
);
}
2023-08-08 19:39:13 +02:00
sub _populate_locations ($dbh) {
2023-11-17 23:16:54 +01:00
# This subroutine crashes the migrations.
# require BurguillosInfo;
# require BurguillosInfo::Tracking;
# my $tracking = BurguillosInfo::Tracking->new( BurguillosInfo->new );
# my $page = 0;
# while (1) {
# last if !_update_request_page( $dbh, $tracking, $page );
# $page += 100;
# }
2023-08-09 11:23:45 +02:00
}
sub _update_request_page ( $dbh, $tracking, $page ) {
my $data = $dbh->selectall_arrayref( <<"EOF", { Slice => {} }, $page );
2023-08-08 19:39:13 +02:00
SELECT uuid, remote_address
FROM requests
2023-08-09 11:23:45 +02:00
WHERE date > NOW() - interval '1 month'
AND country IS NULL
AND subdivision IS NULL
ORDER BY date desc
2023-08-08 20:08:10 +02:00
OFFSET $page
2023-08-09 11:23:45 +02:00
LIMIT ?;
2023-08-08 19:39:13 +02:00
EOF
2023-08-09 11:23:45 +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 );
2023-08-08 19:39:13 +02:00
}
2023-08-09 11:23:45 +02:00
return 1;
2023-08-08 19:39:13 +02:00
}
1;