Rails, via the Rake command, deployed the application’s schema (but not the data) to a different backend for you. You’ve run your first migration! You moved this application from Sqlite3 to MySQL by changing a configuration file and running a Rails migration. After you’ve pulled a copy, navigate to the config directory and open in your favorite editor. If you’re on a Mac, Linux, or Windows 10, the easiest way is probably to spin one up on Docker. You’ll need a MySQL database to follow along with this. But before we start, let’s switch over to a MySQL database so we can use SQL to examine the results more quickly than we can with SQLite. Let’s do a few migrations on our sample app. We’ll look more closely at how to implement rollbacks below. It’s up to you to write the code to undo it, though. If you need functionality that’s not supported by Active Record, you can execute SQL inside a migration. If that’s not what you need, you can write your own. When you add a new model to a Rails application, it generates a migration to create the corresponding table for you. Add a new model (or table)Īdding a new class to an application is a frequent change too. Column changes, however, aren’t reversible. Rails migrations know how to move data to a new type. You can change an existing column to a new name or data type as well. You can also specify a database-specific data type for a column, but this may cause problems if you try to migrate your application to a new platform. You can use all of the Rails basic data types with migrations, and it’ll be matched to the corresponding type in the database you migrate to. If you’re working with Active Records, Rails will create the migration for you. So adding a column to a database is a typical migration. One of the most common changes in an app is adding a field to an existing object. NET, Java, PHP, Node.js, Ruby, and Python. Try Stackify’s free code profiler, Prefix, to write better code on your workstation. Let’s take a look at some specific operations, and then we’ll try a few hands-on migrations in the sample code. You can also roll migrations back, assuming it didn’t do something irreversible like destroying data. Of course, if you write your own database code, you lose most of the database independence, and often labor-saving, advantages of Rails. ![]() You can use the Rails DSL for these changes, or you can use SQL. You can use migrations to make any changes you need to the database(s) your application connects to. ![]() They’re also easy to share across development teams since each member of the team can deploy the migration to their local instance when they update their projects. Migrations are saved as part of your Rails project, so they’re versioned with the rest of your code. You can develop on one database and deploy to another, or deploy to a new database platform in production. So using migrations makes it possible for you to deploy your app to new platforms. If you’re working with Rails Active Records, manipulating the database directly is a bad idea.Īs we’ll see in the example below, Active Record uses migrations to update your app’s schema in schema.rb. This file is what Rails uses to deploy your application to a new database. Rails migrations are useful any time you need to make a change to your application’s database. Rails applications can evolve, and publishing a new migration with a new release of your application isn’t unusual. Each one represents a new version of your database schema. Sure, you can (and should) version your SQL files, but do they belong in the same place? Not everyone feels the same way about that question. They’re written in Ruby and versioned with the rest of your app. ![]() Migrations keep your database schema changes with your application code. Migrations avoid this since you make the modifications in platform-independent Ruby. If you have to write SQL to change your schema, you lose this independence. Rails applications that can stay within the Active Record model are database-independent. Using Rails migrations instead of SQL has several advantages. Migrations give you a way to modify your database schema within your Rails application. Requirements change all the time, and those changes often lead to database changes. ![]() Let’s take a look at what Rails migrations are, why you might need them, and walk through examples using the sample code we used to show you how to troubleshoot Ruby applications. You can roll migrations back, and manage them alongside your application source code. The code is database-independent, so you can easily move your app to a new platform. Instead of managing SQL scripts, you define database changes in a domain-specific language (DSL). A Rails migration is a tool for changing an application’s database schema.
0 Comments
Leave a Reply. |