Live data from Hacker News

Better Database Migrations in Postgres

craigkerstiens.com

31–40 of 89 posts

Re: Better Database Migrations in Postgres

#31

OT: Postgres is an incredible piece of open source software, but the official admin UI pgAdmin is in a state of complete chaos. Any suggestions for an OSS replacement?

Pgadmin is awful, especially when you try using an ssh tunnel. If it is going to crash the process, why put it there?

A decent alternative I use is SQL workbench/j (not related to MySQL) which leverages jdbc to connect to any database and has a long feature list. In my experience, it is on-par with dbeaver.

Re: Better Database Migrations in Postgres

#32
post #3

What's everyone favorite library for doing Postgres migrations using node? I'm using knex.js and still doing migrations mostly by hand.

I'm using knex too, and enjoy the workflow. The built-in primitives for creating and modifying tables work nicely. It gets a little messy when you need to go off-script and `CREATE TRIGGER`s, etc, but I think that knex's approach is still predictable and reliable.

Re: Better Database Migrations in Postgres

#33
DB design and implementation has always fascinated me. How can they build a concurrent index when ten of thousands of records are in and perhaps thousands are throwing out (although in seriousness deleting a row is always a bad idea).

Re: Better Database Migrations in Postgres

#34
post #33

DB design and implementation has always fascinated me. How can they build a concurrent index when ten of thousands of records are in and perhaps thousands are throwing out (although in seriousness deleting a row is always a bad idea).

You can read the explanation: https://www.postgresql.org/docs/9.5/static/sql-createindex.h...

Re: Better Database Migrations in Postgres

#35
post #25

I really just want a deterministic (or sync-based) migration tool. The only two I'm aware of are Innovartis DBGhost and RedGate SQL Compare (though RedGate requires a license everywhere it runs, whereas DBGhost only requires a license to compile the package). This stems from my work years ago with databases in on-premise products. Customers would modify the database schema, causing migration "up" scripts to fail, and…

Sql Server with Management Studio can do something like it with the dacpak format. It calculates differences and what needs to be done.

We have a project at work using this dacpak stuff and tbh it's a nightmare to work with since the dacpaks themselves have to manually generated and distributed.

It also appears to make the projects (in visual studio) unbelievably slow.

Re: Better Database Migrations in Postgres

#36
post #25

I really just want a deterministic (or sync-based) migration tool. The only two I'm aware of are Innovartis DBGhost and RedGate SQL Compare (though RedGate requires a license everywhere it runs, whereas DBGhost only requires a license to compile the package). This stems from my work years ago with databases in on-premise products. Customers would modify the database schema, causing migration "up" scripts to fail, and…

(We should coin a term for this. I propose: "idempotent database updates".)

I'm also a strong proponent of idempotent database updates, and prefer those over classic migrations wherever possible.

Some experience from PostgreSQL (with several years of experience in various applications):

While this approach works pretty well for idempotent changes such as "add column if not exists", it is more tricky when data content is changed by a migration. Although seldom, this alone justified classic migrations, which I always had to use in addition to idempotent upgrades. But I try to keep that part as small as possible.

However, the latter issue might be solved by disciplined usage of names. That is, never reuse or "clean up" column names, table names, index names, view names, function names.

A nice fit into idempotent upgrades is "create or replace function" for database functions. However, there is a caveat that you can't replace it if you change the return type. (Changing the argument types is mostly safe, because then it is a different function for PostgreSQL.) You might be tempted to solve this via "drop function if exists" followed by "create function", but then you need "drop ... cascade", which destroys all views (and perhaps indexes!) that depend on it. Again, the correct solution here is to create a new function with a different name. (And drop old one only at the very end, when everything else is switched to the new one.)

One final note: Always put each migration into a database transaction. And for idempotent updates, put the whole thing into a huge transaction. So when anything goes wrong, nothing happened. You can fix your script and simply try again, without having to cleanup any intermediate mess. This is obviously important on production systems, but also very, very handy during development. For the same reason, while writing a classic migration, always put a "ROLLBACK" at the end. Remove it only when you are fully satisfied with the results.

PostgreSQL is especially strong here, because all DDL actions (alter table, etc.) are transaction safe and can easily be rolled back.

Re: Better Database Migrations in Postgres

#37
post #25

I really just want a deterministic (or sync-based) migration tool. The only two I'm aware of are Innovartis DBGhost and RedGate SQL Compare (though RedGate requires a license everywhere it runs, whereas DBGhost only requires a license to compile the package). This stems from my work years ago with databases in on-premise products. Customers would modify the database schema, causing migration "up" scripts to fail, and…

Mysql Workbench has had a GUI ability to do data modeling, forward and reverse engineer it with a live database, and sync your model with your database (showing you deltas and letting you make changes to the model or the database).

I also have used (with mysql) liquibase with preconditions to script db migrations with conditional logic so that deployment can deal with variations in target db environments, conditionally apply ddl, fail midscript while allowing restart and rollback gracefully.

Re: Better Database Migrations in Postgres

#38
post #23

OT: Postgres is an incredible piece of open source software, but the official admin UI pgAdmin is in a state of complete chaos. Any suggestions for an OSS replacement?

I have taken to using DBeaver. The overall experience is really nice. I don't like that you have to toggle your "Active database" rather than just opening a new window with a new connection to a different database. If that annoys me much more then I plan to try SquirrelSQL and then retry Postage. I never understood the hate that pgAdmin3 received, I liked it a lot. V4 though is a mess.

I'm one of those who disliked pgAdmin3, at least on Mac. It crashed reliably if the database didn't disconnect cleanly and you tried to continue using the program. It crashed randomly when using SSH tunneling. It crashed randomly when resuming from sleep. It crashed reliably when you clicked on objects that no longer exist (ie something outside of pgAdmin deleted a table, then you click on that table in pgAdmin). The window would be lost in the nether if I moved it to a secondary monitor and then unplugged said monitor. The query editor was pretty mediocre. That's just the ones I can remember.

Re: Better Database Migrations in Postgres

#39

OT: Postgres is an incredible piece of open source software, but the official admin UI pgAdmin is in a state of complete chaos. Any suggestions for an OSS replacement?

There is some movement in that space fortunately. Depending on whether you rather want to lean towards analytical or DB management functionality I'd check out PopSQL (https://popsql.io/), Tableplus (https://tableplus.io/) which are not open source but freemium or OmniDB (https://omnidb.org/index.php/en/) which is OSS.

Re: Better Database Migrations in Postgres

#40
post #35

Earlier quoted context omitted.

Sql Server with Management Studio can do something like it with the dacpak format. It calculates differences and what needs to be done.

We have a project at work using this dacpak stuff and tbh it's a nightmare to work with since the dacpaks themselves have to manually generated and distributed. It also appears to make the projects (in visual studio) unbelievably slow.

The way I did was adding a MSBuild script that re-generates the dacpacs for each supported MSSQL version when you compile using a special configuration in Visual Studio (so the dropdown is Debug / Release / RebuildDacpacs).

Then the dacpacs get shipped with the application, along with a zipped SqlPackage.exe (and accompanying libraries) to apply them.

It's been a lot of work and we've had to handle a lot of corner cases through pre-deployment scripts and SqlPackage CLI options, but I haven't seen a 'why does this customer have an [Address] column that's the wrong length and set to nullable???' or 'this report is super slow -> index is missing' ticket since.

Post reply on HN