--- data-migration


-- Create a new project.
INSERT INTO projects (
  id,
  name,
  description,
  state,
  created_at,
  updated_at
) SELECT
    nextval('projects_id_seq'),
    'Default project',
    'Automatically created project',
    'active',
    now(),
    now()
  WHERE
    (SELECT count(*)
     FROM channels) > 0;

-- Autoreplies.
UPDATE autoreplies
SET project_id = (
  SELECT max(id)
  FROM projects);

-- Channels.
UPDATE channels
SET project_id = (
  SELECT max(id)
  FROM projects);

-- Chats.
UPDATE chats
SET project_id = (
  SELECT max(id)
  FROM projects);

-- Clients.
UPDATE clients
SET project_id = (
  SELECT max(id)
  FROM projects);

-- Autoreplies.
UPDATE autoreplies
SET project_id = (
  SELECT max(id)
  FROM projects);
