Got it!! I wrote a Python script to do everything I want:
import re, sqlite3, sys
def list(c):
c.execute('select tags.name from tags')
print 'Current tags:',
tag_list = []
for row in c:
tag_list.append(row[0])
print ', '.join(tag_list)
def rename(c, old, new):
c.execute('select tags.name from tags')
tag_list = []
for row in c:
tag_list.append(row[0])
if new in tag_list:
print 'Cannot rename "' + old + '" to "' + new + '" -- tag exists'
sys.exit(0)
print 'Renaming "' + old + '" to "' + new + '" ...'
# rename tag in all projects' default tags
c.execute('select * from projects where default_tags like "%' + old + '%"')
for row in c:
project_id = row[0]
default_tags = row[10]
match = re.search('(^|(, ))' + old + '((, )|$)', default_tags)
if match:
default_tags = default_tags.replace(match.group(), match.group().replace(old, new)) # form new default tags
c.execute('update projects set default_tags="' + default_tags + '" where id="' + str(project_id) + '"') # update default tags
c.execute('update tags set name="' + new + '" where name="' + old + '"')
def remove(c, tag):
print 'Removing tag "' + tag + '" ...'
# delete all taggings involving tag
c.execute('delete from taggings where id in (select taggings.id from tags,taggings where tags.id=taggings.tag_id and tags.name="' + tag + '")');
# delete tag from all projects' default tags
c.execute('select * from projects where default_tags like "%' + tag + '%"')
for row in c:
project_id = row[0]
default_tags = row[10]
match = re.search('(^|(, ))' + tag + '((, )|$)', default_tags)
if match:
if match.group().startswith(', ') and match.group().endswith(', '):
default_tags = default_tags.replace(match.group(), ', ')
else:
default_tags = default_tags.replace(match.group(), '')
c.execute('update projects set default_tags="' + default_tags + '" where id="' + str(project_id) + '"') # update default tags
c.execute('delete from tags where name="' + tag + '"') # delete tag
if __name__ == '__main__':
if len(sys.argv) < 2:
sys.exit(0)
conn = sqlite3.connect('/home/hanna/tracks/db/tracks-20-blank.sqlite3.db') # open the database
if sys.argv[1] == 'list':
if len(sys.argv) != 2:
sys.exit(0)
list(conn.cursor())
elif sys.argv[1] == 'rename':
if len(sys.argv) != 4:
sys.exit(0)
rename(conn.cursor(), sys.argv[2], sys.argv[3])
elif sys.argv[1] == 'remove':
if len(sys.argv) != 3:
sys.exit(0)
remove(conn.cursor(), sys.argv[2])
else:
sys.exit(0)
# commit changes so other connections can see them and close the db
conn.commit()
conn.close()