@autofleet/sadot 1.6.1 → 1.6.2-alpha.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.
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
const e=require(`../utils/logger/index.cjs`),t=require(`./001-create-core-tables.cjs`),n=require(`./002-create-custom-field-entries.cjs`),r=require(`./003-create-custom-field-model-type-map.cjs`),{Umzug:i,SequelizeStorage:a}=require(`umzug`),o=e=>{let{useCustomFieldsEntries:i=!1,useModelTypeMapping:a=!1}=e;return[{name:`001-create-core-tables`,up:t.up,down:t.down},...i?[{name:`002-create-custom-field-entries`,up:n.up,down:n.down}]:[],...a?[{name:`003-create-custom-field-model-type-map`,up:r.up,down:r.down}]:[]]},s=839274628,c=async(t,n={})=>{let r=t.getQueryInterface(),c=new a({sequelize:t,tableName:`sadot_migrations`}),l=new i({migrations:o(n),context:r,storage:c,logger:{info:t=>e.default.info(`[sadot] ${
|
|
1
|
+
const e=require(`../utils/logger/index.cjs`),t=require(`./001-create-core-tables.cjs`),n=require(`./002-create-custom-field-entries.cjs`),r=require(`./003-create-custom-field-model-type-map.cjs`),{Umzug:i,SequelizeStorage:a}=require(`umzug`),o=e=>{let{useCustomFieldsEntries:i=!1,useModelTypeMapping:a=!1}=e;return[{name:`001-create-core-tables`,up:t.up,down:t.down},...i?[{name:`002-create-custom-field-entries`,up:n.up,down:n.down}]:[],...a?[{name:`003-create-custom-field-model-type-map`,up:r.up,down:r.down}]:[]]},s=839274628,c=async(t,n={})=>{let r=t.getQueryInterface(),c=new a({sequelize:t,tableName:`sadot_migrations`}),l=e=>{try{return JSON.stringify(e)}catch{return String(e)}},u=new i({migrations:o(n),context:r,storage:c,logger:{info:t=>e.default.info(`[sadot] ${l(t)}`),warn:t=>e.default.warn(`[sadot] ${l(t)}`),error:t=>e.default.error(`[sadot] ${l(t)}`),debug:t=>e.default.debug(`[sadot] ${l(t)}`)}}),d=await u.pending();if(d.length===0){e.default.info(`[sadot] no pending migrations, skipping`);return}if(e.default.info(`[sadot] pending migrations detected`,{pending:d.map(e=>e.name)}),(t.options?.pool?.max??5)<2){e.default.info(`[sadot] connection pool too small for advisory lock, running migrations directly`);try{await u.up()}catch(e){throw Error(`[sadot] migration failed: ${e instanceof Error?e.message:String(e)}`)}return}e.default.info(`[sadot] acquiring advisory lock`);let f=await t.transaction();try{if(await t.query(`SELECT pg_advisory_lock(${s})`,{transaction:f}),e.default.info(`[sadot] advisory lock acquired`),(await u.pending()).length>0)try{await u.up()}catch(e){throw Error(`[sadot] migration failed: ${e instanceof Error?e.message:String(e)}`)}else e.default.info(`[sadot] migrations already applied by another instance, skipping`)}finally{await t.query(`SELECT pg_advisory_unlock(${s})`,{transaction:f}),await f.commit(),e.default.info(`[sadot] advisory lock released`)}};exports.runSadotMigrations=c;
|
|
2
2
|
//# sourceMappingURL=index.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","names":["logger"],"sources":["../../src/migrations/index.ts"],"sourcesContent":["import type { InputMigrations } from 'umzug';\nimport type { QueryInterface } from 'sequelize';\nimport type { Sequelize } from 'sequelize-typescript';\nimport logger from '../utils/logger';\nimport * as migration001 from './001-create-core-tables';\nimport * as migration002 from './002-create-custom-field-entries';\nimport * as migration003 from './003-create-custom-field-model-type-map';\n\n// umzug is CJS. Static ESM named imports break in vitest after vi.resetModules()\n// because Vite's SSR transform re-evaluates the module outside the inline bundle.\n// require() goes through Node's native CJS loader and is stable across resets.\n\n// eslint-disable-next-line import/no-commonjs, @typescript-eslint/no-require-imports\nconst { Umzug, SequelizeStorage } = require('umzug') as typeof import('umzug');\n\ninterface MigratorOptions {\n useCustomFieldsEntries?: boolean;\n useModelTypeMapping?: boolean;\n}\n\nconst buildMigrationList = (options: MigratorOptions): InputMigrations<QueryInterface> => {\n const { useCustomFieldsEntries = false, useModelTypeMapping = false } = options;\n return [\n { name: '001-create-core-tables', up: migration001.up, down: migration001.down },\n ...(useCustomFieldsEntries ? [{ name: '002-create-custom-field-entries', up: migration002.up, down: migration002.down }] : []),\n ...(useModelTypeMapping ? [{ name: '003-create-custom-field-model-type-map', up: migration003.up, down: migration003.down }] : []),\n ];\n};\n\n// Stable advisory-lock ID for sadot migrations. Chosen arbitrarily; must not\n// collide with other advisory locks in the same database.\nconst SADOT_MIGRATION_LOCK_ID = 839274628;\n\n/**\n * Runs sadot migrations via umzug. All migrations use IF NOT EXISTS / IF EXISTS,\n * so they are fully idempotent — safe to run against both fresh and existing\n * databases (including DBs previously managed by the legacy sync() path).\n *\n * A PostgreSQL advisory lock is acquired before running migrations so that when\n * multiple pods start concurrently only one of them executes the migrations.\n * The remaining pods block until the lock is released, then proceed — Umzug will\n * see the migrations have already been applied and skip them.\n */\nconst runSadotMigrations = async (sequelize: Sequelize, options: MigratorOptions = {}): Promise<void> => {\n // Need to add a new migration? full migration guide here; https://www.notion.so/autofleet/How-to-Add-a-New-Migration-to-Sadot-36db603f40b98023b239d3c61aa22b00\n const queryInterface = sequelize.getQueryInterface() as unknown as QueryInterface;\n const storage = new SequelizeStorage({ sequelize: sequelize as never, tableName: 'sadot_migrations' });\n\n const umzugLogger = {\n info: (msg: Record<string, unknown>) => logger.info(`[sadot] ${
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["logger"],"sources":["../../src/migrations/index.ts"],"sourcesContent":["import type { InputMigrations } from 'umzug';\nimport type { QueryInterface } from 'sequelize';\nimport type { Sequelize } from 'sequelize-typescript';\nimport logger from '../utils/logger';\nimport * as migration001 from './001-create-core-tables';\nimport * as migration002 from './002-create-custom-field-entries';\nimport * as migration003 from './003-create-custom-field-model-type-map';\n\n// umzug is CJS. Static ESM named imports break in vitest after vi.resetModules()\n// because Vite's SSR transform re-evaluates the module outside the inline bundle.\n// require() goes through Node's native CJS loader and is stable across resets.\n\n// eslint-disable-next-line import/no-commonjs, @typescript-eslint/no-require-imports\nconst { Umzug, SequelizeStorage } = require('umzug') as typeof import('umzug');\n\ninterface MigratorOptions {\n useCustomFieldsEntries?: boolean;\n useModelTypeMapping?: boolean;\n}\n\nconst buildMigrationList = (options: MigratorOptions): InputMigrations<QueryInterface> => {\n const { useCustomFieldsEntries = false, useModelTypeMapping = false } = options;\n return [\n { name: '001-create-core-tables', up: migration001.up, down: migration001.down },\n ...(useCustomFieldsEntries ? [{ name: '002-create-custom-field-entries', up: migration002.up, down: migration002.down }] : []),\n ...(useModelTypeMapping ? [{ name: '003-create-custom-field-model-type-map', up: migration003.up, down: migration003.down }] : []),\n ];\n};\n\n// Stable advisory-lock ID for sadot migrations. Chosen arbitrarily; must not\n// collide with other advisory locks in the same database.\nconst SADOT_MIGRATION_LOCK_ID = 839274628;\n\n/**\n * Runs sadot migrations via umzug. All migrations use IF NOT EXISTS / IF EXISTS,\n * so they are fully idempotent — safe to run against both fresh and existing\n * databases (including DBs previously managed by the legacy sync() path).\n *\n * A PostgreSQL advisory lock is acquired before running migrations so that when\n * multiple pods start concurrently only one of them executes the migrations.\n * The remaining pods block until the lock is released, then proceed — Umzug will\n * see the migrations have already been applied and skip them.\n */\nconst runSadotMigrations = async (sequelize: Sequelize, options: MigratorOptions = {}): Promise<void> => {\n // Need to add a new migration? full migration guide here; https://www.notion.so/autofleet/How-to-Add-a-New-Migration-to-Sadot-36db603f40b98023b239d3c61aa22b00\n const queryInterface = sequelize.getQueryInterface() as unknown as QueryInterface;\n const storage = new SequelizeStorage({ sequelize: sequelize as never, tableName: 'sadot_migrations' });\n\n const safeStringify = (obj: unknown): string => {\n try {\n return JSON.stringify(obj);\n } catch {\n return String(obj);\n }\n };\n\n const umzugLogger = {\n info: (msg: Record<string, unknown>) => logger.info(`[sadot] ${safeStringify(msg)}`),\n warn: (msg: Record<string, unknown>) => logger.warn(`[sadot] ${safeStringify(msg)}`),\n error: (msg: Record<string, unknown>) => logger.error(`[sadot] ${safeStringify(msg)}`),\n debug: (msg: Record<string, unknown>) => logger.debug(`[sadot] ${safeStringify(msg)}`),\n };\n\n const migrator = new Umzug<QueryInterface>({\n migrations: buildMigrationList(options),\n context: queryInterface,\n storage,\n logger: umzugLogger,\n });\n\n const pending = await migrator.pending();\n if (pending.length === 0) {\n logger.info('[sadot] no pending migrations, skipping');\n return;\n }\n\n logger.info('[sadot] pending migrations detected', { pending: pending.map(m => m.name) });\n\n // The advisory lock holds a connection for its entire duration while\n // migrator.up() needs additional pool connections for queries. With\n // pool.max < 2 (common in test environments) this deadlocks.\n // Migrations are idempotent (IF NOT EXISTS / IF EXISTS) so running\n // without the lock is safe — just potentially duplicated work.\n const poolMax = (sequelize.options as any)?.pool?.max ?? 5;\n if (poolMax < 2) {\n logger.info('[sadot] connection pool too small for advisory lock, running migrations directly');\n try {\n await migrator.up();\n } catch (error) {\n throw new Error(`[sadot] migration failed: ${error instanceof Error ? error.message : String(error)}`);\n }\n return;\n }\n\n // Use a transaction to pin lock/unlock to the same pool connection.\n // pg_advisory_lock (session-level) is used so the lock remains held while\n // migrator.up() runs its own queries on other pool connections.\n logger.info('[sadot] acquiring advisory lock');\n const transaction = await sequelize.transaction();\n try {\n await sequelize.query(`SELECT pg_advisory_lock(${SADOT_MIGRATION_LOCK_ID})`, { transaction });\n logger.info('[sadot] advisory lock acquired');\n\n // Re-check after acquiring the lock — another pod may have already\n // applied the migrations while we were waiting.\n const stillPending = await migrator.pending();\n if (stillPending.length > 0) {\n try {\n await migrator.up();\n } catch (error) {\n throw new Error(`[sadot] migration failed: ${error instanceof Error ? error.message : String(error)}`);\n }\n } else {\n logger.info('[sadot] migrations already applied by another instance, skipping');\n }\n } finally {\n await sequelize.query(`SELECT pg_advisory_unlock(${SADOT_MIGRATION_LOCK_ID})`, { transaction });\n await transaction.commit();\n logger.info('[sadot] advisory lock released');\n }\n};\n\nexport { runSadotMigrations };\n"],"mappings":"oMAaM,CAAE,QAAO,oBAAqB,QAAQ,QAAQ,CAO9C,EAAsB,GAA8D,CACxF,GAAM,CAAE,yBAAyB,GAAO,sBAAsB,IAAU,EACxE,MAAO,CACL,CAAE,KAAM,yBAA0B,GAAA,EAAA,GAAqB,KAAA,EAAA,KAAyB,CAChF,GAAI,EAAyB,CAAC,CAAE,KAAM,kCAAmC,GAAA,EAAA,GAAqB,KAAA,EAAA,KAAyB,CAAC,CAAG,EAAE,CAC7H,GAAI,EAAsB,CAAC,CAAE,KAAM,yCAA0C,GAAA,EAAA,GAAqB,KAAA,EAAA,KAAyB,CAAC,CAAG,EAAE,CAClI,EAKG,EAA0B,UAY1B,EAAqB,MAAO,EAAsB,EAA2B,EAAE,GAAoB,CAEvG,IAAM,EAAiB,EAAU,mBAAmB,CAC9C,EAAU,IAAI,EAAiB,CAAa,YAAoB,UAAW,mBAAoB,CAAC,CAEhG,EAAiB,GAAyB,CAC9C,GAAI,CACF,OAAO,KAAK,UAAU,EAAI,MACpB,CACN,OAAO,OAAO,EAAI,GAWhB,EAAW,IAAI,EAAsB,CACzC,WAAY,EAAmB,EAAQ,CACvC,QAAS,EACT,UACA,OAXkB,CAClB,KAAO,GAAiCA,EAAAA,QAAO,KAAK,WAAW,EAAc,EAAI,GAAG,CACpF,KAAO,GAAiCA,EAAAA,QAAO,KAAK,WAAW,EAAc,EAAI,GAAG,CACpF,MAAQ,GAAiCA,EAAAA,QAAO,MAAM,WAAW,EAAc,EAAI,GAAG,CACtF,MAAQ,GAAiCA,EAAAA,QAAO,MAAM,WAAW,EAAc,EAAI,GAAG,CACvF,CAOA,CAAC,CAEI,EAAU,MAAM,EAAS,SAAS,CACxC,GAAI,EAAQ,SAAW,EAAG,CACxB,EAAA,QAAO,KAAK,0CAA0C,CACtD,OAWF,GARA,EAAA,QAAO,KAAK,sCAAuC,CAAE,QAAS,EAAQ,IAAI,GAAK,EAAE,KAAK,CAAE,CAAC,EAOxE,EAAU,SAAiB,MAAM,KAAO,GAC3C,EAAG,CACf,EAAA,QAAO,KAAK,mFAAmF,CAC/F,GAAI,CACF,MAAM,EAAS,IAAI,OACZ,EAAO,CACd,MAAU,MAAM,6BAA6B,aAAiB,MAAQ,EAAM,QAAU,OAAO,EAAM,GAAG,CAExG,OAMF,EAAA,QAAO,KAAK,kCAAkC,CAC9C,IAAM,EAAc,MAAM,EAAU,aAAa,CACjD,GAAI,CAOF,GANA,MAAM,EAAU,MAAM,2BAA2B,EAAwB,GAAI,CAAE,cAAa,CAAC,CAC7F,EAAA,QAAO,KAAK,iCAAiC,EAIxB,MAAM,EAAS,SAAS,EAC5B,OAAS,EACxB,GAAI,CACF,MAAM,EAAS,IAAI,OACZ,EAAO,CACd,MAAU,MAAM,6BAA6B,aAAiB,MAAQ,EAAM,QAAU,OAAO,EAAM,GAAG,MAGxG,EAAA,QAAO,KAAK,mEAAmE,QAEzE,CACR,MAAM,EAAU,MAAM,6BAA6B,EAAwB,GAAI,CAAE,cAAa,CAAC,CAC/F,MAAM,EAAY,QAAQ,CAC1B,EAAA,QAAO,KAAK,iCAAiC"}
|
package/dist/migrations/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{__require as e}from"../_virtual/rolldown_runtime.js";import t from"../utils/logger/index.js";import{down as n,up as r}from"./001-create-core-tables.js";import{down as i,up as a}from"./002-create-custom-field-entries.js";import{down as o,up as s}from"./003-create-custom-field-model-type-map.js";const{Umzug:c,SequelizeStorage:l}=e(`umzug`),u=e=>{let{useCustomFieldsEntries:t=!1,useModelTypeMapping:c=!1}=e;return[{name:`001-create-core-tables`,up:r,down:n},...t?[{name:`002-create-custom-field-entries`,up:a,down:i}]:[],...c?[{name:`003-create-custom-field-model-type-map`,up:s,down:o}]:[]]},d=839274628,f=async(e,n={})=>{let r=e.getQueryInterface(),i=new l({sequelize:e,tableName:`sadot_migrations`}),a=new c({migrations:u(n),context:r,storage:i,logger:{info:e=>t.info(`[sadot] ${
|
|
1
|
+
import{__require as e}from"../_virtual/rolldown_runtime.js";import t from"../utils/logger/index.js";import{down as n,up as r}from"./001-create-core-tables.js";import{down as i,up as a}from"./002-create-custom-field-entries.js";import{down as o,up as s}from"./003-create-custom-field-model-type-map.js";const{Umzug:c,SequelizeStorage:l}=e(`umzug`),u=e=>{let{useCustomFieldsEntries:t=!1,useModelTypeMapping:c=!1}=e;return[{name:`001-create-core-tables`,up:r,down:n},...t?[{name:`002-create-custom-field-entries`,up:a,down:i}]:[],...c?[{name:`003-create-custom-field-model-type-map`,up:s,down:o}]:[]]},d=839274628,f=async(e,n={})=>{let r=e.getQueryInterface(),i=new l({sequelize:e,tableName:`sadot_migrations`}),a=e=>{try{return JSON.stringify(e)}catch{return String(e)}},o=new c({migrations:u(n),context:r,storage:i,logger:{info:e=>t.info(`[sadot] ${a(e)}`),warn:e=>t.warn(`[sadot] ${a(e)}`),error:e=>t.error(`[sadot] ${a(e)}`),debug:e=>t.debug(`[sadot] ${a(e)}`)}}),s=await o.pending();if(s.length===0){t.info(`[sadot] no pending migrations, skipping`);return}if(t.info(`[sadot] pending migrations detected`,{pending:s.map(e=>e.name)}),(e.options?.pool?.max??5)<2){t.info(`[sadot] connection pool too small for advisory lock, running migrations directly`);try{await o.up()}catch(e){throw Error(`[sadot] migration failed: ${e instanceof Error?e.message:String(e)}`)}return}t.info(`[sadot] acquiring advisory lock`);let f=await e.transaction();try{if(await e.query(`SELECT pg_advisory_lock(${d})`,{transaction:f}),t.info(`[sadot] advisory lock acquired`),(await o.pending()).length>0)try{await o.up()}catch(e){throw Error(`[sadot] migration failed: ${e instanceof Error?e.message:String(e)}`)}else t.info(`[sadot] migrations already applied by another instance, skipping`)}finally{await e.query(`SELECT pg_advisory_unlock(${d})`,{transaction:f}),await f.commit(),t.info(`[sadot] advisory lock released`)}};export{f as runSadotMigrations};
|
|
2
2
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["migration001.up","migration001.down","migration002.up","migration002.down","migration003.up","migration003.down","logger"],"sources":["../../src/migrations/index.ts"],"sourcesContent":["import type { InputMigrations } from 'umzug';\nimport type { QueryInterface } from 'sequelize';\nimport type { Sequelize } from 'sequelize-typescript';\nimport logger from '../utils/logger';\nimport * as migration001 from './001-create-core-tables';\nimport * as migration002 from './002-create-custom-field-entries';\nimport * as migration003 from './003-create-custom-field-model-type-map';\n\n// umzug is CJS. Static ESM named imports break in vitest after vi.resetModules()\n// because Vite's SSR transform re-evaluates the module outside the inline bundle.\n// require() goes through Node's native CJS loader and is stable across resets.\n\n// eslint-disable-next-line import/no-commonjs, @typescript-eslint/no-require-imports\nconst { Umzug, SequelizeStorage } = require('umzug') as typeof import('umzug');\n\ninterface MigratorOptions {\n useCustomFieldsEntries?: boolean;\n useModelTypeMapping?: boolean;\n}\n\nconst buildMigrationList = (options: MigratorOptions): InputMigrations<QueryInterface> => {\n const { useCustomFieldsEntries = false, useModelTypeMapping = false } = options;\n return [\n { name: '001-create-core-tables', up: migration001.up, down: migration001.down },\n ...(useCustomFieldsEntries ? [{ name: '002-create-custom-field-entries', up: migration002.up, down: migration002.down }] : []),\n ...(useModelTypeMapping ? [{ name: '003-create-custom-field-model-type-map', up: migration003.up, down: migration003.down }] : []),\n ];\n};\n\n// Stable advisory-lock ID for sadot migrations. Chosen arbitrarily; must not\n// collide with other advisory locks in the same database.\nconst SADOT_MIGRATION_LOCK_ID = 839274628;\n\n/**\n * Runs sadot migrations via umzug. All migrations use IF NOT EXISTS / IF EXISTS,\n * so they are fully idempotent — safe to run against both fresh and existing\n * databases (including DBs previously managed by the legacy sync() path).\n *\n * A PostgreSQL advisory lock is acquired before running migrations so that when\n * multiple pods start concurrently only one of them executes the migrations.\n * The remaining pods block until the lock is released, then proceed — Umzug will\n * see the migrations have already been applied and skip them.\n */\nconst runSadotMigrations = async (sequelize: Sequelize, options: MigratorOptions = {}): Promise<void> => {\n // Need to add a new migration? full migration guide here; https://www.notion.so/autofleet/How-to-Add-a-New-Migration-to-Sadot-36db603f40b98023b239d3c61aa22b00\n const queryInterface = sequelize.getQueryInterface() as unknown as QueryInterface;\n const storage = new SequelizeStorage({ sequelize: sequelize as never, tableName: 'sadot_migrations' });\n\n const umzugLogger = {\n info: (msg: Record<string, unknown>) => logger.info(`[sadot] ${
|
|
1
|
+
{"version":3,"file":"index.js","names":["migration001.up","migration001.down","migration002.up","migration002.down","migration003.up","migration003.down","logger"],"sources":["../../src/migrations/index.ts"],"sourcesContent":["import type { InputMigrations } from 'umzug';\nimport type { QueryInterface } from 'sequelize';\nimport type { Sequelize } from 'sequelize-typescript';\nimport logger from '../utils/logger';\nimport * as migration001 from './001-create-core-tables';\nimport * as migration002 from './002-create-custom-field-entries';\nimport * as migration003 from './003-create-custom-field-model-type-map';\n\n// umzug is CJS. Static ESM named imports break in vitest after vi.resetModules()\n// because Vite's SSR transform re-evaluates the module outside the inline bundle.\n// require() goes through Node's native CJS loader and is stable across resets.\n\n// eslint-disable-next-line import/no-commonjs, @typescript-eslint/no-require-imports\nconst { Umzug, SequelizeStorage } = require('umzug') as typeof import('umzug');\n\ninterface MigratorOptions {\n useCustomFieldsEntries?: boolean;\n useModelTypeMapping?: boolean;\n}\n\nconst buildMigrationList = (options: MigratorOptions): InputMigrations<QueryInterface> => {\n const { useCustomFieldsEntries = false, useModelTypeMapping = false } = options;\n return [\n { name: '001-create-core-tables', up: migration001.up, down: migration001.down },\n ...(useCustomFieldsEntries ? [{ name: '002-create-custom-field-entries', up: migration002.up, down: migration002.down }] : []),\n ...(useModelTypeMapping ? [{ name: '003-create-custom-field-model-type-map', up: migration003.up, down: migration003.down }] : []),\n ];\n};\n\n// Stable advisory-lock ID for sadot migrations. Chosen arbitrarily; must not\n// collide with other advisory locks in the same database.\nconst SADOT_MIGRATION_LOCK_ID = 839274628;\n\n/**\n * Runs sadot migrations via umzug. All migrations use IF NOT EXISTS / IF EXISTS,\n * so they are fully idempotent — safe to run against both fresh and existing\n * databases (including DBs previously managed by the legacy sync() path).\n *\n * A PostgreSQL advisory lock is acquired before running migrations so that when\n * multiple pods start concurrently only one of them executes the migrations.\n * The remaining pods block until the lock is released, then proceed — Umzug will\n * see the migrations have already been applied and skip them.\n */\nconst runSadotMigrations = async (sequelize: Sequelize, options: MigratorOptions = {}): Promise<void> => {\n // Need to add a new migration? full migration guide here; https://www.notion.so/autofleet/How-to-Add-a-New-Migration-to-Sadot-36db603f40b98023b239d3c61aa22b00\n const queryInterface = sequelize.getQueryInterface() as unknown as QueryInterface;\n const storage = new SequelizeStorage({ sequelize: sequelize as never, tableName: 'sadot_migrations' });\n\n const safeStringify = (obj: unknown): string => {\n try {\n return JSON.stringify(obj);\n } catch {\n return String(obj);\n }\n };\n\n const umzugLogger = {\n info: (msg: Record<string, unknown>) => logger.info(`[sadot] ${safeStringify(msg)}`),\n warn: (msg: Record<string, unknown>) => logger.warn(`[sadot] ${safeStringify(msg)}`),\n error: (msg: Record<string, unknown>) => logger.error(`[sadot] ${safeStringify(msg)}`),\n debug: (msg: Record<string, unknown>) => logger.debug(`[sadot] ${safeStringify(msg)}`),\n };\n\n const migrator = new Umzug<QueryInterface>({\n migrations: buildMigrationList(options),\n context: queryInterface,\n storage,\n logger: umzugLogger,\n });\n\n const pending = await migrator.pending();\n if (pending.length === 0) {\n logger.info('[sadot] no pending migrations, skipping');\n return;\n }\n\n logger.info('[sadot] pending migrations detected', { pending: pending.map(m => m.name) });\n\n // The advisory lock holds a connection for its entire duration while\n // migrator.up() needs additional pool connections for queries. With\n // pool.max < 2 (common in test environments) this deadlocks.\n // Migrations are idempotent (IF NOT EXISTS / IF EXISTS) so running\n // without the lock is safe — just potentially duplicated work.\n const poolMax = (sequelize.options as any)?.pool?.max ?? 5;\n if (poolMax < 2) {\n logger.info('[sadot] connection pool too small for advisory lock, running migrations directly');\n try {\n await migrator.up();\n } catch (error) {\n throw new Error(`[sadot] migration failed: ${error instanceof Error ? error.message : String(error)}`);\n }\n return;\n }\n\n // Use a transaction to pin lock/unlock to the same pool connection.\n // pg_advisory_lock (session-level) is used so the lock remains held while\n // migrator.up() runs its own queries on other pool connections.\n logger.info('[sadot] acquiring advisory lock');\n const transaction = await sequelize.transaction();\n try {\n await sequelize.query(`SELECT pg_advisory_lock(${SADOT_MIGRATION_LOCK_ID})`, { transaction });\n logger.info('[sadot] advisory lock acquired');\n\n // Re-check after acquiring the lock — another pod may have already\n // applied the migrations while we were waiting.\n const stillPending = await migrator.pending();\n if (stillPending.length > 0) {\n try {\n await migrator.up();\n } catch (error) {\n throw new Error(`[sadot] migration failed: ${error instanceof Error ? error.message : String(error)}`);\n }\n } else {\n logger.info('[sadot] migrations already applied by another instance, skipping');\n }\n } finally {\n await sequelize.query(`SELECT pg_advisory_unlock(${SADOT_MIGRATION_LOCK_ID})`, { transaction });\n await transaction.commit();\n logger.info('[sadot] advisory lock released');\n }\n};\n\nexport { runSadotMigrations };\n"],"mappings":"8SAaA,KAAM,CAAE,QAAO,oBAAA,EAA6B,QAAQ,CAO9C,EAAsB,GAA8D,CACxF,GAAM,CAAE,yBAAyB,GAAO,sBAAsB,IAAU,EACxE,MAAO,CACL,CAAE,KAAM,yBAA8BA,KAAuBC,OAAmB,CAChF,GAAI,EAAyB,CAAC,CAAE,KAAM,kCAAmC,GAAIC,EAAiB,KAAMC,EAAmB,CAAC,CAAG,EAAE,CAC7H,GAAI,EAAsB,CAAC,CAAE,KAAM,yCAA0C,GAAIC,EAAiB,KAAMC,EAAmB,CAAC,CAAG,EAAE,CAClI,EAKG,EAA0B,UAY1B,EAAqB,MAAO,EAAsB,EAA2B,EAAE,GAAoB,CAEvG,IAAM,EAAiB,EAAU,mBAAmB,CAC9C,EAAU,IAAI,EAAiB,CAAa,YAAoB,UAAW,mBAAoB,CAAC,CAEhG,EAAiB,GAAyB,CAC9C,GAAI,CACF,OAAO,KAAK,UAAU,EAAI,MACpB,CACN,OAAO,OAAO,EAAI,GAWhB,EAAW,IAAI,EAAsB,CACzC,WAAY,EAAmB,EAAQ,CACvC,QAAS,EACT,UACA,OAXkB,CAClB,KAAO,GAAiCC,EAAO,KAAK,WAAW,EAAc,EAAI,GAAG,CACpF,KAAO,GAAiCA,EAAO,KAAK,WAAW,EAAc,EAAI,GAAG,CACpF,MAAQ,GAAiCA,EAAO,MAAM,WAAW,EAAc,EAAI,GAAG,CACtF,MAAQ,GAAiCA,EAAO,MAAM,WAAW,EAAc,EAAI,GAAG,CACvF,CAOA,CAAC,CAEI,EAAU,MAAM,EAAS,SAAS,CACxC,GAAI,EAAQ,SAAW,EAAG,CACxB,EAAO,KAAK,0CAA0C,CACtD,OAWF,GARA,EAAO,KAAK,sCAAuC,CAAE,QAAS,EAAQ,IAAI,GAAK,EAAE,KAAK,CAAE,CAAC,EAOxE,EAAU,SAAiB,MAAM,KAAO,GAC3C,EAAG,CACf,EAAO,KAAK,mFAAmF,CAC/F,GAAI,CACF,MAAM,EAAS,IAAI,OACZ,EAAO,CACd,MAAU,MAAM,6BAA6B,aAAiB,MAAQ,EAAM,QAAU,OAAO,EAAM,GAAG,CAExG,OAMF,EAAO,KAAK,kCAAkC,CAC9C,IAAM,EAAc,MAAM,EAAU,aAAa,CACjD,GAAI,CAOF,GANA,MAAM,EAAU,MAAM,2BAA2B,EAAwB,GAAI,CAAE,cAAa,CAAC,CAC7F,EAAO,KAAK,iCAAiC,EAIxB,MAAM,EAAS,SAAS,EAC5B,OAAS,EACxB,GAAI,CACF,MAAM,EAAS,IAAI,OACZ,EAAO,CACd,MAAU,MAAM,6BAA6B,aAAiB,MAAQ,EAAM,QAAU,OAAO,EAAM,GAAG,MAGxG,EAAO,KAAK,mEAAmE,QAEzE,CACR,MAAM,EAAU,MAAM,6BAA6B,EAAwB,GAAI,CAAE,cAAa,CAAC,CAC/F,MAAM,EAAY,QAAQ,CAC1B,EAAO,KAAK,iCAAiC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@autofleet/sadot",
|
|
3
|
-
"version": "1.6.
|
|
3
|
+
"version": "1.6.2-alpha.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -53,10 +53,10 @@
|
|
|
53
53
|
"express": "^4.21.2",
|
|
54
54
|
"npm-watch": "^0.11.0",
|
|
55
55
|
"supertest": "^7.0.0",
|
|
56
|
-
"@autofleet/logger": "^4.5.1",
|
|
57
|
-
"@autofleet/zehut": "^4.12.8",
|
|
58
56
|
"@autofleet/node-common": "^4.3.13",
|
|
59
|
-
"@autofleet/
|
|
57
|
+
"@autofleet/logger": "^4.5.1",
|
|
58
|
+
"@autofleet/errors": "^3.1.53",
|
|
59
|
+
"@autofleet/zehut": "^4.12.8"
|
|
60
60
|
},
|
|
61
61
|
"peerDependencies": {
|
|
62
62
|
"@autofleet/errors": "^3",
|