DO $$
DECLARE
 version_new_ CONSTANT integer := 3;-- new version number --
 ver_ integer;
BEGIN
  LOCK TABLE variables;
  SELECT version_is('Ls') INTO ver_;
  IF ver_ < version_new_ THEN
    -- begin update --

-- Function: device_insert_or_update(bigint, integer)

-- DROP FUNCTION device_insert_or_update(bigint, integer);

CREATE OR REPLACE FUNCTION device_insert_or_update(
    device_id_ bigint,
    frequency_ integer)
  RETURNS void AS
$BODY$
BEGIN
  INSERT INTO device(device_id, frequency, online)
    VALUES (device_id_, frequency_, now());
EXCEPTION WHEN unique_violation THEN
  UPDATE device SET frequency = frequency_, online=now() WHERE device_id = device_id_;
END;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION device_insert_or_update(bigint, integer)
  OWNER TO su;

INSERT INTO object_settings_type(_otype, key, name, descr, type, min_value, max_value) VALUES (3, 'DeviceExpire', 'Device expire', 'Device listening downlink expire', 'time_period', '30000', '3600000');

PERFORM objects_add_setting(3, 'DeviceExpire', '120000');

    -- end update --
    PERFORM version_set('Ls', version_new_);
  END IF;
END$$;