-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalterTableNameHelper.js
More file actions
53 lines (44 loc) · 1.56 KB
/
alterTableNameHelper.js
File metadata and controls
53 lines (44 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
const { AlterScriptDto } = require('../../types/AlterScriptDto');
const {
isParentContainerActivated,
isObjectInDeltaModelActivated,
getSchemaOfAlterCollection,
getSchemaNameFromCollection,
getNamePrefixedWithSchemaName,
} = require('../../../utils/general');
const { assignTemplates } = require('../../../utils/assignTemplates');
const templates = require('../../../ddlProvider/templates');
/**
* @param {string} oldTableName
* @param {string} newTableName
* @return string
*/
const alterTableName = (oldTableName, newTableName) => {
return assignTemplates({ template: templates.renameTable, templateData: { oldTableName, newTableName } });
};
/**
* @param {Object} collection
* @returns {Array<AlterScriptDto>}
*/
const getRenameTableScriptDtos = collection => {
const collectionSchema = getSchemaOfAlterCollection(collection);
const tableName = collectionSchema?.compMod?.collectionName;
if (!tableName) {
return [];
}
const { old: oldName, new: newName } = tableName;
if (!newName || newName === oldName) {
return [];
}
const schemaName = getSchemaNameFromCollection({ collection: collectionSchema });
const isContainerActivated = isParentContainerActivated(collection);
const isCollectionActivated = isContainerActivated && isObjectInDeltaModelActivated(collection);
const script = alterTableName(
getNamePrefixedWithSchemaName({ name: oldName, schemaName }),
getNamePrefixedWithSchemaName({ name: newName, schemaName }),
);
return [AlterScriptDto.getInstance([script], isCollectionActivated, false)];
};
module.exports = {
getRenameTableScriptDtos,
};