@rebasepro/server-postgres 0.10.0 → 0.10.1-canary.a54c057
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.es.js +47 -0
- package/dist/index.es.js.map +1 -1
- package/package.json +6 -6
- package/src/auth/ensure-tables.ts +74 -0
package/dist/index.es.js
CHANGED
|
@@ -21334,6 +21334,53 @@ async function ensureAuthTablesExist(db, collection) {
|
|
|
21334
21334
|
ALTER TABLE ${sql.raw(usersTableName)}
|
|
21335
21335
|
ADD COLUMN IF NOT EXISTS ${sql.raw(columnDef)}
|
|
21336
21336
|
`);
|
|
21337
|
+
try {
|
|
21338
|
+
const found = (await db.execute(sql`
|
|
21339
|
+
SELECT table_schema, table_name
|
|
21340
|
+
FROM information_schema.tables
|
|
21341
|
+
WHERE table_name = 'refresh_tokens'
|
|
21342
|
+
`)).rows;
|
|
21343
|
+
logger.info(`🔍 refresh_tokens reconcile: found ${found.length} table(s): ${found.map((r) => `"${r.table_schema}"."${r.table_name}"`).join(", ") || "(none)"}`);
|
|
21344
|
+
for (const { table_schema } of found) {
|
|
21345
|
+
const qualified = `"${table_schema}"."refresh_tokens"`;
|
|
21346
|
+
try {
|
|
21347
|
+
await db.execute(sql`ALTER TABLE ${sql.raw(qualified)} ADD COLUMN IF NOT EXISTS user_agent TEXT`);
|
|
21348
|
+
await db.execute(sql`ALTER TABLE ${sql.raw(qualified)} ADD COLUMN IF NOT EXISTS ip_address TEXT`);
|
|
21349
|
+
await db.execute(sql`UPDATE ${sql.raw(qualified)} SET user_agent = '' WHERE user_agent IS NULL`);
|
|
21350
|
+
await db.execute(sql`UPDATE ${sql.raw(qualified)} SET ip_address = '' WHERE ip_address IS NULL`);
|
|
21351
|
+
await db.execute(sql`
|
|
21352
|
+
DELETE FROM ${sql.raw(qualified)} a
|
|
21353
|
+
USING ${sql.raw(qualified)} b
|
|
21354
|
+
WHERE a.ctid < b.ctid
|
|
21355
|
+
AND a.uid = b.uid
|
|
21356
|
+
AND a.user_agent = b.user_agent
|
|
21357
|
+
AND a.ip_address = b.ip_address
|
|
21358
|
+
`);
|
|
21359
|
+
const uniques = await db.execute(sql`
|
|
21360
|
+
SELECT conname, pg_get_constraintdef(oid) AS def
|
|
21361
|
+
FROM pg_constraint
|
|
21362
|
+
WHERE conrelid = ${sql.raw(`'${qualified}'`)}::regclass AND contype = 'u'
|
|
21363
|
+
`);
|
|
21364
|
+
for (const u of uniques.rows) logger.info(` ${qualified} unique: ${u.conname} → ${u.def}`);
|
|
21365
|
+
if (uniques.rows.some((u) => {
|
|
21366
|
+
const cols = (u.def.match(/\(([^)]*)\)/)?.[1] || "").split(",").map((c) => c.trim().replace(/"/g, ""));
|
|
21367
|
+
return cols.length === 3 && cols.includes("uid") && cols.includes("user_agent") && cols.includes("ip_address");
|
|
21368
|
+
})) logger.info(`✓ correct device-session unique already present on ${qualified}`);
|
|
21369
|
+
else {
|
|
21370
|
+
await db.execute(sql`ALTER TABLE ${sql.raw(qualified)} DROP CONSTRAINT IF EXISTS unique_device_session`);
|
|
21371
|
+
await db.execute(sql`
|
|
21372
|
+
ALTER TABLE ${sql.raw(qualified)}
|
|
21373
|
+
ADD CONSTRAINT unique_device_session UNIQUE (uid, user_agent, ip_address)
|
|
21374
|
+
`);
|
|
21375
|
+
logger.info(`✅ Added correct unique_device_session constraint to ${qualified}`);
|
|
21376
|
+
}
|
|
21377
|
+
} catch (perTableError) {
|
|
21378
|
+
logger.warn(`⚠️ refresh_tokens reconcile failed for ${qualified}: ${perTableError instanceof Error ? perTableError.message : String(perTableError)}`);
|
|
21379
|
+
}
|
|
21380
|
+
}
|
|
21381
|
+
} catch (migrationError) {
|
|
21382
|
+
logger.warn(`⚠️ refresh_tokens device-session migration skipped: ${migrationError instanceof Error ? migrationError.message : String(migrationError)}`);
|
|
21383
|
+
}
|
|
21337
21384
|
try {
|
|
21338
21385
|
if ((await db.execute(sql`
|
|
21339
21386
|
SELECT EXISTS (
|