Advertisement
Guest User

Untitled

a guest
Aug 24th, 2017
839
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. MIGRATIONS/SEEDING
  2.  
  3. seed: examples include username = test, password = test, or a 'guest' account.
  4.  
  5. migration: allow to quickly build and rebuild schema in sql dbs
  6.  
  7. in practice, you'll run migrations to set up the schema, but then seed the testing data in.
  8.  
  9. //set up the express server with knex file
  10. express directoryname --save knex pg
  11. cd directoryname
  12. npm install
  13. knex init
  14. mkdir db
  15. touch db/knex.js
  16. //put this in the knex.js every time no questions:
  17. var environment = process.env.NODE_ENV || 'development';
  18. var config = require('../knexfile.js')[environment];
  19. module.exports = require('knex')(config)
  20.  
  21.  
  22. // create a gitignore file
  23. touch .gitignore // you don't want to push node files to git
  24. echo node_modules > .gitignore
  25.  
  26.  
  27. // push new repo to github
  28. git init
  29. git add .
  30. git commit -m "First commit"
  31. git remote add origin PASTE IN REPO URL HERE
  32. git push -u origin master
  33.  
  34.  
  35. // set up the knexfile.js - here's the top portion. all else stays the same for dev environment
  36.  
  37. module.exports = {
  38.  
  39. development: {
  40. client: 'pg',
  41. connection: {
  42. database: 'migpract',
  43. host: '127.0.0.1'
  44. }
  45. },
  46.  
  47.  
  48. // set up the db
  49. createdb migrations
  50. knex migrate:make users - this makes the xxxxxxxx_users.js file in the migrations folder
  51.  
  52. * look in migrations folder for a file. edit the doc to include your column names and data types:
  53.  
  54. exports.up = function(knex, Promise) {
  55. return knex.schema.createTable('users', function(table){
  56. table.increments(); // ID column
  57. table.string('username');
  58. table.string('password');
  59. table.string('email');
  60. })
  61. };
  62. exports.down = function(knex, Promise) {
  63. return knex.schema.dropTable('users');
  64. };
  65.  
  66. // now in termianl
  67. \q
  68. knex migrate:latest
  69. psql migrations;
  70. \dt
  71. select * from migrations;
  72.  
  73. // changing schema
  74. \q
  75. knex migrate:rollback
  76. // user table disappears // does everything in the exports.down handler // now you can add to schema
  77.  
  78. knex migrate:latest
  79. // now our db is updated
  80. psql migrations
  81. \dt
  82. select * from migrations;
  83. 😃 😃 😃
  84.  
  85.  
  86. SEEDING on CLI
  87.  
  88. \q
  89. knex seed:make seed_name // this creates a seed directory with a file in it called seed_name - chose your own name
  90.  
  91. edit knexfile.js to include a seeds key value. like so:
  92.  
  93. development: {
  94. client: 'pg',
  95. connection: {
  96. database: 'migrations',
  97. host: '127.0.0.1'
  98. },
  99. seeds: {
  100. directory: './seeds'
  101. }
  102. },
  103.  
  104.  
  105. edit seeds file in /seeds:
  106.  
  107. exports.seed = function(knex, Promise) {
  108. // Deletes ALL existing entries
  109. return knex('users').del()
  110. .then(function () {
  111. // Inserts seed entries
  112. return knex('users').insert([
  113. {username: 'test', password: 'test', email: "fake@fake.com", age: 12},
  114. {username: 'bob', password: 'bob', email: "fake@fake.com", age: 34},
  115. {username: 'susy', password: '12345', email: "fake@fake.com", age: 412},
  116. {username: 'robotron9000', password: 'test', email: "fake@fake.com", age: 12},
  117. {username: 'admin', password: 'admin', email: "admin@fake.com", age: 35},
  118. ]);
  119. });
  120. };
  121.  
  122. // HERES HOW TO REFRESH YOUR MIGRATIONS AND SEED
  123. \q
  124. knex migrate:rollback
  125. knex migrate:latest
  126. knex seed:run
  127.  
  128. ** 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