django-yadpt-starter
django-yadpt-starter copied to clipboard
Clear the postgis data
The Postgis Docker container that we are using comes with some garbage in it that we do not need.
Consider adding some docs explaining how we can clear it or find a way to automate with a python script or a docker entrypoint script
Something like:
docker-compose -f dev.yml run --rm web bash
python3 manage.py dbshell --settings={{project_name}}.settings.local
# Inside postgres console:
drop schema public cascade;
create schema public;
CREATE EXTENSION postgis;
CREATE EXTENSION postgis_topology;
python3 manage.py makemigrations --settings={{project_name}}.settings.local
python3 manage.py migrate --settings={{project_name}}.settings.local
Maybe run python3 manage.py createsuperuser --settings={{project_name}}.settings.local
as well ??
We can use the following manage command:
class Command(BaseCommand):
help = 'Deletes Everything inside a Postgres Database!'
def handle(self, *args, **options):
name = settings.DATABASES['default']['NAME']
user = settings.DATABASES['default']['USER']
os.environ["PGPASSWORD"] = settings.DATABASES['default']['PASSWORD']
host = settings.DATABASES['default']['HOST']
port = settings.DATABASES['default']['PORT']
command = (
"psql --host={host} --port={port} --user={user} --no-password "
"-c 'drop schema public cascade;'".format(**locals())
)
self.stdout.write('Running PSQL command: ')
self.stdout.write(command)
subprocess.call([command], shell=True)
self.stdout.write('Finished deleting ALL database content!!')
Or maybe just clean the image and create a new image from it and link it to this project.
As an alternative, maybe we can leverage postgres' official image ability to run any initial scripts (*.sh or *.psql) found in the /docker-entrypoint-initdb.d
folder when starting the service.
(source: https://hub.docker.com/_/postgres/ - check the "How to extend this image " section)
If you would like to do additional initialization in an image derived from this one, add one or more *.sql, *.sql.gz, or *.sh scripts under /docker-entrypoint-initdb.d (creating the directory if necessary). After the entrypoint calls initdb to create the default postgres user and database, it will run any *.sql files and source any *.sh scripts found in that directory to do further initialization before starting the service.
It might be a cleaner way of doing it, but I don't think a drop schema public cascade;
can be added to this new .sh or .psql script file.
EDIT: I thought the starter had some sort of special postgres customization... as it does not, a clean postgres image should be enough.
@PedroMD my only problem with a custom "clean image" is that we would have to maintain it (every update and security fixes).
I think what we need is something that only runs the first time we run the starter.
At first i thought we could use one of dockers features (there is probably some sort of entrypoint that only runs the first time the image is created) but now that i think about it, maybe a post python script that the user manually runs with a few protections (so that the user does not drop the db by mistake), that will look into Django's settings file to retrieve the db information, use that db info to enter the docker container and db, drop and create a new one.
What do you think?
PS: this would imply using Psycopg2
https://wiki.postgresql.org/wiki/Psycopg2_Tutorial as a dependency since at this time we would still not be in Django's virtual environment
This issue will be closed after we switch to https://github.com/psychok7/django-yadpt-starter/issues/50