unepwcmc/ProtectedPlanet — pull request #96 by migtorres (2014-08-21 15:35:30+00:00)
Title: Merging and keeping the same CartoDB table names

This one is tricky, but I think it is the only way to import and get the same CartoDB table names. This happens because we can create tables using the CartoDB API.

# What is the objective?

We were creating a new table every month in CartoDB (e.g. wdpapoly_july2014_0). This is a problem because we have to manually redirect the map and restyle it. We need to have a default table for points and a default table for poins.

# How do we fix this?
Basically we delete the old records from the default table and then we insert there the new ones after merging all the import tables. In the end we also delete the major import table.
All of this changes were made in CartoDb::Merger.merge so everything runs smoothly as before.

# Major concerns
The trickiest bit of code is this one:

```
@default_table = table_names[0].split('_')[0..1].join('_').split('_')[0..1].join('_')
```
We are assuming that the tables have the "wdpa_polygon_" and "wdpa_point_" preffixes. We are splitting it on the underscores and then joining them so we have wdpa_ polygon and wdpa_point as default tables. This really depends on the name that comes in the File Geodatabase so we must alert the PA team about it.