Add Makefile, black, reformat with black
This commit is contained in:
		
							parent
							
								
									cfc50b82bb
								
							
						
					
					
						commit
						d2fa4c586f
					
				
					 16 changed files with 978 additions and 366 deletions
				
			
		| 
						 | 
				
			
			@ -9,20 +9,33 @@ from redis import ResponseError
 | 
			
		|||
from redis_developer.connections import get_redis_connection
 | 
			
		||||
from redis_developer.model.model import model_registry
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
redis = get_redis_connection()
 | 
			
		||||
log = logging.getLogger(__name__)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
import importlib
 | 
			
		||||
import pkgutil
 | 
			
		||||
import importlib  # noqa: E402
 | 
			
		||||
import pkgutil  # noqa: E402
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class MigrationError(Exception):
 | 
			
		||||
    pass
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def import_submodules(root_module_name: str):
 | 
			
		||||
    """Import all submodules of a module, recursively."""
 | 
			
		||||
    # TODO: Call this without specifying a module name, to import everything?
 | 
			
		||||
    root_module = importlib.import_module(root_module_name)
 | 
			
		||||
 | 
			
		||||
    if not hasattr(root_module, "__path__"):
 | 
			
		||||
        raise MigrationError(
 | 
			
		||||
            "The root module must be a Python package. "
 | 
			
		||||
            f"You specified: {root_module_name}"
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
    for loader, module_name, is_pkg in pkgutil.walk_packages(
 | 
			
		||||
        root_module.__path__, root_module.__name__ + '.'):
 | 
			
		||||
        root_module.__path__, root_module.__name__ + "."  # type: ignore
 | 
			
		||||
    ):
 | 
			
		||||
        importlib.import_module(module_name)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -77,14 +90,20 @@ class Migrator:
 | 
			
		|||
            except NotImplementedError:
 | 
			
		||||
                log.info("Skipping migrations for %s", name)
 | 
			
		||||
                continue
 | 
			
		||||
            current_hash = hashlib.sha1(schema.encode("utf-8")).hexdigest()
 | 
			
		||||
            current_hash = hashlib.sha1(schema.encode("utf-8")).hexdigest()  # nosec
 | 
			
		||||
 | 
			
		||||
            try:
 | 
			
		||||
                redis.execute_command("ft.info", cls.Meta.index_name)
 | 
			
		||||
            except ResponseError:
 | 
			
		||||
                self.migrations.append(
 | 
			
		||||
                    IndexMigration(name, cls.Meta.index_name, schema, current_hash,
 | 
			
		||||
                                   MigrationAction.CREATE))
 | 
			
		||||
                    IndexMigration(
 | 
			
		||||
                        name,
 | 
			
		||||
                        cls.Meta.index_name,
 | 
			
		||||
                        schema,
 | 
			
		||||
                        current_hash,
 | 
			
		||||
                        MigrationAction.CREATE,
 | 
			
		||||
                    )
 | 
			
		||||
                )
 | 
			
		||||
                continue
 | 
			
		||||
 | 
			
		||||
            stored_hash = redis.get(hash_key)
 | 
			
		||||
| 
						 | 
				
			
			@ -93,11 +112,25 @@ class Migrator:
 | 
			
		|||
            if schema_out_of_date:
 | 
			
		||||
                # TODO: Switch out schema with an alias to avoid downtime -- separate migration?
 | 
			
		||||
                self.migrations.append(
 | 
			
		||||
                    IndexMigration(name, cls.Meta.index_name, schema, current_hash,
 | 
			
		||||
                                   MigrationAction.DROP, stored_hash))
 | 
			
		||||
                    IndexMigration(
 | 
			
		||||
                        name,
 | 
			
		||||
                        cls.Meta.index_name,
 | 
			
		||||
                        schema,
 | 
			
		||||
                        current_hash,
 | 
			
		||||
                        MigrationAction.DROP,
 | 
			
		||||
                        stored_hash,
 | 
			
		||||
                    )
 | 
			
		||||
                )
 | 
			
		||||
                self.migrations.append(
 | 
			
		||||
                     IndexMigration(name, cls.Meta.index_name, schema, current_hash,
 | 
			
		||||
                                    MigrationAction.CREATE, stored_hash))
 | 
			
		||||
                    IndexMigration(
 | 
			
		||||
                        name,
 | 
			
		||||
                        cls.Meta.index_name,
 | 
			
		||||
                        schema,
 | 
			
		||||
                        current_hash,
 | 
			
		||||
                        MigrationAction.CREATE,
 | 
			
		||||
                        stored_hash,
 | 
			
		||||
                    )
 | 
			
		||||
                )
 | 
			
		||||
 | 
			
		||||
    def run(self):
 | 
			
		||||
        # TODO: Migration history
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue