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

--- add columns
ALTER TABLE facebook_channels ADD app_id VARCHAR(255);
ALTER TABLE facebook_channels ADD app_secret VARCHAR(255);
ALTER TABLE facebook_channels ADD page_token VARCHAR(255);
ALTER TABLE facebook_channels ADD created_at DATETIMEOFFSET NOT NULL DEFAULT SYSDATETIMEOFFSET();
ALTER TABLE facebook_channels ADD updated_at DATETIMEOFFSET NOT NULL DEFAULT SYSDATETIMEOFFSET();

--- 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 VARCHAR(255) NOT NULL;
alter table facebook_channels alter column app_secret VARCHAR(255) NOT NULL;
alter table facebook_channels alter column page_token VARCHAR(255) NOT NULL;

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

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

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

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

--- 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;
