Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: migrations for 0.6.7 to 1.2.0 #875

Merged
merged 2 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
12 changes: 6 additions & 6 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...")
print(f"Migrating {self.args.name}...")
db.migrate()
print("Finished")
print(f"Finished migrating {self.args.name}")

return True
42 changes: 29 additions & 13 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", ["add_key_and_reg_state_schemas"]),
("1.2.0", ["rekey_habs"])
]


Expand Down Expand Up @@ -1312,7 +1314,7 @@ def reload(self):
"""
# Check migrations to see if this database is up to date. Error otherwise
if not self.current:
raise kering.DatabaseError("Database migrations must be run.")
raise kering.DatabaseError(f"Database migrations must be run. DB version {self.version}; current {keri.__version__}")

removes = []
for keys, data in self.habs.getItemIter():
Expand Down Expand Up @@ -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)
except Exception as e:
print(f"\nAbandoning migration {migration} with error: {e}")
print(f"\nAbandoning migration {migration} at version {version} 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 All @@ -1382,13 +1392,16 @@ def current(self):
return True

# If database version is ahead of library version, throw exception
if self.version is not None and semver.compare(self.version, keri.__version__) == 1:
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(self.version, ver_no_prerelease) == 1:
raise kering.ConfigurationError(
f"Database version={self.version} is ahead of library version={keri.__version__}")

last = MIGRATIONS[-1]
# If we aren't at latest version, but there are no outstanding migrations, reset version to latest
if self.migs.get(keys=(last[1][0],)) is not None:
# If we aren't at latest version, but there are no outstanding migrations,
# reset version to latest (rightmost (-1) migration is latest)
if self.migs.get(keys=(last[1][-1],)) is not None:
return True

# We have migrations to run
Expand All @@ -1407,12 +1420,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
Loading