   SELECT st_collect(g.geom)
   FROM
   (
-    SELECT (ST_Dump(old_geom)).geom FROM changes WHERE changeset_id = $1 AND element_type = 'Way'
-    UNION ALL
-    SELECT (ST_Dump(new_geom)).geom FROM changes WHERE changeset_id = $1 AND element_type = 'Way'
-    UNION ALL
-    SELECT ST_Boundary(ST_Buffer(old_geom, 0.00000001, 1)) FROM changes WHERE changeset_id = $1 AND element_type = 'Node'
-    UNION ALL
-    SELECT ST_Boundary(ST_Buffer(new_geom, 0.00000001, 1)) FROM changes WHERE changeset_id = $1 AND element_type = 'Node'
+    SELECT (ST_Dump(geom)).geom FROM changes WHERE changeset_id = $1 AND el_type = 'Way'
+    UNION
+    SELECT (ST_Dump(new_geom)).geom FROM changes WHERE changeset_id = $1 AND el_type = 'Way'
+    UNION
+    SELECT ST_Boundary(ST_Buffer(geom, 0.00000001, 1)) FROM changes WHERE changeset_id = $1 AND el_type = 'Node'
+    UNION
+    SELECT ST_Boundary(ST_Buffer(new_geom, 0.00000001, 1)) FROM changes WHERE changeset_id = $1 AND el_type = 'Node'
   ) g);
 
 UPDATE changesets SET geom = changeset_geom WHERE id = $1;

  geom geometry
);

ALTER TABLE ONLY changes ADD CONSTRAINT pk_changes PRIMARY KEY (id);
ALTER TABLE ONLY changesets ADD CONSTRAINT pk_changesets PRIMARY KEY (id);

DROP INDEX IF EXISTS idx_changes_changeset_id;
CREATE INDEX idx_changes_changeset_id ON changes USING btree (changeset_id);

DROP TABLE IF EXISTS changes;
DROP TABLE IF EXISTS changesets;

DROP TYPE IF EXISTS element_type;
CREATE TYPE element_type AS ENUM ('N', 'W', 'R');

DROP TYPE IF EXISTS action;
CREATE TYPE action AS ENUM ('CREATE', 'MODIFY', 'DELETE');

DROP TYPE IF EXISTS change_type;
CREATE TYPE change_type AS ENUM ('NEW', 'DELETE', 'CHANGE_GEOM', 'CHANGE_TAGS');

-- Create a table for changesets.
CREATE TABLE changesets (
  id bigserial PRIMARY KEY,
  user_id bigint NOT NULL REFERENCES users,
  created_at timestamp without time zone NOT NULL,
  closed_at timestamp without time zone NOT NULL,
  num_changes integer NOT NULL DEFAULT 0,
