1.4 KiB · text History 263b40b
//! Client addresses, for staff only: where each account has been seen
//! (sign-in, registration, posting, flagging, app use), and the address a
//! flag came from. Kept `IP_RETENTION_DAYS` (src/ips.rs purges).
//!
//! `ip` is the text form of the address (`inet` casts in queries look up
//! subnets); one row per (user, address).
use sea_orm_migration::prelude::*;

#[derive(DeriveMigrationName)]
pub struct Migration;

const UP: &str = r#"
CREATE TABLE user_ips (
    user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE ON UPDATE CASCADE,
    ip VARCHAR(45) NOT NULL,
    first_seen TIMESTAMPTZ NOT NULL,
    last_seen TIMESTAMPTZ NOT NULL,
    count INTEGER NOT NULL DEFAULT 1,
    last_action VARCHAR(16) NOT NULL,
    PRIMARY KEY (user_id, ip)
);
CREATE INDEX idx_user_ips_ip ON user_ips (ip);
CREATE INDEX idx_user_ips_last_seen ON user_ips (last_seen);

ALTER TABLE post_flags ADD COLUMN ip VARCHAR(45) NULL;
"#;

const DOWN: &str = r#"
ALTER TABLE post_flags DROP COLUMN IF EXISTS ip;
DROP TABLE IF EXISTS user_ips;
"#;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
    async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager.get_connection().execute_unprepared(UP).await.map(|_| ())
    }

    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager.get_connection().execute_unprepared(DOWN).await.map(|_| ())
    }
}