JS-301-NodeServer
  • Introduction
  • js_library
    • Node Server
      • 00 - Intro
        • 01 - Purpose
        • 02 - Back-End Setup
        • 03 - Terms Cheat Sheet
      • 01 - Server Set up
        • 01 - npm packages
        • 02 - Express Intro
        • 03 - Express code
      • 02 - Development Tools
        • 01 - Nodemon Intro
        • 02 - Postman Intro
        • 03 - Postman set up
      • 03 - Routes Intro
        • 01 - Routes intro
        • 02 - Express Router() intro
        • 03 - Challenge 1
        • 04 - Challenge 2
      • 04 - Database Intro
        • 00 - DB Intro and Set up
          • 00 - DB Intro
          • 01 - PostgreSQL Intro
          • 02 - Install
        • 01 - Sequelize Intro
          • 01 - Sequelize intro
          • 02 - Initialize
      • 05 - Model View Controller
        • 01 - MVC
          • 00 - MVC Intro
        • 02 - Models
          • 01 - Intro to Models
          • 02 - Test Model
        • 03 - Controllers
          • 00 - Controllers Intro
          • 01 - Controller Set up
          • 02 - Create Method
          • 03 - req.body()
          • 04 - Crafting the Response
          • 05 - Sending the Response
          • 06 - JSON Response
          • 07 - Error Handling
        • 04 - Conclusion
      • 06 - Tokenization
        • 01 - JWT Intro
          • 01 - JWT intro
        • 02 - User Create
          • 01 - User Create
          • 02 - Refactor
        • 03 - User Token
          • 01 - JWT Package
          • 02 - Adding JWT
          • 03 - ENV
      • 07 - Encryption
        • 01 - bcrypt
        • 02 - bcrypt setup
      • 08 - Session
        • 00 - Session Intro
        • 01 - Sign In Method
        • 02 - Sign In Bcrypt
        • 03 - Sign In JWT
      • 09 - Middleware
        • 01 - Test Client HTML
        • 02 - Test Client JS
        • 03 - Middleware intro
        • 04 - Headers intro
        • 05 - Server Update
        • 06 - Test Post
        • 07 - Test Post Refactor
        • 08 - Post Data
        • 09 - Fetch From One
      • 10 - Authenticated Routes
        • 01 - Intro to Authenticated Routes
        • 02 - Validate Session
        • 03 - Changes to app.js
        • 04 - authtestcontroller.js
        • 05 - Delete an Item
        • 06 - Update an Item
        • 07 - Postman Testing
      • 11 - Authenticated Requests
        • 00 - Additions to index
        • 01 - Anatomy of a Request
        • 02 - Create User
        • 03 - Getting a Token
        • 04 - Get Items From One User
        • 05 - Creating an Item for a User
        • 06 - Get one item
        • 07 - Update an Item
        • 08 - Deleting an Item
        • 09 - Deleting with a Custom Event
      • 12 - Workout Log Server
        • 00 - Intro
      • 13 - More Sequelize Functions
        • Migrations
          • 00 - Intro
          • 01 - init and config
          • 02 - Creating the First Migration
          • 03 - Running Migrations
          • 04 - Reverting Migrations
          • 05 - Seeds
          • 06 - Reverting Seeds
        • Queries
          • 00 - Intro
          • 01 - Queries
Powered by GitBook
On this page
  • What Are Migrations?
  • Migration Setup
  • Further Reading
  1. js_library
  2. Node Server
  3. 13 - More Sequelize Functions
  4. Migrations

00 - Intro

PreviousMigrationsNext01 - init and config

Last updated 7 years ago

Databases can grow quite large over time. They also frequently need to be updated, adding or removing information as a project or app changes. Currently, the only way we have to accomplish this is to change the model, drop the table or database, then re-create it with sequelize.sync(). This creates a giant problem: If you have 1000 user accounts on a website and you change the user table to add some information, dropping the user table would wipe out all of these accounts! This is a problem not only for the users that get deleted, along with any associated information, but will undoubtedly make your users very, very mad. Fortunately, sequelize gives us a way to update the database without losing everything: Migrations.

What Are Migrations?

Simply put, migrations are a way to alter a database or table while keeping the data that's already there. This can also be done with SQL statements but can be very risky if you aren't sure exactly what you're doing. This approach uses JavaScript and the Sequelize CLI (Command Line Interface), which poses a much smaller risk to your database, and by extension your users.

Migration Setup

Before we start actually doing migrations, we have to install a few npm packages. You can just install them locally within your project, but it's much simpler to install them globally. This saves time by not having to re-install the packages for every single project you create. Open a console window and run the following commands:

npm install -g sequelize
npm install -g sequelize-cli
npm install -g pg

NOTE: Mac users may need to use the sudo command if the computer requires admin access.

These commands install Sequelize, the Sequelize CLI, and Postgres on your computer, and the global installation allows access to them from anywhere. Anytime you install something to be used via the command line, you need to close the terminal window and re-open it to use what you've installed. After re-opening the window, type sequelize into the command line. You should see something like this:

sequelize is the keyword for any of the commands described in this chapter, like git is used for Git commands and actions. Any command used the rest of this chapter will need to start with sequelize. If you see any errors, try running the above steps again. Once the cli is working, move on to the next module.

Further Reading

This chapter will be following instructions laid out in the official Sequelize Migration Tutorial found . Feel free to take a look if you want to learn more.

here
SequlizeCLI