Purging user data from dev install

AI-generated summary

In a discussion about purging user data from a Discourse development installation, mmuyskens asks for the best method to clear all data (users, posts, PMs) before a live import. neil suggests that restoring a backup will overwrite existing data, whereas importing does not. For a fresh database using Docker, neil provides a shell script to drop and recreate the PostgreSQL database, run migrations, and restart services. mmuyskens confirms this approach resolves their concern about duplicate entries.

pfaffman shares a custom bash script for their development environment that kills Ruby processes, drops and recreates the database, runs migrations, creates an admin user, and flushes Redis. neil clarifies a previous point, noting that import scripts do not overwrite data, only backup restores do. pfaffman elaborates that import scripts only add new data, making them ideal for incremental updates, though developers must manually clear the database to test code changes effectively.

What’s a good method to purge a dev install before going live - essentially, getting it ready for the final import. (aka killing all users, posts, pm’s, etc.)

Importing Restoring a backup will overwrite all data, if that’s all you need.

If you need a fresh empty database and you’re using the docker setup, you can do this:

./launcher enter app

# in the container as root now

sv stop unicorn

su postgres -c 'psql -c "drop database discourse"'
# or keep it: "alter database discourse rename to discourse_backup"

su postgres -c 'createdb discourse'
su postgres -c 'psql discourse -c "grant all privileges on database discourse to discourse;"'
su postgres -c 'psql discourse -c "alter schema public owner to discourse;"'
su postgres -c 'psql discourse -c "create extension if not exists hstore;"'
su postgres -c 'psql discourse -c "create extension if not exists pg_trgm;"'

RAILS_ENV=production rake db:migrate

sv start unicorn

Perfect. I just assumed importing again would cause duplicate entries. Thanks!

Here’s what I do on my development box, though making a backup of a fresh database and restoring from the web interface is a little faster, I think.

#!/usr/bin/env bash
if [ ! -f discourse.sublime-project ]
then
    echo "Dude. There's no Discourse here."
    exit
else
    echo Killing any running rubies. . .
    killall ruby
    RAILS_ENV=development
    export RAILS_ENV
    sleep 5 # make sure they all quit
    RAILS_ENV=development bundle exec rake db:drop db:create db:migrate

    cat <<EOF | bundle exec rake admin:create
pfaffman@gmail.com
1secretpassword!
1secretpassword!

EOF
    echo Flushing Redis
    redis-cli flushall
    echo Done.
fi

Hmm, I said “importing will overwrite all data”, but I meant “restoring a backup will overwrite all data”. The import scripts will NOT overwrite data.

I missed that this was the point of your question. I guess it’s clear now that the cool thing about the import scripts is that they import only new data, so you can do an import, wait hours for it to process, then run it again with the latest version of the database in a few minutes.

I’m developing an importer, so I have to clear the database after making changes to the code before I can re-run it to see what happened.