Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions packages/core/src/bin/commands/createViews.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,20 @@ export async function createViews({
return;
}

const database = createDatabase({
common,
// Note: `namespace` is not used in this command
namespace: {
schema: cliOptions.schema!,
viewsSchema: undefined,
},
preBuild: buildResult.result,
schemaBuild: emptySchemaBuild,
});

const databaseDiagnostic = await build.databaseDiagnostic({
preBuild: buildResult.result,
database,
});
if (databaseDiagnostic.status === "error") {
common.logger.error({
Expand All @@ -124,17 +136,6 @@ export async function createViews({
return;
}

const database = createDatabase({
common,
// Note: `namespace` is not used in this command
namespace: {
schema: cliOptions.schema!,
viewsSchema: undefined,
},
preBuild: buildResult.result,
schemaBuild: emptySchemaBuild,
});

const endClock = startClock();

const schemaExists = await database.adminQB
Expand Down
108 changes: 56 additions & 52 deletions packages/core/src/bin/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import path from "node:path";
import { createBuild } from "@/build/index.js";
import { type Database, createDatabase } from "@/database/index.js";
import type { Common } from "@/internal/common.js";
import { NonRetryableUserError, ShutdownError } from "@/internal/errors.js";
import {
BaseError,
ShutdownError,
isUserDerivedError,
} from "@/internal/errors.js";
import { createLogger } from "@/internal/logger.js";
import { MetricsService } from "@/internal/metrics.js";
import { buildOptions } from "@/internal/options.js";
Expand Down Expand Up @@ -102,12 +106,6 @@ export async function dev({ cliOptions }: { cliOptions: CliOptions }) {
}

if (result.status === "error") {
if (isInitialBuild === false) {
common.logger.error({
error: result.error,
});
}

// This handles indexing function build failures on hot reload.
metrics.hasError = true;
return;
Expand Down Expand Up @@ -162,23 +160,6 @@ export async function dev({ cliOptions }: { cliOptions: CliOptions }) {
return;
}

const databaseDiagnostic = await build.databaseDiagnostic({
preBuild: preCompileResult.result,
});
if (databaseDiagnostic.status === "error") {
common.logger.error({
msg: "Build failed",
stage: "diagnostic",
error: databaseDiagnostic.error,
});
buildQueue.add({
status: "error",
kind: "indexing",
error: databaseDiagnostic.error,
});
return;
}

const compileSchemaResult = build.compileSchema({
...schemaResult.result,
preBuild: preCompileResult.result,
Expand Down Expand Up @@ -276,11 +257,40 @@ export async function dev({ cliOptions }: { cliOptions: CliOptions }) {
preBuild: preCompileResult.result,
schemaBuild: compileSchemaResult.result,
});
crashRecoveryCheckpoint = await database.migrate({
buildId: indexingBuildResult.result.buildId,
chains: indexingBuildResult.result.chains,
finalizedBlocks: indexingBuildResult.result.finalizedBlocks,

const databaseDiagnostic = await build.databaseDiagnostic({
preBuild: preCompileResult.result,
database,
});
if (databaseDiagnostic.status === "error") {
common.logger.error({
msg: "Build failed",
stage: "diagnostic",
error: databaseDiagnostic.error,
});
buildQueue.add({
status: "error",
kind: "indexing",
error: databaseDiagnostic.error,
});
return;
}

crashRecoveryCheckpoint = await database
.migrate({
buildId: indexingBuildResult.result.buildId,
chains: indexingBuildResult.result.chains,
finalizedBlocks: indexingBuildResult.result.finalizedBlocks,
})
.catch((error) => {
common.logger.error({
msg: "Database migration failed",
stage: "migration",
error: error as Error,
});

throw error;
});

await database.migrateSync();

Expand Down Expand Up @@ -437,37 +447,31 @@ export async function dev({ cliOptions }: { cliOptions: CliOptions }) {

process.on("uncaughtException", (error: Error) => {
if (error instanceof ShutdownError) return;
if (error instanceof NonRetryableUserError) {
common.logger.error({
msg: "uncaughtException",
error,
});

buildQueue.clear();
buildQueue.add({ status: "error", kind: "indexing", error });
if (error instanceof BaseError) {
common.logger.error({ msg: `uncaughtException: ${error.name}` });
if (isUserDerivedError(error)) {
buildQueue.clear();
buildQueue.add({ status: "error", kind: "indexing", error });
} else {
exit({ code: 75 });
}
} else {
common.logger.error({
msg: "uncaughtException",
error,
});
common.logger.error({ msg: "uncaughtException", error });
exit({ code: 75 });
}
});
process.on("unhandledRejection", (error: Error) => {
if (error instanceof ShutdownError) return;
if (error instanceof NonRetryableUserError) {
common.logger.error({
msg: "unhandledRejection",
error,
});

buildQueue.clear();
buildQueue.add({ status: "error", kind: "indexing", error });
if (error instanceof BaseError) {
common.logger.error({ msg: `unhandledRejection: ${error.name}` });
if (isUserDerivedError(error)) {
buildQueue.clear();
buildQueue.add({ status: "error", kind: "indexing", error });
} else {
exit({ code: 75 });
}
} else {
common.logger.error({
msg: "unhandledRejection",
error,
});
common.logger.error({ msg: "unhandledRejection", error });
exit({ code: 75 });
}
});
Expand Down
17 changes: 9 additions & 8 deletions packages/core/src/bin/commands/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,17 @@ export async function list({ cliOptions }: { cliOptions: CliOptions }) {
return;
}

const database = createDatabase({
common,
// Note: `namespace` is not used in this command
namespace: { schema: "public", viewsSchema: undefined },
preBuild: buildResult.result,
schemaBuild: emptySchemaBuild,
});

const databaseDiagnostic = await build.databaseDiagnostic({
preBuild: buildResult.result,
database,
});
if (databaseDiagnostic.status === "error") {
common.logger.error({
Expand All @@ -97,14 +106,6 @@ export async function list({ cliOptions }: { cliOptions: CliOptions }) {
return;
}

const database = createDatabase({
common,
// Note: `namespace` is not used in this command
namespace: { schema: "public", viewsSchema: undefined },
preBuild: buildResult.result,
schemaBuild: emptySchemaBuild,
});

const ponderSchemas = await database.adminQB.wrap((db) =>
db
.select({ schema: TABLES.table_schema })
Expand Down
17 changes: 9 additions & 8 deletions packages/core/src/bin/commands/prune.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,17 @@ export async function prune({ cliOptions }: { cliOptions: CliOptions }) {
return;
}

const database = createDatabase({
common,
// Note: `namespace` is not used in this command
namespace: { schema: "public", viewsSchema: undefined },
preBuild: buildResult.result,
schemaBuild: emptySchemaBuild,
});

const databaseDiagnostic = await build.databaseDiagnostic({
preBuild: buildResult.result,
database,
});
if (databaseDiagnostic.status === "error") {
common.logger.error({
Expand All @@ -104,14 +113,6 @@ export async function prune({ cliOptions }: { cliOptions: CliOptions }) {
return;
}

const database = createDatabase({
common,
// Note: `namespace` is not used in this command
namespace: { schema: "public", viewsSchema: undefined },
preBuild: buildResult.result,
schemaBuild: emptySchemaBuild,
});

const ponderSchemas = await database.adminQB.wrap((db) =>
db
.select({ schema: TABLES.table_schema, tableCount: count() })
Expand Down
27 changes: 14 additions & 13 deletions packages/core/src/bin/commands/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,19 +113,6 @@ export async function serve({ cliOptions }: { cliOptions: CliOptions }) {
return;
}

const databaseDiagnostic = await build.databaseDiagnostic({
preBuild: preCompileResult.result,
});
if (databaseDiagnostic.status === "error") {
common.logger.error({
msg: "Build failed",
stage: "diagnostic",
error: databaseDiagnostic.error,
});
await exit({ code: 75 });
return;
}

const compileSchemaResult = build.compileSchema({
...schemaResult.result,
preBuild: preCompileResult.result,
Expand Down Expand Up @@ -164,6 +151,20 @@ export async function serve({ cliOptions }: { cliOptions: CliOptions }) {
schemaBuild: compileSchemaResult.result,
});

const databaseDiagnostic = await build.databaseDiagnostic({
preBuild: preCompileResult.result,
database,
});
if (databaseDiagnostic.status === "error") {
common.logger.error({
msg: "Build failed",
stage: "diagnostic",
error: databaseDiagnostic.error,
});
await exit({ code: 75 });
return;
}

const schemaExists = await database.adminQB
.wrap((db) =>
db
Expand Down
46 changes: 29 additions & 17 deletions packages/core/src/bin/commands/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,19 +137,6 @@ export async function start({
return;
}

const databaseDiagnostic = await build.databaseDiagnostic({
preBuild: preCompileResult.result,
});
if (databaseDiagnostic.status === "error") {
common.logger.error({
msg: "Build failed",
stage: "diagnostic",
error: databaseDiagnostic.error,
});
await exit({ code: 75 });
return;
}

const compileSchemaResult = build.compileSchema({
...schemaResult.result,
preBuild: preCompileResult.result,
Expand Down Expand Up @@ -225,11 +212,36 @@ export async function start({
preBuild: preCompileResult.result,
schemaBuild: compileSchemaResult.result,
});
const crashRecoveryCheckpoint = await database.migrate({
buildId: indexingBuildResult.result.buildId,
chains: indexingBuildResult.result.chains,
finalizedBlocks: indexingBuildResult.result.finalizedBlocks,

const databaseDiagnostic = await build.databaseDiagnostic({
preBuild: preCompileResult.result,
database,
});
if (databaseDiagnostic.status === "error") {
common.logger.error({
msg: "Build failed",
stage: "diagnostic",
error: databaseDiagnostic.error,
});
await exit({ code: 75 });
return;
}

const crashRecoveryCheckpoint = await database
.migrate({
buildId: indexingBuildResult.result.buildId,
chains: indexingBuildResult.result.chains,
finalizedBlocks: indexingBuildResult.result.finalizedBlocks,
})
.catch((error) => {
common.logger.error({
msg: "Database migration failed",
stage: "migration",
error: error as Error,
});

throw error;
});

await database.migrateSync();

Expand Down
Loading