     |> repo.insert_or_update!()
   end
 
+  defp do_sync_both(repo_a, repo_b) do
+    case do_sync_all_resources(repo_a) do
+      :ok ->
+        do_sync_all_resources(repo_b)
+      err -> err
+    end
+  end
+
   defp do_sync_all_resources(repo) do
-    with :ok <- sync_resource(repo, Device, "/api/device"),
+    with :ok  <- sync_resource(repo, Device, "/api/device"),
          :ok <- sync_resource(repo, FarmEvent, "/api/farm_events"),
          :ok <- sync_resource(repo, Peripheral, "/api/peripherals"),
          :ok <- sync_resource(repo, Point, "/api/points"),

  end

  def handle_call(:flip, _, %{repos: [repo_a, repo_b], needs_hard_sync: true} = state) do
    Logger.warn 3, "Forcing full sync."
    BotState.set_sync_status(:syncing)
    do_sync_all_resources(repo_a)
    do_sync_all_resources(repo_b)
    BotState.set_sync_status(:synced)
    copy_configs(repo_b)
    flip_repos_in_cs()

    {:reply, :ok, %{state | repos: [repo_b, repo_a], needs_hard_sync: false}}
  end

  def handle_call(:flip, _, %{repos: [repo_a, repo_b]} = state) do
    Logger.busy 3, "Syncing"
    BotState.set_sync_status(:syncing)
    ConfigStorage.all(SyncCmd)
    |> Enum.sort(&Timex.before?(&1.inserted_at, &2.inserted_at))
    |> Enum.each(&apply_sync_cmd(repo_a, &1))

    flip_repos_in_cs()
    BotState.set_sync_status(:synced)
    copy_configs(repo_b)
    Logger.success 3, "Sync complete."
    {:reply, repo_b, %{state | repos: [repo_b, repo_a]}}
  end

  def handle_call({:register_sync_cmd, remote_id, kind, body}, _from, state) do
    [_current_repo, other_repo] = state.repos
    case SyncCmd.changeset(struct(SyncCmd, %{remote_id: remote_id, kind: kind, body: body})) |> ConfigStorage.insert do
      {:ok, sync_cmd} ->

  end

  @doc false
  def force_hard_sync do
    GenServer.call(__MODULE__, :force_hard_sync)
  end

  @doc false
  def start_link(repos) do
    GenServer.start_link(__MODULE__, repos, [name: __MODULE__])
  end

  def init([repo_a, repo_b]) do
    # Delete any old sync cmds.
    destroy_all_sync_cmds()

    needs_hard_sync = if ConfigStorage.get_config_value(:bool, "settings", "first_sync") || auto_sync?() do
      do_sync_both(repo_a, repo_b)
      ConfigStorage.update_config_value(:bool, "settings", "first_sync", false)
      BotState.set_sync_status(:synced)
      false
