pgsql-client 0.0.1 → 1.1.0

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/stream.js ADDED
@@ -0,0 +1,99 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.streamSql = streamSql;
4
+ const child_process_1 = require("child_process");
5
+ const pg_env_1 = require("pg-env");
6
+ const stream_1 = require("stream");
7
+ function setArgs(config) {
8
+ const args = [
9
+ '-U', config.user,
10
+ '-h', config.host,
11
+ '-d', config.database
12
+ ];
13
+ if (config.port) {
14
+ args.push('-p', String(config.port));
15
+ }
16
+ return args;
17
+ }
18
+ // Converts a string to a readable stream (replaces streamify-string)
19
+ function stringToStream(text) {
20
+ const stream = new stream_1.Readable({
21
+ read() {
22
+ this.push(text);
23
+ this.push(null);
24
+ }
25
+ });
26
+ return stream;
27
+ }
28
+ /**
29
+ * Executes SQL statements by streaming them to psql.
30
+ *
31
+ * IMPORTANT: PostgreSQL stderr handling
32
+ * -------------------------------------
33
+ * PostgreSQL sends different message types to stderr, not just errors:
34
+ * - ERROR: Actual SQL errors (should fail)
35
+ * - WARNING: Potential issues (informational)
36
+ * - NOTICE: Informational messages (should NOT fail)
37
+ * - INFO: Informational messages (should NOT fail)
38
+ * - DEBUG: Debug messages (should NOT fail)
39
+ *
40
+ * Example scenario that previously caused false failures:
41
+ * When running SQL like:
42
+ * GRANT administrator TO app_user;
43
+ *
44
+ * If app_user is already a member of administrator, PostgreSQL outputs:
45
+ * NOTICE: role "app_user" is already a member of role "administrator"
46
+ *
47
+ * This is NOT an error - the GRANT succeeded (it's idempotent). But because
48
+ * this message goes to stderr, the old implementation would reject the promise
49
+ * and fail the test, even though nothing was wrong.
50
+ *
51
+ * Solution:
52
+ * 1. Buffer stderr instead of rejecting immediately on any output
53
+ * 2. Use ON_ERROR_STOP=1 so psql exits with non-zero code on actual SQL errors
54
+ * 3. Only reject if the exit code is non-zero, using buffered stderr as the error message
55
+ *
56
+ * This way, NOTICE/WARNING messages are collected but don't cause failures,
57
+ * while actual SQL errors still properly fail with meaningful error messages.
58
+ */
59
+ async function streamSql(config, sql) {
60
+ const args = [
61
+ ...setArgs(config),
62
+ // ON_ERROR_STOP=1 makes psql exit with a non-zero code when it encounters
63
+ // an actual SQL error. Without this, psql might continue executing subsequent
64
+ // statements and exit with code 0 even if some statements failed.
65
+ '-v', 'ON_ERROR_STOP=1'
66
+ ];
67
+ return new Promise((resolve, reject) => {
68
+ const sqlStream = stringToStream(sql);
69
+ // Buffer stderr instead of rejecting immediately. This allows us to collect
70
+ // all output (including harmless NOTICE messages) and only use it for error
71
+ // reporting if the process actually fails.
72
+ let stderrBuffer = '';
73
+ const proc = (0, child_process_1.spawn)('psql', args, {
74
+ env: (0, pg_env_1.getSpawnEnvWithPg)(config)
75
+ });
76
+ sqlStream.pipe(proc.stdin);
77
+ // Collect stderr output. We don't reject here because stderr may contain
78
+ // harmless NOTICE/WARNING messages that shouldn't cause test failures.
79
+ proc.stderr.on('data', (data) => {
80
+ stderrBuffer += data.toString();
81
+ });
82
+ // Determine success/failure based on exit code, not stderr content.
83
+ // Exit code 0 = success (even if there were NOTICE messages on stderr)
84
+ // Exit code non-zero = actual error occurred
85
+ proc.on('close', (code) => {
86
+ if (code !== 0) {
87
+ // Include the buffered stderr in the error message for debugging
88
+ reject(new Error(stderrBuffer || `psql exited with code ${code}`));
89
+ }
90
+ else {
91
+ resolve();
92
+ }
93
+ });
94
+ // Handle spawn errors (e.g., psql not found)
95
+ proc.on('error', (error) => {
96
+ reject(error);
97
+ });
98
+ });
99
+ }