-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
288 additions
and
266 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { promises as fs } from "fs"; | ||
import { ManyToOneField, PolymorphicFieldComponent } from "./EntityDbMetadata"; | ||
import { Config, EntityDbMetadata } from "./index"; | ||
import { sortByNonDeferredForeignKeys } from "./sortForeignKeys"; | ||
|
||
export async function maybeSetForeignKeyOrdering(config: Config, entities: EntityDbMetadata[]): Promise<boolean> { | ||
let hasError = false; | ||
|
||
// Hopefully all FKs are deferred, but if not... | ||
const hasAnyNonDeferredFks = entities.some((e) => e.nonDeferredFks.length > 0); | ||
if (!hasAnyNonDeferredFks) { | ||
// Great, nothing to do | ||
for (const entity of entities) entity.nonDeferredFkOrder = 0; | ||
} else { | ||
// Always sort the non-deferred FKs to establish insert/flush order | ||
const { notNullCycles } = sortByNonDeferredForeignKeys(entities); | ||
|
||
// Log the mere existence of the non-deferred FKs | ||
const { nonDeferredForeignKeys: setting = "warn" } = config; | ||
const nonDeferredFks = entities.flatMap((e) => e.nonDeferredFks.map((m2o) => ({ entity: e, m2o }))); | ||
if (setting === "error" || setting === "warn") { | ||
const flag = setting === "error" ? "ERROR" : "WARNING"; | ||
console.log(`${flag}: Found ${nonDeferredFks.length} foreign keys that are not DEFERRABLE/INITIALLY DEFERRED`); | ||
for (const { entity, m2o } of nonDeferredFks) console.log(`${entity.tableName}.${m2o.columnName}`); | ||
console.log(""); | ||
|
||
console.log("Please either:"); | ||
console.log(" - Alter your migration to create the FKs as deferred (ideal)"); | ||
console.log(" - Execute the generated alter-foreign-keys.sql file (one-time fix)"); | ||
console.log(" - Set 'nonDeferredFks: ignore' in joist-config.json"); | ||
console.log(""); | ||
|
||
console.log("See https://joist-orm.io/docs/getting-started/schema-assumptions#deferred-constraints"); | ||
console.log(""); | ||
|
||
await writeAlterTables(nonDeferredFks); | ||
|
||
if (setting === "error") hasError = true; | ||
} else if (setting === "ignore") { | ||
// We trust the user to know what they're doing | ||
} | ||
|
||
// Always treat these as errors? | ||
if (notNullCycles.length > 0) { | ||
console.log(`ERROR: Found a schema cycle of not-null foreign keys:`); | ||
notNullCycles.forEach((cycle) => console.log(cycle)); | ||
console.log(""); | ||
console.log("These cycles can cause fatal em.flush & flush_test_database errors."); | ||
console.log(""); | ||
console.log("Please make one of the FKs involved in the cycle nullable."); | ||
console.log(""); | ||
hasError ??= true; | ||
} | ||
} | ||
|
||
return hasError; | ||
} | ||
|
||
async function writeAlterTables( | ||
nonDeferredFks: Array<{ entity: EntityDbMetadata; m2o: ManyToOneField | PolymorphicFieldComponent }>, | ||
): Promise<void> { | ||
const queries = nonDeferredFks.map(({ entity, m2o }) => { | ||
return `ALTER TABLE ${entity.tableName} ALTER CONSTRAINT ${m2o.constraintName} DEFERRABLE INITIALLY DEFERRED;`; | ||
}); | ||
await fs.writeFile("./alter-foreign-keys.sql", queries.join("\n")); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.