From e1821953a4f579ba3f062866a0837868f15aa7e4 Mon Sep 17 00:00:00 2001 From: Jaylen Douglas Date: Tue, 10 Jan 2023 20:11:42 -0500 Subject: [PATCH] Working on new feature - don't run up() method on already executed migrations. --- README.md | 14 +++++++------- mongrations/cache.py | 11 ++++++++++- mongrations/cli.py | 7 +++++++ mongrations/main.py | 25 ++++++++++++++++++++++--- mongrations/version.py | 2 +- 5 files changed, 47 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 683e626..df42d96 100644 --- a/README.md +++ b/README.md @@ -25,19 +25,19 @@ class Mongration(Database): super(Database, self).__init__() def up(self): - collection = self.db['members'] + collection = self.db['moderators'] data = { 'accountId': 1, - 'username': 'admin', - 'email': 'admin@able.digital', - 'firstName': 'Site', - 'lastName': 'Owner' + 'username': 'bot', + 'email': 'bot@able.digital', + 'firstName': 'Moderator', + 'lastName': 'Bot' } collection.insert_one(data) def down(self): - collection = self.db['members'] - collection.delete_one({'username': 'admin'}) + collection = self.db['moderators'] + collection.delete_one({'username': 'bot'}) Mongrations(Mongration) diff --git a/mongrations/cache.py b/mongrations/cache.py index 1125953..9cf3a7b 100644 --- a/mongrations/cache.py +++ b/mongrations/cache.py @@ -77,7 +77,8 @@ def _initial_write(self): "createdAt": "", "updatedAt": "", "lastMigration": "", - "migrations": [] + "migrations": [], + "executed": [] } self._write_file_obj(data) @@ -121,6 +122,7 @@ def undo_migration(self, remove_migration: bool = False): cache = self._get_file_object() if remove_migration: cache['migrations'] = cache['migrations'][:-1] + cache['executed'].remove(cache['migrations'][-1]) self._write_file_obj(cache) return cache['lastMigration'] except FileNotFoundError: @@ -155,3 +157,10 @@ def create_migration_file(self): with open(self._mongration_file, mode='r', encoding='utf-8') as open_mf: data = json.load(open_mf) mf.write(json.dumps(data, indent=2)) + + def has_executed(self, filepath): + cache = self._get_file_object() + if filepath in cache['executed']: + return True + return False + diff --git a/mongrations/cli.py b/mongrations/cli.py index ace3f25..5766d20 100644 --- a/mongrations/cli.py +++ b/mongrations/cli.py @@ -86,6 +86,13 @@ def undo(): if value.lower() == 'y': main.undo() +@cli.command() +@click.argument('filename', nargs=1) +def down(filename): + """Run the down() method on a specified migration file""" + main.down(last_migration_only=False, specific_file=filename) + + @cli.command() def inspect(): diff --git a/mongrations/main.py b/mongrations/main.py index fdcaa51..c692c2b 100644 --- a/mongrations/main.py +++ b/mongrations/main.py @@ -10,8 +10,8 @@ def __init__(self): self._cache = Cache() self._cache._set_file_path() - @staticmethod - def _command_line_interface(migrations: list, state: str): + def _command_line_interface(self, migrations: list, state: str): + sucessful_runs =[] success = True if len(migrations) == 0: print('No migrations to run.') @@ -19,6 +19,9 @@ def _command_line_interface(migrations: list, state: str): print(f'{state.upper()}: Running {len(migrations)} migration{"" if len(migrations) <= 1 else "s"}...') for migration in migrations: migration_file_path = join(getcwd(), migration) + if self._cache.has_executed(migration_file_path): + print("Already ran migration.") + continue command = shlex.split(f'python3 {migration_file_path}') proc = subprocess.Popen(command, stdout=subprocess.PIPE, env=environ.copy()) for line in io.TextIOWrapper(proc.stdout, encoding='utf8', newline=''): @@ -30,12 +33,28 @@ def _command_line_interface(migrations: list, state: str): print(line) if success is False: break + if len(sucessful_runs) > 0: + config_file = self._cache._get_file_object() + new_data = config_file['executed'] + new_data.extend(sucessful_runs) + self._cache._write_file_obj(new_data) if success: print('Migrations complete.') - def down(self, last_migration_only=False): + def down(self, last_migration_only=False, specific_file=None): + specific_file_found = False environ['MIGRATION_MIGRATE_STATE'] = 'DOWN' migrations = self._cache.migrations_file_list(last_migration=last_migration_only) + if specific_file is not None: + for migration in migrations: + name = basename(migration).replace('.py', '') + if name == specific_file.replace('.py', ''): + migrations = [migration] + specific_file_found = True + break + if not specific_file_found: + print(f'File not found: {specific_file}') + sys.exit(86) self._command_line_interface(migrations, 'down') def migrate(self): diff --git a/mongrations/version.py b/mongrations/version.py index b3ddbc4..7b344ec 100644 --- a/mongrations/version.py +++ b/mongrations/version.py @@ -1 +1 @@ -__version__ = '1.1.1' +__version__ = '1.1.2'