Skip to content

Commit

Permalink
feat: add referenced database & schema to TableForeignKey
Browse files Browse the repository at this point in the history
  • Loading branch information
imnotjames committed Jul 21, 2021
1 parent ebf50b1 commit fff6b11
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/driver/aurora-data-api/AuroraDataApiQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1399,6 +1399,7 @@ export class AuroraDataApiQueryRunner extends BaseQueryRunner implements QueryRu
return new TableForeignKey({
name: dbForeignKey["CONSTRAINT_NAME"],
columnNames: foreignKeys.map(dbFk => dbFk["COLUMN_NAME"]),
referencedDatabase: dbForeignKey["REFERENCED_TABLE_SCHEMA"],
referencedTableName: referencedTableName,
referencedColumnNames: foreignKeys.map(dbFk => dbFk["REFERENCED_COLUMN_NAME"]),
onDelete: dbForeignKey["ON_DELETE"],
Expand Down
1 change: 1 addition & 0 deletions src/driver/cockroachdb/CockroachQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1665,6 +1665,7 @@ export class CockroachQueryRunner extends BaseQueryRunner implements QueryRunner
return new TableForeignKey({
name: dbForeignKey["constraint_name"],
columnNames: foreignKeys.map(dbFk => dbFk["column_name"]),
referencedSchema: dbForeignKey["referenced_table_schema"],
referencedTableName: referencedTableName,
referencedColumnNames: foreignKeys.map(dbFk => dbFk["referenced_column_name"]),
onDelete: dbForeignKey["on_delete"],
Expand Down
1 change: 1 addition & 0 deletions src/driver/mysql/MysqlQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1583,6 +1583,7 @@ export class MysqlQueryRunner extends BaseQueryRunner implements QueryRunner {
return new TableForeignKey({
name: dbForeignKey["CONSTRAINT_NAME"],
columnNames: foreignKeys.map(dbFk => dbFk["COLUMN_NAME"]),
referencedDatabase: dbForeignKey["REFERENCED_TABLE_SCHEMA"],
referencedTableName: referencedTableName,
referencedColumnNames: foreignKeys.map(dbFk => dbFk["REFERENCED_COLUMN_NAME"]),
onDelete: dbForeignKey["ON_DELETE"],
Expand Down
1 change: 1 addition & 0 deletions src/driver/oracle/OracleQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1438,6 +1438,7 @@ export class OracleQueryRunner extends BaseQueryRunner implements QueryRunner {
return new TableForeignKey({
name: dbForeignKey["CONSTRAINT_NAME"],
columnNames: foreignKeys.map(dbFk => dbFk["COLUMN_NAME"]),
referencedDatabase: table.database,
referencedTableName: dbForeignKey["REFERENCED_TABLE_NAME"],
referencedColumnNames: foreignKeys.map(dbFk => dbFk["REFERENCED_COLUMN_NAME"]),
onDelete: dbForeignKey["ON_DELETE"],
Expand Down
12 changes: 12 additions & 0 deletions src/driver/sap/SapQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,8 @@ export class SapQueryRunner extends BaseQueryRunner implements QueryRunner {
return new TableForeignKey({
name: dbForeignKey["CONSTRAINT_NAME"],
columnNames: foreignKeys.map(dbFk => dbFk["COLUMN_NAME"]),
referencedDatabase: newTable.database,
referencedSchema: newTable.schema,
referencedTableName: newTable.name, // we use renamed table name
referencedColumnNames: foreignKeys.map(dbFk => dbFk["REFERENCED_COLUMN_NAME"]),
onDelete: dbForeignKey["DELETE_RULE"] === "RESTRICT" ? "NO ACTION" : dbForeignKey["DELETE_RULE"],
Expand Down Expand Up @@ -614,6 +616,8 @@ export class SapQueryRunner extends BaseQueryRunner implements QueryRunner {
return new TableForeignKey({
name: dbForeignKey["CONSTRAINT_NAME"],
columnNames: foreignKeys.map(dbFk => dbFk["COLUMN_NAME"]),
referencedDatabase: table.database,
referencedSchema: table.schema,
referencedTableName: table.name,
referencedColumnNames: foreignKeys.map(dbFk => dbFk["REFERENCED_COLUMN_NAME"]),
onDelete: dbForeignKey["DELETE_RULE"] === "RESTRICT" ? "NO ACTION" : dbForeignKey["DELETE_RULE"],
Expand Down Expand Up @@ -953,6 +957,8 @@ export class SapQueryRunner extends BaseQueryRunner implements QueryRunner {
return new TableForeignKey({
name: dbForeignKey["CONSTRAINT_NAME"],
columnNames: foreignKeys.map(dbFk => dbFk["COLUMN_NAME"]),
referencedDatabase: table.database,
referencedSchema: table.schema,
referencedTableName: table.name,
referencedColumnNames: foreignKeys.map(dbFk => dbFk["REFERENCED_COLUMN_NAME"]),
onDelete: dbForeignKey["DELETE_RULE"] === "RESTRICT" ? "NO ACTION" : dbForeignKey["DELETE_RULE"],
Expand Down Expand Up @@ -1090,6 +1096,8 @@ export class SapQueryRunner extends BaseQueryRunner implements QueryRunner {
return new TableForeignKey({
name: dbForeignKey["CONSTRAINT_NAME"],
columnNames: foreignKeys.map(dbFk => dbFk["COLUMN_NAME"]),
referencedDatabase: table.database,
referencedSchema: table.schema,
referencedTableName: table.name,
referencedColumnNames: foreignKeys.map(dbFk => dbFk["REFERENCED_COLUMN_NAME"]),
onDelete: dbForeignKey["DELETE_RULE"] === "RESTRICT" ? "NO ACTION" : dbForeignKey["DELETE_RULE"],
Expand Down Expand Up @@ -1158,6 +1166,8 @@ export class SapQueryRunner extends BaseQueryRunner implements QueryRunner {
return new TableForeignKey({
name: dbForeignKey["CONSTRAINT_NAME"],
columnNames: foreignKeys.map(dbFk => dbFk["COLUMN_NAME"]),
referencedDatabase: table.database,
referencedSchema: table.schema,
referencedTableName: table.name,
referencedColumnNames: foreignKeys.map(dbFk => dbFk["REFERENCED_COLUMN_NAME"]),
onDelete: dbForeignKey["DELETE_RULE"] === "RESTRICT" ? "NO ACTION" : dbForeignKey["DELETE_RULE"],
Expand Down Expand Up @@ -1673,6 +1683,8 @@ export class SapQueryRunner extends BaseQueryRunner implements QueryRunner {
return new TableForeignKey({
name: dbForeignKey["CONSTRAINT_NAME"],
columnNames: foreignKeys.map(dbFk => dbFk["COLUMN_NAME"]),
referencedDatabase: table.database,
referencedSchema: dbForeignKey["REFERENCED_SCHEMA_NAME"],
referencedTableName: referencedTableName,
referencedColumnNames: foreignKeys.map(dbFk => dbFk["REFERENCED_COLUMN_NAME"]),
onDelete: dbForeignKey["DELETE_RULE"] === "RESTRICT" ? "NO ACTION" : dbForeignKey["DELETE_RULE"],
Expand Down
2 changes: 2 additions & 0 deletions src/driver/sqlserver/SqlServerQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1891,6 +1891,8 @@ export class SqlServerQueryRunner extends BaseQueryRunner implements QueryRunner
return new TableForeignKey({
name: dbForeignKey["FK_NAME"],
columnNames: foreignKeys.map(dbFk => dbFk["COLUMN_NAME"]),
referencedDatabase: dbForeignKey["TABLE_CATALOG"],
referencedSchema: dbForeignKey["REF_SCHEMA"],
referencedTableName: referencedTableName,
referencedColumnNames: foreignKeys.map(dbFk => dbFk["REF_COLUMN"]),
onDelete: dbForeignKey["ON_DELETE"].replace("_", " "), // SqlServer returns NO_ACTION, instead of NO ACTION
Expand Down
2 changes: 1 addition & 1 deletion src/schema-builder/RdbmsSchemaBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ export class RdbmsSchemaBuilder implements SchemaBuilder {
if (newKeys.length === 0)
continue;

const dbForeignKeys = newKeys.map(foreignKeyMetadata => TableForeignKey.create(foreignKeyMetadata));
const dbForeignKeys = newKeys.map(foreignKeyMetadata => TableForeignKey.create(foreignKeyMetadata, this.connection.driver));
this.connection.logger.logSchemaBuild(`creating a foreign keys: ${newKeys.map(key => key.name).join(", ")} on table "${table.name}"`);
await this.queryRunner.createForeignKeys(table, dbForeignKeys);
}
Expand Down
10 changes: 10 additions & 0 deletions src/schema-builder/options/TableForeignKeyOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ export interface TableForeignKeyOptions {
*/
columnNames: string[];

/**
* Database of the Table referenced in the foreign key.
*/
referencedDatabase?: string;

/**
* Schema of the Table referenced in the foreign key.
*/
referencedSchema?: string;

/**
* Table referenced in the foreign key.
*/
Expand Down
6 changes: 5 additions & 1 deletion src/schema-builder/table/Table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,11 @@ export class Table {
this.indices = options.indices.map(index => new TableIndex(index));

if (options.foreignKeys)
this.foreignKeys = options.foreignKeys.map(foreignKey => new TableForeignKey(foreignKey));
this.foreignKeys = options.foreignKeys.map(foreignKey => new TableForeignKey({
...foreignKey,
referencedDatabase: foreignKey?.referencedDatabase || options.database,
referencedSchema: foreignKey?.referencedSchema || options.schema,
}));

if (options.uniques)
this.uniques = options.uniques.map(unique => new TableUnique(unique));
Expand Down
19 changes: 18 additions & 1 deletion src/schema-builder/table/TableForeignKey.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {ForeignKeyMetadata} from "../../metadata/ForeignKeyMetadata";
import {TableForeignKeyOptions} from "../options/TableForeignKeyOptions";
import { Driver } from "../../driver/Driver";

/**
* Foreign key from the database stored in this class.
Expand All @@ -20,6 +21,16 @@ export class TableForeignKey {
*/
columnNames: string[] = [];

/**
* Database of Table referenced in the foreign key.
*/
referencedDatabase?: string;

/**
* Database of Table referenced in the foreign key.
*/
referencedSchema?: string;

/**
* Table referenced in the foreign key.
*/
Expand Down Expand Up @@ -56,6 +67,8 @@ export class TableForeignKey {
this.name = options.name;
this.columnNames = options.columnNames;
this.referencedColumnNames = options.referencedColumnNames;
this.referencedDatabase = options.referencedDatabase;
this.referencedSchema = options.referencedSchema;
this.referencedTableName = options.referencedTableName;
this.onDelete = options.onDelete;
this.onUpdate = options.onUpdate;
Expand All @@ -74,6 +87,8 @@ export class TableForeignKey {
name: this.name,
columnNames: [...this.columnNames],
referencedColumnNames: [...this.referencedColumnNames],
referencedDatabase: this.referencedDatabase,
referencedSchema: this.referencedSchema,
referencedTableName: this.referencedTableName,
onDelete: this.onDelete,
onUpdate: this.onUpdate,
Expand All @@ -88,11 +103,13 @@ export class TableForeignKey {
/**
* Creates a new table foreign key from the given foreign key metadata.
*/
static create(metadata: ForeignKeyMetadata): TableForeignKey {
static create(metadata: ForeignKeyMetadata, driver: Driver): TableForeignKey {
return new TableForeignKey(<TableForeignKeyOptions>{
name: metadata.name,
columnNames: metadata.columnNames,
referencedColumnNames: metadata.referencedColumnNames,
referencedDatabase: metadata.referencedEntityMetadata.database,
referencedSchema: metadata.referencedEntityMetadata.schema,
referencedTableName: metadata.referencedTablePath,
onDelete: metadata.onDelete,
onUpdate: metadata.onUpdate,
Expand Down

0 comments on commit fff6b11

Please sign in to comment.