Search code examples
kiwi-tcms

Reset all tables data but and restore initial settings


I have using Kiwi TCMS System in development, so I have created more test data. Now I want to delete all the test data, so I tried to reset the Kiwi DB tables using the following command as suggested in the article, https://kiwitcms.org/blog/atodorov/2018/07/30/how-to-backup-docker-volumes-for-kiwi-tcms/

First taken the backup,

docker exec -it kiwi_web /Kiwi/manage.py dumpdata --all --indent 2 > database.json

Then deleted all table records,

docker exec -it kiwi_web /bin/bash -c '/Kiwi/manage.py sqlflush | /Kiwi/manage.py dbshell'

I know this command has deleted everything including initial setup data completely from DB, so I have tried to apply the initial setup using the following commands,

docker exec -it kiwi_web /Kiwi/manage.py migrate
docker exec -it kiwi_web /Kiwi/manage.py createsuperuser
docker exec -it kiwi_web /Kiwi/manage.py refresh_permissions

The First 2 worked fine, but the last refresh_permissions was throwing an error, Refer to the error screenshot

enter image description here

enter image description here

This leads, some of the permission-related settings like groups are not restored.

Seems I'm doing some wrong way to delete all data and restore with the required initial setup. pLease correct me what would be the best way here.


Solution

  • You are getting errors because you don't understand what is happening. The commands and blog post referenced above are for backup & restore of the entire database, not selectively removing information which you don't want to be there anymore:

    1. sqlflush will remove all information from all tables
    2. migrate will create DB schema (which already exists) and initial records in some tables. Because the DB schema already exists and the internal Django reference to which migration has been applied last hasn't changed you get a "No migrations to apply" message
    3. Because no migrations were applied some initial records aren't created. In this case a group with hard-coded name "Administrator".
    4. refresh_permissions fails because it is looking for a group with the name "Administrator"

    You can either:

    1. Re-create all missing records by hand or
    2. Create a clean installation of Kiwi TCMS and use that as your main instance or
    3. Keep the POC instance and delete only the records you don't want - either one-by-one from the admin panel or using an ORM query via manage.py shell.