<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="208240" data-post-id="208240">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="samwightt" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/samwightt/120/22335_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  samwightt
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="WestKeys" data-post="3" data-topic="38521">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/westkeys/48/21675_2.png" class="avatar"> WestKeys:</div>
<blockquote>
<p>I believe that would remove all the data under <code>:column_name</code></p>
</blockquote>
</aside>
<p>Yep this would remove all data on the column, which is not what the original poster wanted.</p>
<aside class="quote no-group" data-username="WestKeys" data-post="3" data-topic="38521">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/westkeys/48/21675_2.png" class="avatar"> WestKeys:</div>
<blockquote>
<p>But you still need to handle typecasting the actual data in that column</p>
</blockquote>
</aside>
<p>I’m fairly sure this should happen automatically if converting from a string column to an integer column. <a href="https://stackoverflow.com/questions/577712/change-type-of-a-column-with-numbers-from-varchar-to-int" rel="noopener nofollow ugc">See here for conditions when this will fail.</a></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="208240" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/convert-string-column-to-integer-in-db/38521/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-208240" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="208240"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #11"></div>
  </section>
</div>
    <div class="postbit" id="208242" data-post-id="208242">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="benwilson512" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/benwilson512/120/1457_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  benwilson512
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Author of Craft GraphQL APIs in Elixir with Absinthe</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I updated my post.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="208242" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/convert-string-column-to-integer-in-db/38521/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-208242" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="208242"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #12"></div>
  </section>
</div>
    <div class="postbit" id="208267" data-post-id="208267">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="evadne" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/evadne/120/13949_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  evadne
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>For the OP. In Postgres you can define an enum and in Postgrex/Ecto interact with the column as if it was a varchar / text field so you can keep on doing what you are doing in the Elixir app. Using integer fields to back an application-defined enum field smells like what Rails would do and is not really recommended because when you look at the DB you just see a bunch of numbers.</p>
<p>Please study <a href="https://www.postgresql.org/docs/current/datatype-enum.html" class="inline-onebox" rel="noopener nofollow ugc">PostgreSQL: Documentation: 18: 8.7.&nbsp;Enumerated Types</a></p>
<p>If you insist, you can use a case statement to avoid doing multiple updates</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="208267" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/convert-string-column-to-integer-in-db/38521/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-208267" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="208267"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #13"></div>
  </section>
</div>
    <div class="postbit" id="208280" data-post-id="208280">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="amnu3387" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  amnu3387
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>This is one of those questions that look fairly simple but in practical terms can be quite complex, building on what ben said,</p>
<h3><a name="p-208280-step-1-1" class="anchor" href="#p-208280-step-1-1" aria-label="Heading link" rel="nofollow"></a>step 1</h3>
<ul>
<li>add the new column</li>
</ul>
<h3><a name="p-208280-step-2-2" class="anchor" href="#p-208280-step-2-2" aria-label="Heading link" rel="nofollow"></a>step 2</h3>
<ul>
<li>change either the db or app to be able to automatically save the new values as well as the old ones</li>
<li>update all existing rows to have the new column filled</li>
</ul>
<h3><a name="p-208280-step-3-3" class="anchor" href="#p-208280-step-3-3" aria-label="Heading link" rel="nofollow"></a>step 3</h3>
<ul>
<li>roll out a deploy to a new version of your app that only uses the new column in its schema definition and can translate requests using the old schema into new ones</li>
<li>if you have a frontend as well do the same there</li>
</ul>
<h3><a name="p-208280-step-4-4" class="anchor" href="#p-208280-step-4-4" aria-label="Heading link" rel="nofollow"></a>step 4</h3>
<ul>
<li>if you created a db trigger on 2, drop it here too</li>
<li>delete the old column</li>
</ul>
<p>This would solve the question even if you have many nodes deployed but it really depends on - is it ok if some request errors out while we’re doing this? Or it’s of the uttermost importance that not a single request is botched. Depending on the answer for that you can make it easier or more complex.</p>
<h4><a name="p-208280-note-on-step-2-5" class="anchor" href="#p-208280-note-on-step-2-5" aria-label="Heading link" rel="nofollow"></a>note on step 2</h4>
<p>can be either at the app level (changing the code paths that save those records to now save also the new col) or at the db level with a trigger that auto populates the new column based on the value of the old one whenever an insert or update is done for that table. This is needed because if someone would save a record between you updating the existing rows and you having finished the deployment of the new app version, it would still only save the old column for those requests.</p>
<h4><a name="p-208280-note-on-step-3-6" class="anchor" href="#p-208280-note-on-step-3-6" aria-label="Heading link" rel="nofollow"></a>note on step 3</h4>
<p>gets more complex if you’re using a spa client, as they won’t receive the new “bundle” until they refresh their page. So even if you do all these steps they will be stuck on a version of code that expects the old schema and functions with the old schema alone (forms, etc). The best UX is to somehow display a warning to the user telling them they should refresh (like adding a warning/info when trying to save a form and you detect that the form only has the old column and not the new one). With html only (no spa clients) you can just redirect or serve the new version on submission.</p>
<p>It kinda boils down to what you need to guarantee while doing this. Sometimes it’s ok to go YOLO.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="208280" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/convert-string-column-to-integer-in-db/38521/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-208280" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="208280"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>