RegioHelden / django-scrubber

Anonymizer for Django database data
Other
25 stars 10 forks source link

Support for multiple / non-default database #35

Open farialima opened 2 years ago

farialima commented 2 years ago

Description

I use multiple databases in my app -- and Django-scrubber only scrubs the default one.

What I Did

My app uses 3 DBs, with the same models, for different customers, so my settings (in dev -- copied from prod) have this:

DATABASES = {
    key: {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': f'devdb-${key}',
        'USER': 'test',
        'PASSWORD': os.getenv("DB_TEST_PASSWORD"),
        'HOST': 'db.mydomain.com',
        'PORT': '5432',
    } for key in ['default', 'customer1', 'customer2', 'customer3']
}

I'd like to scrub the 3 customer DBs, but Django-scrubber doesn't let me do that. It would be nice to have a --database <db> option, or --all-databases

A workaround is to add this in my manage.py's main() function

    if len(sys.argv) > 1 and sys.argv[1] == "scrub_data":
        if "--database" in sys.argv:
            from django.conf import settings

            database_index = sys.argv.index("--database")
            del sys.argv[database_index]
            settings.DATABASES["default"] = settings.DATABASES[sys.argv[database_index]]
            print(f"Scrubbing database {sys.argv[database_index]}")
            del sys.argv[database_index]

then I execute: ./manage.py scrub_data --database customer1, etc.

it's working well, but it's a little ugly... would be nicer to have a clean support in django-scrubber :)

lociii commented 2 years ago

Hey François,

sorry for the very very late response. Would you mind submitting a pull request with the necessary changes to support a database selection out of the box?

Thanks, Jens

farialima commented 2 years ago

I've started to implement on, it's not finished -- and I won't have time to put more effort on it. But maybe someone will finish it ?

https://github.com/RegioHelden/django-scrubber/pull/45