@open-mercato/cli 0.11.1-develop.8920.1.d3c2cc4681 → 0.11.1-develop.8929.1.493f6c653c
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/.turbo/turbo-build.log +6 -2
- package/dist/agentic/guides/module-facts.json +356 -385
- package/dist/agentic/guides/module-facts.v2.json +356 -385
- package/dist/agentic/guides/modules/agent_orchestrator/active-extension-bindings.md +1 -1
- package/dist/agentic/guides/modules/agent_orchestrator/exact-override-targets.md +1 -1
- package/dist/agentic/guides/modules/agent_orchestrator/index.md +1 -1
- package/dist/agentic/guides/modules/agent_orchestrator/workers.md +1 -1
- package/dist/agentic/guides/modules/ai_assistant/exact-override-targets.md +2 -2
- package/dist/agentic/guides/modules/ai_assistant/index.md +1 -1
- package/dist/agentic/guides/modules/ai_assistant/workers.md +2 -2
- package/dist/agentic/guides/modules/api_docs/index.md +1 -1
- package/dist/agentic/guides/modules/api_keys/index.md +1 -1
- package/dist/agentic/guides/modules/assisted_selling/index.md +1 -1
- package/dist/agentic/guides/modules/attachments/index.md +1 -1
- package/dist/agentic/guides/modules/audit_logs/index.md +1 -1
- package/dist/agentic/guides/modules/auth/entities.md +1 -0
- package/dist/agentic/guides/modules/auth/index.md +2 -2
- package/dist/agentic/guides/modules/auth/umes-hosts.md +1 -0
- package/dist/agentic/guides/modules/availability/index.md +1 -1
- package/dist/agentic/guides/modules/backups/index.md +1 -1
- package/dist/agentic/guides/modules/business_rules/index.md +1 -1
- package/dist/agentic/guides/modules/carrier_inpost/index.md +1 -1
- package/dist/agentic/guides/modules/cart/index.md +1 -1
- package/dist/agentic/guides/modules/catalog/index.md +1 -1
- package/dist/agentic/guides/modules/channel_apns/index.md +1 -1
- package/dist/agentic/guides/modules/channel_discord/index.md +1 -1
- package/dist/agentic/guides/modules/channel_expo/index.md +1 -1
- package/dist/agentic/guides/modules/channel_fcm/index.md +1 -1
- package/dist/agentic/guides/modules/channel_gmail/index.md +1 -1
- package/dist/agentic/guides/modules/channel_imap/index.md +1 -1
- package/dist/agentic/guides/modules/channel_ms365/index.md +1 -1
- package/dist/agentic/guides/modules/channel_resend/index.md +1 -1
- package/dist/agentic/guides/modules/channel_ses/index.md +1 -1
- package/dist/agentic/guides/modules/channel_smtp/index.md +1 -1
- package/dist/agentic/guides/modules/checkout/index.md +1 -1
- package/dist/agentic/guides/modules/communication_channels/index.md +1 -1
- package/dist/agentic/guides/modules/configs/index.md +1 -1
- package/dist/agentic/guides/modules/content/index.md +1 -1
- package/dist/agentic/guides/modules/currencies/index.md +1 -1
- package/dist/agentic/guides/modules/customer_accounts/exact-override-targets.md +4 -4
- package/dist/agentic/guides/modules/customer_accounts/index.md +1 -1
- package/dist/agentic/guides/modules/customer_accounts/workers.md +4 -4
- package/dist/agentic/guides/modules/customer_groups/index.md +1 -1
- package/dist/agentic/guides/modules/customers/index.md +1 -1
- package/dist/agentic/guides/modules/dashboards/index.md +1 -1
- package/dist/agentic/guides/modules/data_sync/index.md +1 -1
- package/dist/agentic/guides/modules/design_system/index.md +1 -1
- package/dist/agentic/guides/modules/devices/index.md +1 -1
- package/dist/agentic/guides/modules/dictionaries/index.md +1 -1
- package/dist/agentic/guides/modules/directory/index.md +1 -1
- package/dist/agentic/guides/modules/documents/index.md +1 -1
- package/dist/agentic/guides/modules/ecommerce/index.md +1 -1
- package/dist/agentic/guides/modules/entities/index.md +1 -1
- package/dist/agentic/guides/modules/eudr/index.md +1 -1
- package/dist/agentic/guides/modules/events/index.md +1 -1
- package/dist/agentic/guides/modules/feature_toggles/index.md +1 -1
- package/dist/agentic/guides/modules/forms/index.md +1 -1
- package/dist/agentic/guides/modules/gateway_autopay/index.md +1 -1
- package/dist/agentic/guides/modules/gateway_stripe/index.md +1 -1
- package/dist/agentic/guides/modules/gateway_tpay/index.md +1 -1
- package/dist/agentic/guides/modules/generators/index.md +1 -1
- package/dist/agentic/guides/modules/gus_regon/index.md +1 -1
- package/dist/agentic/guides/modules/inbox_ops/index.md +1 -1
- package/dist/agentic/guides/modules/integrations/index.md +1 -1
- package/dist/agentic/guides/modules/ledger/index.md +1 -1
- package/dist/agentic/guides/modules/maintenance/index.md +1 -1
- package/dist/agentic/guides/modules/messages/index.md +1 -1
- package/dist/agentic/guides/modules/notifications/index.md +1 -1
- package/dist/agentic/guides/modules/onboarding/index.md +1 -1
- package/dist/agentic/guides/modules/payment_gateways/exact-override-targets.md +1 -1
- package/dist/agentic/guides/modules/payment_gateways/index.md +1 -1
- package/dist/agentic/guides/modules/payment_gateways/workers.md +1 -1
- package/dist/agentic/guides/modules/perspectives/index.md +1 -1
- package/dist/agentic/guides/modules/phone_calls/index.md +1 -1
- package/dist/agentic/guides/modules/planner/index.md +1 -1
- package/dist/agentic/guides/modules/portal/index.md +1 -1
- package/dist/agentic/guides/modules/progress/index.md +1 -1
- package/dist/agentic/guides/modules/promotions/index.md +1 -1
- package/dist/agentic/guides/modules/push_notifications/index.md +1 -1
- package/dist/agentic/guides/modules/query_index/index.md +1 -1
- package/dist/agentic/guides/modules/record_locks/index.md +1 -1
- package/dist/agentic/guides/modules/resources/index.md +1 -1
- package/dist/agentic/guides/modules/sales/exact-override-targets.md +4 -4
- package/dist/agentic/guides/modules/sales/index.md +1 -1
- package/dist/agentic/guides/modules/sales/setup.md +1 -1
- package/dist/agentic/guides/modules/sales_invoices_gl_posting/index.md +1 -1
- package/dist/agentic/guides/modules/scheduler/di-registrations-rich.md +3 -3
- package/dist/agentic/guides/modules/scheduler/di-service-tokens.md +3 -3
- package/dist/agentic/guides/modules/scheduler/exact-override-targets.md +4 -9
- package/dist/agentic/guides/modules/scheduler/index.md +3 -4
- package/dist/agentic/guides/modules/scheduler/workers.md +1 -1
- package/dist/agentic/guides/modules/search/di-registrations-rich.md +4 -4
- package/dist/agentic/guides/modules/search/di-service-tokens.md +13 -0
- package/dist/agentic/guides/modules/search/exact-override-targets.md +4 -4
- package/dist/agentic/guides/modules/search/index.md +3 -2
- package/dist/agentic/guides/modules/security/index.md +1 -1
- package/dist/agentic/guides/modules/seeds/index.md +1 -1
- package/dist/agentic/guides/modules/shipping_carriers/index.md +1 -1
- package/dist/agentic/guides/modules/sso/index.md +1 -1
- package/dist/agentic/guides/modules/staff/index.md +1 -1
- package/dist/agentic/guides/modules/storage_s3/index.md +1 -1
- package/dist/agentic/guides/modules/sync_akeneo/index.md +1 -1
- package/dist/agentic/guides/modules/sync_excel/index.md +1 -1
- package/dist/agentic/guides/modules/sync_shopify/index.md +1 -1
- package/dist/agentic/guides/modules/system_status_overlays/index.md +1 -1
- package/dist/agentic/guides/modules/telemetry/index.md +1 -1
- package/dist/agentic/guides/modules/tenant_boundaries/generator-plugins.md +11 -0
- package/dist/agentic/guides/modules/tenant_boundaries/index.md +13 -0
- package/dist/agentic/guides/modules/tenant_boundaries/owned-contract-module-metadata.md +11 -0
- package/dist/agentic/guides/modules/tillio/index.md +1 -1
- package/dist/agentic/guides/modules/translations/index.md +1 -1
- package/dist/agentic/guides/modules/upgrade_actions/index.md +1 -1
- package/dist/agentic/guides/modules/warranty_claims/index.md +1 -1
- package/dist/agentic/guides/modules/webhooks/index.md +1 -1
- package/dist/agentic/guides/modules/wms/index.md +1 -1
- package/dist/agentic/guides/modules/workflows/index.md +1 -1
- package/dist/agentic/guides/reference-module-facts.json +2 -2
- package/dist/agentic/guides/reference-modules/example/index.md +1 -1
- package/dist/agentic/guides/upstream/manifest.json +1 -1
- package/dist/agentic/shared/AGENTS.md.template +3 -3
- package/dist/agentic/shared/ai/harness/cases.json +4 -4
- package/dist/bin.js +6 -3
- package/dist/bin.js.map +2 -2
- package/dist/i18n/de.json +5 -1
- package/dist/i18n/en.json +5 -1
- package/dist/i18n/es.json +5 -1
- package/dist/i18n/ko.json +5 -1
- package/dist/i18n/pl.json +5 -1
- package/dist/lib/cli-bootstrap-mode.js +10 -1
- package/dist/lib/cli-bootstrap-mode.js.map +2 -2
- package/dist/lib/db/commands.js +18 -3
- package/dist/lib/db/commands.js.map +2 -2
- package/dist/lib/db/maintenance-client.js +72 -0
- package/dist/lib/db/maintenance-client.js.map +7 -0
- package/dist/lib/generate-watch-structure.js +2 -1
- package/dist/lib/generate-watch-structure.js.map +2 -2
- package/dist/lib/generators/database-declarations.js +21 -0
- package/dist/lib/generators/database-declarations.js.map +7 -0
- package/dist/lib/generators/module-registry.js +19 -3
- package/dist/lib/generators/module-registry.js.map +2 -2
- package/dist/lib/init-existing-database.js +91 -1
- package/dist/lib/init-existing-database.js.map +2 -2
- package/dist/lib/init-i18n.js +13 -0
- package/dist/lib/init-i18n.js.map +7 -0
- package/dist/lib/init-secrets.js +2 -10
- package/dist/lib/init-secrets.js.map +2 -2
- package/dist/lib/scheduler-supervisor.js +47 -7
- package/dist/lib/scheduler-supervisor.js.map +2 -2
- package/dist/lib/worker-job-handler.js +36 -26
- package/dist/lib/worker-job-handler.js.map +2 -2
- package/dist/mercato.js +93 -54
- package/dist/mercato.js.map +3 -3
- package/package.json +7 -7
- package/src/__tests__/mercato.test.ts +166 -9
- package/src/bin.ts +10 -4
- package/src/i18n/de.json +5 -1
- package/src/i18n/en.json +5 -1
- package/src/i18n/es.json +5 -1
- package/src/i18n/ko.json +5 -1
- package/src/i18n/pl.json +5 -1
- package/src/lib/__tests__/cli-bootstrap-mode.test.ts +76 -1
- package/src/lib/__tests__/generate-watch-structure.test.ts +26 -0
- package/src/lib/__tests__/rotation-kms-warning-presentation.test.ts +1 -1
- package/src/lib/__tests__/scheduler-supervisor.test.ts +107 -0
- package/src/lib/__tests__/worker-job-handler.database.test.ts +76 -0
- package/src/lib/cli-bootstrap-mode.ts +12 -2
- package/src/lib/db/__tests__/commands.migrate-reindex.test.ts +72 -1
- package/src/lib/db/__tests__/maintenance-client.test.ts +70 -0
- package/src/lib/db/commands.ts +20 -6
- package/src/lib/db/maintenance-client.ts +54 -0
- package/src/lib/generate-watch-structure.ts +2 -1
- package/src/lib/generators/__tests__/database-declarations.test.ts +69 -0
- package/src/lib/generators/__tests__/incremental-registry.test.ts +1 -1
- package/src/lib/generators/__tests__/module-subset.test.ts +17 -1
- package/src/lib/generators/database-declarations.ts +19 -0
- package/src/lib/generators/module-registry.ts +23 -4
- package/src/lib/init-existing-database.ts +107 -0
- package/src/lib/init-i18n.ts +11 -0
- package/src/lib/init-secrets.ts +2 -12
- package/src/lib/scheduler-supervisor.ts +40 -7
- package/src/lib/worker-job-handler.ts +39 -28
- package/src/mercato.ts +92 -53
- package/dist/agentic/guides/modules/scheduler/cli-commands.md +0 -15
package/dist/mercato.js
CHANGED
|
@@ -2,11 +2,24 @@ import { registerWorkerShutdownHook, runWorker } from "@open-mercato/queue/worke
|
|
|
2
2
|
import { isProductionBuildPhase, startModuleRuntimes } from "./lib/module-runtimes.js";
|
|
3
3
|
import { getCliModules, hasCliModules, registerCliModules } from "./registry.js";
|
|
4
4
|
import { parseBooleanToken } from "@open-mercato/shared/lib/boolean";
|
|
5
|
+
import { createCliMaintenanceClient } from "./lib/db/maintenance-client.js";
|
|
6
|
+
import { requiresDatabaseRuntimeForMigration } from "./lib/cli-bootstrap-mode.js";
|
|
7
|
+
import { runDatabaseTenantSetup } from "@open-mercato/shared/lib/db/setup";
|
|
8
|
+
import { runWithFreshDatabaseExecutionContext, isDatabaseRuntimeEnabled } from "@open-mercato/shared/lib/db/runtime";
|
|
9
|
+
import { runDatabaseCliCommand } from "@open-mercato/shared/lib/db/cli";
|
|
5
10
|
import { getSslConfig } from "@open-mercato/shared/lib/db/ssl";
|
|
6
11
|
import { getRedisUrl, getRedisUrlOrThrow, parseRedisUrl } from "@open-mercato/shared/lib/redis/connection";
|
|
7
12
|
import { formatInitSummary } from "./lib/init-summary.js";
|
|
8
13
|
import { resolveInitDerivedSecrets, resolveInitSuperadminCredentials } from "./lib/init-secrets.js";
|
|
9
|
-
import {
|
|
14
|
+
import { createInitTranslator } from "./lib/init-i18n.js";
|
|
15
|
+
import {
|
|
16
|
+
decideExistingDatabase,
|
|
17
|
+
formatIncompleteInitializationMessage,
|
|
18
|
+
formatSkipIfInitializedMessage,
|
|
19
|
+
hasIncompleteInitialization,
|
|
20
|
+
openInitializationSession,
|
|
21
|
+
withInitializationClient
|
|
22
|
+
} from "./lib/init-existing-database.js";
|
|
10
23
|
import { assertEphemeralDatabaseConnection, assertEphemeralDatabaseOwnership, readEphemeralDatabaseGuard } from "./lib/testing/ephemeral-database-guard.js";
|
|
11
24
|
import {
|
|
12
25
|
resolveAutoSpawnWorkersMode,
|
|
@@ -203,6 +216,7 @@ function formatInitFailureMessage(error) {
|
|
|
203
216
|
return fallbackMessage;
|
|
204
217
|
}
|
|
205
218
|
async function ensureDatabaseExists(dbUrl) {
|
|
219
|
+
if (isDatabaseRuntimeEnabled()) return true;
|
|
206
220
|
let parsed;
|
|
207
221
|
try {
|
|
208
222
|
parsed = new URL(dbUrl);
|
|
@@ -595,7 +609,7 @@ async function runModuleCommand(allModules, moduleName, commandName, args = [],
|
|
|
595
609
|
throw new Error(`Command "${commandName}" not found in module "${moduleName}"`);
|
|
596
610
|
}
|
|
597
611
|
}
|
|
598
|
-
await resolved.command
|
|
612
|
+
await runDatabaseCliCommand(resolved.command, args, `${moduleName}:${commandName}`);
|
|
599
613
|
return true;
|
|
600
614
|
}
|
|
601
615
|
async function runPostGenerateStructuralInvalidation(quiet) {
|
|
@@ -809,14 +823,15 @@ async function run(argv = process.argv) {
|
|
|
809
823
|
if (first === "init") {
|
|
810
824
|
const { execSync } = await import("child_process");
|
|
811
825
|
console.log("\u{1F680} Initializing Open Mercato app...\n");
|
|
826
|
+
let initializationSession;
|
|
812
827
|
try {
|
|
813
828
|
const initArgs = parts.slice(1).filter(Boolean);
|
|
814
829
|
const reinstall = initArgs.includes("--reinstall") || initArgs.includes("-r");
|
|
815
830
|
const ephemeralDatabaseGuard = readEphemeralDatabaseGuard(initArgs);
|
|
816
831
|
if (ephemeralDatabaseGuard) {
|
|
817
832
|
assertEphemeralDatabaseConnection(ephemeralDatabaseGuard, process.env.DATABASE_URL);
|
|
818
|
-
const { Client
|
|
819
|
-
const verificationClient = new
|
|
833
|
+
const { Client } = await import("pg");
|
|
834
|
+
const verificationClient = new Client({ connectionString: process.env.DATABASE_URL, ssl: getSslConfig(), connectionTimeoutMillis: 5e3 });
|
|
820
835
|
try {
|
|
821
836
|
await verificationClient.connect();
|
|
822
837
|
await assertEphemeralDatabaseOwnership(ephemeralDatabaseGuard, verificationClient);
|
|
@@ -877,7 +892,6 @@ async function run(argv = process.argv) {
|
|
|
877
892
|
if (reinstall) {
|
|
878
893
|
await ensureEnvLoaded();
|
|
879
894
|
console.log("\u267B\uFE0F Reinstall mode enabled: dropping all database tables...");
|
|
880
|
-
const { Client: Client2 } = await import("pg");
|
|
881
895
|
const dbUrl2 = process.env.DATABASE_URL;
|
|
882
896
|
if (!dbUrl2) {
|
|
883
897
|
console.error("DATABASE_URL is not set. Aborting reinstall.");
|
|
@@ -885,7 +899,7 @@ async function run(argv = process.argv) {
|
|
|
885
899
|
}
|
|
886
900
|
const dbExists = ephemeralDatabaseGuard ? true : await ensureDatabaseExists(dbUrl2);
|
|
887
901
|
if (!dbExists) return 1;
|
|
888
|
-
const client =
|
|
902
|
+
const client = await createCliMaintenanceClient(dbUrl2);
|
|
889
903
|
try {
|
|
890
904
|
await client.connect();
|
|
891
905
|
if (ephemeralDatabaseGuard) {
|
|
@@ -967,37 +981,35 @@ async function run(argv = process.argv) {
|
|
|
967
981
|
console.error("DATABASE_URL is not set. Aborting initialization.");
|
|
968
982
|
return 1;
|
|
969
983
|
}
|
|
970
|
-
const { Client: Client2 } = await import("pg");
|
|
971
984
|
const dbExists = ephemeralDatabaseGuard ? true : await ensureDatabaseExists(dbUrl2);
|
|
972
985
|
if (!dbExists) return 1;
|
|
973
|
-
const client =
|
|
986
|
+
const client = await createCliMaintenanceClient(dbUrl2);
|
|
974
987
|
try {
|
|
975
988
|
await client.connect();
|
|
976
989
|
const tableCheck = await client.query(
|
|
977
|
-
`SELECT to_regclass('
|
|
990
|
+
`SELECT to_regclass('users') AS regclass`
|
|
978
991
|
);
|
|
979
|
-
const
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
992
|
+
const countResult = tableCheck.rows[0]?.regclass ? await client.query("SELECT COUNT(*)::text AS count FROM users") : null;
|
|
993
|
+
const existingUsersCount = Number.parseInt(countResult?.rows[0]?.count ?? "0", 10);
|
|
994
|
+
if (existingUsersCount > 0 && await hasIncompleteInitialization(client)) {
|
|
995
|
+
console.error(formatIncompleteInitializationMessage(dbUrl2));
|
|
996
|
+
return 1;
|
|
997
|
+
}
|
|
998
|
+
const existingDatabase = decideExistingDatabase(existingUsersCount, skipIfInitialized);
|
|
999
|
+
if (existingDatabase === "skip") {
|
|
1000
|
+
for (const line of formatSkipIfInitializedMessage(existingUsersCount, dbUrl2)) console.log(line);
|
|
1001
|
+
return 0;
|
|
1002
|
+
}
|
|
1003
|
+
if (existingDatabase === "abort") {
|
|
1004
|
+
console.error(
|
|
1005
|
+
`\u274C Initialization aborted: found ${existingUsersCount} existing user(s) in the database.`
|
|
983
1006
|
);
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
if (existingDatabase === "abort") {
|
|
991
|
-
console.error(
|
|
992
|
-
`\u274C Initialization aborted: found ${existingUsersCount} existing user(s) in the database.`
|
|
993
|
-
);
|
|
994
|
-
console.error(
|
|
995
|
-
" To reset and initialize from scratch, run: yarn mercato init --reinstall"
|
|
996
|
-
);
|
|
997
|
-
console.error(" Standalone shortcut: yarn setup --reinstall");
|
|
998
|
-
console.error(" Shortcut script: yarn reinstall");
|
|
999
|
-
return 1;
|
|
1000
|
-
}
|
|
1007
|
+
console.error(
|
|
1008
|
+
" To reset and initialize from scratch, run: yarn mercato init --reinstall"
|
|
1009
|
+
);
|
|
1010
|
+
console.error(" Standalone shortcut: yarn setup --reinstall");
|
|
1011
|
+
console.error(" Shortcut script: yarn reinstall");
|
|
1012
|
+
return 1;
|
|
1001
1013
|
}
|
|
1002
1014
|
} finally {
|
|
1003
1015
|
try {
|
|
@@ -1030,9 +1042,24 @@ async function run(argv = process.argv) {
|
|
|
1030
1042
|
registerCliModules(bootstrapData.modules);
|
|
1031
1043
|
console.log("\u2705 Bootstrap complete\n");
|
|
1032
1044
|
const allModules = await buildAllModules();
|
|
1045
|
+
const dbUrl = process.env.DATABASE_URL;
|
|
1046
|
+
if (!dbUrl) throw new Error("[internal] DATABASE_URL is missing after initialization preflight");
|
|
1047
|
+
const session = await openInitializationSession(dbUrl);
|
|
1048
|
+
initializationSession = session;
|
|
1049
|
+
if (session.clearedInterruptedAttempt) {
|
|
1050
|
+
console.log(createInitTranslator(process.env)("cli.init.messages.interruptedAttemptCleared"));
|
|
1051
|
+
}
|
|
1052
|
+
const runBeforeUsersExist = async (action) => {
|
|
1053
|
+
try {
|
|
1054
|
+
await action();
|
|
1055
|
+
} catch (error) {
|
|
1056
|
+
await session.cancelWithoutUsers();
|
|
1057
|
+
throw error;
|
|
1058
|
+
}
|
|
1059
|
+
};
|
|
1033
1060
|
if (allModules.some((module) => module.id === "configs")) {
|
|
1034
1061
|
console.log("\u2699\uFE0F Restoring module defaults...");
|
|
1035
|
-
await runModuleCommand(allModules, "configs", "restore-defaults", []);
|
|
1062
|
+
await runBeforeUsersExist(() => runModuleCommand(allModules, "configs", "restore-defaults", []));
|
|
1036
1063
|
console.log("\u2705 Module defaults restored\n");
|
|
1037
1064
|
}
|
|
1038
1065
|
const findArgValue = (names, fallback) => {
|
|
@@ -1081,19 +1108,17 @@ async function run(argv = process.argv) {
|
|
|
1081
1108
|
if (skipPasswordPolicy) {
|
|
1082
1109
|
setupArgs.push("--skip-password-policy");
|
|
1083
1110
|
}
|
|
1084
|
-
await runModuleCommand(allModules, "auth", "setup", setupArgs);
|
|
1085
|
-
const
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
const tenantId = orgResult?.rows?.[0]?.tenant_id ?? null;
|
|
1096
|
-
const orgId = orgResult?.rows?.[0]?.org_id ?? null;
|
|
1111
|
+
await runBeforeUsersExist(() => runModuleCommand(allModules, "auth", "setup", setupArgs));
|
|
1112
|
+
const initializedOrganization = await withInitializationClient(dbUrl, async (client) => {
|
|
1113
|
+
const orgResult = await client.query(
|
|
1114
|
+
`SELECT o.id AS org_id, o.tenant_id FROM organizations o
|
|
1115
|
+
JOIN users u ON u.organization_id = o.id
|
|
1116
|
+
LIMIT 1`
|
|
1117
|
+
);
|
|
1118
|
+
return orgResult.rows[0];
|
|
1119
|
+
});
|
|
1120
|
+
const tenantId = initializedOrganization?.tenant_id;
|
|
1121
|
+
const orgId = initializedOrganization?.org_id;
|
|
1097
1122
|
if (!tenantId || !orgId) {
|
|
1098
1123
|
throw new Error("Auth setup failed to create a tenant/org. Aborting init.");
|
|
1099
1124
|
}
|
|
@@ -1128,18 +1153,18 @@ async function run(argv = process.argv) {
|
|
|
1128
1153
|
}
|
|
1129
1154
|
console.log("\u{1F4DA} Seeding module defaults...");
|
|
1130
1155
|
const { createRequestContainer } = await import("@open-mercato/shared/lib/di/container");
|
|
1131
|
-
const seedContainer = await createRequestContainer();
|
|
1156
|
+
const seedContainer = await runWithFreshDatabaseExecutionContext({ kind: "maintenance", tenantId: null, source: "cli", operation: "seed-bootstrap" }, () => createRequestContainer());
|
|
1132
1157
|
const seedEm = seedContainer.resolve("em");
|
|
1133
1158
|
const seedCtx = { em: seedEm, tenantId, organizationId: orgId, container: seedContainer };
|
|
1134
1159
|
for (const mod2 of allModules) {
|
|
1135
1160
|
if (mod2.setup?.seedDefaults) {
|
|
1136
1161
|
console.log(` \u{1F4E6} ${mod2.id}...`);
|
|
1137
|
-
await mod2.setup.seedDefaults(
|
|
1162
|
+
await runDatabaseTenantSetup(seedCtx, (context) => mod2.setup.seedDefaults(context));
|
|
1138
1163
|
}
|
|
1139
1164
|
}
|
|
1140
1165
|
console.log("\u2705 Module defaults seeded\n");
|
|
1141
1166
|
const { ensureCustomRoleAcls } = await import("@open-mercato/core/modules/auth/lib/setup-app");
|
|
1142
|
-
await
|
|
1167
|
+
await runDatabaseTenantSetup({ em: seedEm, tenantId, organizationId: orgId }, (context) => ensureCustomRoleAcls(context.em, tenantId, allModules));
|
|
1143
1168
|
if (skipExamples) {
|
|
1144
1169
|
console.log("\u{1F6AB} Example data seeding skipped (--no-examples)\n");
|
|
1145
1170
|
} else {
|
|
@@ -1147,7 +1172,7 @@ async function run(argv = process.argv) {
|
|
|
1147
1172
|
for (const mod2 of allModules) {
|
|
1148
1173
|
if (mod2.setup?.seedExamples) {
|
|
1149
1174
|
console.log(` \u{1F4E6} ${mod2.id}...`);
|
|
1150
|
-
await mod2.setup.seedExamples(
|
|
1175
|
+
await runDatabaseTenantSetup(seedCtx, (context) => mod2.setup.seedExamples(context));
|
|
1151
1176
|
}
|
|
1152
1177
|
}
|
|
1153
1178
|
console.log("\u2705 Example data seeded\n");
|
|
@@ -1199,6 +1224,7 @@ async function run(argv = process.argv) {
|
|
|
1199
1224
|
} else {
|
|
1200
1225
|
console.log("");
|
|
1201
1226
|
}
|
|
1227
|
+
await session.complete();
|
|
1202
1228
|
const adminPasswordOverride = derivedSecrets.adminPassword;
|
|
1203
1229
|
const employeePasswordOverride = derivedSecrets.employeePassword;
|
|
1204
1230
|
const createdUsers = [];
|
|
@@ -1230,6 +1256,8 @@ async function run(argv = process.argv) {
|
|
|
1230
1256
|
} catch (error) {
|
|
1231
1257
|
console.error("\u274C Initialization failed:", formatInitFailureMessage(error));
|
|
1232
1258
|
return 1;
|
|
1259
|
+
} finally {
|
|
1260
|
+
await initializationSession?.close().catch(() => void 0);
|
|
1233
1261
|
}
|
|
1234
1262
|
}
|
|
1235
1263
|
if (first === "agentic:init") {
|
|
@@ -1359,10 +1387,17 @@ async function run(argv = process.argv) {
|
|
|
1359
1387
|
return 1;
|
|
1360
1388
|
}
|
|
1361
1389
|
const { createRequestContainer } = await import("@open-mercato/shared/lib/di/container");
|
|
1362
|
-
const seedContainer = await createRequestContainer();
|
|
1390
|
+
const seedContainer = await runWithFreshDatabaseExecutionContext({ kind: "maintenance", tenantId: null, source: "cli", operation: "seed-bootstrap" }, () => createRequestContainer());
|
|
1363
1391
|
const seedEm = seedContainer.resolve("em");
|
|
1364
1392
|
const { Organization } = await import("@open-mercato/core/modules/directory/data/entities");
|
|
1365
|
-
const orgs = await seedEm.find(Organization, { deletedAt: null }, { populate: ["tenant"] })
|
|
1393
|
+
const orgs = !isDatabaseRuntimeEnabled() ? await seedEm.find(Organization, { deletedAt: null }, { populate: ["tenant"] }) : await runWithFreshDatabaseExecutionContext({ kind: "maintenance", tenantId: null, source: "cli", operation: "seed-discovery" }, async () => {
|
|
1394
|
+
const discovery = await createRequestContainer();
|
|
1395
|
+
try {
|
|
1396
|
+
return await discovery.resolve("em").find(Organization, { deletedAt: null }, { populate: ["tenant"] });
|
|
1397
|
+
} finally {
|
|
1398
|
+
await discovery.dispose();
|
|
1399
|
+
}
|
|
1400
|
+
});
|
|
1366
1401
|
if (orgs.length === 0) {
|
|
1367
1402
|
console.error("\u274C No organizations found. Run yarn initialize first.");
|
|
1368
1403
|
return 1;
|
|
@@ -1377,11 +1412,11 @@ async function run(argv = process.argv) {
|
|
|
1377
1412
|
for (const mod2 of modulesToSeed) {
|
|
1378
1413
|
if (mod2.setup?.seedDefaults) {
|
|
1379
1414
|
console.log(` \u{1F4E6} ${mod2.id}...`);
|
|
1380
|
-
await mod2.setup.seedDefaults(
|
|
1415
|
+
await runDatabaseTenantSetup(seedCtx, (context) => mod2.setup.seedDefaults(context));
|
|
1381
1416
|
}
|
|
1382
1417
|
}
|
|
1383
1418
|
const { ensureCustomRoleAcls } = await import("@open-mercato/core/modules/auth/lib/setup-app");
|
|
1384
|
-
await
|
|
1419
|
+
await runDatabaseTenantSetup({ em: seedEm, tenantId, organizationId }, (context) => ensureCustomRoleAcls(context.em, tenantId, allModules));
|
|
1385
1420
|
}
|
|
1386
1421
|
console.log("\n\u2705 seed:defaults complete.");
|
|
1387
1422
|
return 0;
|
|
@@ -1797,8 +1832,12 @@ async function run(argv = process.argv) {
|
|
|
1797
1832
|
},
|
|
1798
1833
|
{
|
|
1799
1834
|
command: "migrate",
|
|
1800
|
-
|
|
1835
|
+
databaseAccess: "maintenance",
|
|
1836
|
+
help: "Applies every pending migration using the configured database connection.\nTenant boundaries use the maintenance login. This mutates the database \u2014 check .env before running it.",
|
|
1801
1837
|
run: async () => {
|
|
1838
|
+
if (requiresDatabaseRuntimeForMigration() && !isDatabaseRuntimeEnabled()) {
|
|
1839
|
+
throw new Error("[internal] Tenant boundaries were requested but are not registered. Enable the tenant_boundaries module and run yarn generate before migrating.");
|
|
1840
|
+
}
|
|
1802
1841
|
const { createResolver } = await import("./lib/resolver.js");
|
|
1803
1842
|
const { dbMigrate } = await import("./lib/db/index.js");
|
|
1804
1843
|
const resolver = createResolver();
|
|
@@ -2402,7 +2441,7 @@ async function run(argv = process.argv) {
|
|
|
2402
2441
|
}
|
|
2403
2442
|
try {
|
|
2404
2443
|
const commandExitCode = await runWithCapturedExitCode(async () => {
|
|
2405
|
-
await cmd
|
|
2444
|
+
await runDatabaseCliCommand(cmd, rest, `${modName}:${cmdName}`);
|
|
2406
2445
|
});
|
|
2407
2446
|
if (!quietGeneration && (modName !== "deploy" || cmdName !== "railway")) {
|
|
2408
2447
|
const ms = Date.now() - started;
|