--
-- Title:      Activities Schema
-- Database:   HSQL
-- Since:      V3.0.0 Schema
--
-- Note: The Activities schema is NOT managed by Hibernate
--


CREATE TABLE alf_activity_post (
  sequence_id bigint generated by default as identity (start with 1),
  post_date timestamp NOT NULL,
  status varchar(10) NOT NULL,
  activity_data varchar(4000) NOT NULL,
  post_user_id varchar(255) NOT NULL,
  job_task_node integer NOT NULL,
  site_network varchar(255) default NULL,
  app_tool varchar(36) default NULL,
  activity_type varchar(255) NOT NULL,
  last_modified timestamp NOT NULL,
  primary key (sequence_id)
);

CREATE INDEX jobtasknode_idx on alf_activity_post(job_task_node);
CREATE INDEX status_idx on alf_activity_post(status);


CREATE TABLE alf_activity_feed (
  id bigint generated by default as identity (start with 1),
  post_id bigint default NULL,
  post_date timestamp NOT NULL,
  activity_summary varchar(4000) default NULL,
  feed_user_id varchar(255) NOT NULL,
  activity_type varchar(255) NOT NULL,
  activity_format varchar(10) default NULL,
  site_network varchar(255) default NULL,
  app_tool varchar(36) default NULL,
  post_user_id varchar(255) NOT NULL,
  feed_date timestamp NOT NULL,
  primary key (id)
);

CREATE INDEX postdate_idx ON alf_activity_feed(post_date);
CREATE INDEX feeduserid_idx ON alf_activity_feed(feed_user_id);
CREATE INDEX postuserid_idx ON alf_activity_feed(post_user_id);
CREATE INDEX sitenetwork_idx ON alf_activity_feed(site_network);
CREATE INDEX activityformat_idx ON alf_activity_feed(activity_format);


CREATE TABLE alf_activity_feed_control (
  id bigint generated by default as identity (start with 1),
  feed_user_id varchar(255) NOT NULL,
  site_network varchar(255) NOT NULL,
  app_tool varchar(36) default NULL,
  last_modified timestamp NOT NULL,
  primary key (id)
);

CREATE INDEX feedcontroluserid_idx ON alf_activity_feed_control(feed_user_id);

--
-- Record script finish
--
DELETE FROM alf_applied_patch WHERE id = 'patch.db-V3.0-0-CreateActivitiesTables';
INSERT INTO alf_applied_patch
  (id, description, fixes_from_schema, fixes_to_schema, applied_to_schema, target_schema, applied_on_date, applied_to_server, was_executed, succeeded, report)
  VALUES
  (
    'patch.db-V3.0-0-CreateActivitiesTables', 'Executed script create V3.0: Created activities tables',
    0, 125, -1, 126, null, 'UNKNOWN', 1, 1, 'Script completed'
  );
