relay email changes to pg

This commit is contained in:
Spine
2025-07-18 21:32:03 +00:00
parent 9010b558bc
commit b6d2cbfcfd
4 changed files with 56 additions and 5 deletions

View File

@@ -143,10 +143,15 @@ class Pg {
public function insert(string $query, ...$args): int {
$begin = microtime(true);
$st = $this->prepare($query);
if ($st->execute([...$args])) {
$id = (int)$this->pdo->lastInsertId();
$this->stats->register($query, $id, $begin, [...$args]);
return $id;
try {
if ($st->execute([...$args])) {
$id = (int)$this->pdo->lastInsertId();
$this->stats->register($query, $id, $begin, [...$args]);
return $id;
}
} catch (\PDOException $e) {
throw new \PDOException($e->getMessage()
. " $query " . json_encode($args, JSON_UNESCAPED_SLASHES));
}
$this->stats->error($query);
return 0;

View File

@@ -224,6 +224,12 @@ class History extends \Gazelle\BaseUser {
", $this->user->id, $newEmail, $ipaddr, $useragent
);
$affected = self::$db->affected_rows();
$this->pg()->insert("
insert into history_email
(id_user, email, useragent, ip)
values (?, ?, ?, ?::inet)
", $this->id, $newEmail, $useragent, $ipaddr
);
if ($notify) {
$irc::sendMessage(
$this->user->username(),

View File

@@ -158,7 +158,13 @@ class UserCreator extends Base {
INSERT INTO users_history_emails
(UserID, Email, IP, useragent, created)
VALUES (?, ?, ?, ?, now() - INTERVAL ? SECOND)
', $this->id, $e, $ipaddr, $useragent, $past--
', $this->id, $e, $ipaddr, $useragent, $past
);
$this->pg()->insert("
insert into history_email
(id_user, email, useragent, ip, created)
values (?, ?, ?, ?::inet, now() - '1 second'::interval * ?)
", $this->id, $e, $useragent, $ipaddr, $past--
);
}

View File

@@ -0,0 +1,34 @@
<?php
declare(strict_types=1);
use Phinx\Migration\AbstractMigration;
use Phinx\Util\Literal;
final class HistoryUserEmail extends AbstractMigration {
public function up(): void {
$this->table('history_email', ['id' => false, 'primary_key' => 'id_history_email'])
->addColumn('id_history_email', 'integer', ['identity' => true])
->addColumn('id_user', 'integer')
->addColumn('ip', 'inet')
->addColumn('created', 'timestamp', ['timezone' => true, 'default' => 'CURRENT_TIMESTAMP'])
->addColumn('email', Literal::from('citext'))
->addColumn('useragent', 'text')
->save();
$this->query(
<<<END_SQL
insert into history_email
(id_user, email, ip, created, useragent)
select "UserID", "Email", "IP"::inet, created, useragent
from relay.users_history_emails
END_SQL
);
$this->query("
create index he_u_idx on history_email (id_user)
");
}
public function down(): void {
$this->table('history_email')->drop()->save();
}
}