Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dbrestore fails with non-existent relation #478

Open
ericswpark opened this issue Apr 4, 2023 · 7 comments · May be fixed by #511 or #515
Open

dbrestore fails with non-existent relation #478

ericswpark opened this issue Apr 4, 2023 · 7 comments · May be fixed by #511 or #515

Comments

@ericswpark
Copy link

ericswpark commented Apr 4, 2023

Bug Report

Describe the bug

Trying to restore from a backup, I'm running into the following error:

Error running:  pg_restore --dbname=postgresql://postgres:password@db:5432/shipper --single-transaction --clean 
pg_restore: error: could not execute query: ERROR:  relation "public.core_statistics" does not exist
Command was: ALTER TABLE ONLY public.core_statistics DROP CONSTRAINT shipper_statistics_build_id_98571750_fk_shipper_build_id;

To Reproduce

  1. Back up the database with dbbackup
  2. Restore the database with dbrestore

Expected behavior

The backup is restored successfully.

Screenshots or reproduction

The Django project I'm encountering the problem with can be found here: https://github.com/shipperstack/shipper

I am using the Docker Compose of the project which can be found here: https://github.com/shipperstack/shipper-docker

It is possible that some of the DB schema might be a little weird because of all the migrations, but I am pretty confident in asying that 2.x and above have consistent migrations that shouldn't impose a problem on django-dbbackup (but worth keeping in mind).

Versions

Django-dbbackup

  • pypi: 4.0.2

External tools

  • Python: 3.11.2-r0 (alpine-3.17)
  • Django: 4.1.7
  • OS: Linux, Docker
@ericswpark ericswpark changed the title dbrestore fails with non-existent relaiton dbrestore fails with non-existent relation Apr 4, 2023
@romintomasetti
Copy link

romintomasetti commented May 6, 2023

I'm also affected by this bug.

Removing --clean makes it work though.

@hkhanna
Copy link

hkhanna commented Aug 14, 2023

Interesting, for me, removing --single-transaction did the trick.

For those who find this, issue, I simply added this to my Django confirm: DBBACKUP_CONNECTORS = {"default": {"SINGLE_TRANSACTION": False}}

@lvieirajr
Copy link

Can we add an option on these configurations for postgres to allow us to use the if-exists condition on these drop constraints?
https://www.postgresql.org/docs/current/app-pgrestore.html

@DmytroLitvinov
Copy link

+1 for --if-exists

@DmytroLitvinov
Copy link

DmytroLitvinov commented Feb 24, 2024

Had an issue with droping constaint which does not exists anymore where I needed --if-exists option.
As a hotfix provided locally next code:

# django-dbbackups
# https://github.com/jazzband/django-dbbackup/issues/478#issuecomment-1677907470
DBBACKUP_CONNECTORS = {'default': {'IF_EXISTS': True}}
DBBACKUP_CONNECTOR_MAPPING = {
    'django.db.backends.postgresql': 'server.apps.core.db.postgresql.PgDumpBinaryConnector',
}

And in the server.apps.core.db.postgresql module next:

from dbbackup.db.postgresql import PgDumpBinaryConnector as CorePgDumpBinaryConnector, create_postgres_uri


class PgDumpBinaryConnector(CorePgDumpBinaryConnector):
    if_exists = False

    def _restore_dump(self, dump):
        dbname = create_postgres_uri(self)
        cmd = f"{self.restore_cmd} {dbname}"

        if self.single_transaction:
            cmd += " --single-transaction"
        if self.drop:
            cmd += " --clean"
        if self.if_exists:
            cmd += " --if-exists"
        cmd = f"{self.restore_prefix} {cmd} {self.restore_suffix}"
        stdout, stderr = self.run_command(cmd, stdin=dump, env=self.restore_env)
        return stdout, stderr

@Archmonger
Copy link
Contributor

PRs to resolve this are welcome!

DmytroLitvinov added a commit to DmytroLitvinov/django-dbbackup that referenced this issue Mar 5, 2024
@DmytroLitvinov DmytroLitvinov linked a pull request Mar 5, 2024 that will close this issue
4 tasks
DmytroLitvinov added a commit to DmytroLitvinov/django-dbbackup that referenced this issue Mar 5, 2024
@DmytroLitvinov
Copy link

Hi @Archmonger ,
Created PR for resolving it in the library :)

WillNilges pushed a commit to WillNilges/django-dbbackup that referenced this issue Apr 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
6 participants