Giter Club home page Giter Club logo

chugs-db's Introduction

chugs-db

This is the change management tool for the chugs database. Migrations can be created, ran and rolled back using this tool.

Getting started

Install Knex (http://knexjs.org/)

yarn global add knex

Creating a migration

knex migrate:make <migration_name>

This will create a JavaScipt file in the /migrations folder with an up and down function. Simply add code to the up functions to describe how the database should look if this migration is applied and and add code to the down function to describe how the database should look if this migration is rolled back.

Example

// creating the table

exports.up = (knex, Promise) => {
  return knex.schema
    .createTable('product', table => {
      table.bigIncrements('id').primary();
      table.string('name');
      table.string('description');
      table.float('price');
      table.int('quantity');
      table.int('size');
    })
};

// drop the table
exports.down = (knex, Promise) => {
  return knex.schema
    .dropTableIfExists('product')
};

Running a migration

knex migrate:latest

This will run all migration scripts against the database.

Rolling back a migration

knex migrate:rollback

This will remove migration scripts from the database one at a time.

Creating a seed

knex seed:make <seed_name>

The folder structure for seeds is such that we have a /seeds folder and within that we have sub-folders representing different environments. e.g. /seeds/dev

When this command is executed, a JavaScript file named after the name given is created in the appropriate directory. Database entries can be entered here as JSON objects.

Example

exports.seed = function(knex, Promise) {
  // Deletes ALL existing entries
  return knex('table_name').del()
    .then(function () {
      // Inserts seed entries
      return knex('table_name').insert([
        { id: 1, name: 'test-name1', description: 'test-description1'},
        { id: 2, value: 'test-name2', description: 'test-description2' },
        { id: 3, value: 'test-name3', description: 'test-description3' },
        { id: 4, value: 'test-name4', description: 'test-description4' }
      ]);
    })

.then() functions can be chained on as required if seeding multiple tables.

Running a seed

knex seed:run

This will persist the data from seed files to the database.

Environments

The knexfile.js file located at the top level of this repository describes configuration for databases deployed in different environments. Sqlite3 is used for development which means that rather than a real database, a file is created in this folder that can be opened using SQLiteStudio.

SQLiteStudio can be installed by doing the following:

brew install sqlite3

More reading

http://knexjs.org/#Migrations

chugs-db's People

Watchers

Gary Russell avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.