From 1747a25c0c6eb6eab50a194da6ecef25f4fb5c9e Mon Sep 17 00:00:00 2001 From: neodarz Date: Mon, 13 Feb 2017 13:17:10 +0100 Subject: Add fix for the id when you add the first entry in the database --- web/controllers/audio_file_api_controller.ex | 28 ++++++++++++++++++++++++---- web/controllers/audio_file_controller.ex | 28 ++++++++++++++++++++++++---- 2 files changed, 48 insertions(+), 8 deletions(-) (limited to 'web') diff --git a/web/controllers/audio_file_api_controller.ex b/web/controllers/audio_file_api_controller.ex index 3dc278e..0ba0b54 100644 --- a/web/controllers/audio_file_api_controller.ex +++ b/web/controllers/audio_file_api_controller.ex @@ -11,11 +11,31 @@ defmodule TheTranscriberBackend.AudioFileAPIController do def create(conn, %{"audio_file" => %{"audio_duration" => audio_duration, "audio_path" => upload, "audio_name" => audio_name}}) do - query = "select nextval('audio_file_id_seq')" - result = Ecto.Adapters.SQL.query!(Repo, query, []) - [[repo_last_id]] = result.rows # A beautiful pattern match :) + query_table_is_empty = "select * from audio_file;" + result_table_is_empty = Ecto.Adapters.SQL.query!(Repo, query_table_is_empty, []) + result_table_is_empty_rows = result_table_is_empty.rows + case [] do + ^result_table_is_empty_rows -> + repo_last_id = 1 - path = "/media/phoenix_test/#{repo_last_id}_#{upload.filename}" + # Don't ask why, but it would seem that you must select nextval + # BEFORE currval... Yes ! Seriously... + query_nextval = "select nextval('audio_file_id_seq');" + result_nextval = Ecto.Adapters.SQL.query!(Repo, query_nextval, []) + + query_setval = "select setval('audio_file_id_seq', 1);" + result_setval = Ecto.Adapters.SQL.query!(Repo, query_setval, []) + + query_currval= "select currval('audio_file_id_seq');" + result_currval = Ecto.Adapters.SQL.query!(Repo, query_currval, []) + + _ -> + query_currval = "select max(id) from audio_file;" + result_currval = Ecto.Adapters.SQL.query!(Repo, query_currval, []) + [[repo_last_id]] = result_currval.rows # A beautiful pattern match :) + end + + path = "/media/phoenix_test/#{repo_last_id + 1}_#{upload.filename}" File.cp(upload.path, path) changeset = AudioFile.changeset(%AudioFile{}, diff --git a/web/controllers/audio_file_controller.ex b/web/controllers/audio_file_controller.ex index c4e1af0..e0f32e6 100644 --- a/web/controllers/audio_file_controller.ex +++ b/web/controllers/audio_file_controller.ex @@ -15,11 +15,31 @@ defmodule TheTranscriberBackend.AudioFileController do def create(conn, %{"audio_file" => %{"audio_duration" => audio_duration, "audio_path" => upload, "audio_name" => audio_name}}) do - query = "select nextval('audio_file_id_seq')" - result = Ecto.Adapters.SQL.query!(Repo, query, []) - [[repo_last_id]] = result.rows # A beautiful pattern match :) + query_table_is_empty = "select * from audio_file;" + result_table_is_empty = Ecto.Adapters.SQL.query!(Repo, query_table_is_empty, []) + result_table_is_empty_rows = result_table_is_empty.rows + case [] do + ^result_table_is_empty_rows -> + repo_last_id = 1 + + # Don't ask why, but it would seem that you must select nextval + # BEFORE currval... Yes ! Seriously... + query_nextval = "select nextval('audio_file_id_seq');" + result_nextval = Ecto.Adapters.SQL.query!(Repo, query_nextval, []) + + query_setval = "select setval('audio_file_id_seq', 1);" + result_setval = Ecto.Adapters.SQL.query!(Repo, query_setval, []) + + query_currval= "select currval('audio_file_id_seq');" + result_currval = Ecto.Adapters.SQL.query!(Repo, query_currval, []) + + _ -> + query_currval = "select max(id) from audio_file;" + result_currval = Ecto.Adapters.SQL.query!(Repo, query_currval, []) + [[repo_last_id]] = result_currval.rows # A beautiful pattern match :) + end - path = "/media/phoenix_test/#{repo_last_id + 1}_#{upload.filename}" + path = "/media/phoenix_test/#{repo_last_id}_#{upload.filename}" File.cp(upload.path, path) changeset = AudioFile.changeset(%AudioFile{}, -- cgit v1.2.1