From 49af1b643209bfe6b5aac5361fa6f7d5dc5bc944 Mon Sep 17 00:00:00 2001 From: Andrey Lugovskoy Date: Fri, 24 Apr 2026 15:40:28 +0400 Subject: [PATCH] Add created_at to pairs for pair growth stats MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Migration 000016. Existing 825 rows keep created_at=0 (we can't backfill accurately); new /bridge pairs get a real unix timestamp. SQLite uses INSERT … ON CONFLICT … DO UPDATE SET prefix=0 (previously INSERT OR REPLACE, which would have wiped created_at on conflict). Co-Authored-By: Claude Opus 4.7 (1M context) --- migrations/postgres/000016_pairs_created_at.down.sql | 1 + migrations/postgres/000016_pairs_created_at.up.sql | 1 + migrations/sqlite/000016_pairs_created_at.down.sql | 1 + migrations/sqlite/000016_pairs_created_at.up.sql | 1 + postgres.go | 4 ++-- sqlite.go | 5 ++++- 6 files changed, 10 insertions(+), 3 deletions(-) create mode 100644 migrations/postgres/000016_pairs_created_at.down.sql create mode 100644 migrations/postgres/000016_pairs_created_at.up.sql create mode 100644 migrations/sqlite/000016_pairs_created_at.down.sql create mode 100644 migrations/sqlite/000016_pairs_created_at.up.sql diff --git a/migrations/postgres/000016_pairs_created_at.down.sql b/migrations/postgres/000016_pairs_created_at.down.sql new file mode 100644 index 0000000..7326fc4 --- /dev/null +++ b/migrations/postgres/000016_pairs_created_at.down.sql @@ -0,0 +1 @@ +ALTER TABLE pairs DROP COLUMN created_at; diff --git a/migrations/postgres/000016_pairs_created_at.up.sql b/migrations/postgres/000016_pairs_created_at.up.sql new file mode 100644 index 0000000..400d416 --- /dev/null +++ b/migrations/postgres/000016_pairs_created_at.up.sql @@ -0,0 +1 @@ +ALTER TABLE pairs ADD COLUMN created_at BIGINT NOT NULL DEFAULT 0; diff --git a/migrations/sqlite/000016_pairs_created_at.down.sql b/migrations/sqlite/000016_pairs_created_at.down.sql new file mode 100644 index 0000000..7326fc4 --- /dev/null +++ b/migrations/sqlite/000016_pairs_created_at.down.sql @@ -0,0 +1 @@ +ALTER TABLE pairs DROP COLUMN created_at; diff --git a/migrations/sqlite/000016_pairs_created_at.up.sql b/migrations/sqlite/000016_pairs_created_at.up.sql new file mode 100644 index 0000000..d2af423 --- /dev/null +++ b/migrations/sqlite/000016_pairs_created_at.up.sql @@ -0,0 +1 @@ +ALTER TABLE pairs ADD COLUMN created_at INTEGER NOT NULL DEFAULT 0; diff --git a/postgres.go b/postgres.go index 24d8fa2..04be02d 100644 --- a/postgres.go +++ b/postgres.go @@ -64,8 +64,8 @@ func (r *pgRepo) Register(key, platform string, chatID int64) (bool, string, err } _, err = r.db.Exec( - "INSERT INTO pairs (tg_chat_id, max_chat_id, prefix) VALUES ($1, $2, 0) ON CONFLICT (tg_chat_id, max_chat_id) DO NOTHING", - tgID, maxID) + "INSERT INTO pairs (tg_chat_id, max_chat_id, prefix, created_at) VALUES ($1, $2, 0, $3) ON CONFLICT (tg_chat_id, max_chat_id) DO NOTHING", + tgID, maxID, time.Now().Unix()) return true, "", err } diff --git a/sqlite.go b/sqlite.go index 031c602..93cc3e1 100644 --- a/sqlite.go +++ b/sqlite.go @@ -60,7 +60,10 @@ func (r *sqliteRepo) Register(key, platform string, chatID int64) (bool, string, tgID, maxID = peerChatID, chatID } - _, err = r.db.Exec("INSERT OR REPLACE INTO pairs (tg_chat_id, max_chat_id, prefix) VALUES (?, ?, 0)", tgID, maxID) + _, err = r.db.Exec( + `INSERT INTO pairs (tg_chat_id, max_chat_id, prefix, created_at) VALUES (?, ?, 0, ?) + ON CONFLICT(tg_chat_id, max_chat_id) DO UPDATE SET prefix = 0`, + tgID, maxID, time.Now().Unix()) return true, "", err }