(schema: AnySchema)
| 10 | } from "./create"; |
| 11 | |
| 12 | export function validateSchema(schema: AnySchema) { |
| 13 | if (!valid(schema.version)) { |
| 14 | throw new Error(`the version ${schema.version} is invalid.`); |
| 15 | } |
| 16 | |
| 17 | const tables = Object.values(schema.tables); |
| 18 | |
| 19 | function validateForeignKey(key: ForeignKey) { |
| 20 | if ( |
| 21 | key.table === key.referencedTable && |
| 22 | (key.onUpdate !== "RESTRICT" || key.onDelete !== "RESTRICT") |
| 23 | ) { |
| 24 | throw new Error( |
| 25 | `[${key.name}] Due to the limitations of MSSQL & Prisma MongoDB, you cannot specify other foreign key actions than "RESTRICT" for self-referencing foreign keys.` |
| 26 | ); |
| 27 | } |
| 28 | |
| 29 | for (const col of key.columns) { |
| 30 | if ( |
| 31 | !col.isNullable && |
| 32 | (key.onUpdate === "SET NULL" || key.onDelete === "SET NULL") |
| 33 | ) { |
| 34 | throw new Error( |
| 35 | `[${key.name}] You are using "SET NULL" as foreign key action, but some columns are non-nullable.` |
| 36 | ); |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | function isCompositeColumnsUnique(table: AnyTable, columns: AnyColumn[]) { |
| 42 | if (columns.length === 1 && columns[0] instanceof IdColumn) return true; |
| 43 | |
| 44 | const columnNames = columns.map((col) => col.ormName); |
| 45 | for (const con of table.getUniqueConstraints()) { |
| 46 | if ( |
| 47 | deepEqual( |
| 48 | con.columns.map((col) => col.ormName), |
| 49 | columnNames |
| 50 | ) |
| 51 | ) |
| 52 | return true; |
| 53 | } |
| 54 | |
| 55 | return false; |
| 56 | } |
| 57 | |
| 58 | function validateRelation(relation: AnyRelation) { |
| 59 | if (!relation.implied && !relation.foreignKey) { |
| 60 | throw new Error( |
| 61 | `[${relation.name}] You must define foreign key for explicit relations due the limitations of Prisma.` |
| 62 | ); |
| 63 | } |
| 64 | |
| 65 | // ignore implied |
| 66 | if (relation.implied) return; |
| 67 | |
| 68 | if ( |
| 69 | relation.implying?.type === "one" && |
no test coverage detected