Tag History Restore after rename

I had to rename a folder that a large set of tags were in. I would like to preserve the history. Is there any easy way to do this?

Hi!

This depends on your definition of ‘easy’, but there is a way to do this, provided you are comfortable-ish in SQL and Python. Quick disclaimer: we will be directly editing the tag history database, which I do not recommend doing without the help of an IA employee or a good amount of experience with tag history. Always take a backup of your sql data before making any edits just to be safe.

Assuming all you did was rename a folder, the query will be quite simple. The tough part will be batch processing these queries so you don’t have to rename every entry once per tag. First, you’ll need to know the old path to your folder (before the rename), as well as the new path they currently reside at. Once you have this info, you’ll need to write a script like the following:

# I've got dummy values filled in here. You'll need to update the oldFolderName, oldPath, newFolderName, and datasource variables to reflect names on your system
oldFolderName = "motor 1"    #The old (pre-rename) folder name
oldPath = "historical/motors/motor 1"    #The path to the pre-rename folder
newFolderName = "motor 2"    #The new (current) folder name
datasource = "historySamples"    #The name of your database connection the historian is logging to

findFolderQuery = "SELECT tagpath FROM sqlth_te WHERE tagpath LIKE ?"
oldTagPaths = system.db.runPrepQuery(findFolderQuery, [oldPath + "/%"], datasource)

for row in oldTagPaths:
	oldFolderPath = row[0]
	newPathUpdate = "UPDATE sqlth_te SET tagpath = ? WHERE tagpath = ?"
	newFolderPath = oldFolderPath.replace(oldFolderName, newFolderName)
	system.db.runPrepUpdate(newPathUpdate, [newFolderPath, oldFolderPath], datasource)

If any of this looks foreign or makes you uncomfortable, I suggest emailing support before running this script. Be aware this is not the solution for all history problems! This will only fix a simple folder or tag rename.

Let me know how this goes!

2 Likes

Terrific! np, I can do this. I figured someone probably had it built already somewhere :slight_smile: Thank you!

Great! I just spotted one small problem and fixed it, make sure you copy the code I posted after refreshing this page :smiley:

1 Like