-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
200 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -129,3 +129,4 @@ dmypy.json | |
.pyre/ | ||
|
||
.idea/ | ||
/jarvis/logs/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
worker: pyttman runclient jarvis | ||
worker: pyttman runfile jarvis jarvis/migrations/migrate.py upgrade && pyttman runclient jarvis |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
jarvis/migrations/00000001_change_list_field_to_string_field_recipe.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from jarvis.abilities.recipes.models import Recipe | ||
|
||
__doc__ = "Change the name field in Recipe to Text field instead of List field" | ||
|
||
|
||
def upgrade(): | ||
for recipe in Recipe.objects.all(): | ||
if isinstance(recipe.name, list): | ||
recipe.name = " ".join(recipe.name) | ||
recipe.save() | ||
|
||
|
||
def downgrade(): | ||
for recipe in Recipe.objects.all(): | ||
if isinstance(recipe.name, str): | ||
recipe.name = recipe.name.split() | ||
recipe.save() |
20 changes: 20 additions & 0 deletions
20
jarvis/migrations/00000002_add_comment_string_field_to_recipe.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from mongoengine import StringField | ||
from jarvis.abilities.recipes.models import Recipe | ||
|
||
__doc__ = " Assign a 'comment' field to the Recipe model." | ||
|
||
|
||
def upgrade(): | ||
Recipe.comment = StringField(required=False) | ||
for recipe in Recipe.objects.all(): | ||
if hasattr(recipe, "comment"): | ||
continue | ||
recipe.comment = "" | ||
recipe.save() | ||
|
||
|
||
def downgrade(): | ||
for recipe in Recipe.objects.all(): | ||
if hasattr(recipe, "comment"): | ||
del recipe.comment | ||
recipe.save() |
24 changes: 24 additions & 0 deletions
24
jarvis/migrations/00000003_add_comment_string_field_to_debt.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from mongoengine import StringField | ||
from jarvis.abilities.finance.models import Debt | ||
|
||
column_to_add = StringField(required=False) | ||
column_name = "comment" | ||
|
||
__doc__ = "Assign a 'comment' field to the Debt model." | ||
|
||
|
||
def upgrade(): | ||
Debt.comment = column_to_add | ||
|
||
for debt in Debt.objects.all(): | ||
if hasattr(debt, column_name): | ||
continue | ||
debt.comment = "" | ||
debt.save() | ||
|
||
|
||
def downgrade(): | ||
for debt in Debt.objects.all(): | ||
if hasattr(debt, column_name): | ||
del debt.comment | ||
debt.save() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import inspect | ||
import re | ||
import sys | ||
from importlib import import_module | ||
from pathlib import Path | ||
|
||
from pyttman import app | ||
from jarvis.models import MigrationVersion | ||
|
||
|
||
if __name__ == "__main__": | ||
# Set the path base dir to the current working directory | ||
# so that the migrations module can be imported. | ||
|
||
if (version_cursor := MigrationVersion.objects.first()) is None: | ||
version_cursor = MigrationVersion.objects.create() | ||
|
||
migrations_dir = Path(app.settings.APP_BASE_DIR) / "migrations" | ||
if not migrations_dir.exists(): | ||
raise FileNotFoundError(f"Could not find migrations directory: " | ||
f"{migrations_dir.as_posix()}") | ||
|
||
sys.path.append(migrations_dir.as_posix()) | ||
|
||
args = set(sys.argv[1:]) | ||
if "current" in args: | ||
# fill with zeros to 8 digits | ||
print(f"{MigrationVersion.objects.first().version:08d}") | ||
exit(0) | ||
|
||
steps_limit = sys.argv[-1] | ||
steps_limit = re.sub("[+-]", "", steps_limit) | ||
if steps_limit.isdigit(): | ||
steps_limit = abs(int(steps_limit)) | ||
else: | ||
steps_limit = None | ||
|
||
|
||
current_version = file_version = version_cursor.version | ||
upgrade = "upgrade" in args | ||
downgrade = "downgrade" in args | ||
performed_migrations = 0 | ||
|
||
if upgrade: | ||
migration_files = migrations_dir.glob("*.py") | ||
elif downgrade: | ||
migration_files = reversed(list(migrations_dir.glob("*.py"))) | ||
else: | ||
print("Please specify either 'upgrade' or 'downgrade' as an argument.") | ||
exit(0) | ||
|
||
print("Running migrations...") | ||
|
||
for migration_file in migration_files: | ||
if migration_file.name.startswith("00000000"): | ||
continue | ||
elif migration_file.name == "migrate.py": | ||
continue | ||
|
||
try: | ||
file_version_str = migration_file.name.split("_")[0] | ||
file_version = int(file_version_str) | ||
except ValueError: | ||
raise ValueError(f"Could not parse version from migration file: " | ||
f"{migration_file.as_posix()}") | ||
|
||
if upgrade and file_version > current_version: | ||
method = "upgrade" | ||
elif downgrade and file_version < current_version: | ||
method = "downgrade" | ||
else: | ||
continue | ||
|
||
migration_module = import_module(migration_file.stem) | ||
func = getattr(migration_module, method) | ||
print(f" >> Running {method} in migration {file_version_str}: " | ||
f"'{inspect.getdoc(migration_module)}'") | ||
func() | ||
performed_migrations += 1 | ||
version_cursor.version = file_version | ||
version_cursor.save() | ||
|
||
if steps_limit is not None and performed_migrations >= steps_limit: | ||
break | ||
|
||
if performed_migrations == 0: | ||
print("\nNo migrations to perform.") | ||
else: | ||
print(f"\nPerformed {performed_migrations} migrations.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.