sqldirstate: extract the upgrade and downgrade logic

Summary: This is to make implementation of automatic upgrade easier.

Test Plan: Will add a test for it in one of the next diffs

Reviewers: #mercurial, durham, ttung

Subscribers: mjpieters

Differential Revision: https://phabricator.intern.facebook.com/D3356494
This commit is contained in:
Mateusz Kwapich 2016-05-27 14:54:48 -07:00
parent 576dc9525e
commit d19764dd10
2 changed files with 43 additions and 18 deletions

View File

@ -71,6 +71,33 @@ def wrapshelveaborttransaction(orig, repo):
else:
return orig(repo)
def upgrade(ui, repo):
if issqldirstate(repo):
raise error.Abort('repo already has sqldirstate')
wlock = repo.wlock()
try:
repo.dirstate._read()
tosql(repo.dirstate)
repo.requirements.add('sqldirstate')
repo._writerequirements()
repo.dirstate._opener.unlink('dirstate')
del repo.dirstate
finally:
wlock.release()
def downgrade(ui, repo):
if not issqldirstate(repo):
raise error.Abort('repo doesn\'t have sqldirstate')
wlock = repo.lock()
try:
toflat(repo.dirstate)
repo.requirements.remove('sqldirstate')
repo._writerequirements()
repo.dirstate._opener.unlink('dirstate.sqlite3')
del repo.dirstate
finally:
wlock.release()
def featuresetup(ui, supported):
# don't die on seeing a repo with the sqldirstate requirement
supported |= set(['sqldirstate'])
@ -92,25 +119,14 @@ def uisetup(ui):
# debug commands
cmdtable = {}
command = cmdutil.command(cmdtable)
@command('debugsqldirstate', [], 'hg debugsqldirstate [on|off]')
def debugsqldirstate(ui, repo, cmd, **opts):
""" migrate to sqldirstate """
if cmd == "on":
if 'sqldirstate' not in repo.requirements:
repo.dirstate._read()
tosql(repo.dirstate)
repo.requirements.add('sqldirstate')
repo._writerequirements()
repo.dirstate._opener.unlink('dirstate')
else:
raise error.Abort("sqldirstate is already enabled")
if cmd == "off":
if 'sqldirstate' in repo.requirements:
toflat(repo.dirstate)
repo.requirements.remove('sqldirstate')
repo._writerequirements()
repo.dirstate._opener.unlink('dirstate.sqlite3')
else:
raise error.Abort("sqldirstate is disabled")
upgrade(ui, repo)
elif cmd == "off":
downgrade(ui, repo)
else:
raise error.Abort("bad command")

View File

@ -416,7 +416,11 @@ def makedirstate(cls):
return sqldirstate
def tosql(dirstate):
# converts a flat dirstate to sqldirstate
"""" converts flat dirstate to sqldirstate
note: the sole responsibility of this function is to write the new dirstate
it's not touching anything but the DBFILE
"""
sqlfilename = dirstate._opener.join(DBFILE)
try:
os.unlink(sqlfilename)
@ -455,6 +459,11 @@ def tosql(dirstate):
sqlconn.commit()
def toflat(sqldirstate):
"""" converts sqldirstate to flat dirstate
note: the sole responsibility of this function is to write the new dirstate
it's not touching anything but the dirstate file
"""
# converts a sqldirstate to a flat one
st = sqldirstate._opener("dirstate", "w", atomictemp=True)
newmap = {}