TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Zero-downtime database migrations

101 pointsby fredsters_salmost 11 years ago

8 comments

dlubarovalmost 11 years ago
&gt; We also use Postgres which doesn&#x27;t create the obstacles that some other database systems have, for example with locking tables on schema changes.<p>Actually schema changes still require exclusive table locks, they&#x27;re just held very briefly since Postgres doesn&#x27;t need to rewrite each tuple. (With some exceptions, like adding a NOT NULL column, or reducing the size of a VARCHAR.)<p>I don&#x27;t mean to be pedantic, it can be important because the exclusive lock can be blocked by other long-running queries. Then Postgres will try to grant the alter table&#x27;s exclusive lock before other conflicting locks, so normal reads and writes will be locked out until the long query completes or one of the queries is killed.
评论 #8022190 未加载
评论 #8023916 未加载
dangrossmanalmost 11 years ago
pt-online-schema-change, part of the Percona Toolkit, is my go-to tool for making production changes in MySQL.<p>Improvely and W3Counter both have tons of multi-gigabyte tables. A regular ALTER TABLE statement could take hours to run, and would lock the table the entire time, essentially taking down the service.<p>Percona&#x27;s tool makes a copy of the table, runs the ALTER TABLE statements on the copy, then sets up triggers on the original to temporarily mirror data-altering queries to the copy. It then intelligently batches the data migration from the old table to the new one to avoid overloading the server, while printing progress and ETA on the console. When the copying&#x27;s done, the old table is dropped and the modified one renamed in a single transaction.<p>It takes just one command and has always worked flawlessly.<p><a href="http://www.percona.com/doc/percona-toolkit/2.2/pt-online-schema-change.html" rel="nofollow">http:&#x2F;&#x2F;www.percona.com&#x2F;doc&#x2F;percona-toolkit&#x2F;2.2&#x2F;pt-online-sch...</a>
评论 #8023388 未加载
评论 #8021844 未加载
评论 #8022022 未加载
评论 #8021841 未加载
评论 #8023654 未加载
评论 #8022612 未加载
jpollockalmost 11 years ago
The column change phase 2 has a sneaky bug in it.<p>In the order presented:<p>1) Run migration<p>2) Apply trigger<p>There will be a point in time where the migration has been done and the trigger hasn&#x27;t been applied.<p>Even sneakier, if the trigger is applied in code and not in the database, there will be period in time where some of the code is running the trigger and some isn&#x27;t.<p>The trigger needs to be in production and executing prior to the backfill running.<p>I&#x27;m also loathe to rely on &quot;triggers&quot; in application model logic. I&#x27;ve seen too many platforms where the operations engineers interact directly with the database, resulting in required logic not executing.
评论 #8022212 未加载
oelmekkialmost 11 years ago
Good idea for the fake delete column. I&#x27;ve always seen non rollback-able column removal migrations as something we just have to do with, this totally solves the problem.<p>As for when is the good time to actually remove the column, I think this can merge pretty well with my current thinking about migrations.<p>A new developer joined my company (we use rails as well). When setting things up, he tried to run `rake db:migrate` (my fault, I mentioned it in my doc instead of `rake db:schema:load`, I wrote doc as I was setting up the very first lines of the project). There was several years of migrations, some of them not working anymore since they used code that does not exist anymore (like `#add_hstore_index` from activerecord-postgres-hstore, which has been replaced by native postgres support).<p>This made me thinks that there is no need to maintain migrations in repository if we can&#x27;t use them anymore. And thus, from time to time, migrations should be flushed. This flush should be the perfect time to actually delete columns (well, this should be done just before). That way, we ensure our migrations can constantly be rolled back, without breaking anything.<p>Edit : refining on that, we probably don&#x27;t want to flush all migrations, but only the older ones. So, how do we insert the migration to actually remove data ? We can&#x27;t just put it between old migrations and the ones we&#x27;ll keep, since it would be considered already migrated. We can&#x27;t remove it directly in sql console either, or we would have to do it on every machine and this would caused desync among developers machines.<p>I think the best way to do it would be to append a new non-reversible (but that does not break on reverse) migration, that uses `table_exists?` and `column_exists?` to ensure it is only ran on databases that contains the deprecated columns &#x2F; tables. Something like :<p><pre><code> class RemoveDeprecated &lt; ActiveRecord::Migration def up remove_column :foos, :bar_deprecated if column_exists? :foos, :bar_deprecated end def down end end</code></pre>
评论 #8024296 未加载
jipiboilyalmost 11 years ago
JP, author of the post here - let me know if you have any additions &#x2F; questions!
评论 #8021826 未加载
评论 #8022166 未加载
评论 #8021884 未加载
评论 #8023884 未加载
msherryalmost 11 years ago
When I saw this, I first thought it was an extension of <a href="https://news.ycombinator.com/item?id=5326159" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=5326159</a> ;)
评论 #8021946 未加载
juntoalmost 11 years ago
For .NET developers I can recommend Fluent Migrator. Works brilliantly with Octopus Deploy.
wikwocketalmost 11 years ago
I prefer our approach:<p><i>&gt; Case #1: I want to add a new column</i><p>Issue an ALTER TABLE ADD COLUMN on the production database. Try not to make it NOT NULL.<p><i>&gt; Case #2: I want to change a column type</i><p>Add another column (see Case #1) of the same name, with a suffix like &quot;_2&quot; or &quot;_NUM&quot; so you can tell them apart.<p><i>&gt; Case #3: I want to rename a column</i><p>Don&#x27;t risk it. Keep using the original column name. Very few of the column names reflect the reality of that they contain anyway. It&#x27;s little bits of insider knowledge like this (&quot;Product name is actually stored in ITEMNAME_LONG, ITEMNAME is only a CHAR(15) so we keep UPC in there&quot;) that contribute to our fast-paced, agile work environment.