- Add slug field with uniqueness constraint to Organization - Use python-slugify to generate slug from name and import that in migration - Require name in all /rename and org creation requests - Auto-generate slug for new org with no slug or when /rename is called w/o a slug - Auto-generate slug for 'default-org' based on name - Add /api/orgs/slugs GET endpoint to return all slugs in use - tests: extend backend test-requirements.txt from requirements to allow testing slugify - tests: move get_redis_crawl_stats() to avoid extra dependency in utils
		
			
				
	
	
		
			34 lines
		
	
	
		
			998 B
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			34 lines
		
	
	
		
			998 B
		
	
	
	
		
			Python
		
	
	
	
	
	
"""
 | 
						|
Migration 0019 - Organization slug
 | 
						|
"""
 | 
						|
from btrixcloud.migrations import BaseMigration
 | 
						|
from btrixcloud.utils import slug_from_name
 | 
						|
 | 
						|
 | 
						|
MIGRATION_VERSION = "0019"
 | 
						|
 | 
						|
 | 
						|
class Migration(BaseMigration):
 | 
						|
    """Migration class."""
 | 
						|
 | 
						|
    def __init__(self, mdb, migration_version=MIGRATION_VERSION):
 | 
						|
        super().__init__(mdb, migration_version)
 | 
						|
 | 
						|
    async def migrate_up(self):
 | 
						|
        """Perform migration up.
 | 
						|
 | 
						|
        Add slug to all existing orgs.
 | 
						|
        """
 | 
						|
        # pylint: disable=duplicate-code
 | 
						|
        mdb_orgs = self.mdb["organizations"]
 | 
						|
        async for org in mdb_orgs.find({"slug": {"$eq": None}}):
 | 
						|
            oid = org["_id"]
 | 
						|
            slug = slug_from_name(org["name"])
 | 
						|
            try:
 | 
						|
                await mdb_orgs.find_one_and_update(
 | 
						|
                    {"_id": oid}, {"$set": {"slug": slug}}
 | 
						|
                )
 | 
						|
            # pylint: disable=broad-exception-caught
 | 
						|
            except Exception as err:
 | 
						|
                print(f"Error adding slug to org {oid}: {err}", flush=True)
 |