Skip to content

Commit 0c469ba

Browse files
committed
deployment deletes production items
determines if item is PTD based on external name
1 parent 2398ac0 commit 0c469ba

File tree

3 files changed

+119
-28
lines changed

3 files changed

+119
-28
lines changed

cls/SourceControl/Git/Production.cls

Lines changed: 54 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ ClassMethod ExportPTD(internalName As %String, nameMethod) As %Status
101101
/// exports the PTD for this item to the file system under the directory specified
102102
ClassMethod ExportConfigItemSettings(productionClass As %String, item As %RegisteredObject, nameMethod As %String) As %Status
103103
{
104-
Set internalName = productionClass_"||Settings-"_item.Name_"|"_item.ClassName_".PTD"
104+
set internalName = ..CreateInternalName(productionClass, item.Name, item.ClassName, 0)
105105
Set externalName = $ClassMethod($$SrcCtrlCls^%buildccr, nameMethod, internalName)
106106
Set filename = ##class(%File).NormalizeFilename(externalName)
107107
set st = ##class(Ens.Deployment.Utils).CreatePTDFromItem(.item, .ptdName)
@@ -115,7 +115,7 @@ ClassMethod ExportConfigItemSettings(productionClass As %String, item As %Regist
115115
/// class name
116116
ClassMethod ExportProductionSettings(productionClass As %String, nameMethod As %String) As %Status
117117
{
118-
Set internalName = productionClass_"||ProductionSettings-"_productionClass_".PTD"
118+
set internalName = ..CreateInternalName(productionClass,,,0)
119119
Set class = ##class(%Dictionary.CompiledClass).%OpenId(productionClass)
120120
Set sc = ##class(Ens.Deployment.Utils).CreatePTDFromProduction(class, .ptdName)
121121
If $$$ISERR(sc) {
@@ -153,11 +153,11 @@ ClassMethod GetModifiedItemsBeforeSave(internalName, Location, Output modifiedIt
153153
}
154154
set modifiedInternalName = ""
155155
if $isobject(modifiedItem) {
156-
set modifiedInternalName = productionName _ "||Settings-" _ modifiedItem.Name _ "|" _ modifiedItem.ClassName _ ".PTD"
156+
set modifiedInternalName = ..CreateInternalName(productionName, modifiedItem.Name, modifiedItem.ClassName, 0)
157157
} else {
158158
// cannot check %IsModified on production config settings because they are not actually modified at this point.
159159
// workaround: just assume any change not to a specific item is to the production settings
160-
set modifiedInternalName = productionName _ "||ProductionSettings-" _ productionName _ ".PTD"
160+
set modifiedInternalName = ..CreateInternalName(productionName,,,1)
161161
}
162162
}
163163
if (modifiedInternalName '= "") {
@@ -193,14 +193,14 @@ ClassMethod GetModifiedItemsAfterSave(internalName, Output modifiedItems)
193193
$$$ThrowSQLIfError(rs.%SQLCODE, rs.%Message)
194194
while rs.%Next() {
195195
if '$get(^mtempsscProd($job,"items", $listbuild(rs.Name, rs.ClassName))) {
196-
set itemInternalName = productionName _ "||Settings-" _ rs.Name _ "|" _ rs.ClassName _ ".PTD"
196+
set itemInternalName = ..CreateInternalName(productionName, rs.Name, rs.ClassName, 0)
197197
set modifiedItems(itemInternalName) = "A"
198198
}
199199
kill ^mtempsscProd($job,"items", $listbuild(rs.Name, rs.ClassName))
200200
}
201201
set key = $order(^mtempsscProd($job,"items",""))
202202
while (key '= "") {
203-
set itemInternalName = productionName _ "||Settings-" _ $listget(key,1) _ "|" _ $listget(key,2) _ ".PTD"
203+
set itemInternalName = ..CreateInternalName(productionName, $listget(key,1), $listget(key,2), 0)
204204
set modifiedItems(itemInternalName) = "D"
205205
set key = $order(^mtempsscProd($job,"items",key))
206206
}
@@ -242,23 +242,25 @@ ClassMethod IsProductionClass(className As %String, nameMethod As %String) As %B
242242
return 0
243243
}
244244

245-
/// Given a file name for a PTD item, returns a suggested internal name.
246-
ClassMethod ParseExternalName(externalName, Output internalName)
245+
/// Given a file name for a PTD item, returns a suggested internal name. This method assumes that the file exists on disk.
246+
ClassMethod ParseExternalName(externalName, Output internalName = "", Output productionName = "")
247247
{
248-
set file = $piece(externalName, "/", *)
249-
set deployDoc = ##class(EnsLib.EDI.XML.Document).%New(externalName)
250-
set exportNotesPTDText = $ZCVT(deployDoc.GetValueAt("/Export/Document[1]/1"),"I","XML")
251-
set exportNotesPTD = ##class(EnsLib.EDI.XML.Document).%New(exportNotesPTDText)
252-
set productionName = exportNotesPTD.GetValueAt("/Deployment/Creation/SourceProduction")
253-
if $extract(file,1,9) = "ProdStgs-" {
254-
set internalName = productionName_"||ProductionSettings-"_productionName_".PTD"
255-
} else {
256-
// Special case for Config Item Settings PTD, requires checking PTD CDATA for Item and Class name
257-
set settingsPTDText = $zconvert(deployDoc.GetValueAt("/Export/Document[2]/1"),"I","XML")
258-
set settingsPTD = ##class(EnsLib.EDI.XML.Document).%New(settingsPTDText)
259-
set itemClass = settingsPTD.GetValueAt("/Item/@ClassName")
260-
set itemName = settingsPTD.GetValueAt("/Item/@Name")
261-
set internalName = productionName_"||Settings-"_itemName_"|"_itemClass_".PTD"
248+
if ##class(%File).Exists(externalName) {
249+
set file = $piece(externalName, "/", *)
250+
set deployDoc = ##class(EnsLib.EDI.XML.Document).%New(externalName)
251+
set exportNotesPTDText = $ZCVT(deployDoc.GetValueAt("/Export/Document[1]/1"),"I","XML")
252+
set exportNotesPTD = ##class(EnsLib.EDI.XML.Document).%New(exportNotesPTDText)
253+
set productionName = exportNotesPTD.GetValueAt("/Deployment/Creation/SourceProduction")
254+
if $extract(file,1,9) = "ProdStgs-" {
255+
set internalName = ..CreateInternalName(productionName,,,1)
256+
} else {
257+
// Special case for Config Item Settings PTD, requires checking PTD CDATA for Item and Class name
258+
set settingsPTDText = $zconvert(deployDoc.GetValueAt("/Export/Document[2]/1"),"I","XML")
259+
set settingsPTD = ##class(EnsLib.EDI.XML.Document).%New(settingsPTDText)
260+
set itemClass = settingsPTD.GetValueAt("/Item/@ClassName")
261+
set itemName = settingsPTD.GetValueAt("/Item/@Name")
262+
set internalName = ..CreateInternalName(productionName, itemName, itemClass, 0)
263+
}
262264
}
263265
}
264266

@@ -288,6 +290,36 @@ ClassMethod ParseInternalName(internalName, noFolders As %Boolean = 0, Output fi
288290
set fileName = $translate($replace(name, ".", "_") _ ".xml", "\", "/")
289291
}
290292

293+
/// Calculates the internal name for a decomposed production item
294+
ClassMethod CreateInternalName(productionName = "", itemName = "", itemClassName = "", isProductionSettings As %Boolean = 0)
295+
{
296+
return $select(
297+
isProductionSettings: productionName_"||ProductionSettings-"_productionName_".PTD",
298+
1: productionName _ "||Settings-" _ itemName _ "|" _ itemClassName _ ".PTD"
299+
)
300+
}
301+
302+
/// Given an external name for a PTD item, removes that item from the production.
303+
ClassMethod RemoveItemByExternalName(externalName, nameMethod) As %Status
304+
{
305+
set sc = $$$OK
306+
set productionName = $replace($piece($replace(externalName,"\","/"),"/",*-1),"_",".")
307+
set production = ##class(Ens.Config.Production).%OpenId(productionName,,.sc)
308+
$$$QuitOnError(sc)
309+
set itemToRemove = $$$NULLOREF
310+
for i=1:1:production.Items.Count() {
311+
set configItem = production.Items.GetAt(i)
312+
set itemInternalName = ..CreateInternalName(productionName, configItem.Name, configItem.ClassName)
313+
set itemExternalName = $classmethod($$SrcCtrlCls^%buildccr, nameMethod, itemInternalName)
314+
if itemExternalName = externalName {
315+
set itemToRemove = configItem
316+
quit
317+
}
318+
}
319+
do production.RemoveItem(itemToRemove)
320+
return production.%Save()
321+
}
322+
291323
/// Given an internal name for a PTD item, removes that item from the production.
292324
ClassMethod RemoveItem(internalName, noFolders As %Boolean = 0) As %Status
293325
{

cls/SourceControl/Git/PullEventHandler/IncrementalLoad.cls

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Method OnPull() As %Status
1717
if ((internalName = "") && (..ModifiedFiles(i).changeType '= "D")) {
1818
write !, ..ModifiedFiles(i).externalName, " was not imported into the database and will not be compiled. "
1919
} elseif (..ModifiedFiles(i).changeType = "D") {
20-
set sc = ..DeleteFile(internalName)
20+
set sc = ..DeleteFile(internalName, ..ModifiedFiles(i).externalName)
2121
if sc {
2222
write !, ..ModifiedFiles(i).externalName, " was deleted."
2323
} else {
@@ -38,10 +38,13 @@ Method OnPull() As %Status
3838
quit $system.OBJ.CompileList(.compilelist, "ck")
3939
}
4040

41-
Method DeleteFile(item As %String) As %Status
41+
Method DeleteFile(item As %String = "", externalName As %String = "") As %Status
4242
{
4343
set sc = $$$OK
44-
set type = ##class(SourceControl.Git.Utils).Type(item)
44+
set type = $select(
45+
..ItemIsPTD(externalName): "ptd",
46+
1: ##class(SourceControl.Git.Utils).Type(item)
47+
)
4548
set name = ##class(SourceControl.Git.Utils).NameWithoutExtension(item)
4649
set settings = ##class(SourceControl.Git.Settings).%New()
4750
set deleted = 1
@@ -54,8 +57,9 @@ Method DeleteFile(item As %String) As %Status
5457
}elseif type = "csp" {
5558
set sc = $System.CSP.DeletePage(item)
5659
} elseif settings.decomposeProductions && (type = "ptd") {
60+
set normalizedFilePath = ##class(%File).NormalizeFilename(##class(SourceControl.Git.Utils).TempFolder()_externalName)
5761
set sc = ##class(%SYSTEM.Status).AppendStatus(
58-
##class(SourceControl.Git.Production).RemoveItem(item),
62+
##class(SourceControl.Git.Production).RemoveItemByExternalName(normalizedFilePath,"FullExternalName"),
5963
##class(%Library.RoutineMgr).Delete(item)
6064
)
6165
}elseif ##class(SourceControl.Git.Utils).UserTypeCached(item) {
@@ -65,8 +69,10 @@ Method DeleteFile(item As %String) As %Status
6569
}
6670

6771
if deleted && $$$ISOK(sc) {
68-
do ##class(SourceControl.Git.Utils).RemoveRoutineTSH(item)
69-
kill $$$TrackedItems(##class(SourceControl.Git.Utils).NormalizeExtension(item))
72+
if (item '= "") {
73+
do ##class(SourceControl.Git.Utils).RemoveRoutineTSH(item)
74+
kill $$$TrackedItems(##class(SourceControl.Git.Utils).NormalizeExtension(item))
75+
}
7076
} else {
7177
if +$system.Status.GetErrorCodes(sc) = $$$ClassDoesNotExist {
7278
// if something we wanted to delete is already deleted -- good!
@@ -76,4 +82,23 @@ Method DeleteFile(item As %String) As %Status
7682
return sc
7783
}
7884

85+
/// Determines whether an item has type PTD based on the external name, not reliant on the file existing
86+
ClassMethod ItemIsPTD(externalName) As %Boolean
87+
{
88+
if $zconvert($piece(externalName,".",*),"l") '= "xml" {
89+
return 0
90+
}
91+
set settings = ##class(SourceControl.Git.Settings).%New()
92+
set normFilePath = $replace(externalName,"\","/")
93+
set key = $order($$$SourceMapping("PTD",""))
94+
while (key '= "") {
95+
set directory = $replace($$$SourceMapping("PTD",key), "\","/")
96+
if $find(normFilePath, directory) = ($length(directory) + 1) {
97+
return 1
98+
}
99+
set key = $order($$$SourceMapping("PTD",key))
100+
}
101+
return 0
102+
}
103+
79104
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
Include SourceControl.Git
2+
3+
Class UnitTest.SourceControl.Git.PullEventHandler.IncrementalLoad Extends %UnitTest.TestCase
4+
{
5+
6+
Property Mappings [ MultiDimensional ];
7+
8+
Method TestItemIsPTD()
9+
{
10+
do $$$AssertNotTrue(##class(SourceControl.Git.PullEventHandler.IncrementalLoad).ItemIsPTD("cls/test.xml"))
11+
do $$$AssertNotTrue(##class(SourceControl.Git.PullEventHandler.IncrementalLoad).ItemIsPTD("ptd/test.md"))
12+
do $$$AssertNotTrue(##class(SourceControl.Git.PullEventHandler.IncrementalLoad).ItemIsPTD(""))
13+
do $$$AssertTrue(##class(SourceControl.Git.PullEventHandler.IncrementalLoad).ItemIsPTD("ptd/test.xml"))
14+
do $$$AssertTrue(##class(SourceControl.Git.PullEventHandler.IncrementalLoad).ItemIsPTD("ptd2/test.xml"))
15+
do $$$AssertTrue(##class(SourceControl.Git.PullEventHandler.IncrementalLoad).ItemIsPTD("ptd2\test.xml"))
16+
}
17+
18+
Method OnBeforeAllTests() As %Status
19+
{
20+
merge ..Mappings = @##class(SourceControl.Git.Utils).MappingsNode()
21+
kill @##class(SourceControl.Git.Utils).MappingsNode()
22+
set $$$SourceMapping("PTD", "*") = "ptd/"
23+
set $$$SourceMapping("PTD", "Some.Production") = "ptd2/"
24+
quit $$$OK
25+
}
26+
27+
Method %OnClose() As %Status
28+
{
29+
kill @##class(SourceControl.Git.Utils).MappingsNode()
30+
merge @##class(SourceControl.Git.Utils).MappingsNode() = ..Mappings
31+
quit $$$OK
32+
}
33+
34+
}

0 commit comments

Comments
 (0)