@take-out/postgres 0.2.10 → 0.3.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/dist/cjs/cli.cjs +0 -0
- package/dist/cjs/scripts/drizzle-migrations-sync.cjs +0 -0
- package/dist/esm/cli.mjs +0 -0
- package/dist/esm/getDBClient.mjs.map +1 -1
- package/dist/esm/index.js +6 -14
- package/dist/esm/index.js.map +1 -6
- package/dist/esm/scripts/drizzle-migrations-sync.mjs +0 -0
- package/dist/esm/sql.mjs.map +1 -1
- package/package.json +3 -3
- package/src/chunkedQuery.ts +3 -3
- package/src/cli.ts +2 -2
- package/src/createDb.ts +1 -1
- package/src/createServerHelpers.ts +1 -1
- package/src/getDBClient.ts +13 -13
- package/src/migrate.ts +2 -2
- package/src/scripts/build-migrations.ts +1 -1
- package/src/scripts/drizzle-migrations-sync.ts +1 -1
- package/src/sql.ts +2 -2
- package/types/getDBClient.d.ts +1 -1
- package/types/getDBClient.d.ts.map +1 -1
- package/types/scripts/build-migrations.d.ts +1 -1
- package/types/scripts/build-migrations.d.ts.map +1 -1
- package/types/sql.d.ts +2 -2
- package/types/sql.d.ts.map +1 -1
- package/dist/cjs/build.js +0 -21
- package/dist/cjs/build.js.map +0 -6
- package/dist/cjs/chunkedQuery.js +0 -51
- package/dist/cjs/chunkedQuery.js.map +0 -6
- package/dist/cjs/cli.js +0 -128
- package/dist/cjs/cli.js.map +0 -6
- package/dist/cjs/createDb.js +0 -28
- package/dist/cjs/createDb.js.map +0 -6
- package/dist/cjs/createPool.js +0 -32
- package/dist/cjs/createPool.js.map +0 -6
- package/dist/cjs/createServerHelpers.js +0 -28
- package/dist/cjs/createServerHelpers.js.map +0 -6
- package/dist/cjs/getDBClient.js +0 -168
- package/dist/cjs/getDBClient.js.map +0 -6
- package/dist/cjs/index.js +0 -27
- package/dist/cjs/index.js.map +0 -6
- package/dist/cjs/migrate.js +0 -144
- package/dist/cjs/migrate.js.map +0 -6
- package/dist/cjs/scripts/build-migrations.js +0 -69
- package/dist/cjs/scripts/build-migrations.js.map +0 -6
- package/dist/cjs/scripts/drizzle-migrations-sync.js +0 -118
- package/dist/cjs/scripts/drizzle-migrations-sync.js.map +0 -6
- package/dist/cjs/scripts/migration-add.js +0 -40
- package/dist/cjs/scripts/migration-add.js.map +0 -6
- package/dist/cjs/scripts/pg_dump.js +0 -35
- package/dist/cjs/scripts/pg_dump.js.map +0 -6
- package/dist/cjs/scripts/psql.js +0 -36
- package/dist/cjs/scripts/psql.js.map +0 -6
- package/dist/cjs/sql.js +0 -35
- package/dist/cjs/sql.js.map +0 -6
- package/dist/esm/build.js +0 -5
- package/dist/esm/build.js.map +0 -6
- package/dist/esm/chunkedQuery.js +0 -35
- package/dist/esm/chunkedQuery.js.map +0 -6
- package/dist/esm/cli.js +0 -134
- package/dist/esm/cli.js.map +0 -6
- package/dist/esm/createDb.js +0 -13
- package/dist/esm/createDb.js.map +0 -6
- package/dist/esm/createPool.js +0 -8
- package/dist/esm/createPool.js.map +0 -6
- package/dist/esm/createServerHelpers.js +0 -13
- package/dist/esm/createServerHelpers.js.map +0 -6
- package/dist/esm/getDBClient.js +0 -144
- package/dist/esm/getDBClient.js.map +0 -6
- package/dist/esm/migrate.js +0 -129
- package/dist/esm/migrate.js.map +0 -6
- package/dist/esm/scripts/build-migrations.js +0 -46
- package/dist/esm/scripts/build-migrations.js.map +0 -6
- package/dist/esm/scripts/drizzle-migrations-sync.js +0 -104
- package/dist/esm/scripts/drizzle-migrations-sync.js.map +0 -6
- package/dist/esm/scripts/migration-add.js +0 -25
- package/dist/esm/scripts/migration-add.js.map +0 -6
- package/dist/esm/scripts/pg_dump.js +0 -19
- package/dist/esm/scripts/pg_dump.js.map +0 -6
- package/dist/esm/scripts/psql.js +0 -20
- package/dist/esm/scripts/psql.js.map +0 -6
- package/dist/esm/sql.js +0 -19
- package/dist/esm/sql.js.map +0 -6
package/dist/cjs/createDb.js.map
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": ["../../src/createDb.ts"],
|
|
4
|
-
"mappings": ";;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,2BAA6C,sCAE7C,oBAA2B;AAEpB,MAAM,WAAW,CACtB,kBACA,WAC4B;AAC5B,QAAM,WAAO,8BAAW,gBAAgB;AACxC,aAAO,8BAAQ,MAAM;AAAA,IACnB;AAAA,IACA,QAAQ;AAAA,EACV,CAAC;AACH;",
|
|
5
|
-
"names": []
|
|
6
|
-
}
|
package/dist/cjs/createPool.js
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
var __create = Object.create;
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __getProtoOf = Object.getPrototypeOf, __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: !0 });
|
|
9
|
-
}, __copyProps = (to, from, except, desc) => {
|
|
10
|
-
if (from && typeof from == "object" || typeof from == "function")
|
|
11
|
-
for (let key of __getOwnPropNames(from))
|
|
12
|
-
!__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
13
|
-
return to;
|
|
14
|
-
};
|
|
15
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
16
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
17
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
18
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
19
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
20
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: !0 }) : target,
|
|
21
|
-
mod
|
|
22
|
-
)), __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: !0 }), mod);
|
|
23
|
-
var createPool_exports = {};
|
|
24
|
-
__export(createPool_exports, {
|
|
25
|
-
createPool: () => createPool
|
|
26
|
-
});
|
|
27
|
-
module.exports = __toCommonJS(createPool_exports);
|
|
28
|
-
var import_pg = __toESM(require("pg"), 1);
|
|
29
|
-
const createPool = (connectionString) => new import_pg.default.Pool({
|
|
30
|
-
connectionString
|
|
31
|
-
});
|
|
32
|
-
//# sourceMappingURL=createPool.js.map
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
-
var __export = (target, all) => {
|
|
6
|
-
for (var name in all)
|
|
7
|
-
__defProp(target, name, { get: all[name], enumerable: !0 });
|
|
8
|
-
}, __copyProps = (to, from, except, desc) => {
|
|
9
|
-
if (from && typeof from == "object" || typeof from == "function")
|
|
10
|
-
for (let key of __getOwnPropNames(from))
|
|
11
|
-
!__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
12
|
-
return to;
|
|
13
|
-
};
|
|
14
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: !0 }), mod);
|
|
15
|
-
var createServerHelpers_exports = {};
|
|
16
|
-
__export(createServerHelpers_exports, {
|
|
17
|
-
createServerHelpers: () => createServerHelpers
|
|
18
|
-
});
|
|
19
|
-
module.exports = __toCommonJS(createServerHelpers_exports);
|
|
20
|
-
var import_getDBClient = require("./getDBClient"), import_sql = require("./sql");
|
|
21
|
-
function createServerHelpers(pool) {
|
|
22
|
-
const sql = (0, import_sql.createSql)(pool);
|
|
23
|
-
return (0, import_sql.setDefaultPool)(pool), {
|
|
24
|
-
sql,
|
|
25
|
-
getDBClient: (options = {}) => (0, import_getDBClient.getDBClient)({ pool, ...options })
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
//# sourceMappingURL=createServerHelpers.js.map
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": ["../../src/createServerHelpers.ts"],
|
|
4
|
-
"mappings": ";;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,yBAAqD,0BACrD,aAA0C;AAWnC,SAAS,oBAAoB,MAA2B;AAC7D,QAAM,UAAM,sBAAU,IAAI;AAC1B,wCAAe,IAAI,GAEZ;AAAA,IACL;AAAA,IACA,aAAa,CAAC,UAAU,CAAC,UAAM,gCAAY,EAAE,MAAM,GAAG,QAAQ,CAAC;AAAA,EACjE;AACF;",
|
|
5
|
-
"names": []
|
|
6
|
-
}
|
package/dist/cjs/getDBClient.js
DELETED
|
@@ -1,168 +0,0 @@
|
|
|
1
|
-
var __create = Object.create;
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __getProtoOf = Object.getPrototypeOf, __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: !0 });
|
|
9
|
-
}, __copyProps = (to, from, except, desc) => {
|
|
10
|
-
if (from && typeof from == "object" || typeof from == "function")
|
|
11
|
-
for (let key of __getOwnPropNames(from))
|
|
12
|
-
!__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
13
|
-
return to;
|
|
14
|
-
};
|
|
15
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
16
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
17
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
18
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
19
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
20
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: !0 }) : target,
|
|
21
|
-
mod
|
|
22
|
-
)), __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: !0 }), mod);
|
|
23
|
-
var getDBClient_exports = {};
|
|
24
|
-
__export(getDBClient_exports, {
|
|
25
|
-
getDBClient: () => getDBClient,
|
|
26
|
-
getNewClient: () => getNewClient,
|
|
27
|
-
queryDb: () => queryDb
|
|
28
|
-
});
|
|
29
|
-
module.exports = __toCommonJS(getDBClient_exports);
|
|
30
|
-
var import_pg = __toESM(require("pg"), 1);
|
|
31
|
-
const cache = /* @__PURE__ */ new Map(), createPoolKey = (connectionString) => connectionString, getOrCreatePoolCache = (connectionString, config) => {
|
|
32
|
-
const key = createPoolKey(connectionString);
|
|
33
|
-
return cache.has(key) || cache.set(key, {
|
|
34
|
-
pool: new import_pg.default.Pool(config),
|
|
35
|
-
maxConnections: null,
|
|
36
|
-
reservedConnections: null,
|
|
37
|
-
openedConnections: null,
|
|
38
|
-
openedConnectionsLastUpdate: null
|
|
39
|
-
}), cache.get(key);
|
|
40
|
-
};
|
|
41
|
-
async function getDBClient(options = {}) {
|
|
42
|
-
const { pool, connectionString, retries = 8 } = options;
|
|
43
|
-
if (!pool && !connectionString)
|
|
44
|
-
throw new Error("Either pool or connectionString must be provided");
|
|
45
|
-
let client = null;
|
|
46
|
-
try {
|
|
47
|
-
return client = await tryToGetNewClientFromPool(pool, connectionString, retries), client;
|
|
48
|
-
} catch (error) {
|
|
49
|
-
throw console.error("Failed to get DB client:", error), error;
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
async function tryToGetNewClientFromPool(providedPool, connectionString, retries) {
|
|
53
|
-
const { default: retry } = await import("async-retry");
|
|
54
|
-
return await retry(
|
|
55
|
-
async () => {
|
|
56
|
-
if (providedPool)
|
|
57
|
-
return await providedPool.connect();
|
|
58
|
-
if (!connectionString)
|
|
59
|
-
throw new Error("No connection string provided");
|
|
60
|
-
return await getOrCreatePoolCache(connectionString, {
|
|
61
|
-
connectionString,
|
|
62
|
-
connectionTimeoutMillis: 5e3,
|
|
63
|
-
// idle_session_timeout set to 35s on server, client timeout at 30s
|
|
64
|
-
// fix via https://github.com/brianc/node-postgres/issues/2718#issuecomment-2094885323
|
|
65
|
-
idleTimeoutMillis: 3e4,
|
|
66
|
-
allowExitOnIdle: !0
|
|
67
|
-
}).pool.connect();
|
|
68
|
-
},
|
|
69
|
-
{
|
|
70
|
-
retries,
|
|
71
|
-
minTimeout: 300,
|
|
72
|
-
factor: 2,
|
|
73
|
-
maxTimeout: 8e3
|
|
74
|
-
}
|
|
75
|
-
);
|
|
76
|
-
}
|
|
77
|
-
async function queryDb(queryText, params, options = {}) {
|
|
78
|
-
let client = null;
|
|
79
|
-
try {
|
|
80
|
-
return client = await tryToGetNewClientFromPool(
|
|
81
|
-
options.pool,
|
|
82
|
-
options.connectionString,
|
|
83
|
-
options.retries || 8
|
|
84
|
-
), await client.query(queryText, params);
|
|
85
|
-
} catch (error) {
|
|
86
|
-
throw console.error("Database query failed:", {
|
|
87
|
-
query: queryText,
|
|
88
|
-
error: error instanceof Error ? error.message : String(error)
|
|
89
|
-
}), error;
|
|
90
|
-
} finally {
|
|
91
|
-
if (client && options.connectionString)
|
|
92
|
-
if (await checkForTooManyConnections(
|
|
93
|
-
client,
|
|
94
|
-
options.connectionString
|
|
95
|
-
)) {
|
|
96
|
-
const poolCache = cache.get(createPoolKey(options.connectionString));
|
|
97
|
-
client.release(), await poolCache?.pool.end(), poolCache && cache.delete(createPoolKey(options.connectionString));
|
|
98
|
-
} else
|
|
99
|
-
client.release();
|
|
100
|
-
else client && client.release();
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
async function checkForTooManyConnections(client, connectionString) {
|
|
104
|
-
const poolCache = cache.get(createPoolKey(connectionString));
|
|
105
|
-
if (!poolCache) return !1;
|
|
106
|
-
const currentTime = Date.now(), openedConnectionsMaxAge = 1e4, maxConnectionsTolerance = 0.9;
|
|
107
|
-
if (poolCache.maxConnections === null || poolCache.reservedConnections === null) {
|
|
108
|
-
const [maxConnections, reservedConnections] = await getConnectionLimits(client);
|
|
109
|
-
poolCache.maxConnections = maxConnections, poolCache.reservedConnections = reservedConnections;
|
|
110
|
-
}
|
|
111
|
-
if (poolCache.openedConnections === null || poolCache.openedConnectionsLastUpdate === null || currentTime - poolCache.openedConnectionsLastUpdate > openedConnectionsMaxAge) {
|
|
112
|
-
const openedConnections = await getOpenedConnections(client, connectionString);
|
|
113
|
-
poolCache.openedConnections = openedConnections, poolCache.openedConnectionsLastUpdate = currentTime;
|
|
114
|
-
}
|
|
115
|
-
return poolCache.openedConnections > (poolCache.maxConnections - poolCache.reservedConnections) * maxConnectionsTolerance ? (console.warn(
|
|
116
|
-
`Too many connections detected: ${poolCache.openedConnections}/${poolCache.maxConnections - poolCache.reservedConnections}`
|
|
117
|
-
), !0) : !1;
|
|
118
|
-
}
|
|
119
|
-
async function getConnectionLimits(client) {
|
|
120
|
-
const maxConnectionsResult = await client.query("SHOW max_connections"), reservedConnectionResult = await client.query(
|
|
121
|
-
"SHOW superuser_reserved_connections"
|
|
122
|
-
);
|
|
123
|
-
return [
|
|
124
|
-
Number.parseInt(maxConnectionsResult.rows[0].max_connections, 10),
|
|
125
|
-
Number.parseInt(reservedConnectionResult.rows[0].superuser_reserved_connections, 10)
|
|
126
|
-
];
|
|
127
|
-
}
|
|
128
|
-
async function getOpenedConnections(client, connectionString) {
|
|
129
|
-
const dbName = new URL(connectionString).pathname.slice(1), openConnectionsResult = await client.query(
|
|
130
|
-
"SELECT numbackends as opened_connections FROM pg_stat_database WHERE datname = $1",
|
|
131
|
-
[dbName]
|
|
132
|
-
);
|
|
133
|
-
return Number.parseInt(
|
|
134
|
-
openConnectionsResult.rows[0]?.opened_connections || 0,
|
|
135
|
-
10
|
|
136
|
-
);
|
|
137
|
-
}
|
|
138
|
-
async function getNewClient(options = {}) {
|
|
139
|
-
const { connectionString } = options;
|
|
140
|
-
if (!connectionString)
|
|
141
|
-
throw new Error("connectionString is required for getNewClient");
|
|
142
|
-
try {
|
|
143
|
-
return await tryToGetNewClient(connectionString);
|
|
144
|
-
} catch (error) {
|
|
145
|
-
throw console.error("Failed to get new client:", error), error;
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
async function tryToGetNewClient(connectionString) {
|
|
149
|
-
const configurations = {
|
|
150
|
-
connectionString,
|
|
151
|
-
connectionTimeoutMillis: 5e3,
|
|
152
|
-
idleTimeoutMillis: 3e4,
|
|
153
|
-
allowExitOnIdle: !0
|
|
154
|
-
}, { default: retry } = await import("async-retry");
|
|
155
|
-
return await retry(
|
|
156
|
-
async () => {
|
|
157
|
-
const newClient = new import_pg.default.Client(configurations);
|
|
158
|
-
return await newClient.connect(), newClient;
|
|
159
|
-
},
|
|
160
|
-
{
|
|
161
|
-
retries: 10,
|
|
162
|
-
minTimeout: 100,
|
|
163
|
-
factor: 2,
|
|
164
|
-
maxTimeout: 5e3
|
|
165
|
-
}
|
|
166
|
-
);
|
|
167
|
-
}
|
|
168
|
-
//# sourceMappingURL=getDBClient.js.map
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": ["../../src/getDBClient.ts"],
|
|
4
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,gBAA+C;AAY/C,MAAM,QAAQ,oBAAI,IAShB,GAEI,gBAAgB,CAAC,qB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QAAQ;AAAA,MACR,YAAY;AAAA,IACd;AAAA,EACF;AAGF;",
|
|
5
|
-
"names": ["pg"]
|
|
6
|
-
}
|
package/dist/cjs/index.js
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
-
var __export = (target, all) => {
|
|
6
|
-
for (var name in all)
|
|
7
|
-
__defProp(target, name, { get: all[name], enumerable: !0 });
|
|
8
|
-
}, __copyProps = (to, from, except, desc) => {
|
|
9
|
-
if (from && typeof from == "object" || typeof from == "function")
|
|
10
|
-
for (let key of __getOwnPropNames(from))
|
|
11
|
-
!__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
12
|
-
return to;
|
|
13
|
-
};
|
|
14
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: !0 }), mod);
|
|
15
|
-
var index_exports = {};
|
|
16
|
-
__export(index_exports, {
|
|
17
|
-
createDb: () => import_createDb.createDb,
|
|
18
|
-
createServerHelpers: () => import_createServerHelpers.createServerHelpers,
|
|
19
|
-
createSql: () => import_sql.createSql,
|
|
20
|
-
getDBClient: () => import_getDBClient.getDBClient,
|
|
21
|
-
processInChunks: () => import_chunkedQuery.processInChunks,
|
|
22
|
-
queryDb: () => import_getDBClient.queryDb,
|
|
23
|
-
updateInChunks: () => import_chunkedQuery.updateInChunks
|
|
24
|
-
});
|
|
25
|
-
module.exports = __toCommonJS(index_exports);
|
|
26
|
-
var import_sql = require("./sql"), import_createServerHelpers = require("./createServerHelpers"), import_getDBClient = require("./getDBClient"), import_chunkedQuery = require("./chunkedQuery"), import_createDb = require("./createDb");
|
|
27
|
-
//# sourceMappingURL=index.js.map
|
package/dist/cjs/index.js.map
DELETED
package/dist/cjs/migrate.js
DELETED
|
@@ -1,144 +0,0 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
-
var __export = (target, all) => {
|
|
6
|
-
for (var name in all)
|
|
7
|
-
__defProp(target, name, { get: all[name], enumerable: !0 });
|
|
8
|
-
}, __copyProps = (to, from, except, desc) => {
|
|
9
|
-
if (from && typeof from == "object" || typeof from == "function")
|
|
10
|
-
for (let key of __getOwnPropNames(from))
|
|
11
|
-
!__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
12
|
-
return to;
|
|
13
|
-
};
|
|
14
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: !0 }), mod);
|
|
15
|
-
var migrate_exports = {};
|
|
16
|
-
__export(migrate_exports, {
|
|
17
|
-
migrate: () => migrate
|
|
18
|
-
});
|
|
19
|
-
module.exports = __toCommonJS(migrate_exports);
|
|
20
|
-
var import_node_path = require("node:path"), import_getDBClient = require("./getDBClient");
|
|
21
|
-
const DEFAULT_MIGRATION_TIMEOUT = 5e3, isServerless = !!(process.env.AWS_LAMBDA_FUNCTION_NAME || process.env.AWS_LAMBDA_RUNTIME_API || process.env.LAMBDA_RUNTIME_DIR || process.env.IS_SERVERLESS);
|
|
22
|
-
async function withTimeout(promise, ms, name) {
|
|
23
|
-
let timeoutId;
|
|
24
|
-
const timeoutPromise = new Promise((_, reject) => {
|
|
25
|
-
timeoutId = setTimeout(() => {
|
|
26
|
-
reject(new Error(`migration "${name}" timed out after ${ms}ms`));
|
|
27
|
-
}, ms);
|
|
28
|
-
});
|
|
29
|
-
try {
|
|
30
|
-
return await Promise.race([promise, timeoutPromise]);
|
|
31
|
-
} finally {
|
|
32
|
-
clearTimeout(timeoutId);
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
async function migrate(options) {
|
|
36
|
-
const {
|
|
37
|
-
connectionString,
|
|
38
|
-
migrationsGlob,
|
|
39
|
-
createDatabases = [],
|
|
40
|
-
onMigrationComplete,
|
|
41
|
-
gitSha,
|
|
42
|
-
cvrDb,
|
|
43
|
-
changeDb,
|
|
44
|
-
defaultTimeout = DEFAULT_MIGRATION_TIMEOUT
|
|
45
|
-
} = options, client = await (0, import_getDBClient.getDBClient)({ connectionString }), hasDB = async (name) => !!(await client.query(`
|
|
46
|
-
SELECT 1 FROM pg_database WHERE datname = '${name}'
|
|
47
|
-
`)).rows.length;
|
|
48
|
-
if (cvrDb || changeDb) {
|
|
49
|
-
if (!cvrDb)
|
|
50
|
-
throw new Error("Missing cvrDb");
|
|
51
|
-
const zeroDBNames = [(0, import_node_path.basename)(cvrDb || ""), (0, import_node_path.basename)(changeDb || "")].filter(Boolean);
|
|
52
|
-
for (const name of zeroDBNames)
|
|
53
|
-
await hasDB(name) || await client.query(`CREATE DATABASE ${name};`);
|
|
54
|
-
}
|
|
55
|
-
for (const dbUrl of createDatabases) {
|
|
56
|
-
const name = (0, import_node_path.basename)(dbUrl);
|
|
57
|
-
await hasDB(name) || await client.query(`CREATE DATABASE ${name};`);
|
|
58
|
-
}
|
|
59
|
-
try {
|
|
60
|
-
await client.query("BEGIN"), await client.query("SET search_path = public"), await client.query(`
|
|
61
|
-
CREATE TABLE IF NOT EXISTS public.migrations (
|
|
62
|
-
id SERIAL PRIMARY KEY,
|
|
63
|
-
name VARCHAR(255) NOT NULL,
|
|
64
|
-
run_on TIMESTAMP NOT NULL DEFAULT NOW()
|
|
65
|
-
)
|
|
66
|
-
`);
|
|
67
|
-
const appliedMigrations = await client.query("SELECT name FROM public.migrations"), appliedMigrationNames = new Set(appliedMigrations.rows.map((row) => row.name)), tsMigrationsSorted = Object.entries(migrationsGlob).sort(([a], [b]) => a.localeCompare(b)).map(([file, run]) => ({
|
|
68
|
-
name: (0, import_node_path.basename)(file).replace(".ts", ""),
|
|
69
|
-
run
|
|
70
|
-
})).filter(({ name }) => /^[\d]+/.test(name)), migrations = [...await Promise.all(
|
|
71
|
-
tsMigrationsSorted.map(async ({ name, run }) => {
|
|
72
|
-
if (appliedMigrationNames.has(name))
|
|
73
|
-
return null;
|
|
74
|
-
try {
|
|
75
|
-
return { ...await run(), name };
|
|
76
|
-
} catch (error) {
|
|
77
|
-
throw console.error(`Failed to load TypeScript migration ${name}:`, error), error;
|
|
78
|
-
}
|
|
79
|
-
})
|
|
80
|
-
).then((migrations2) => migrations2.filter(Boolean))].sort((a, b) => a.name.localeCompare(b.name));
|
|
81
|
-
if (!migrations.length)
|
|
82
|
-
await client.query("COMMIT");
|
|
83
|
-
else {
|
|
84
|
-
for (const migration of migrations) {
|
|
85
|
-
if (migration.up) {
|
|
86
|
-
const sp = `sp_${migration.name.replace(/\W/g, "_")}`;
|
|
87
|
-
await client.query(`SAVEPOINT ${sp}`);
|
|
88
|
-
try {
|
|
89
|
-
const timeout = migration.timeout ?? defaultTimeout;
|
|
90
|
-
await withTimeout(migration.up(client), timeout, migration.name), await client.query(`RELEASE SAVEPOINT ${sp}`);
|
|
91
|
-
} catch (e) {
|
|
92
|
-
if (await client.query(`ROLLBACK TO SAVEPOINT ${sp}`), [
|
|
93
|
-
"42710",
|
|
94
|
-
// duplicate_object
|
|
95
|
-
"42P07",
|
|
96
|
-
// duplicate_table
|
|
97
|
-
"42701",
|
|
98
|
-
// duplicate_column
|
|
99
|
-
"42P16"
|
|
100
|
-
// invalid_table_definition (e.g. constraint already exists)
|
|
101
|
-
].includes(e?.code))
|
|
102
|
-
console.info(
|
|
103
|
-
`[migrate] ${migration.name}: ${e.message} (${e.code}), recording as applied`
|
|
104
|
-
);
|
|
105
|
-
else
|
|
106
|
-
throw e;
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
await client.query("INSERT INTO public.migrations (name) VALUES ($1)", [
|
|
110
|
-
migration.name
|
|
111
|
-
]);
|
|
112
|
-
}
|
|
113
|
-
await client.query("COMMIT");
|
|
114
|
-
}
|
|
115
|
-
} catch (e) {
|
|
116
|
-
console.error("Migration failed, rolling back:", e), await client.query("ROLLBACK");
|
|
117
|
-
try {
|
|
118
|
-
client.release(!1);
|
|
119
|
-
} catch (releaseErr) {
|
|
120
|
-
console.error("Error releasing connection after rollback:", releaseErr);
|
|
121
|
-
}
|
|
122
|
-
throw e;
|
|
123
|
-
}
|
|
124
|
-
onMigrationComplete && await onMigrationComplete();
|
|
125
|
-
try {
|
|
126
|
-
client.release(!1);
|
|
127
|
-
} catch (err) {
|
|
128
|
-
console.error("Error releasing connection gracefully, trying to destroy:", err);
|
|
129
|
-
try {
|
|
130
|
-
client.release(!0);
|
|
131
|
-
} catch (destroyErr) {
|
|
132
|
-
console.error("Error destroying connection:", destroyErr);
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
await exitProcess();
|
|
136
|
-
}
|
|
137
|
-
async function exitProcess() {
|
|
138
|
-
typeof process > "u" || isServerless || (await new Promise((resolve) => {
|
|
139
|
-
process.stderr.write("", () => {
|
|
140
|
-
process.stdout.write("", () => resolve());
|
|
141
|
-
});
|
|
142
|
-
}), process.exit(0));
|
|
143
|
-
}
|
|
144
|
-
//# sourceMappingURL=migrate.js.map
|
package/dist/cjs/migrate.js.map
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": ["../../src/migrate.ts"],
|
|
4
|
-
"mappings": ";;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAUA,uBAAyB,sBAEzB,qBAA4B;AAY5B,MAAM,4BAA4B,KAe5B,eAAe,CAAC,EACpB,QAAQ,IAAI,4BACZ,QAAQ,IAAI,0BACZ,QAAQ,IAAI,sBACZ,QAAQ,IAAI;AAGd,eAAe,YAAe,SAAqB,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
|
-
var __create = Object.create;
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __getProtoOf = Object.getPrototypeOf, __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: !0 });
|
|
9
|
-
}, __copyProps = (to, from, except, desc) => {
|
|
10
|
-
if (from && typeof from == "object" || typeof from == "function")
|
|
11
|
-
for (let key of __getOwnPropNames(from))
|
|
12
|
-
!__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
13
|
-
return to;
|
|
14
|
-
};
|
|
15
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
16
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
17
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
18
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
19
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
20
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: !0 }) : target,
|
|
21
|
-
mod
|
|
22
|
-
)), __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: !0 }), mod);
|
|
23
|
-
var build_migrations_exports = {};
|
|
24
|
-
__export(build_migrations_exports, {
|
|
25
|
-
buildMigrations: () => buildMigrations
|
|
26
|
-
});
|
|
27
|
-
module.exports = __toCommonJS(build_migrations_exports);
|
|
28
|
-
var import_node_child_process = require("node:child_process"), import_node_path = __toESM(require("node:path"), 1);
|
|
29
|
-
async function buildMigrations(options) {
|
|
30
|
-
const {
|
|
31
|
-
migrationsDir,
|
|
32
|
-
outFile = "migrate-dist.js",
|
|
33
|
-
target = "node22",
|
|
34
|
-
aliases = {}
|
|
35
|
-
} = options, { build } = await import("vite"), migrateFile = import_node_path.default.join(migrationsDir, "..", "migrate.ts"), result = await build({
|
|
36
|
-
configFile: !1,
|
|
37
|
-
resolve: {
|
|
38
|
-
alias: aliases
|
|
39
|
-
},
|
|
40
|
-
define: {
|
|
41
|
-
"process.env.GIT_SHA": JSON.stringify(
|
|
42
|
-
(0, import_node_child_process.execSync)("git rev-parse HEAD").toString().trim()
|
|
43
|
-
)
|
|
44
|
-
},
|
|
45
|
-
build: {
|
|
46
|
-
outDir: import_node_path.default.dirname(migrateFile),
|
|
47
|
-
target,
|
|
48
|
-
minify: !1,
|
|
49
|
-
emptyOutDir: !1,
|
|
50
|
-
copyPublicDir: !1,
|
|
51
|
-
lib: {
|
|
52
|
-
name: "migrate",
|
|
53
|
-
formats: ["es"],
|
|
54
|
-
entry: migrateFile
|
|
55
|
-
},
|
|
56
|
-
rollupOptions: {
|
|
57
|
-
external: (id) => id.startsWith("node:") || id === "pg" ? !0 : id.startsWith("/") ? !1 : !id.startsWith(".") && !id.startsWith("/"),
|
|
58
|
-
output: {
|
|
59
|
-
format: "es",
|
|
60
|
-
inlineDynamicImports: !0,
|
|
61
|
-
exports: "named",
|
|
62
|
-
entryFileNames: outFile
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
});
|
|
67
|
-
return console.info(`\u2713 Built migration bundle: ${outFile}`), result;
|
|
68
|
-
}
|
|
69
|
-
//# sourceMappingURL=build-migrations.js.map
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": ["../../../src/scripts/build-migrations.ts"],
|
|
4
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,gCAAyB,+BACzB,mBAAiB;AASjB,eAAsB,gBAAgB,SAAiC;AACrE,QAAM;AAAA,IACJ;AAAA,IACA,UAAU;AAAA,IACV,SAAS;AAAA,IACT,UAAU,CAAC;AAAA,EACb,IAAI,SAEE,EAAE,MAAM,IAAI,MAAM,OAAO,MAAM,GAE/B,cAAc,iBAAAA,QAAK,KAAK,eAAe,MAAM,YAAY,GAEzD,SAAS,MAAM,MAAM;AAAA,IACzB,YAAY;AAAA,IACZ,SAAS;AAAA,MACP,OAAO;AAAA,IACT;AAAA,IACA,QAAQ;AAAA,MACN,uBAAuB,KAAK;AAAA,YAC1B,oCAAS,oBAAoB,EAAE,SAAS,EAAE,KAAK;AAAA,MACjD;AAAA,IACF;AAAA,IACA,OAAO;AAAA,MACL,QAAQ,iBAAAA,QAAK,QAAQ,WAAW;AAAA,MAChC;AAAA,MACA,QAAQ;AAAA,MACR,aAAa;AAAA,MACb,eAAe;AAAA,MACf,KAAK;AAAA,QACH,MAAM;AAAA,QACN,SAAS,CAAC,IAAI;AAAA,QACd,OAAO;AAAA,MACT;AAAA,MACA,eAAe;AAAA,QACb,UAAU,CAAC,OAEL,GAAG,WAAW,OAAO,KAAK,OAAO,OAAa,KAE9C,GAAG,WAAW,GAAG,IAAU,KAE3B,CAAC,GAAG,WAAW,GAAG,KAAK,CAAC,GAAG,WAAW,GAAG;AAAA,QAG/C,QAAQ;AAAA,UACN,QAAQ;AAAA,UACR,sBAAsB;AAAA,UACtB,SAAS;AAAA,UACT,gBAAgB;AAAA,QAClB;AAAA,MACF;AAAA,IACF;AAAA,EACF,CAAC;AAED,iBAAQ,KAAK,kCAA6B,OAAO,EAAE,GAC5C;AACT;",
|
|
5
|
-
"names": ["path"]
|
|
6
|
-
}
|
|
@@ -1,118 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env bun
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: !0 });
|
|
9
|
-
}, __copyProps = (to, from, except, desc) => {
|
|
10
|
-
if (from && typeof from == "object" || typeof from == "function")
|
|
11
|
-
for (let key of __getOwnPropNames(from))
|
|
12
|
-
!__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
13
|
-
return to;
|
|
14
|
-
};
|
|
15
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: !0 }), mod);
|
|
16
|
-
var drizzle_migrations_sync_exports = {};
|
|
17
|
-
__export(drizzle_migrations_sync_exports, {
|
|
18
|
-
syncDrizzleMigrations: () => syncDrizzleMigrations
|
|
19
|
-
});
|
|
20
|
-
module.exports = __toCommonJS(drizzle_migrations_sync_exports);
|
|
21
|
-
var import_node_fs = require("node:fs"), import_promises = require("node:fs/promises"), import_node_path = require("node:path");
|
|
22
|
-
function getMigrationNumber(filename) {
|
|
23
|
-
const match = filename.match(/^(\d+)/);
|
|
24
|
-
return match && match[1] ? Number.parseInt(match[1], 10) : null;
|
|
25
|
-
}
|
|
26
|
-
async function getHighestMigrationNumber(dir) {
|
|
27
|
-
const files = await (0, import_promises.readdir)(dir);
|
|
28
|
-
let highest = -1;
|
|
29
|
-
for (const file of files) {
|
|
30
|
-
const num = getMigrationNumber(file);
|
|
31
|
-
num !== null && num > highest && (highest = num);
|
|
32
|
-
}
|
|
33
|
-
return highest;
|
|
34
|
-
}
|
|
35
|
-
async function renameNewDrizzleMigrations(migrationsPath, sqlFiles) {
|
|
36
|
-
const drizzlePattern = /^(\d{4})_[a-z]+_[a-z_]+\.sql$/, files = await (0, import_promises.readdir)(migrationsPath), highestNumber = await getHighestMigrationNumber(migrationsPath), newDrizzleSqlFiles = sqlFiles.filter((file) => {
|
|
37
|
-
if (!drizzlePattern.test(file)) return !1;
|
|
38
|
-
const tsFile = file.replace(".sql", ".ts");
|
|
39
|
-
return !files.includes(tsFile);
|
|
40
|
-
});
|
|
41
|
-
if (newDrizzleSqlFiles.length === 0) return sqlFiles;
|
|
42
|
-
const migrationGroups = /* @__PURE__ */ new Map();
|
|
43
|
-
for (const file of newDrizzleSqlFiles) {
|
|
44
|
-
const num = file.substring(0, 4);
|
|
45
|
-
migrationGroups.has(num) || migrationGroups.set(num, []), migrationGroups.get(num).push(file);
|
|
46
|
-
}
|
|
47
|
-
let nextNumber = highestNumber + 1;
|
|
48
|
-
const renamedFiles = [];
|
|
49
|
-
for (const [originalNum, groupFiles] of migrationGroups)
|
|
50
|
-
if (Number.parseInt(originalNum, 10) <= highestNumber) {
|
|
51
|
-
const newNumStr = nextNumber.toString().padStart(4, "0");
|
|
52
|
-
console.info(`Renumbering new drizzle migration ${originalNum} to ${newNumStr}`);
|
|
53
|
-
for (const file of groupFiles) {
|
|
54
|
-
const newName = file.replace(/^\d{4}/, newNumStr), oldPath = (0, import_node_path.join)(migrationsPath, file), newPath = (0, import_node_path.join)(migrationsPath, newName);
|
|
55
|
-
await (0, import_promises.rename)(oldPath, newPath), console.info(` Renamed ${file} -> ${newName}`), renamedFiles.push(newName);
|
|
56
|
-
}
|
|
57
|
-
const metaDir = (0, import_node_path.join)(migrationsPath, "meta");
|
|
58
|
-
if ((0, import_node_fs.existsSync)(metaDir)) {
|
|
59
|
-
const metaFiles = await (0, import_promises.readdir)(metaDir), snapshotFile = `${originalNum}_snapshot.json`;
|
|
60
|
-
if (metaFiles.includes(snapshotFile)) {
|
|
61
|
-
const newSnapshotName = `${newNumStr}_snapshot.json`;
|
|
62
|
-
await (0, import_promises.rename)((0, import_node_path.join)(metaDir, snapshotFile), (0, import_node_path.join)(metaDir, newSnapshotName)), console.info(` Renamed meta/${snapshotFile} -> meta/${newSnapshotName}`);
|
|
63
|
-
}
|
|
64
|
-
const journalPath = (0, import_node_path.join)(metaDir, "_journal.json");
|
|
65
|
-
if ((0, import_node_fs.existsSync)(journalPath)) {
|
|
66
|
-
const journal = JSON.parse(await (0, import_promises.readFile)(journalPath, "utf-8"));
|
|
67
|
-
let journalUpdated = !1;
|
|
68
|
-
for (const entry of journal.entries) {
|
|
69
|
-
const tagMatch = entry.tag.match(/^\d+_(.+)$/);
|
|
70
|
-
if (tagMatch)
|
|
71
|
-
for (const file of groupFiles) {
|
|
72
|
-
const fileSuffix = file.replace(".sql", "").replace(/^\d+_/, "");
|
|
73
|
-
if (tagMatch[1] === fileSuffix) {
|
|
74
|
-
const newTag = `${newNumStr}_${fileSuffix}`;
|
|
75
|
-
console.info(` Updated journal tag: ${entry.tag} -> ${newTag}`), entry.tag = newTag, journalUpdated = !0;
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
journalUpdated && await (0, import_promises.writeFile)(journalPath, JSON.stringify(journal, null, 4));
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
nextNumber++;
|
|
83
|
-
} else
|
|
84
|
-
renamedFiles.push(...groupFiles);
|
|
85
|
-
return sqlFiles.map((file) => {
|
|
86
|
-
if (newDrizzleSqlFiles.indexOf(file) !== -1) {
|
|
87
|
-
for (const renamed of renamedFiles)
|
|
88
|
-
if (renamed.includes(file.substring(5)))
|
|
89
|
-
return renamed;
|
|
90
|
-
}
|
|
91
|
-
return file;
|
|
92
|
-
});
|
|
93
|
-
}
|
|
94
|
-
async function syncDrizzleMigrations(options) {
|
|
95
|
-
const { migrationsDir } = options;
|
|
96
|
-
let sqlFiles = (await (0, import_promises.readdir)(migrationsDir)).filter((file) => (0, import_node_path.extname)(file) === ".sql");
|
|
97
|
-
console.info(`Found ${sqlFiles.length} SQL files to convert to migrations.`), sqlFiles = await renameNewDrizzleMigrations(migrationsDir, sqlFiles);
|
|
98
|
-
for (const sqlFile of sqlFiles) {
|
|
99
|
-
const tsFileName = `${(0, import_node_path.basename)(sqlFile, ".sql")}.ts`, tsFilePath = (0, import_node_path.join)(migrationsDir, tsFileName);
|
|
100
|
-
if ((0, import_node_fs.existsSync)(tsFilePath)) {
|
|
101
|
-
const sqlStat = await (0, import_promises.stat)((0, import_node_path.join)(migrationsDir, sqlFile));
|
|
102
|
-
if ((await (0, import_promises.stat)(tsFilePath)).mtimeMs > sqlStat.mtimeMs)
|
|
103
|
-
continue;
|
|
104
|
-
console.info(`Updating ${tsFileName} as SQL file has been modified.`);
|
|
105
|
-
} else
|
|
106
|
-
console.info(`Creating ${tsFileName}`);
|
|
107
|
-
const migrationContent = `import type { PoolClient } from 'pg'
|
|
108
|
-
import sql from './${sqlFile}?raw'
|
|
109
|
-
|
|
110
|
-
export async function up(client: PoolClient) {
|
|
111
|
-
await client.query(sql)
|
|
112
|
-
}
|
|
113
|
-
`;
|
|
114
|
-
await (0, import_promises.writeFile)(tsFilePath, migrationContent), console.info(`Successfully created ${tsFileName}`);
|
|
115
|
-
}
|
|
116
|
-
console.info("Migration sync completed.");
|
|
117
|
-
}
|
|
118
|
-
//# sourceMappingURL=drizzle-migrations-sync.js.map
|