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

  SELECT EXISTS (SELECT true FROM information_schema.tables WHERE  table_schema = 'public' AND table_name = 'object_log') INTO exists_;

IF NOT exists_ THEN

-- Table: object_log

-- DROP TABLE object_log;

CREATE TABLE object_log
(
  _id bigserial,
  _object integer NOT NULL,
  period_time timestamp with time zone NOT NULL,
  thread_name text,
  work_name text,
  total_time bigint DEFAULT 1,
  circles bigint DEFAULT 0,
  work_time bigint DEFAULT 0,
  longest_work bigint DEFAULT 0,
  CONSTRAINT object_log_pkey PRIMARY KEY (_id),
  CONSTRAINT object_log__object_fkey FOREIGN KEY (_object)
      REFERENCES object (_id) MATCH SIMPLE
      ON UPDATE CASCADE ON DELETE CASCADE
)
WITH (
  OIDS=FALSE
);
ALTER TABLE object_log
  OWNER TO su;
GRANT ALL ON TABLE object_log TO su;
GRANT SELECT, UPDATE ON TABLE object_log TO usr;

-- Index: object_log__object__ostype_idx

-- DROP INDEX object_log__object__ostype_idx;

CREATE INDEX object_log__object__ostype_idx
  ON object_log
  USING btree
  (_object, period_time);
END IF;

PERFORM objects_add_setting(5, 'SyncEvents', '0');
PERFORM objects_add_setting(5, 'SyncLogs', '1');

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