@systemfsoftware/timescale-drizzle 0.8.0 → 0.10.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/README.md +286 -228
- package/dist/cli.js +35 -3
- package/dist/cli.js.map +1 -1
- package/dist/index.cjs +90 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +31 -1
- package/dist/index.d.ts +31 -1
- package/dist/index.js +85 -5
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -128,7 +128,7 @@ function brand(value, kind) {
|
|
|
128
128
|
function extrasKind(value) {
|
|
129
129
|
if (typeof value !== "object" || value === null) return null;
|
|
130
130
|
const kind = value[KIND];
|
|
131
|
-
if (kind === "function" || kind === "trigger" || kind === "extension" || kind === "comment" || kind === "partitionedTable" || kind === "partition" || kind === "detach" || kind === "hypertable" || kind === "continuousAggregate" || kind === "viewDefinition" || kind === "grant") return kind;
|
|
131
|
+
if (kind === "function" || kind === "trigger" || kind === "extension" || kind === "comment" || kind === "partitionedTable" || kind === "partition" || kind === "detach" || kind === "hypertable" || kind === "continuousAggregate" || kind === "viewDefinition" || kind === "grant" || kind === "timescaleServer") return kind;
|
|
132
132
|
return null;
|
|
133
133
|
}
|
|
134
134
|
function requireComment(comment, where) {
|
|
@@ -1095,7 +1095,8 @@ function collectExtras(modules) {
|
|
|
1095
1095
|
hypertables: [],
|
|
1096
1096
|
continuousAggregates: [],
|
|
1097
1097
|
viewDefinitions: [],
|
|
1098
|
-
grants: []
|
|
1098
|
+
grants: [],
|
|
1099
|
+
servers: []
|
|
1099
1100
|
};
|
|
1100
1101
|
const seen = /* @__PURE__ */ new Set();
|
|
1101
1102
|
const list = Array.isArray(modules) ? modules : [modules];
|
|
@@ -1121,7 +1122,8 @@ function defineExtras(input) {
|
|
|
1121
1122
|
hypertables: input.hypertables ?? [],
|
|
1122
1123
|
continuousAggregates: input.continuousAggregates ?? [],
|
|
1123
1124
|
viewDefinitions: input.viewDefinitions ?? [],
|
|
1124
|
-
grants: input.grants ?? []
|
|
1125
|
+
grants: input.grants ?? [],
|
|
1126
|
+
servers: input.servers ?? []
|
|
1125
1127
|
});
|
|
1126
1128
|
}
|
|
1127
1129
|
function walk(value, extras, seen) {
|
|
@@ -1184,6 +1186,10 @@ function walk(value, extras, seen) {
|
|
|
1184
1186
|
for (const dependency of definition.dependsOn) walk(dependency, extras, seen);
|
|
1185
1187
|
return;
|
|
1186
1188
|
}
|
|
1189
|
+
if (kind === "timescaleServer") {
|
|
1190
|
+
extras.servers.push(value);
|
|
1191
|
+
return;
|
|
1192
|
+
}
|
|
1187
1193
|
if (kind === "grant") {
|
|
1188
1194
|
const grant = value;
|
|
1189
1195
|
extras.grants.push(grant);
|
|
@@ -4196,8 +4202,80 @@ function bumpStamp(stamp) {
|
|
|
4196
4202
|
const at = Date.UTC(Number(stamp.slice(0, 4)), Number(stamp.slice(4, 6)) - 1, Number(stamp.slice(6, 8)), Number(stamp.slice(8, 10)), Number(stamp.slice(10, 12)), Number(stamp.slice(12, 14)));
|
|
4197
4203
|
return utcStamp(new Date(at + 1e3));
|
|
4198
4204
|
}
|
|
4205
|
+
// src/server.ts
|
|
4206
|
+
const TIMESCALE_LAUNCHER = 1;
|
|
4207
|
+
const POSTGRES_PARALLEL_PER_GATHER_DEFAULT = 2;
|
|
4208
|
+
function count(where, name, value, minimum) {
|
|
4209
|
+
if (!Number.isInteger(value) || value < minimum) throw new PgExtrasError(`${where}: ${name} must be an integer of at least ${minimum}; got ${value}`);
|
|
4210
|
+
return value;
|
|
4211
|
+
}
|
|
4212
|
+
function timescaleServer(config) {
|
|
4213
|
+
const where = "timescaleServer";
|
|
4214
|
+
const databasesWithExtension = count(where, "databasesWithExtension", config.databasesWithExtension, 1);
|
|
4215
|
+
const concurrentJobs = count(where, "concurrentJobs", config.concurrentJobs, 0);
|
|
4216
|
+
const parallelWorkers = count(where, "parallelWorkers", config.parallelWorkers ?? 0, 0);
|
|
4217
|
+
const reservedPostgresWorkers = count(where, "reservedPostgresWorkers", config.reservedPostgresWorkers ?? 4, 0);
|
|
4218
|
+
const preloadLibraries = [...config.preloadLibraries ?? ["timescaledb"]];
|
|
4219
|
+
if (!preloadLibraries.includes("timescaledb")) throw new PgExtrasError(`${where}: preloadLibraries must include timescaledb, or its jobs never start`);
|
|
4220
|
+
for (const library of preloadLibraries) if (!/^[A-Za-z0-9_]+$/.test(library)) throw new PgExtrasError(`${where}: preload library "${library}" is not a plain library name`);
|
|
4221
|
+
const backgroundWorkers = databasesWithExtension + concurrentJobs;
|
|
4222
|
+
const perGather = Math.min(parallelWorkers, POSTGRES_PARALLEL_PER_GATHER_DEFAULT);
|
|
4223
|
+
const settings = {
|
|
4224
|
+
shared_preload_libraries: preloadLibraries.join(","),
|
|
4225
|
+
"timescaledb.max_background_workers": String(backgroundWorkers),
|
|
4226
|
+
max_parallel_workers: String(parallelWorkers),
|
|
4227
|
+
max_parallel_workers_per_gather: String(perGather),
|
|
4228
|
+
max_parallel_maintenance_workers: String(perGather),
|
|
4229
|
+
max_worker_processes: String(TIMESCALE_LAUNCHER + backgroundWorkers + parallelWorkers + reservedPostgresWorkers)
|
|
4230
|
+
};
|
|
4231
|
+
return brand({
|
|
4232
|
+
databasesWithExtension,
|
|
4233
|
+
concurrentJobs,
|
|
4234
|
+
parallelWorkers,
|
|
4235
|
+
reservedPostgresWorkers,
|
|
4236
|
+
preloadLibraries,
|
|
4237
|
+
settings,
|
|
4238
|
+
postgresArgs: Object.entries(settings).flatMap(([name, value]) => ["-c", `${name}=${value}`])
|
|
4239
|
+
}, "timescaleServer");
|
|
4240
|
+
}
|
|
4241
|
+
function hypertableJobs(hypertable) {
|
|
4242
|
+
return [
|
|
4243
|
+
hypertable.columnstorePolicy,
|
|
4244
|
+
hypertable.retention,
|
|
4245
|
+
hypertable.reorder
|
|
4246
|
+
].filter((job) => job !== null).length;
|
|
4247
|
+
}
|
|
4248
|
+
function aggregateJobs(aggregate) {
|
|
4249
|
+
return aggregate.refreshPolicies.length + [aggregate.columnstorePolicy, aggregate.retention].filter((job) => job !== null).length;
|
|
4250
|
+
}
|
|
4251
|
+
function declaredJobCount(extras) {
|
|
4252
|
+
return extras.hypertables.reduce((sum, hypertable) => sum + hypertableJobs(hypertable), 0) + extras.continuousAggregates.reduce((sum, aggregate) => sum + aggregateJobs(aggregate), 0);
|
|
4253
|
+
}
|
|
4254
|
+
function assertTimescaleServer(extras) {
|
|
4255
|
+
const problems = timescaleServerProblems(extras);
|
|
4256
|
+
if (problems.length > 0) throw new PgExtrasError(`timescaleServer: ${problems.join("; ")}`);
|
|
4257
|
+
}
|
|
4258
|
+
function timescaleServerProblems(extras) {
|
|
4259
|
+
if (extras.servers.length === 0) return [];
|
|
4260
|
+
if (extras.servers.length > 1) return [`the schema declares ${extras.servers.length} timescaleServer values; declare one`];
|
|
4261
|
+
const server = extras.servers[0];
|
|
4262
|
+
const jobs = declaredJobCount(extras);
|
|
4263
|
+
if (jobs > 0 && server.concurrentJobs === 0) return [`the schema declares ${jobs} TimescaleDB jobs and its timescaleServer allows 0 concurrent jobs`];
|
|
4264
|
+
return [];
|
|
4265
|
+
}
|
|
4266
|
+
const TIMESCALE_SERVER_SETTINGS_QUERY = "SELECT name, setting, pending_restart FROM pg_settings WHERE name = ANY($1::text[])";
|
|
4267
|
+
const comparable = (name, value) => name === "shared_preload_libraries" ? value.split(",").map((library) => library.trim()).filter((library) => library !== "").join(",") : value;
|
|
4268
|
+
function timescaleServerDrift(server, rows) {
|
|
4269
|
+
return Object.entries(server.settings).flatMap(([name, declared]) => {
|
|
4270
|
+
const live = rows.find((row) => row.name === name);
|
|
4271
|
+
if (!live) return [`${name}: the server does not report it; declared ${declared}`];
|
|
4272
|
+
if (live.pending_restart) return [`${name}: changed but waiting on a restart; declared ${declared}`];
|
|
4273
|
+
if (comparable(name, live.setting) !== comparable(name, declared)) return [`${name}: the server runs ${live.setting}; declared ${declared}`];
|
|
4274
|
+
return [];
|
|
4275
|
+
});
|
|
4276
|
+
}
|
|
4199
4277
|
// src/version.ts
|
|
4200
|
-
const VERSION = "0.
|
|
4278
|
+
const VERSION = "0.10.0";
|
|
4201
4279
|
// src/generate.ts
|
|
4202
4280
|
const UP_HEADER = `-- Generated by timescale-drizzle ${VERSION}. Review before applying.`;
|
|
4203
4281
|
const DOWN_HEADER = `-- Down SQL for timescale-drizzle ${VERSION}. drizzle-kit migrate does not run this file.`;
|
|
@@ -4211,6 +4289,7 @@ function loadStoredSnapshot(out) {
|
|
|
4211
4289
|
}
|
|
4212
4290
|
}
|
|
4213
4291
|
function planExtras(extras, previous, casing) {
|
|
4292
|
+
assertTimescaleServer(extras);
|
|
4214
4293
|
const next = snapshotExtras(extras, casing ? { casing } : void 0);
|
|
4215
4294
|
return {
|
|
4216
4295
|
next,
|
|
@@ -4245,10 +4324,12 @@ function pendingStatements(extras, out, casing) {
|
|
|
4245
4324
|
return planExtras(extras, loadStoredSnapshot(out), casing).up;
|
|
4246
4325
|
}
|
|
4247
4326
|
function renderIdempotent(extras, options) {
|
|
4327
|
+
assertTimescaleServer(extras);
|
|
4248
4328
|
return joinStatements(idempotentStatements(snapshotExtras(extras, options?.casing ? { casing: options.casing } : void 0)), options?.breakpoints ?? true, UP_HEADER);
|
|
4249
4329
|
}
|
|
4250
4330
|
exports.BREAKPOINT = BREAKPOINT;
|
|
4251
4331
|
exports.PgExtrasError = PgExtrasError;
|
|
4332
|
+
exports.TIMESCALE_SERVER_SETTINGS_QUERY = TIMESCALE_SERVER_SETTINGS_QUERY;
|
|
4252
4333
|
exports.VERSION = VERSION;
|
|
4253
4334
|
exports.addColumnstorePolicySql = addColumnstorePolicySql;
|
|
4254
4335
|
exports.addContinuousAggregateColumnstorePolicySql = addContinuousAggregateColumnstorePolicySql;
|
|
@@ -4260,6 +4341,7 @@ exports.addRetentionPolicySql = addRetentionPolicySql;
|
|
|
4260
4341
|
exports.alterContinuousAggregateChunkIntervalSql = alterContinuousAggregateChunkIntervalSql;
|
|
4261
4342
|
exports.alterContinuousAggregateRealTimeSql = alterContinuousAggregateRealTimeSql;
|
|
4262
4343
|
exports.alterTriggerEnabledSql = alterTriggerEnabledSql;
|
|
4344
|
+
exports.assertTimescaleServer = assertTimescaleServer;
|
|
4263
4345
|
exports.attachPartitionSql = attachPartitionSql;
|
|
4264
4346
|
exports.collectExtras = collectExtras;
|
|
4265
4347
|
exports.commentOn = commentOn;
|
|
@@ -4274,6 +4356,7 @@ exports.createHypertableSql = createHypertableSql;
|
|
|
4274
4356
|
exports.createHypertableStatements = createHypertableStatements;
|
|
4275
4357
|
exports.createPartitionSql = createPartitionSql;
|
|
4276
4358
|
exports.createTriggerSql = createTriggerSql;
|
|
4359
|
+
exports.declaredJobCount = declaredJobCount;
|
|
4277
4360
|
exports.defineExtras = defineExtras;
|
|
4278
4361
|
exports.detachPartitionSql = detachPartitionSql;
|
|
4279
4362
|
exports.diffSnapshots = diffSnapshots;
|
|
@@ -4317,6 +4400,9 @@ exports.renderIdempotent = renderIdempotent;
|
|
|
4317
4400
|
exports.setChunkTimeIntervalSql = setChunkTimeIntervalSql;
|
|
4318
4401
|
exports.snapshotExtras = snapshotExtras;
|
|
4319
4402
|
exports.splitMigrationSql = splitMigrationSql;
|
|
4403
|
+
exports.timescaleServer = timescaleServer;
|
|
4404
|
+
exports.timescaleServerDrift = timescaleServerDrift;
|
|
4405
|
+
exports.timescaleServerProblems = timescaleServerProblems;
|
|
4320
4406
|
exports.updatedAtTrigger = updatedAtTrigger;
|
|
4321
4407
|
exports.viewDefinition = viewDefinition;
|
|
4322
4408
|
exports.withComment = withComment;
|