Skip to content

Commit bcf8537

Browse files
committed
Restore the mail settings and templates in one transaction after the spec
The four DELETE/INSERT statements of the cleanup now run in one mysql call inside a transaction. If any fails, the client exits without COMMIT, so the install is never left half restored and the backup tables stay. db() gets the JSDoc the other spec helpers have. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EY98MP9mUiKL95YcoDGcUm
1 parent d62a5fc commit bcf8537

1 file changed

Lines changed: 16 additions & 7 deletions

File tree

‎tests/mail-template-approval.spec.js‎

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,14 @@ const BASE = process.env.E2E_BASE_URL || 'http://localhost:8081';
1313
const MAILPIT_API = process.env.MAILPIT_API || 'http://localhost:8025/api/v1';
1414
const RUN = Date.now().toString(36);
1515

16+
/**
17+
* Runs one statement against the test database and returns its tab-separated output,
18+
* trimmed. CI connects over TCP (E2E_DB_HOST/E2E_DB_PORT, no socket); a local run may
19+
* use a socket.
20+
* @param {string} sql
21+
* @returns {string}
22+
*/
1623
function db(sql) {
17-
// CI connects over TCP (E2E_DB_HOST/E2E_DB_PORT, no socket); a local run may use a socket.
1824
const args = [];
1925
if (process.env.E2E_DB_HOST) {
2026
args.push('-h', process.env.E2E_DB_HOST);
@@ -101,12 +107,15 @@ test('approving a loan that starts today sends the edited «Pronto per il ritiro
101107
if (loanId) db(`DELETE FROM prestiti WHERE id=${loanId}`);
102108
if (bookId) { db(`DELETE FROM copie WHERE libro_id=${bookId}`); db(`DELETE FROM libri WHERE id=${bookId}`); }
103109
if (userId) db(`DELETE FROM utenti WHERE id=${userId}`);
104-
db(`DELETE FROM system_settings WHERE category='email'`);
105-
db(`INSERT INTO system_settings SELECT * FROM zz_mail_backup_${RUN}`);
106-
db(`DROP TABLE zz_mail_backup_${RUN}`);
107-
db(`DELETE FROM email_templates WHERE locale='it_IT' AND name IN ('loan_approved','loan_pickup_ready')`);
108-
db(`INSERT INTO email_templates SELECT * FROM zz_tpl_backup_${RUN}`);
109-
db(`DROP TABLE zz_tpl_backup_${RUN}`);
110+
// One transaction in one mysql call: on any error the client stops and disconnects
111+
// without COMMIT, so nothing is half restored and the backups are still there.
112+
db(`START TRANSACTION;
113+
DELETE FROM system_settings WHERE category='email';
114+
INSERT INTO system_settings SELECT * FROM zz_mail_backup_${RUN};
115+
DELETE FROM email_templates WHERE locale='it_IT' AND name IN ('loan_approved','loan_pickup_ready');
116+
INSERT INTO email_templates SELECT * FROM zz_tpl_backup_${RUN};
117+
COMMIT`);
118+
db(`DROP TABLE zz_mail_backup_${RUN}, zz_tpl_backup_${RUN}`);
110119
await page.request.get(`${BASE}/_e2e/flush-cache`);
111120
}
112121
});

0 commit comments

Comments
 (0)