--- move-facebook-specific-data-to-facebook-channels

--- add columns
ALTER TABLE facebook_channels ADD app_id VARCHAR;
ALTER TABLE facebook_channels ADD app_secret VARCHAR;
ALTER TABLE facebook_channels ADD page_token VARCHAR;
ALTER TABLE facebook_channels ADD created_at TIMESTAMPTZ NOT NULL DEFAULT now();
ALTER TABLE facebook_channels ADD updated_at TIMESTAMPTZ NOT NULL DEFAULT now();

--- copy data
UPDATE facebook_channels
SET
  app_id = channels.facebook_app_id,
  app_secret = channels.facebook_app_secret,
  page_token = channels.facebook_page_token
FROM channels
WHERE facebook_channels.id = channels.id;

alter table facebook_channels alter column app_id SET NOT NULL;
alter table facebook_channels alter column app_secret SET NOT NULL;
alter table facebook_channels alter column page_token SET NOT NULL;

--- create indexes
CREATE UNIQUE INDEX fb_channels_page_token_uniq
  ON facebook_channels (page_token)
  WHERE deleted = FALSE;

CREATE UNIQUE INDEX fb_channels_page_id_uniq
  ON facebook_channels (page_id)
  WHERE deleted = FALSE;

CREATE INDEX fb_channels_app_id_idx
  ON facebook_channels (app_id)
  WHERE deleted = FALSE;

--- remove old indexes
DROP INDEX channels_facebook_page_id_uniq;
DROP INDEX channels_facebook_page_token_uniq;

--- remove old columns
ALTER TABLE channels DROP COLUMN facebook_page_id;
ALTER TABLE channels DROP COLUMN facebook_app_id;
ALTER TABLE channels DROP COLUMN facebook_app_secret;
ALTER TABLE channels DROP COLUMN facebook_page_token;


