Skip to content
Draft
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
20 changes: 20 additions & 0 deletions core/actions/incremental_table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,14 @@ export class IncrementalTable extends ActionBuilder<dataform.Table> {
this.proto.fileName,
{ validateTarget: true },
);
this.uniqueKeyAssertions.forEach((assertion) => {
assertion.database(database);
assertion.setParentAction(dataform.Target.create(this.proto.target));
});
if (this.rowConditionsAssertion) {
this.rowConditionsAssertion.database(database);
this.rowConditionsAssertion.setParentAction(dataform.Target.create(this.proto.target));
}
return this;
}

Expand All @@ -544,6 +552,18 @@ export class IncrementalTable extends ActionBuilder<dataform.Table> {
this.proto.fileName,
{ validateTarget: true },
);
this.uniqueKeyAssertions.forEach((assertion) => {
if (this.inlineAssertionsFollowActionSchema()) {
assertion.schema(schema);
}
assertion.setParentAction(dataform.Target.create(this.proto.target));
});
if (this.rowConditionsAssertion) {
if (this.inlineAssertionsFollowActionSchema()) {
this.rowConditionsAssertion.schema(schema);
}
this.rowConditionsAssertion.setParentAction(dataform.Target.create(this.proto.target));
}
return this;
}

Expand Down
42 changes: 40 additions & 2 deletions core/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ export abstract class ActionBuilder<T> {
public session: Session;
public includeAssertionsForDependency: Map<string, boolean> = new Map();
protected isLakehouseTarget = false;
/**
* The dataset explicitly configured for this action's inline assertions (`assertions.dataset`),
* if any. Takes precedence over the project-level default assertion dataset.
*/
protected inlineAssertionsDataset?: string;

constructor(session?: Session) {
this.session = session;
Expand Down Expand Up @@ -90,6 +95,14 @@ export abstract class ActionBuilder<T> {
/** Creates the final protobuf representation. */
public abstract compile(): T;

/**
* Whether inline assertions should be written to the same dataset as their parent action, i.e.
* neither an action-level nor a project-level assertion dataset has been configured.
*/
protected inlineAssertionsFollowActionSchema(): boolean {
return !this.inlineAssertionsDataset && !this.session.projectConfig.assertionSchema;
}

protected generateInlineAssertions(
tableAssertionsConfig: dataform.ActionConfig.TableAssertionsConfig,
proto: dataform.Table,
Expand Down Expand Up @@ -117,12 +130,27 @@ export abstract class ActionBuilder<T> {
];
}
const jobLabels = proto.actionDescriptor?.jobLabels;
const project = proto.target.database || undefined;
// `schema` is the legacy name for `dataset`; it is accepted here for callers of the JS API's
// `.assertions()` method, which bypasses the legacy config conversion.
this.inlineAssertionsDataset =
(tableAssertionsConfig as any).schema || tableAssertionsConfig.dataset || undefined;
const dataset =
this.inlineAssertionsDataset ||
this.session.projectConfig.assertionSchema ||
proto.target.schema ||
undefined;
if (uniqueKeys) {
uniqueKeys.forEach(({ uniqueKey }, index) => {
const uniqueKeyAssertion = this.session
.assert(
`${assertionPrefix}${proto.target.schema}_${proto.target.name}_assertions_uniqueKey_${index}`,
dataform.ActionConfig.AssertionConfig.create({ filename: proto.fileName, jobLabels }),
dataform.ActionConfig.AssertionConfig.create({
filename: proto.fileName,
project,
dataset,
jobLabels,
}),
)
.query((ctx) =>
this.session.compilationSql().indexAssertion(ctx.ref(proto.target), uniqueKey),
Expand All @@ -149,7 +177,12 @@ export abstract class ActionBuilder<T> {
inlineAssertions.rowConditionsAssertion = this.session
.assert(
`${assertionPrefix}${proto.target.schema}_${proto.target.name}_assertions_rowConditions`,
dataform.ActionConfig.AssertionConfig.create({ filename: proto.fileName, jobLabels }),
dataform.ActionConfig.AssertionConfig.create({
filename: proto.fileName,
project,
dataset,
jobLabels,
}),
)
.query((ctx) =>
this.session
Expand Down Expand Up @@ -390,6 +423,7 @@ export interface ILegacyTableAssertions {
uniqueKeys?: string[][];
nonNull?: string | string[];
rowConditions?: string[];
schema?: string;
}

export class LegacyConfigConverter {
Expand Down Expand Up @@ -436,6 +470,10 @@ export class LegacyConfigConverter {
if (typeof legacyConfig.assertions.nonNull === "string") {
legacyConfig.assertions.nonNull = [legacyConfig.assertions.nonNull];
}
if (legacyConfig.assertions.schema) {
legacyConfig.assertions.dataset = legacyConfig.assertions.schema;
delete legacyConfig.assertions.schema;
}
}
return legacyConfig;
}
Expand Down
Loading
Loading