mirror of
https://github.com/felipegcoutinho/openmonetis.git
synced 2026-05-09 02:51:46 +00:00
feat(payers): gerar share_code na aplicação e remover pgcrypto
Move a geração do share_code do PostgreSQL para a camada de aplicação, eliminando a dependência da extensão pgcrypto no setup do banco. - schema: drop default substr(encode(gen_random_bytes(24), 'base64'), 1, 24) da coluna share_code em pagadores (continua NOT NULL) - nova util generateShareCode() em shared/lib/payers/share-code.ts (server-only, usa crypto.randomBytes do Node) - chamadas explícitas em createPayerAction, ensureDefaultPagadorForUser, resetUserAppData e mock-data ao inserir pagadores - migration 0028_fancy_reaper renumerada (0027 já estava ocupado por arquivo órfão); journal e snapshot atualizados - remove etapa de habilitação de pgcrypto do docker-entrypoint.sh - remove scripts/postgres/ (init.sql e enable-extensions.ts) Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -44,6 +44,7 @@ import {
|
||||
PAYER_ROLE_THIRD_PARTY,
|
||||
PAYER_STATUS_OPTIONS,
|
||||
} from "@/shared/lib/payers/constants";
|
||||
import { generateShareCode } from "@/shared/lib/payers/share-code";
|
||||
import { normalizeNameFromEmail } from "@/shared/lib/payers/utils";
|
||||
import {
|
||||
addMonthsToDate,
|
||||
@@ -537,6 +538,7 @@ async function ensureAdminPayer(targetUser: typeof user.$inferSelect) {
|
||||
note: null,
|
||||
role: PAYER_ROLE_ADMIN,
|
||||
isAutoSend: false,
|
||||
shareCode: generateShareCode(),
|
||||
userId: targetUser.id,
|
||||
})
|
||||
.returning({ id: payers.id, name: payers.name });
|
||||
@@ -870,6 +872,7 @@ async function main() {
|
||||
note: definition.note,
|
||||
role: PAYER_ROLE_THIRD_PARTY,
|
||||
isAutoSend: definition.isAutoSend,
|
||||
shareCode: generateShareCode(),
|
||||
userId: targetUser.id,
|
||||
})
|
||||
.returning({ id: payers.id });
|
||||
|
||||
@@ -1,45 +0,0 @@
|
||||
import * as fs from "node:fs";
|
||||
import * as path from "node:path";
|
||||
import { config } from "dotenv";
|
||||
import { drizzle } from "drizzle-orm/node-postgres";
|
||||
import { Pool } from "pg";
|
||||
|
||||
// Load environment variables from .env
|
||||
config();
|
||||
|
||||
async function initDatabase() {
|
||||
const databaseUrl = process.env.DATABASE_URL;
|
||||
|
||||
if (!databaseUrl) {
|
||||
console.error("DATABASE_URL environment variable is required");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const pool = new Pool({ connectionString: databaseUrl });
|
||||
const db = drizzle(pool);
|
||||
|
||||
try {
|
||||
console.log("🔧 Initializing database extensions...");
|
||||
|
||||
// Read and execute init.sql as a single query
|
||||
const initSqlPath = path.join(
|
||||
process.cwd(),
|
||||
"scripts",
|
||||
"postgres",
|
||||
"init.sql",
|
||||
);
|
||||
const initSql = fs.readFileSync(initSqlPath, "utf-8");
|
||||
|
||||
console.log("Executing init.sql...");
|
||||
await db.execute(initSql);
|
||||
|
||||
console.log("✅ Database initialization completed");
|
||||
} catch (error) {
|
||||
console.error("❌ Database initialization failed:", error);
|
||||
process.exit(1);
|
||||
} finally {
|
||||
await pool.end();
|
||||
}
|
||||
}
|
||||
|
||||
initDatabase();
|
||||
@@ -1,10 +0,0 @@
|
||||
-- Script de inicialização do PostgreSQL para Docker
|
||||
-- Este script é executado automaticamente quando o banco é criado pela primeira vez
|
||||
|
||||
-- Habilitar extensão pgcrypto (necessária para gen_random_bytes usado pelo Drizzle)
|
||||
CREATE EXTENSION IF NOT EXISTS pgcrypto;
|
||||
-- Log de sucesso
|
||||
DO $$
|
||||
BEGIN
|
||||
RAISE NOTICE '✅ Extensão pgcrypto habilitada com sucesso';
|
||||
END $$;
|
||||
Reference in New Issue
Block a user