![]() ![]() New requirements force you to add two new columns, first_name and last_name. Let's say you have a users table with columns email and password. Let's see an example with Ecto, Elixir's database framework. This results in concise pieces of code to alter the database schema that can be executed "in both directions". Most database frameworks can infer the rollback steps from the migration definition if the migration is simple enough. "Rolling back" a migration means executing SQL statements that revert the changes done in that migration. The framework uses this table to only run migrations that have not been run yet.Īnother benefit is that you'll usually write migrations in a way that makes it possible to roll them back. To be precise, the framework that runs the migrations is the one storing information in that table. Migrations have a few selling points over executing direct SQL commands.įirst, database migrations usually keep information about the migrations themselves in a dedicated database table. I'll write a few words here for context, but go read that if you want to dig deeper. Wikipedia does a great job at explaining migrations, their history, and their use cases. If you're interested in the workflow I’m currently using for Elixir, jump to the last section of the post.ĭB migrations are pieces of code that you run to change the schema (and data, if desired) of your database. I mostly write Elixir, so my examples will be in Elixir. ![]() I'll start with some context on database migrations, I'll expand on the Git commits analogy, and I'll show you what I've been doing instead. In this post, I want to dig a bit deeper into the topic. However, lately I started to view migrations more like Git commits than like active pieces of code in my applications. I love to be able to change the shape of tables and move data around in a controlled way to avoid issues and downtime. You can be running in minutes.Database migrations are great. Fly.io ❤️ Elixirįly.io is a great way to run your Phoenix LiveView app close to your users. With that taken care of, we can run mix ecto.dump, delete old migrations, and still support deploying to a fresh database when needed. With these additional steps, we can ensure that our structure.sql file is loaded in both a local environment and on the server. Ideally, this code could be extracted and made more readily available upstream. Pulling the code out of the mix task was necessary because the dev tool mix isn’t available on the server after generating a release. The needed code was taken and adapted from the mix ecto.load task in ecto_sql. Require Logger def migrate do load_app () for repo Logger. To run psql on the server, we need to add the postgresql-client package to our Dockerfile in the final layer. We will rely on the _load/2 function to do a lot of the work, but it depends on the psql command being available on the server. We need to take extra steps to get our structure.sql loaded. This means our mix tup solution doesn’t work in production. The problem we have in our production environment is that the development tool mix is not available on the server after we build and deploy a release. When we run fly launch or fly deploy, by default the file will be included and that means it will be present on the server. That file should be committed to our version control system. Testing a mature app on Fly.io before bringing over the full database.Īssuming the mix ecto.dump command was used, we have a priv/repo/structure.sql file generated for us.Deploying a new clean staging environment.How do we load our structure.sql file on the server when mix ecto.load is not available? ![]() The problem we’re facing comes after building a mix release and we are deploying to an empty database in a production environment where mix is not available. If you were migrating your database to a new server, there are other options available. Things were great and you could handle the local dev and test setup using mix ecto.load. Check out how to get started! ProblemĪt some point, you or your team ran mix ecto.dump and generated a priv/repo/structure.sql file. Fly.io happens to be a great place to run Phoenix applications. This post is about how to deploy your Elixir app to a fresh database after you’ve run mix ecto.dump and deleted old migration files. We run apps for our users on hardware we host around the world. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |