Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

feat: updated migration to support .mjs instead of .ts

=732484d3321a
diff --git a/backend/package.json b/backend/package.json
index 43409e619d..88c027d24a 100644
--- a/backend/package.json
+++ b/backend/package.json
@@ -46,20 +46,20 @@
"generate:component": "tsx ./scripts/create-backend-file.ts",
"generate:schema": "tsx ./scripts/generate-schema-types.ts && eslint --fix --ext ts ./src/db/schemas",
"auditlog-migration:latest": "knex --knexfile ./src/db/auditlog-knexfile.ts --client pg migrate:latest",
- "auditlog-migration:up": "knex --knexfile ./src/db/auditlog-knexfile.ts --client pg migrate:up",
- "auditlog-migration:down": "knex --knexfile ./src/db/auditlog-knexfile.ts --client pg migrate:down",
- "auditlog-migration:list": "knex --knexfile ./src/db/auditlog-knexfile.ts --client pg migrate:list",
- "auditlog-migration:status": "knex --knexfile ./src/db/auditlog-knexfile.ts --client pg migrate:status",
- "auditlog-migration:unlock": "knex --knexfile ./src/db/auditlog-knexfile.ts migrate:unlock",
- "auditlog-migration:rollback": "knex --knexfile ./src/db/auditlog-knexfile.ts migrate:rollback",
+ "auditlog-migration:up": "knex --knexfile ./dist/db/auditlog-knexfile.ts --client pg migrate:up",
+ "auditlog-migration:down": "knex --knexfile ./dist/db/auditlog-knexfile.ts --client pg migrate:down",
+ "auditlog-migration:list": "knex --knexfile ./dist/db/auditlog-knexfile.ts --client pg migrate:list",
+ "auditlog-migration:status": "knex --knexfile ./dist/db/auditlog-knexfile.ts --client pg migrate:status",
+ "auditlog-migration:unlock": "knex --knexfile ./dist/db/auditlog-knexfile.ts migrate:unlock",
+ "auditlog-migration:rollback": "knex --knexfile ./dist/db/auditlog-knexfile.ts migrate:rollback",
"migration:new": "tsx ./scripts/create-migration.ts",
- "migration:up": "npm run auditlog-migration:up && knex --knexfile ./src/db/knexfile.ts --client pg migrate:up",
- "migration:down": "npm run auditlog-migration:down && knex --knexfile ./src/db/knexfile.ts --client pg migrate:down",
- "migration:list": "npm run auditlog-migration:list && knex --knexfile ./src/db/knexfile.ts --client pg migrate:list",
- "migration:latest": "npm run auditlog-migration:latest && knex --knexfile ./src/db/knexfile.ts --client pg migrate:latest",
- "migration:status": "npm run auditlog-migration:status && knex --knexfile ./src/db/knexfile.ts --client pg migrate:status",
- "migration:rollback": "npm run auditlog-migration:rollback && knex --knexfile ./src/db/knexfile.ts migrate:rollback",
- "migration:unlock": "npm run auditlog-migration:unlock && knex --knexfile ./src/db/knexfile.ts migrate:unlock",
+ "migration:up": "npm run auditlog-migration:up && knex --knexfile ./dist/db/knexfile.mjs --client pg migrate:up",
+ "migration:down": "npm run auditlog-migration:down && knex --knexfile ./dist/db/knexfile.ts --client pg migrate:down",
+ "migration:list": "npm run auditlog-migration:list && knex --knexfile ./dist/db/knexfile.ts --client pg migrate:list",
+ "migration:latest": "npm run auditlog-migration:latest && knex --knexfile ./dist/db/knexfile.mjs --client pg migrate:latest",
+ "migration:status": "npm run auditlog-migration:status && knex --knexfile ./dist/db/knexfile.ts --client pg migrate:status",
+ "migration:rollback": "npm run auditlog-migration:rollback && knex --knexfile ./dist/db/knexfile.ts migrate:rollback",
+ "migration:unlock": "npm run auditlog-migration:unlock && knex --knexfile ./dist/db/knexfile.ts migrate:unlock",
"migrate:org": "tsx ./scripts/migrate-organization.ts",
"seed:new": "tsx ./scripts/create-seed-file.ts",
"seed": "knex --knexfile ./src/db/knexfile.ts --client pg seed:run",
diff --git a/backend/src/auto-start-migrations.ts b/backend/src/auto-start-migrations.ts
index 5177eaf4aa..81401855df 100644
--- a/backend/src/auto-start-migrations.ts
+++ b/backend/src/auto-start-migrations.ts
@@ -14,9 +14,10 @@ type TArgs = {
logger: Logger;
};
+const isProduction = process.env.NODE_ENV === "production";
const migrationConfig = {
directory: path.join(__dirname, "./db/migrations"),
- extension: "ts",
+ loadExtensions: [".mjs", ".ts"],
tableName: "infisical_migrations"
};
@@ -31,6 +32,31 @@ const migrationStatusCheckErrorHandler = (err: Error) => {
export const runMigrations = async ({ applicationDb, auditLogDb, logger }: TArgs) => {
try {
+ // akhilmhdh(Feb 10 2025): 6 months from now remove this
+ if (isProduction) {
+ const migrationTable = migrationConfig.tableName;
+ const hasMigrationTable = await applicationDb.schema.hasTable(migrationTable);
+ if (hasMigrationTable) {
+ const firstFile = (await applicationDb(migrationTable).where({}).first()) as { name: string };
+ if (firstFile?.name?.includes(".ts")) {
+ await applicationDb(migrationTable).update({
+ name: applicationDb.raw("REPLACE(name, '.ts', '.mjs')")
+ });
+ }
+ }
+ if (auditLogDb) {
+ const hasMigrationTableInAuditLog = await auditLogDb.schema.hasTable(migrationTable);
+ if (hasMigrationTableInAuditLog) {
+ const firstFile = (await auditLogDb(migrationTable).where({}).first()) as { name: string };
+ if (firstFile?.name?.includes(".ts")) {
+ await auditLogDb(migrationTable).update({
+ name: auditLogDb.raw("REPLACE(name, '.ts', '.mjs')")
+ });
+ }
+ }
+ }
+ }
+
const shouldRunMigration = Boolean(
await applicationDb.migrate.status(migrationConfig).catch(migrationStatusCheckErrorHandler)
); // db.length - code.length
diff --git a/backend/src/db/knexfile.ts b/backend/src/db/knexfile.ts
index 8af2b59ab2..8cf80b7445 100644
--- a/backend/src/db/knexfile.ts
+++ b/backend/src/db/knexfile.ts
@@ -38,7 +38,8 @@ export default {
directory: "./seeds"
},
migrations: {
- tableName: "infisical_migrations"
+ tableName: "infisical_migrations",
+ loadExtensions: [".mjs"]
}
},
production: {
@@ -62,7 +63,8 @@ export default {
max: 10
},
migrations: {
- tableName: "infisical_migrations"
+ tableName: "infisical_migrations",
+ loadExtensions: [".mjs"]
}
}
} as Knex.Config;