DEV Community

Junior Oliveira
Junior Oliveira

Posted on • Originally published at dev.to on

5 3

The best and easy way to handle database migrations (version control)

Do know about database version control? It's called migrations and here I will tell you how to do it in a simple way.

If you know about it and work with JavaScript, you probably have tried one of these tools to handle database migrations: Sequelize, TypeORM, Bookshelf, etc… But, seriously… Are you happy with these options? I'm not.

Since I started to work with Java and met the beautiful Flyway, I was missing a tool as great as it is in another languages. And now comes the good news: there's a "dockerized" version that you can use with any language you want!

So if you trust me, keep reading about this magical tool.

The examples bellow are here: https://github.com/arojunior/tests-with-nodejs-graphql

version: "3"
services:
flyway_mysql:
image: boxfuse/flyway:5.2.4
command: "flyway migrate"
volumes:
- ./docker/mysql/migrations/config:/flyway/conf
- ./docker/mysql/migrations/sql:/flyway/sql

Then we can create the config file and the migrations, as you can see in the volumes arguments.

folder tree

flyway.driver=com.mysql.jdbc.Driver
flyway.url=jdbc:mysql://mysql:3306/mydb?autoReconnect=true&useUnicode=true&characterEncoding=utf-8
flyway.user=user
flyway.password=password
view raw flyway.conf hosted with ❤ by GitHub

Those names for the migration files are following a convention that you can find here in the documentation, but it's very simple:

filenames convention

Did you notice that the file extension is .sql? Yes! You are able to write plain SQL scripts and don't need to learn a new syntax or object mappings. That's what I love in this tool.

CREATE TABLE post
(
id int PRIMARY KEY AUTO_INCREMENT,
content text,
likes int,
dislikes int,
created datetime
);
INSERT INTO post VALUES (0, 'Description 1', 0, 0, '2019-01-19 17:00:00');
INSERT INTO post VALUES (0, 'Description 2', 0, 0, '2019-01-19 17:00:00');
INSERT INTO post VALUES (0, 'Description 3', 0, 0, '2019-01-19 17:00:00');
INSERT INTO post VALUES (0, 'Description 4', 0, 0, '2019-01-19 17:00:00');

Flyway will create a table to manage the migrations and it runs automagically every time you start the container. It will check the applied scripts and run the new ones. No additional command is needed, you just have to create the new files in the specified folder.

Flyway supports also SQL Server, Postgres, Oracle, MariaDB and more…

Take a look in the documentation for more infos: https://flywaydb.org/documentation/migrations

Now clap your hands for this amazing tool.


API Trace View

How I Cut 22.3 Seconds Off an API Call with Sentry 🕒

Struggling with slow API calls? Dan Mindru walks through how he used Sentry's new Trace View feature to shave off 22.3 seconds from an API call.

Get a practical walkthrough of how to identify bottlenecks, split tasks into multiple parallel tasks, identify slow AI model calls, and more.

Read more →

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay