Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MIGRATIONS/SEEDING
- seed: examples include username = test, password = test, or a 'guest' account.
- migration: allow to quickly build and rebuild schema in sql dbs
- in practice, you'll run migrations to set up the schema, but then seed the testing data in.
- //set up the express server with knex file
- express directoryname --save knex pg
- cd directoryname
- npm install
- knex init
- mkdir db
- touch db/knex.js
- //put this in the knex.js every time no questions:
- var environment = process.env.NODE_ENV || 'development';
- var config = require('../knexfile.js')[environment];
- module.exports = require('knex')(config)
- // create a gitignore file
- touch .gitignore // you don't want to push node files to git
- echo node_modules > .gitignore
- // push new repo to github
- git init
- git add .
- git commit -m "First commit"
- git remote add origin PASTE IN REPO URL HERE
- git push -u origin master
- // set up the knexfile.js - here's the top portion. all else stays the same for dev environment
- module.exports = {
- development: {
- client: 'pg',
- connection: {
- database: 'migpract',
- host: '127.0.0.1'
- }
- },
- // set up the db
- createdb migrations
- knex migrate:make users - this makes the xxxxxxxx_users.js file in the migrations folder
- * look in migrations folder for a file. edit the doc to include your column names and data types:
- exports.up = function(knex, Promise) {
- return knex.schema.createTable('users', function(table){
- table.increments(); // ID column
- table.string('username');
- table.string('password');
- table.string('email');
- })
- };
- exports.down = function(knex, Promise) {
- return knex.schema.dropTable('users');
- };
- // now in termianl
- \q
- knex migrate:latest
- psql migrations;
- \dt
- select * from migrations;
- // changing schema
- \q
- knex migrate:rollback
- // user table disappears // does everything in the exports.down handler // now you can add to schema
- knex migrate:latest
- // now our db is updated
- psql migrations
- \dt
- select * from migrations;
- 😃 😃 😃
- SEEDING on CLI
- \q
- knex seed:make seed_name // this creates a seed directory with a file in it called seed_name - chose your own name
- edit knexfile.js to include a seeds key value. like so:
- development: {
- client: 'pg',
- connection: {
- database: 'migrations',
- host: '127.0.0.1'
- },
- seeds: {
- directory: './seeds'
- }
- },
- edit seeds file in /seeds:
- exports.seed = function(knex, Promise) {
- // Deletes ALL existing entries
- return knex('users').del()
- .then(function () {
- // Inserts seed entries
- return knex('users').insert([
- {username: 'test', password: 'test', email: "fake@fake.com", age: 12},
- {username: 'bob', password: 'bob', email: "fake@fake.com", age: 34},
- {username: 'susy', password: '12345', email: "fake@fake.com", age: 412},
- {username: 'robotron9000', password: 'test', email: "fake@fake.com", age: 12},
- {username: 'admin', password: 'admin', email: "admin@fake.com", age: 35},
- ]);
- });
- };
- // HERES HOW TO REFRESH YOUR MIGRATIONS AND SEED
- \q
- knex migrate:rollback
- knex migrate:latest
- knex seed:run
- ** if severe debugging needs to happen, delete the knex_migration & knex_migration_lock tables in the db.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement