Skip to content

Commit 97413d4

Browse files
committed
Fix temporary tablespace directory not existing in Postgres container on CI
1 parent 67f2030 commit 97413d4

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

tests/conftest.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import os
21
import tempfile
32
import uuid
43

54
import pytest
65

6+
from django.conf import settings
77
from django.contrib.postgres.signals import register_type_handlers
88
from django.db import connection
99

@@ -35,11 +35,24 @@ def django_db_setup(django_db_setup, django_db_blocker):
3535
with django_db_blocker.unblock():
3636
qn = connection.ops.quote_name
3737

38-
with tempfile.TemporaryDirectory() as temp_dir:
39-
if not os.path.exists(temp_dir):
40-
os.makedirs(temp_dir)
38+
db_hostname = settings.DATABASES[connection.alias]["HOST"]
4139

40+
with tempfile.TemporaryDirectory() as temp_dir:
4241
with connection.cursor() as cursor:
42+
# If the database is remote, like in a CI environment, make
43+
# sure the temporary directory exists in the container
44+
# that PostgreSQL is running.
45+
#
46+
# Note that this only typically works in CI environments
47+
# where we have utter control to execute arbitary commands.
48+
if db_hostname and db_hostname not in (
49+
"127.0.0.1",
50+
"localhost",
51+
):
52+
cursor.execute(
53+
f"COPY (select 1) TO PROGRAM 'mkdir --mode=777 -p {temp_dir}'"
54+
)
55+
4356
cursor.execute(
4457
f"CREATE TABLESPACE {qn(custom_tablespace_name)} LOCATION %s",
4558
(temp_dir,),

0 commit comments

Comments
 (0)