Skip to content

Commit

Permalink
Merge pull request #213 from GeoNode/ISSUE_212
Browse files Browse the repository at this point in the history
Fixes #212 PG_USE_COPY should be optional
  • Loading branch information
giohappy authored Nov 13, 2023
2 parents c2a4c1e + 25489c1 commit 2d8ae57
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ IMPORTER_GLOBAL_RATE_LIMIT= # default 5
IMPORTER_PUBLISHING_RATE_LIMIT= # default 5
IMPORTER_RESOURCE_CREATION_RATE_LIMIT= # default 10
IMPORTER_RESOURCE_COPY_RATE_LIMIT = # default 10
# https://gdal.org/drivers/vector/pg.html#configuration-options
DISABLE_PG_COPY_OGR2OGR= # If TRUE disable the usage of COPY during the import in postgres. Disable it can affect the performance
```

## Troubleshooting
Expand Down
37 changes: 35 additions & 2 deletions importer/handlers/common/tests_vector.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import uuid
from celery.canvas import Signature
from celery import group
Expand Down Expand Up @@ -225,7 +226,7 @@ def test_import_with_ogr2ogr_without_errors_should_call_the_right_command(

_open.assert_called_once()
_open.assert_called_with(
f'/usr/bin/ogr2ogr --config PG_USE_COPY YES -f PostgreSQL PG:" dbname=\'geonode_data\' host=localhost port=5434 user=\'geonode\' password=\'geonode\' " "{self.valid_files.get("base_file")}" -lco DIM=2 -nln alternate "dataset"',
f'/usr/bin/ogr2ogr --config PG_USE_COPY YES -f PostgreSQL PG:" dbname=\'geonode_data\' host=localhost port=5434 user=\'geonode\' password=\'geonode\' " "{self.valid_files.get("base_file")}" -nln alternate "dataset"',
stdout=-1,
stderr=-1,
shell=True, # noqa
Expand All @@ -251,7 +252,39 @@ def test_import_with_ogr2ogr_with_errors_should_raise_exception(self, _open):

_open.assert_called_once()
_open.assert_called_with(
f'/usr/bin/ogr2ogr --config PG_USE_COPY YES -f PostgreSQL PG:" dbname=\'geonode_data\' host=localhost port=5434 user=\'geonode\' password=\'geonode\' " "{self.valid_files.get("base_file")}" -lco DIM=2 -nln alternate "dataset"',
f'/usr/bin/ogr2ogr --config PG_USE_COPY YES -f PostgreSQL PG:" dbname=\'geonode_data\' host=localhost port=5434 user=\'geonode\' password=\'geonode\' " "{self.valid_files.get("base_file")}" -nln alternate "dataset"',
stdout=-1,
stderr=-1,
shell=True, # noqa
)

@patch.dict(os.environ, {"DISABLE_PG_COPY_OGR2OGR": "True"}, clear=True)
@patch("importer.handlers.common.vector.Popen")
def test_import_with_ogr2ogr_without_errors_should_call_the_right_command_if_copy_is_disabled(
self, _open
):
_uuid = uuid.uuid4()

comm = MagicMock()
comm.communicate.return_value = b"", b""
_open.return_value = comm

_task, alternate, execution_id = import_with_ogr2ogr(
execution_id=str(_uuid),
files=self.valid_files,
original_name="dataset",
handler_module_path=str(self.handler),
ovverwrite_layer=False,
alternate="alternate",
)

self.assertEqual("ogr2ogr", _task)
self.assertEqual(alternate, "alternate")
self.assertEqual(str(_uuid), execution_id)

_open.assert_called_once()
_open.assert_called_with(
f'/usr/bin/ogr2ogr --config PG_USE_COPY NO -f PostgreSQL PG:" dbname=\'geonode_data\' host=localhost port=5434 user=\'geonode\' password=\'geonode\' " "{self.valid_files.get("base_file")}" -nln alternate "dataset"',
stdout=-1,
stderr=-1,
shell=True, # noqa
Expand Down
6 changes: 5 additions & 1 deletion importer/handlers/common/vector.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import ast
from django.db import connections
from importer.publisher import DataPublisher
from importer.utils import call_rollback_function, find_key_recursively
Expand Down Expand Up @@ -161,7 +162,10 @@ def create_ogr2ogr_command(files, original_name, ovverwrite_layer, alternate):
db_host, db_port = db_host.split(":")
db_name = _uri.split("@")[1].split("/")[1]

options = "--config PG_USE_COPY YES "

disable_pg_copy = ast.literal_eval(os.getenv("DISABLE_PG_COPY_OGR2OGR", "False"))
options = f"--config PG_USE_COPY {'NO' if disable_pg_copy else 'YES'} "

options += (
"-f PostgreSQL PG:\" dbname='%s' host=%s port=%s user='%s' password='%s' \" "
% (db_name, db_host, db_port, db_user, db_password)
Expand Down

0 comments on commit 2d8ae57

Please sign in to comment.