Skip to content

Importing data

Jon Froehlich edited this page Dec 20, 2021 · 19 revisions

Django makes it relatively easy to dump a database from one computer (dumpdata) and reload it on another (loaddata). This is useful and important for testing and bootstrapping your local development environment.

Because so much of the Makeability Lab website relies on artifacts (like PDF and PowerPoint files as well as thumbnails), it's not sufficient to just dump/load the database. We must also copy these files (stored in /code/media) from the exporting computer to the importing computer. I'll walk through everything below.

Dumping the data

1. Dumping the database

To dump the database data, you need to create an interactive bash shell into the Docker container and run the dumpdata command.

  1. Run the interactive shell in terminal: > docker exec -it makeabilitylabwebsite_website_1 bash

  2. From the Docker shell, run the dumpdata command. Make sure you have admin access.

    apache@43cd75e1d80c:/code$ python manage.py dumpdata > dumped_data.json
    bash: dumped_data.json: Permission denied
    apache@43cd75e1d80c:/code$ su
    Password:
    root@43cd75e1d80c:/code# python manage.py dumpdata > dumped_data.json
    
  3. You might also have to copy the dumped_data.json file out of the Docker container and into your local filesystem. So, go back into terminal (not the Docker shell) and type > docker cp makeabilitylabwebsite_website_1:/code/dumped_data.json .

  4. Now you should have a dumped_data.json file that you can copy over to your 2nd computer.

2. Copying the media directory

The second primary step is to copy the media directory from the Docker container into your local filesystem.

  1. From terminal (not the Docker shell), type > docker cp makeabilitylabwebsite_website_1:/code/media ., which should copy this media directory to your local filesystem
  2. I also zipped up this dir (because it can be huge) to transport it over to my 2nd computer for importing.

Importing the data

Now, on a second computer, we can import that dumped_data.json file into Django and copy over the media dir.

1. Copying over the media directory

We need to first copy over the media dir into the Docker container.

  1. From your second computer's terminal, type > docker cp ./media makeabilitylabwebsite_website_1:/code/., which will copy the media dir into the Docker container at /code/.
  2. Make sure the media dir and subdirs have appropriate permissions. I launched an interactive shell docker exec -it makeabilitylabwebsite_website_1 bash and then set all media to chmod -R 777 (but probably could use chmod -R 755)
  3. In the interactive shell, you can also check that the contents have been copied over correctly

2. Importing the database data

Finally, we can import the dumped_data.json file:

  1. First, launch an interactive shell. From your second computer's terminal, type: > docker exec -it makeabilitylabwebsite_website_1 bash
  2. From within this shell, type $ python manage.py loaddata dumped_data.json and it should work.

Here's an example:

apache@e86363656952:/code$ python manage.py loaddata dumped_data.json
Publication 'This is a test' has just been saved with PDF=/code/media/publications/Froehlich_ThisIsATest_ASSETS2021.pdf, checking to see if we should auto-generate a thumbnail
Checking for thumbnail at '/code/media/publications/images/Froehlich_ThisIsATest_ASSETS2021.jpg', otherwise will auto-generate
The thumbnail at `/code/media/publications/images/Froehlich_ThisIsATest_ASSETS2021.jpg` exists
No need to save, the thumbnail '/code/media/publications/images/Froehlich_ThisIsATest_ASSETS2021.jpg' already exists!
Publication 'This is a test' has just been saved with PDF=/code/media/publications/Froehlich_ThisIsATest_ASSETS2021.pdf, checking to see if we should auto-generate a thumbnail
Checking for thumbnail at '/code/media/publications/images/Froehlich_ThisIsATest_ASSETS2021.jpg', otherwise will auto-generate
The thumbnail at `/code/media/publications/images/Froehlich_ThisIsATest_ASSETS2021.jpg` exists
No need to save, the thumbnail '/code/media/publications/images/Froehlich_ThisIsATest_ASSETS2021.jpg' already exists!
Publication 'The Future of Global-Scale Spatial Data Collection and Analyses on Urban (in)Accessibility for People with Disabilities' has just been saved with PDF=/code/media/publications/Froehlich_TheFutureOfGlobalScaleSpatialDataCollectionAndAnalysesOnUrbanInAccessibilityForPeopleWithDisabilities_SPATIALDATASCIENCESYMPOSIUM2021.pdf, checking to see if we should auto-generate a thumbnail
Checking for thumbnail at '/code/media/publications/images/Froehlich_TheFutureOfGlobalScaleSpatialDataCollectionAndAnalysesOnUrbanInAccessibilityForPeopleWithDisabilities_SPATIALDATASCIENCESYMPOSIUM2021.jpg', otherwise will auto-generate
The thumbnail at `/code/media/publications/images/Froehlich_TheFutureOfGlobalScaleSpatialDataCollectionAndAnalysesOnUrbanInAccessibilityForPeopleWithDisabilities_SPATIALDATASCIENCESYMPOSIUM2021.jpg` exists
No need to save, the thumbnail '/code/media/publications/images/Froehlich_TheFutureOfGlobalScaleSpatialDataCollectionAndAnalysesOnUrbanInAccessibilityForPeopleWithDisabilities_SPATIALDATASCIENCESYMPOSIUM2021.jpg' already exists!
Publication 'The Future of Global-Scale Spatial Data Collection and Analyses on Urban (in)Accessibility for People with Disabilities' has just been saved with PDF=/code/media/publications/Froehlich_TheFutureOfGlobalScaleSpatialDataCollectionAndAnalysesOnUrbanInAccessibilityForPeopleWithDisabilities_SPATIALDATASCIENCESYMPOSIUM2021.pdf, checking to see if we should auto-generate a thumbnail
Checking for thumbnail at '/code/media/publications/images/Froehlich_TheFutureOfGlobalScaleSpatialDataCollectionAndAnalysesOnUrbanInAccessibilityForPeopleWithDisabilities_SPATIALDATASCIENCESYMPOSIUM2021.jpg', otherwise will auto-generate
The thumbnail at `/code/media/publications/images/Froehlich_TheFutureOfGlobalScaleSpatialDataCollectionAndAnalysesOnUrbanInAccessibilityForPeopleWithDisabilities_SPATIALDATASCIENCESYMPOSIUM2021.jpg` exists
No need to save, the thumbnail '/code/media/publications/images/Froehlich_TheFutureOfGlobalScaleSpatialDataCollectionAndAnalysesOnUrbanInAccessibilityForPeopleWithDisabilities_SPATIALDATASCIENCESYMPOSIUM2021.jpg' already exists!
Installed 189 object(s) from 1 fixture(s)
Clone this wiki locally