Skip to content

Commit

Permalink
feat: migrations for 0.6.7 to 1.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
kentbull committed Oct 8, 2024
1 parent 480d608 commit b50993d
Show file tree
Hide file tree
Showing 7 changed files with 279 additions and 186 deletions.
162 changes: 0 additions & 162 deletions src/keri/app/cli/commands/migrate.py

This file was deleted.

4 changes: 2 additions & 2 deletions src/keri/app/cli/commands/migrate/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

def handler(args):
"""
Launch KERI database initialization
List local LMDB database migrations and their completion status
Args:
args(Namespace): arguments object from command line
Expand All @@ -25,7 +25,7 @@ def handler(args):
return [lister]


parser = argparse.ArgumentParser(description='Cleans and migrates a database and keystore')
parser = argparse.ArgumentParser(description='Lists the local LMDB migrations and their completion status')
parser.set_defaults(handler=handler,
transferable=True)

Expand Down
14 changes: 7 additions & 7 deletions src/keri/app/cli/commands/migrate/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@

def handler(args):
"""
Launch KERI database initialization
Launch KERI database migrator
Args:
args(Namespace): arguments object from command line
"""
clean = MigrateDoer(args)
return [clean]
migrator = MigrateDoer(args)
return [migrator]


parser = argparse.ArgumentParser(description='Cleans and migrates a database and keystore')
parser = argparse.ArgumentParser(description='Cleans and migrates a database and keystore up to the latest source code version')
parser.set_defaults(handler=handler,
transferable=True)

Expand Down Expand Up @@ -60,8 +60,8 @@ def recur(self, tyme):
except kering.DatabaseError:
pass

print("Migrating...")
db.migrate()
print("Finished")
print(f"Migrating {self.args.name}...")
db.migrate(name=self.args.name, base=self.args.base, temp=self.args.temp)
print(f"Finished migrating {self.args.name}")

return True
33 changes: 23 additions & 10 deletions src/keri/db/basing.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@


MIGRATIONS = [
("1.1.0", ["rekey_habs"])
("0.6.8", ["hab_data_rename"]),
("1.0.0", ["hab_data_rename", "add_key_and_reg_state_schemas"]),
("1.2.0", ["hab_data_rename", "add_key_and_reg_state_schemas", "rekey_habs"])
]


Expand Down Expand Up @@ -1335,7 +1337,7 @@ def reload(self):
for keys in removes: # remove bare .habs records
self.habs.rem(keys=keys)

def migrate(self):
def migrate(self, name, base, temp):
""" Run all migrations required
Run all migrations that are required from the current version of database up to the current version
Expand All @@ -1346,24 +1348,32 @@ def migrate(self):
"""
for (version, migrations) in MIGRATIONS:
# Check to see if this is for an older version
# Only run migration if current source code version is at or below the migration version
ver = semver.VersionInfo.parse(keri.__version__)
ver_no_prerelease = semver.Version(ver.major, ver.minor, ver.patch)
if self.version is not None and semver.compare(version, ver_no_prerelease) > 0:
print(
f"Skipping migration {version} as higher than the current KERI version {keri.__version__}")
continue
# Skip migrations already run - where version less than (-1) or equal to (0) database version
if self.version is not None and semver.compare(version, self.version) != 1:
continue

print(f"Migrating database v{self.version} --> v{version}")
for migration in migrations:
modName = f"keri.db.migrations.{migration}"
if self.migs.get(keys=(migration,)) is not None:
continue

mod = importlib.import_module(modName)
try:
print(f"running migration {modName}")
mod.migrate(self)
mod.migrate(self, name=name, base=base, temp=temp)
except Exception as e:
print(f"\nAbandoning migration {migration} with error: {e}")
return

self.migs.pin(keys=(migration,), val=coring.Dater())
self.version = version # update database version after successful migration

self.version = keri.__version__

Expand Down Expand Up @@ -1407,12 +1417,15 @@ def complete(self, name=None):
migrations = []
if not name:
for version, migs in MIGRATIONS:
for mig in migs:
dater = self.migs.get(keys=(mig,))
migrations.append((mig, dater))
# Print entries only for migrations that have been run
if self.version is not None and semver.compare(version, self.version) <= 0:
for mig in migs:
dater = self.migs.get(keys=(mig,))
migrations.append((mig, dater))
else:
if name not in MIGRATIONS or not self.migs.get(keys=(name,)):
raise ValueError(f"No migration named {name}")
for version, migs in MIGRATIONS: # check all migrations for each version
if name not in migs or not self.migs.get(keys=(name,)):
raise ValueError(f"No migration named {name}")
migrations.append((name, self.migs.get(keys=(name,))))

return migrations
Expand Down
Loading

0 comments on commit b50993d

Please sign in to comment.