DO $$
  BEGIN
    IF EXISTS
        ( SELECT 1
          FROM   information_schema.tables
          WHERE  table_schema = 'public'
          AND    table_name = 'qna_table'
        )
    THEN
      UPDATE qna_table
      SET state_id = state_none.id
      FROM
        (SELECT id FROM qna_state_table WHERE name = 'STATE_NONE') AS state_none,
        (SELECT id FROM qna_state_table WHERE name = 'STATE_RELABELLING') AS state_relabelling
      WHERE state_id = state_relabelling.id;
    END IF;
  END
$$;

DO $$
  BEGIN
    IF EXISTS
        ( SELECT 1
          FROM   information_schema.tables
          WHERE  table_schema = 'public'
          AND    table_name = 'qna_version_table'
        )
    THEN
      UPDATE qna_version_table
      SET state_id = state_none.id
      FROM
        (SELECT id FROM qna_state_table WHERE name = 'STATE_NONE') AS state_none,
        (SELECT id FROM qna_state_table WHERE name = 'STATE_RELABELLING') AS state_relabelling
      WHERE state_id = state_relabelling.id;
    END IF;
  END
$$;

DO $$
  BEGIN
    IF EXISTS
        ( SELECT 1
          FROM   information_schema.tables
          WHERE  table_schema = 'public'
          AND    table_name = 'qna_state_change_table'
        )
    THEN
      UPDATE qna_state_change_table
      SET from_state_id = state_none.id
      FROM
        (SELECT id FROM qna_state_table WHERE name = 'STATE_NONE') AS state_none,
        (SELECT id FROM qna_state_table WHERE name = 'STATE_RELABELLING') AS state_relabelling
      WHERE from_state_id = state_relabelling.id;
    END IF;
  END
$$;

DO $$
  BEGIN
    IF EXISTS
        ( SELECT 1
          FROM   information_schema.tables
          WHERE  table_schema = 'public'
          AND    table_name = 'qna_state_change_table'
        )
    THEN
      UPDATE qna_state_change_table
      SET to_state_id = state_none.id
      FROM
        (SELECT id FROM qna_state_table WHERE name = 'STATE_NONE') AS state_none,
        (SELECT id FROM qna_state_table WHERE name = 'STATE_RELABELLING') AS state_relabelling
      WHERE to_state_id = state_relabelling.id;
    END IF;
  END
$$;

DO $$
  BEGIN
    IF EXISTS
        ( SELECT 1
          FROM   information_schema.tables
          WHERE  table_schema = 'public'
          AND    table_name = 'qna_state_table'
        )
    THEN
      DELETE FROM qna_state_table
      WHERE name = 'STATE_RELABELLING';
    END IF;
  END
$$;
