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
  • Refactor config.json
  • Reverting a Seed
  • Challenge
  • Migration Wrap-Up
  1. js_library
  2. Node Server
  3. 13 - More Sequelize Functions
  4. Migrations

06 - Reverting Seeds

Previous05 - SeedsNextQueries

Last updated 7 years ago

Just like migration files, seed files can be reverted. However, you still need to be careful what you revert. In the seed from the previous module, if we were to revert, the down function deletes everything because it sets null to the tests table. In fact, when testing this out, we proved it:

There are two commands that can revert seeds: db:seed:undo:all reverts all seeds, while db:seed:undo --seed [filename] will revert the most recent seed. However, by default, a list of seeders is not stored in a database table like migrations. Without storing the seeds, we can't revert a single seed. We change this by adding to our config.json file.

Refactor config.json

We need to add two lines to our file in each of the three sections: the first tells Sequelize to store the seeds, while the second tells it where to store them. Add the following to the development object (You may want to add them to the other two objects as well, just to be safe):

  "development": {
    "username": "postgres", 
    "password": "9074dewberry1136",  
    "database": "workoutlog",
    "host": "127.0.0.1",
    "port": "5432",
    "dialect": "postgres",
    "seederStorage": "sequelize", //<--ADD THIS
    "seederStorageTableName": "sequelize_data" //<---ADD THIS
  }

The next time we run a seed, a table called sequelize_data will be created in our database with a list of seeds that have been run. We can then use the individual db:seed:undo command to revert a single seed. Run sequelize db:seed:all to add some data, then refresh your database to make sure that this table was created properly.

Reverting a Seed

In order to revert just one seed, we need to tell the down function to search for something specific. Take this table, for example:

We could search for the mostly updated row by using the updatedAt, or we could search for a specific row by using one of the columns we've provided data for. Let's make Sequelize delete a row where firstName is Gary by making the following change to the down function:

return queryInterface.bulkDelete('tests', {firstName: 'Gary'});

We remove the null argument and add the search term into the object. When we run the revert, we get the following result:

Challenge

Create a new seed with sequelize seed:generate, then run sequelize db:seed:all to put both seeds' data into the database. Change the down function of the new seed to remove its data you just entered (Just the second seed!) and run sequelize db:seed:undo --seed [newSeed]. Refresh your database your database to ensure it worked.

Migration Wrap-Up

Migrations are very important to any kind of back-end programming or database job. Doing them incorrectly can be catastrophic, however. Taking the time now to practice using migrations and seeders will be very beneficial in the long run. Consult the Sequelize migration tutorial in the beginning of the chapter for any questions you might have, as well as some advanced tips not covered here. Additionally, tutorial presents the basic concepts covered here in a little more detail, but not as densely as the docs.

this
oops
chess
seedRevert