Repository: geofileops/geofileops
Pull request #549 (2024-07-29 13:13:01)

ENH: also rename column name when only casing is different
possible to rename column when only casing changes

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/geofileops/geofileops/pull/549?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=geofileops) Report
Attention: Patch coverage is `75.00000%` with `1 line` in your changes missing coverage. Please review.
> Project coverage is 91.18%. Comparing base [(`5f08842`)](https://app.codecov.io/gh/geofileops/geofileops/commit/5f08842e475a5aaff2f4ace75e7bc77c9c2fe023?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=geofileops) to head [(`020d505`)](https://app.codecov.io/gh/geofileops/geofileops/commit/020d5051f15dabfecefed1ebf03b9aa41eee7138?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=geofileops).
> Report is 46 commits behind head on main.

| [Files](https://app.codecov.io/gh/geofileops/geofileops/pull/549?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=geofileops) | Patch % | Lines |
|---|---|---|
| [geofileops/fileops.py](https://app.codecov.io/gh/geofileops/geofileops/pull/549?src=pr&el=tree&filepath=geofileops%2Ffileops.py&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=geofileops#diff-Z2VvZmlsZW9wcy9maWxlb3BzLnB5) | 75.00% | [1 Missing :warning: ](https://app.codecov.io/gh/geofileops/geofileops/pull/549?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=geofileops) |

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##             main     #549      +/-   ##
==========================================
- Coverage   93.11%   91.18%   -1.94%     
==========================================
  Files          21       21              
  Lines        4113     4186      +73     
==========================================
- Hits         3830     3817      -13     
- Misses        283      369      +86     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/geofileops/geofileops/pull/549?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=geofileops).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=geofileops).


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8386b4f..a4a9e2d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,11 @@
 # CHANGELOG
 
+## 0.10 (????-??-??)
+
+### Improvements
+
+- Add support for renaming column with only difference in casing (#549)
+
 ## 0.9.1 (2024-07-18)
 
 ### Bugs fixed
diff --git a/geofileops/fileops.py b/geofileops/fileops.py
index c7622ef..da25882 100644
--- a/geofileops/fileops.py
+++ b/geofileops/fileops.py
@@ -742,6 +742,22 @@ def rename_column(
         if not datasource_layer.TestCapability(gdal.ogr.OLCAlterFieldDefn):
             raise ValueError(f"rename_column not supported for {path}")
 
+        # If the column name only differs in case, we need to rename it first to a
+        # temporary column name to avoid an error.
+        if column_name.lower() == new_column_name.lower():
+            columns_lower = {column.lower() for column in info.columns}
+            for index in range(9999):
+                temp_column_name = f"tmp_{index}"
+                if temp_column_name not in columns_lower:
+                    break
+            sql_stmt = (
+                f'ALTER TABLE "{layer}" '
+                f'RENAME COLUMN "{column_name}" TO "{temp_column_name}"'
+            )
+            result = datasource.ExecuteSQL(sql_stmt)
+            datasource.ReleaseResultSet(result)
+            column_name = f"{temp_column_name}"
+
         # Rename column
         sql_stmt = (
             f'ALTER TABLE "{layer}" '


Test patch:
diff --git a/tests/test_geofile.py b/tests/test_geofile.py
index 11e0e72..f458bfd 100644
--- a/tests/test_geofile.py
+++ b/tests/test_geofile.py
@@ -1077,6 +1077,13 @@ def test_rename_column(tmp_path, suffix):
     assert "OPPERVL" not in result_layerinfo.columns
     assert "area" in result_layerinfo.columns
 
+    # Rename column with different casing
+    gfo.add_column(str(test_path), "TMP_0", "TEXT")
+    gfo.rename_column(str(test_path), "area", "AREA")
+    result_layerinfo = gfo.get_layerinfo(test_path, raise_on_nogeom=raise_on_nogeom)
+    assert "area" not in result_layerinfo.columns
+    assert "AREA" in result_layerinfo.columns
+
 
 def test_rename_column_unsupported(tmp_path):
     path = test_helper.get_testfile("polygon-parcel", dst_dir=tmp_path, suffix=".shp")