@plyaz/db 0.2.0 → 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/cli/index.js CHANGED
@@ -347,7 +347,11 @@ var MigrationManager = class {
347
347
  const isEmptyOrComment = trimmedLine === "" || trimmedLine.startsWith("--");
348
348
  current += line + "\n";
349
349
  if (isEmptyOrComment) continue;
350
- const dollarState = this.processDollarDelimiters(line, inDollarBlock, dollarTag);
350
+ const dollarState = this.processDollarDelimiters(
351
+ line,
352
+ inDollarBlock,
353
+ dollarTag
354
+ );
351
355
  inDollarBlock = dollarState.inDollarBlock;
352
356
  dollarTag = dollarState.dollarTag;
353
357
  const isEndOfStatement = !inDollarBlock && trimmedLine.endsWith(";");
@@ -398,7 +402,9 @@ var MigrationManager = class {
398
402
  await adapter.query(statement);
399
403
  const isInterval = (i + 1) % PROGRESS_LOG_INTERVAL === 0;
400
404
  const isLast = i === total - 1;
401
- const isSignificant = Boolean(description.match(/^(CREATE TABLE|CREATE FUNCTION|CREATE TRIGGER)/i));
405
+ const isSignificant = Boolean(
406
+ description.match(/^(CREATE TABLE|CREATE FUNCTION|CREATE TRIGGER)/i)
407
+ );
402
408
  if (isInterval || isLast || isSignificant) {
403
409
  console.log(` ✓ [${i + 1}/${total}] ${description}`);
404
410
  }
@@ -821,7 +827,11 @@ var SeedManager = class {
821
827
  const isEmptyOrComment = trimmedLine === "" || trimmedLine.startsWith("--");
822
828
  current += line + "\n";
823
829
  if (isEmptyOrComment) continue;
824
- const dollarState = this.processDollarDelimiters(line, inDollarBlock, dollarTag);
830
+ const dollarState = this.processDollarDelimiters(
831
+ line,
832
+ inDollarBlock,
833
+ dollarTag
834
+ );
825
835
  inDollarBlock = dollarState.inDollarBlock;
826
836
  dollarTag = dollarState.dollarTag;
827
837
  if (!inDollarBlock && trimmedLine.endsWith(";") && current.trim()) {
@@ -8408,8 +8418,28 @@ async function loadConfig(configPath) {
8408
8418
  return getEnvFallbackConfig();
8409
8419
  }
8410
8420
  __name(loadConfig, "loadConfig");
8411
- async function initDatabase(configPath) {
8421
+ var MIGRATION_POOL_SETTINGS = {
8422
+ max: 3,
8423
+ // Fewer connections, more stable
8424
+ idleTimeoutMillis: 0,
8425
+ // Never timeout idle connections during migrations
8426
+ connectionTimeoutMillis: 3e4,
8427
+ // 30s to establish connection
8428
+ keepAlive: true,
8429
+ keepAliveInitialDelayMillis: 5e3,
8430
+ // Start keepalive after 5s
8431
+ allowExitOnIdle: false
8432
+ // Don't exit while migrations running
8433
+ };
8434
+ async function initDatabase(configPath, forMigration = false) {
8412
8435
  const config = await loadConfig(configPath);
8436
+ if (forMigration && config.config) {
8437
+ config.pool = {
8438
+ ...MIGRATION_POOL_SETTINGS,
8439
+ ...config.pool
8440
+ // User config can still override
8441
+ };
8442
+ }
8413
8443
  const db = await createDatabaseService(config);
8414
8444
  let adapter = db.adapter;
8415
8445
  while (adapter.baseAdapter) {
@@ -8437,7 +8467,7 @@ program.name("plyaz-db").description("Database management CLI for @plyaz/db").ve
8437
8467
  var migrateCommand = program.command("migrate").description("Database migration commands");
8438
8468
  migrateCommand.command("up").description("Run pending migrations").option("-t, --target <version>", "Target migration version").action(async (options) => {
8439
8469
  try {
8440
- const { adapter, config } = await initDatabase();
8470
+ const { adapter, config } = await initDatabase(void 0, true);
8441
8471
  const migrationManager = new MigrationManager({
8442
8472
  adapter,
8443
8473
  migrationsPath: config.migrationsPath ?? "./migrations",
@@ -8459,7 +8489,7 @@ migrateCommand.command("up").description("Run pending migrations").option("-t, -
8459
8489
  });
8460
8490
  migrateCommand.command("down").description("Rollback migrations").option("-s, --steps <number>", "Number of migrations to rollback", "1").action(async (options) => {
8461
8491
  try {
8462
- const { adapter, config } = await initDatabase();
8492
+ const { adapter, config } = await initDatabase(void 0, true);
8463
8493
  const migrationManager = new MigrationManager({
8464
8494
  adapter,
8465
8495
  migrationsPath: config.migrationsPath ?? "./migrations",
@@ -8522,7 +8552,7 @@ migrateCommand.command("reset").description("Rollback all migrations").option("-
8522
8552
  process.exit(1);
8523
8553
  }
8524
8554
  try {
8525
- const { adapter, config } = await initDatabase();
8555
+ const { adapter, config } = await initDatabase(void 0, true);
8526
8556
  const migrationManager = new MigrationManager({
8527
8557
  adapter,
8528
8558
  migrationsPath: config.migrationsPath ?? "./migrations",
@@ -8565,7 +8595,7 @@ migrateCommand.command("generate-down").description("Generate DOWN sections for
8565
8595
  var seedCommand = program.command("seed").description("Database seeding commands");
8566
8596
  seedCommand.command("run").description("Run seeds").option("-n, --name <name>", "Specific seed to run").option("--skip-existing", "Skip seeds that have already been run").action(async (options) => {
8567
8597
  try {
8568
- const { adapter, config } = await initDatabase();
8598
+ const { adapter, config } = await initDatabase(void 0, true);
8569
8599
  const seedManager = new SeedManager({
8570
8600
  adapter,
8571
8601
  seedsPath: config.seedsPath ?? "./seeds",
@@ -8625,7 +8655,7 @@ seedCommand.command("reset").description("Reset seeds (run cleanup functions)").
8625
8655
  process.exit(1);
8626
8656
  }
8627
8657
  try {
8628
- const { adapter, config } = await initDatabase();
8658
+ const { adapter, config } = await initDatabase(void 0, true);
8629
8659
  const seedManager = new SeedManager({
8630
8660
  adapter,
8631
8661
  seedsPath: config.seedsPath ?? "./seeds",