one more test passing

This commit is contained in:
Kononnable 2019-10-08 00:47:53 +02:00
parent 886147e4c0
commit 74ace5f7a6
3 changed files with 48 additions and 58 deletions

View File

@ -477,7 +477,12 @@ function applyNamingStrategy(
model.forEach(entity => {
entity.relations.forEach(relation => {
const oldName = relation.fieldName;
const newName = namingStrategy.relationName(relation, entity);
let newName = namingStrategy.relationName(relation, entity);
newName = TomgUtils.findNameForNewField(
newName,
entity,
oldName
);
const relatedEntity = model.find(
v => v.tscName === relation.relatedTable
@ -504,38 +509,18 @@ function applyNamingStrategy(
function changeColumnNames(model: Entity[]) {
model.forEach(entity => {
entity.columns.forEach(column => {
const newName = namingStrategy.columnName(
column.tscName,
column
const oldName = column.tscName;
let newName = namingStrategy.columnName(column.tscName, column);
newName = TomgUtils.findNameForNewField(
newName,
entity,
oldName
);
entity.indices.forEach(index => {
index.columns.map(column2 =>
column2 === column.tscName ? newName : column2
);
});
// TODO: Should relational columns be changed by both changeRelationNames and changeColumnNames?
// model.forEach(entity2 => {
// entity2.columns.forEach(column2 => {
// column2.relations
// .filter(
// relation =>
// relation.relatedTable === entity.tscName &&
// relation.relatedColumn === column.tscName
// )
// .forEach(v => {
// v.relatedColumn = newName;
// });
// column2.relations
// .filter(
// relation =>
// relation.relatedTable === entity.tscName &&
// relation.ownerColumn === column.tscName
// )
// .forEach(v => {
// v.ownerColumn = newName;
// });
// });
// });
column.tscName = newName;
});

View File

@ -1,4 +1,5 @@
import * as packagejson from "../package.json";
import { Entity } from "./models/Entity";
export function LogError(
errText: string,
@ -24,3 +25,30 @@ export function LogError(
export function packageVersion() {
return `${(packagejson as any).name}@${(packagejson as any).version}`;
}
export function findNameForNewField(
_fieldName: string,
entity: Entity,
columnOldName = ""
) {
let fieldName = _fieldName;
const validNameCondition = () =>
(entity.columns.every(v => v.tscName !== fieldName) &&
entity.relations.every(v => v.fieldName !== fieldName)) ||
(columnOldName && columnOldName === fieldName);
if (!validNameCondition()) {
fieldName += "_";
for (
let i = 2;
i <= entity.columns.length + entity.relations.length;
i++
) {
fieldName =
fieldName.substring(0, fieldName.length - i.toString().length) +
i.toString();
if (validNameCondition()) {
break;
}
}
}
return fieldName;
}

View File

@ -88,7 +88,9 @@ export default abstract class AbstractDriver {
entity.relations.length === 2 &&
entity.relations.every(
v => v.relationOptions && v.relationType !== "ManyToMany"
)
) &&
entity.relations[0].relatedTable !==
entity.relations[1].relatedTable
);
manyToManyEntities.forEach(junctionEntity => {
const firstEntity = dbModel.find(
@ -110,11 +112,11 @@ export default abstract class AbstractDriver {
firstRelation.relatedTable = secondEntity.tscName;
secondRelation.relatedTable = firstEntity.tscName;
firstRelation.fieldName = this.findNameForNewField(
firstRelation.fieldName = TomgUtils.findNameForNewField(
secondEntity.tscName,
firstEntity
);
secondRelation.fieldName = this.findNameForNewField(
secondRelation.fieldName = TomgUtils.findNameForNewField(
firstEntity.tscName,
secondEntity
);
@ -341,19 +343,19 @@ export default abstract class AbstractDriver {
}
let fieldName = "";
if (relationTmp.ownerColumns.length === 1) {
fieldName = AbstractDriver.findNameForNewField(
fieldName = TomgUtils.findNameForNewField(
ownerColumn.tscName,
ownerEntity
);
} else {
fieldName = AbstractDriver.findNameForNewField(
fieldName = TomgUtils.findNameForNewField(
relationTmp.relatedTable.tscName,
ownerEntity
);
}
ownerRelation = {
fieldName,
relatedField: AbstractDriver.findNameForNewField(
relatedField: TomgUtils.findNameForNewField(
relationTmp.ownerTable.tscName,
relationTmp.relatedTable
),
@ -389,31 +391,6 @@ export default abstract class AbstractDriver {
return entities;
}
private static findNameForNewField(_fieldName: string, entity: Entity) {
let fieldName = _fieldName;
const validNameCondition =
entity.columns.every(v => v.tscName !== fieldName) &&
entity.relations.every(v => v.fieldName !== fieldName);
if (!validNameCondition) {
fieldName += "_";
for (
let i = 2;
i <= entity.columns.length + entity.relations.length;
i++
) {
fieldName =
fieldName.substring(
0,
fieldName.length - i.toString().length
) + i.toString();
if (validNameCondition) {
break;
}
}
}
return fieldName;
}
public abstract async GetCoulmnsFromEntity(
entities: Entity[],
schema: string,