Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mKlXR-0001xE-1w for pgsql-general@arkaria.postgresql.org; Mon, 30 Aug 2021 17:57:41 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mKlXP-0000n3-VV for pgsql-general@arkaria.postgresql.org; Mon, 30 Aug 2021 17:57:39 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mKlXO-0000mf-CX for pgsql-general@lists.postgresql.org; Mon, 30 Aug 2021 17:57:39 +0000 Received: from wout5-smtp.messagingengine.com ([64.147.123.21]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mKlXK-0008WT-8A for pgsql-general@lists.postgresql.org; Mon, 30 Aug 2021 17:57:38 +0000 Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.west.internal (Postfix) with ESMTP id D4D9E32009E1; Mon, 30 Aug 2021 13:57:29 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute1.internal (MEProxy); Mon, 30 Aug 2021 13:57:30 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:cc:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm3; bh=C xyA7XlkU7MPJhXzknwz4adiLXd8+dUmcT0Hn7VXtrg=; b=F3BJGapvTvNUFkYbX vi9wGAJcIVamXLwuVTwY81PskR2u8ZlLK5OIleSQ+Albdd18ISf4ZTpvQWSglSqS cJEMJqp0+f57mI6mCTu+sim+xOPzuxuxZdmW/nuI2S+BdfEM/QEevMGiOlV6uYbz XvoXZwGJto0aFhEaLWVTNz9ZL2nuJXXdU/nkF4dSXiiNKaC36596tsoWyI6AgdYH TssWBJlmZLPKR0hCsksF7LTF3Yxy1yIHjEvOLL65Sp4W+mpdmrSxkN/oE7gaulR3 +/KOY25F4zBsKnpQ2XWA97uggsh3VaD4F37hHbB91PtZTbYfofROk2BsRj781qZz W3zQA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm3; bh=CxyA7XlkU7MPJhXzknwz4adiLXd8+dUmcT0Hn7VXt rg=; b=pozjMkMeKhVQAAC5dCW2BgoumMbjZdfvqdnaJVYEW32uv/uzCap/o93M3 2dpH4JWJrstkuhuYXeIKH802cwyBHBbXbk9fof3iCo+K7k61RVL895lUHokkiixZ SqOGoC46vZZU5H2v9uCwD1f+JN8+/kXY8juM+fItWENJMkucyOvSqbqZdMflLgYk wJa2dOXyOBawpmCMEiE20Nk0gPh//6OP9ksb5S45r9ypOi5xC2xPo3ylEFW9lvPB Yhq/uXSEgNd29rYzr2u1zzVN1TLE+9fCXBR2dbhAUF4ffgpuJKXnUNdByn3825Vl XUE1IetAY4LvtH6FqWfsPpj7NsZeA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvtddrudduledguddulecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpefuvfhfhffkffgfgggjtgfgsehtkeertddtfeejnecuhfhrohhmpeetughr ihgrnhcumfhlrghvvghruceorggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtg homheqnecuggftrfgrthhtvghrnhepteetkedtieelgfdtuefgudefffduvdetjefgkedu gffhjeetheeuudffffetuddtnecuffhomhgrihhnpehsqhhithgthhdrohhrghenucevlh hushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegrughrihgrnhdr khhlrghvvghrsegrkhhlrghvvghrrdgtohhm X-ME-Proxy: Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 30 Aug 2021 13:57:28 -0400 (EDT) Subject: Re: Idempotent DDL Updates To: Miles Elam , Julien Rouhaud Cc: pgsql-general References: From: Adrian Klaver Message-ID: Date: Mon, 30 Aug 2021 10:57:27 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.7.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 8/30/21 8:56 AM, Miles Elam wrote: > > > On Fri, Aug 27, 2021 at 7:14 PM Julien Rouhaud > wrote: > > > Note that the IF EXISTS / IF NOT EXISTS are *not* idempotent.  If you > need to write idempotent schema update scripts, you need to query the > catalogs to check if the specific change you want to apply has already > been applied or not. > > Poor choice of words. You're absolutely right. The goal is for the > script to be idempotent, not that individual statements like that are > idempotent. > > For example, adding ADD COLUMN foo IF NOT EXISTS and DROP COLUMN… to the > script in addition to CREATE TABLE IF NOT EXISTS statements so that the > end result is always the same column definitions no matter how often the > script is run. > > Eventually the individual ADD and DROP COLUMN statements can be removed > once all databases are up to date. > > Not sure that querying the catalogs is strictly necessary though… Could > you say more? > > This removes the ability to have "down" migration scripts, but I'll be > honest, I've never actually used a "down" script in production. If the > "up" script failed for some reason, the validity of the logic in the > "down" script is immediately suspect. It's always a new "up" script to > fix the problem. That's leaving aside the issue of "down" scripts not > getting anywhere near the same level of scrutiny and testing as "up" > migration scripts get. I think you need to investigate Sqitch: https://sqitch.org/ When working on dev database I run the deploy(up) script and then the revert(down) every time I do a change to make sure it does work. Not only that I routinely revert back to some previous state. Helped by Sqitch tags that allow you set a marker in your change history. I'm going to say that if you spend some time with the documentation you will find that Sqitch is the scratch that eliminates your itch:) > > - Miles -- Adrian Klaver adrian.klaver@aklaver.com