 
 -- create a new scenario
 
-CREATE OR REPLACE FUNCTION create_scenario(fc jsonb)
-  RETURNS integer AS
+CREATE OR REPLACE FUNCTION create_scenario(scName text, fc jsonb)
+  RETURNS bigint AS
 $func$
 DECLARE
-  scID integer;
+  ScID bigint;
   curTime TIMESTAMP;
-  geomID_max integer;
+  geomID_max bigint;
 BEGIN
   -- get time of insertion
   curTime := CURRENT_TIMESTAMP;
 
-  -- create a new scenario and keep its id in scID variable
-  INSERT INTO scenario DEFAULT VALUES RETURNING scenario.id INTO scID;
+  -- create a new scenario and keep its id in ScID variable
+  INSERT INTO scenario (name) VALUES (scName) RETURNING scenario.id INTO ScID;
 
   -- insert all scenario properties
   INSERT INTO scenario_prop_history (scenario_id, name, ts_update, value)
-       SELECT scID, sprops.key, curTime, sprops.value
+       SELECT ScID, sprops.key, curTime, sprops.value
          FROM jsonb_each_text(jsonb_strip_nulls(fc -> 'properties') ) sprops;
   INSERT INTO scenario_prop (scenario_id, name, last_update)
        SELECT ph.scenario_id, ph.name, ph.ts_update
          FROM scenario_prop_history ph
-        WHERE ph.ts_update = curTime AND ph.scenario_id = scID;
+        WHERE ph.ts_update = curTime AND ph.scenario_id = ScID;
 
   -- parse all features
   CREATE TEMP TABLE features ON COMMIT DROP AS


-- create all tables in DB

CREATE TABLE sc_geometry (scenario_id integer NOT NULL, id integer NOT NULL, last_update timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL, alive bool DEFAULT 'TRUE' NOT NULL, PRIMARY KEY (scenario_id, id));
CREATE TABLE sc_geometry_history (scenario_id integer NOT NULL, id integer NOT NULL, ts_update timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL, ts_next_update timestamp, PRIMARY KEY (scenario_id, id, ts_update));
SELECT AddGeometryColumn ('sc_geometry_history','geom',4326,'Geometry',3);
CREATE TABLE sc_geometry_prop (scenario_id integer NOT NULL, geometry_id integer NOT NULL, name text NOT NULL, last_update timestamp DEFAULT NULL NOT NULL, alive bool DEFAULT 'TRUE' NOT NULL, PRIMARY KEY (scenario_id, geometry_id, name));
CREATE TABLE sc_geometry_prop_history (scenario_id integer NOT NULL, geometry_id integer NOT NULL, name text NOT NULL, ts_update timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL, ts_next_update timestamp DEFAULT NULL, value text NOT NULL, PRIMARY KEY (scenario_id, geometry_id, name, ts_update));
CREATE TABLE scenario (id SERIAL, alive bool DEFAULT 'TRUE' NOT NULL, PRIMARY KEY (id));
CREATE TABLE scenario_prop (scenario_id integer NOT NULL, name text NOT NULL, last_update timestamp DEFAULT NULL NOT NULL, alive bool DEFAULT 'TRUE' NOT NULL, PRIMARY KEY (scenario_id, name));
CREATE TABLE scenario_prop_history (scenario_id integer NOT NULL, name text NOT NULL, ts_update timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL, ts_next_update timestamp DEFAULT NULL, value text NOT NULL, PRIMARY KEY (scenario_id, name, ts_update));
CREATE INDEX sc_geometry_scenario_id ON sc_geometry (scenario_id);
CREATE INDEX sc_geometry_id ON sc_geometry (id);
CREATE INDEX sc_geometry_history_scenario_id ON sc_geometry_history (scenario_id);

ALTER TABLE sc_geometry_prop DROP CONSTRAINT latest_geom_prop;
ALTER TABLE sc_geometry DROP CONSTRAINT latest_geometry;
ALTER TABLE scenario_prop DROP CONSTRAINT latest_sc_prop;
ALTER TABLE sc_geometry_prop_history DROP CONSTRAINT prev_geom_prop_version;
ALTER TABLE sc_geometry_history DROP CONSTRAINT prev_geometry_version;
ALTER TABLE scenario_prop_history DROP CONSTRAINT prev_sc_prop_version;
ALTER TABLE sc_geometry_prop DROP CONSTRAINT sc_geometry_properties;
ALTER TABLE sc_geometry DROP CONSTRAINT scenario_content;
ALTER TABLE scenario_prop DROP CONSTRAINT scenario_properies;
