<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Moaad FATTALI on Medium]]></title>
        <description><![CDATA[Stories by Moaad FATTALI on Medium]]></description>
        <link>https://medium.com/@f.moaad?source=rss-b8ccc74940e4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*xvu9DzyfJvd_XutATDH8eg.png</url>
            <title>Stories by Moaad FATTALI on Medium</title>
            <link>https://medium.com/@f.moaad?source=rss-b8ccc74940e4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 00:59:30 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@f.moaad/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Using PostgreSQL views to ensure backwards-compatible, non-breaking migrations]]></title>
            <link>https://medium.com/ovrsea/using-postgresql-views-to-ensure-backwards-compatible-non-breaking-migrations-017288e77f06?source=rss-b8ccc74940e4------2</link>
            <guid isPermaLink="false">https://medium.com/p/017288e77f06</guid>
            <dc:creator><![CDATA[Moaad FATTALI]]></dc:creator>
            <pubDate>Sat, 06 Jan 2024 19:25:16 GMT</pubDate>
            <atom:updated>2024-01-08T17:21:02.839Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Y4ANPlujWgE9t752J0UJqQ.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@karlpb?utm_content=creditCopyText&amp;utm_medium=referral&amp;utm_source=unsplash">Karl Paul Baldacchino</a> on <a href="https://unsplash.com/photos/brown-elephant-walking-on-brown-field-during-daytime-A3Tp9XbR3BU?utm_content=creditCopyText&amp;utm_medium=referral&amp;utm_source=unsplash">Unsplash</a></figcaption></figure><h3>TL;DR</h3><p>You can use PostgreSQL views to rename tables and columns in a backward-compatible way.</p><h4>Renaming a table from foo to bar:</h4><h4>Step 1:</h4><pre>CREATE VIEW bar AS SELECT * FROM foo; <br>-- creates an &quot;alias&quot; for the table: <br>-- you can now address it as foo (real table) or bar (table alias)<br>-- it will point to the same table</pre><h4>Step 2:</h4><p>Change the code to use bar instead of foo and deploy</p><h4>Step 3:</h4><p>Once you have decommissioned the old code that references the table as foo , you can run a (transactional!) migration with</p><pre>DROP VIEW bar;<br>ALTER TABLE foo RENAME to bar;</pre><p>Works also with renaming columns in a similar fashion !</p><h3>General principle for non-breaking / backwards-compatible / no downtime migrations</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CV2HMtzi9aRqAew6n_drzw.png" /></figure><p>Why is it important to have backwards-compatible migrations ?</p><p>The alternative is to wait for ongoing processes to finish and then shut off your app, run your migration, and then restart your app. While this may be acceptable for non-critical, low traffic systems, it is generally not something you can afford to do.</p><p>Even if you <em>can </em>reasonably<em> </em>introduce downtime during your migrations, doing so discourages you to adopt continuous deployment practices: if your deployments degrade user experience by causing downtime, you’ll be incentivized to deploy less.</p><p>Additionally, backwards-compatible migrations are much less stressful to deploy, even if they require a little more upfront work. In the chance that something goes wrong, it’s less likely to break production.</p><h3>Introduction to Views</h3><p>One can think of views as <em>named queries</em>.</p><p>Instead of having to explicitly having to write this query everytime:</p><pre>SELECT column1, column2 FROM some_table LEFT JOIN some_other_table ON condition;</pre><p>You can create a view:</p><pre>CREATE VIEW my_complex_query AS SELECT column1, column2 FROM some_table LEFT JOIN some_other_table ON condition</pre><p>and operate on my_complex_query directly.</p><p>We can also see my_complex_query as a computed table !</p><h3>Renaming a table</h3><p>Let’s consider a simple case where we had a table named invoices — containing billing data of our clients. This name was fine until we started handling the invoices from our suppliers in our system ! To avoid confusion, we must now express the separation between client invoices and supplier invoices. This requires us to rename our old invoices table ;).</p><h3>Naive way</h3><p>We can do so by running a simple RENAME:</p><pre>ALTER TABLE invoices RENAME TO client_invoices;</pre><p>But this would break old, still running code :(</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GsB49Qk8jPP7NLwV0GA9aQ.png" /></figure><h3>Using views</h3><p>This time, we won’t simply rename — we will also create a view to keep the old code working:</p><pre>ALTER TABLE invoices RENAME TO client_invoices;<br>CREATE VIEW invoices AS SELECT id, ... FROM client_invoices;</pre><p>That way, we can address this table as invoices OR client_invoices, and postgreSQL makes sure the two are perfectly in sync!</p><p>Make sure this migration is transactional, so your database is always in the initial state or the final state ! (i.e client_invoices is always defined either as a view or as the new name of the table)</p><p>When we finally decommission the old code, the app completely forgot about the invoices table. We can run a migration to safely remove the View:</p><pre>DROP VIEW invoices;</pre><p>You have now successfully renamed table invoices to client_invoices without downtime or breaking changes !</p><h3>Renaming a column</h3><p>Using the exact same principle, we can rename columns — the downside is we will be forced to rename the table as well (Take the opportunity to give it a more meaningful name, or use the technique again to rename it back, choice is yours!)</p><h4>Naive way</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_dj_7AKms1NZOS8FrEFiYA.png" /></figure><h4>Using views</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*q0XmkZK180iSayuXUHeihw.png" /></figure><h3>Conclusion</h3><p>We’ve seen two simple examples where PostgreSQL Views can help us refactor our database structure without introducing breaking changes from the code’s perspective.</p><p>You can see a code illustration of the first example (renaming table) <a href="https://github.com/surizom/migrations-with-views/blob/main/viewMigrations.test.ts">here</a></p><p>For the sake of simplicity, the code in the examples was written in SELECT. For simple views such as the ones we used and for PostgreSQL 9.3+ — UPDATE, INSERT, DELETE statements also work !</p><p>See <a href="https://www.postgresql.org/docs/current/sql-createview.html">postgreSQL doc</a> for more info:</p><blockquote><em>A view is automatically updatable if it satisfies all of the following conditions:</em></blockquote><blockquote>The view must have exactly one entry in its FROM list, which must be a table or another updatable view.</blockquote><blockquote>The view definition must not contain WITH, DISTINCT, GROUP BY, HAVING, LIMIT, or OFFSET clauses at the top level.</blockquote><blockquote>The view definition must not contain set operations (UNION, INTERSECT or EXCEPT) at the top level.</blockquote><blockquote>The view’s select list must not contain any aggregates, window functions or set-returning functions.</blockquote><p>Keep in mind that views are just virtual tables that can be defined by any SELECT query. Depending on the use case, you can use them to handle much more complex refactoring operations:</p><ul><li>splitting tables,</li><li>regrouping them,</li><li>moving tables or columns around,</li><li>organizing them in domain-bounded schemas</li></ul><p>and make your database schema (almost!) as malleable as your code.</p><p>There are many other use cases to views I find useful that I didn’t mention in this article, for example:</p><ul><li>We can use views as a “database API”, either to simplify a complex database model for a read use case or to expose a clean interface for downstream data consumers and avoid coupling</li><li>We can use views for frequently used queries which we want to name with minimal overhead</li><li>In read use cases where we can afford eventual consistency, we can use materialized views for read use cases to greatly enhance performance on expensive reads. If you have a very complex query with a lot of joins that takes a long time to load, you can cache the result in a materialized view and put as many relevant indexes as you’d like !</li></ul><p>So I can only encourage you to use them liberally :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=017288e77f06" width="1" height="1" alt=""><hr><p><a href="https://medium.com/ovrsea/using-postgresql-views-to-ensure-backwards-compatible-non-breaking-migrations-017288e77f06">Using PostgreSQL views to ensure backwards-compatible, non-breaking migrations</a> was originally published in <a href="https://medium.com/ovrsea">OVRSEA</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Power up your Node.js debugging and error handling with the new Error cause feature]]></title>
            <link>https://medium.com/ovrsea/power-up-your-node-js-debugging-and-error-handling-with-the-new-error-cause-feature-4136c563126a?source=rss-b8ccc74940e4------2</link>
            <guid isPermaLink="false">https://medium.com/p/4136c563126a</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[node]]></category>
            <category><![CDATA[error]]></category>
            <category><![CDATA[debugging]]></category>
            <category><![CDATA[causes]]></category>
            <dc:creator><![CDATA[Moaad FATTALI]]></dc:creator>
            <pubDate>Tue, 25 Jan 2022 16:15:18 GMT</pubDate>
            <atom:updated>2022-01-29T10:22:40.467Z</atom:updated>
            <content:encoded><![CDATA[<h3>TL;DR:</h3><p>A new feature named <em>“Error cause” </em>was recently introduced in Node.js and in browsers. The feature allows you to chain errors like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1d577abf76eaec6ac8e0ea5161e086d6/href">https://medium.com/media/1d577abf76eaec6ac8e0ea5161e086d6/href</a></iframe><p>This feature already exists in other languages and platforms like Java, Python or .NET under various names : <em>Inner Exception</em>, <em>Nested Exception</em>, <em>Error origin</em>, <em>Error from</em>, <em>Exception chaining</em>, etc.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*F7XXPlFt1TdXDlmAr7HMnA.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@dav420?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">David Pupaza</a> on <a href="https://unsplash.com/license?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><h3>The Problem</h3><p>If you’re a software engineer like me, I don’t need to tell you how <strong>hard</strong> debugging can be.</p><p>More often than not, the most cumbersome bugs are the ones where we <strong>don’t have enough context</strong>:</p><ul><li>🤔 Why do we have a “<em>Request failed</em>” while trying to call this external API ?</li><li>🤔 Why did our microservice throw a <em>“Failed to save entity”</em> error in this particular case ?</li><li>🤔 What does this error message mean ?</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/884/0*vTBSHE3hZXDlcoJY" /></figure><p>Generic errors like these certainly don’t help us in understanding, reproducing or fixing failures in our software.</p><p>In this article, I aim to provide you with a way to avoid these kinds of situations at <strong>little to no cost</strong>, thanks to a new feature proposed by the <a href="https://tc39.es/">TC39 committee</a>.</p><p>To illustrate this feature, we will consider a very simple example, where we try to fetch two objects, <strong>foo</strong> and <strong>bar</strong>, from an API.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aa475a87e41487428f42ca96f3e28b7b/href">https://medium.com/media/aa475a87e41487428f42ca96f3e28b7b/href</a></iframe><p>We see a big problem with our error handling: It is difficult, when the <em>apiFetch</em> function throws an error, to identify the step at which the error occurred. Were we fetching foo or bar at the time of the error? Indeed, our error lacks <strong>context.</strong></p><p>To remedy this, we can try to throw an error a bit higher so we can identify the step at which the error occurred:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bc9ff2033de222675ddec937fcada386/href">https://medium.com/media/bc9ff2033de222675ddec937fcada386/href</a></iframe><p>We added context to our error, but we also lost something important!</p><p>When the fetch<em> </em>function throws an error, the error contains details such as: request payload, HTTP status, error code returned by the API… (the details depends on the http client library you’re using)</p><p>By catching this error and rethrowing a brand new generic one (“An error has occurred while X”), we lose all of this precious information. Indeed, our error lacks <strong>content.</strong></p><h3>The old solution</h3><p>We could try to instantiate the error like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f2e65225ab8767c57bc61313ec008984/href">https://medium.com/media/f2e65225ab8767c57bc61313ec008984/href</a></iframe><p>But this approach has its drawbacks:</p><ul><li>😕 Only the <strong>error message</strong> of <em>apiError</em> is added to the newly constructed Error ! Among potentially other things, we won’t have access to the <a href="https://nodejs.org/dist/latest-v17.x/docs/api/errors.html#errorstack">stack trace</a> <em>(loss of information)</em></li><li>😕 When having more than two errors, it can be hard to identify where <strong>one error starts and the other ends</strong> <em>(decreased readability of logs)</em></li><li>😕 This doesn’t explicit the <strong>intent</strong> of our code, which is to express that the new Error is caused by the apiError we’re catching <em>(room for improvement in the readability of our code)</em></li></ul><h3>The new solution</h3><p>Thankfully, Starting with node <strong>16.9.0</strong>, we can add a <strong>cause</strong> to our error constructor, like so:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9737ff960c1381d0a030448380010d00/href">https://medium.com/media/9737ff960c1381d0a030448380010d00/href</a></iframe><p>This way, we can build causality links between our errors !</p><p>Thanks to this feature, at each step of our code that can go wrong (ex: fetching foo), we’ll catch errors thrown by functions below, throw an error that specifies <strong>context</strong> (ex: An error has occurred while trying to fetch foo) together with the <strong>content</strong> of the error (ex: 404 — no such object found).</p><p>Starting with node <strong>17.3.0</strong>, logging an error also logs all its causes recursively.</p><p>So instead of the error looking like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0de83a7aae25d8e6900112cb370e9904/href">https://medium.com/media/0de83a7aae25d8e6900112cb370e9904/href</a></iframe><p>or like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cfd9825bfde5e1da8a1c69d898771131/href">https://medium.com/media/cfd9825bfde5e1da8a1c69d898771131/href</a></iframe><p>It will now look like this :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/33c132281bb94a85b5980d2fdad28774/href">https://medium.com/media/33c132281bb94a85b5980d2fdad28774/href</a></iframe><p>We can add as many error levels as we like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1e1635e1f16f1707419b457e4282d916/href">https://medium.com/media/1e1635e1f16f1707419b457e4282d916/href</a></iframe><p>Our logs and errors are now much clearer and contain much <strong>more context</strong> !</p><h4>Typescript</h4><p>For TS users, please keep in mind that the typings for this feature are not yet ready (as of Typescript 4.5.4).</p><p>However, with the help of a temporary <em>//@ts-ignore</em>, the feature can still be used in the meanwhile:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a3678c0c9b0fce02e2aa18154bcd0bde/href">https://medium.com/media/a3678c0c9b0fce02e2aa18154bcd0bde/href</a></iframe><h4>Availability</h4><p>While trying out or implementing this feature, don’t forget to use the appropriate node version 😉. I personally use <a href="https://github.com/nvm-sh/nvm">nvm</a> to easily switch between node versions.</p><p>I’ve mentioned that the feature is available in node 16.9.0 (for cause constructor only) but it is also available to use in your frontend code, for Chrome (93+), firefox (91+) and Safari (15+) ! Logging the error cause is not available on all platforms but it’ll surely be soon enough :)</p><p>For older versions, you can also use polyfills (see the <a href="https://github.com/tc39/proposal-error-cause">repository of the proposal</a> for more details)</p><h3>Takeaway:</h3><ul><li>Debugging errors is easier when having immediate access to both error context and error details</li><li>One way to achieve this is to apply a “catch + rethrow with context” pattern, using the new error cause feature:</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1d577abf76eaec6ac8e0ea5161e086d6/href">https://medium.com/media/1d577abf76eaec6ac8e0ea5161e086d6/href</a></iframe><ul><li>This way, we can have more complete and easily actionable logs</li><li>This feature is brand new, so don’t be surprised if not yet 100% operational on your platform</li></ul><p>Happy debugging ! 😊</p><h3>Sources :</h3><p><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/Error#rethrowing_an_error_with_a_cause">MDN Javascript Reference</a></p><p><a href="https://nodejs.org/en/blog/release/v16.9.0/#error-cause">Node.js Release note</a></p><p><a href="https://github.com/nodejs/node/issues/38725">Node.js github issue</a></p><p><a href="https://github.com/tc39/proposal-error-cause">TC39 Proposal repository</a></p><p><a href="https://github.com/microsoft/TypeScript/issues/45167">Typescript error cause issue</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4136c563126a" width="1" height="1" alt=""><hr><p><a href="https://medium.com/ovrsea/power-up-your-node-js-debugging-and-error-handling-with-the-new-error-cause-feature-4136c563126a">Power up your Node.js debugging and error handling with the new Error cause feature</a> was originally published in <a href="https://medium.com/ovrsea">OVRSEA</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>