Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
+42
-19
@@ -36,7 +36,11 @@ import {
|
||||
type TenantManifestModelEntry,
|
||||
type TenantSnapshotManifest,
|
||||
} from "./manifest.js";
|
||||
import { TENANT_SCOPED_MODELS } from "./models.js";
|
||||
import {
|
||||
TENANT_LIFECYCLE_MODELS,
|
||||
tenantWhereForLifecycleModel,
|
||||
type TenantLifecycleModel,
|
||||
} from "./derived-models.js";
|
||||
import { assertSuperAdminForNoneProfile, writeSnapshotAudit } from "./audit.js";
|
||||
import { snapshotReplacer } from "./json-codec.js";
|
||||
import { collectAndWriteGlobals, type GlobalsPrismaRunner } from "./globals-runner.js";
|
||||
@@ -65,6 +69,16 @@ export interface PrismaRunner extends Partial<GlobalsPrismaRunner> {
|
||||
tenantId: string,
|
||||
pageSize: number,
|
||||
): AsyncIterable<Record<string, unknown>>;
|
||||
/**
|
||||
* Stream FK-derived tenant rows using a relation-filter where clause.
|
||||
* This is separate from findManyByTenant because these models deliberately
|
||||
* have no tenantId scalar of their own.
|
||||
*/
|
||||
findManyByWhere(
|
||||
modelName: string,
|
||||
where: Readonly<Record<string, unknown>>,
|
||||
pageSize: number,
|
||||
): AsyncIterable<Record<string, unknown>>;
|
||||
}
|
||||
|
||||
export interface SnapshotS3Output {
|
||||
@@ -160,13 +174,12 @@ export async function runTenantSnapshot(opts: RunSnapshotOptions): Promise<RunSn
|
||||
|
||||
const pageSize = opts.pageSize ?? 1000;
|
||||
const modelEntries: TenantManifestModelEntry[] = [];
|
||||
for (const { model, tenantField } of TENANT_SCOPED_MODELS) {
|
||||
const filePath = join(postgresDir, `${model}.jsonl`);
|
||||
logger.info(`Extracting model ${model}`);
|
||||
for (const entry of TENANT_LIFECYCLE_MODELS) {
|
||||
const filePath = join(postgresDir, `${entry.model}.jsonl`);
|
||||
logger.info(`Extracting model ${entry.model}`);
|
||||
const { rowCount, sizeBytes, sha256 } = await writeModelJsonl({
|
||||
prisma: opts.prisma,
|
||||
modelName: model,
|
||||
tenantField,
|
||||
entry,
|
||||
tenantId: opts.tenantId,
|
||||
pageSize,
|
||||
profile,
|
||||
@@ -174,8 +187,8 @@ export async function runTenantSnapshot(opts: RunSnapshotOptions): Promise<RunSn
|
||||
outPath: filePath,
|
||||
});
|
||||
modelEntries.push({
|
||||
model,
|
||||
path: `postgres/${model}.jsonl`,
|
||||
model: entry.model,
|
||||
path: `postgres/${entry.model}.jsonl`,
|
||||
rowCount,
|
||||
sizeBytes,
|
||||
checksum: { algorithm: "sha256", value: sha256 },
|
||||
@@ -192,7 +205,10 @@ export async function runTenantSnapshot(opts: RunSnapshotOptions): Promise<RunSn
|
||||
const wantGlobals = opts.includeGlobals !== false;
|
||||
if (wantGlobals) {
|
||||
const globalsRunner = opts.prisma as Partial<GlobalsPrismaRunner>;
|
||||
if (typeof globalsRunner.findManyByIds === "function") {
|
||||
if (
|
||||
typeof globalsRunner.findManyByIds === "function" &&
|
||||
typeof globalsRunner.findManyByForeignKeys === "function"
|
||||
) {
|
||||
const globalsDir = join(stageDir, "globals");
|
||||
await mkdir(globalsDir, { recursive: true });
|
||||
const collected = await collectAndWriteGlobals({
|
||||
@@ -208,7 +224,7 @@ export async function runTenantSnapshot(opts: RunSnapshotOptions): Promise<RunSn
|
||||
}
|
||||
} else {
|
||||
logger.warn(
|
||||
"Snapshot runner does not implement findManyByIds — skipping globals extraction. Restore into an empty DB will fail on FKs to global rows.",
|
||||
"Snapshot runner does not implement the globals lookup methods — skipping globals extraction. Restore into an empty DB will fail on FKs to global rows.",
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -314,8 +330,7 @@ export async function runTenantSnapshot(opts: RunSnapshotOptions): Promise<RunSn
|
||||
|
||||
interface WriteModelJsonlOptions {
|
||||
prisma: PrismaRunner;
|
||||
modelName: string;
|
||||
tenantField: string;
|
||||
entry: TenantLifecycleModel;
|
||||
tenantId: string;
|
||||
pageSize: number;
|
||||
profile: (typeof PROFILES)[ProfileName];
|
||||
@@ -330,13 +345,21 @@ async function writeModelJsonl(
|
||||
const hash = createHash("sha256");
|
||||
let rowCount = 0;
|
||||
|
||||
for await (const row of opts.prisma.findManyByTenant(
|
||||
opts.modelName,
|
||||
opts.tenantField,
|
||||
opts.tenantId,
|
||||
opts.pageSize,
|
||||
)) {
|
||||
const anonymized = anonymizeRow(opts.profile, opts.modelName, row, opts.ctx);
|
||||
const rows =
|
||||
opts.entry.scope === "direct"
|
||||
? opts.prisma.findManyByTenant(
|
||||
opts.entry.model,
|
||||
opts.entry.tenantField,
|
||||
opts.tenantId,
|
||||
opts.pageSize,
|
||||
)
|
||||
: opts.prisma.findManyByWhere(
|
||||
opts.entry.model,
|
||||
tenantWhereForLifecycleModel(opts.entry, opts.tenantId),
|
||||
opts.pageSize,
|
||||
);
|
||||
for await (const row of rows) {
|
||||
const anonymized = anonymizeRow(opts.profile, opts.entry.model, row, opts.ctx);
|
||||
const line = `${JSON.stringify(anonymized, snapshotReplacer)}\n`;
|
||||
hash.update(line);
|
||||
rowCount += 1;
|
||||
|
||||
Reference in new issue
Block a user