Skip to content

PostgresqlFactory error when base_dir and cache_initialized_db are set #24

@brmzkw

Description

@brmzkw

This issue is also related to https://github.com/tk0miya/testing.postgresql

I want to set cache_initialized_db=True to reduce the number of invocations of initdb between tests, but also to keep the data directories between test invokations.

import testing.postgresql

factory = testing.postgresql.PostgresqlFactory(
    cache_initialized_db=True,
    base_dir='/tmp/xxx'
)

psql = factory()
factory.clear_cache()
Traceback (most recent call last):
  File "/venv/lib/python3.7/site-packages/testing/common/database.py", line 111, in setup
    copytree(self.settings['copy_data_from'], data_dir)
  File "/usr/lib/python3.7/shutil.py", line 321, in copytree
    os.makedirs(dst)
  File "/usr/lib/python3.7/os.py", line 221, in makedirs
    mkdir(name, mode)
FileExistsError: [Errno 17] File exists: '/tmp/xxx/data'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "toto.py", line 9, in <module>
    psql = factory()
  File "/venv/lib/python3.7/site-packages/testing/common/database.py", line 57, in __call__
    return self.target_class(**self.settings)
  File "/venv/lib/python3.7/site-packages/testing/common/database.py", line 96, in __init__
    self.setup()
  File "/venv/lib/python3.7/site-packages/testing/common/database.py", line 115, in setup
    (self.settings['copy_data_from'], data_dir, exc))
RuntimeError: could not copytree /tmp/xxx/data to /tmp/xxx/data: FileExistsError(17, 'File exists')

When both arguments are set, testing.common.database::Database.setup() calls shutil.copytree with the same path as source and destination.

There is a possible workaround:

import testing.postgresql


class Postgresql(testing.postgresql.Postgresql):
    def setup(self):
        data_dir = self.get_data_directory()
        if self.settings['copy_data_from'] == data_dir:
            return
        return super().setup()


class PostgresqlFactory(testing.postgresql.PostgresqlFactory):
    target_class = Postgresql


factory = PostgresqlFactory(
    cache_initialized_db=True,
    base_dir='/tmp/xxx'
)

psql = factory()

factory.clear_cache()

Do you think the check can be moved to testing.common.database::Database?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions