Guest User

Untitled

a guest
Apr 25th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. # Migrations and Seeds Instructor Notes
  2.  
  3. ## Objectives
  4. * Set up database connection using knex in a brand new express project
  5. * Create migrations
  6. * Create seeds
  7.  
  8. ## How do you setup a database connection in nodejs
  9. * Create empty npm project
  10. * Install `knex` and `pg`
  11. * Create `knexfile.js` and add configuration. Where would you get that configuration from?
  12. * Create `db` folder
  13. * Create `index.js` file. What is contained in this file? Where would you find an example of it?
  14. * Add `knex` script to `package.json`
  15. * Create database
  16.  
  17. ## How do you create migrations
  18. * Create a migrations for school-api. What is the command to create a migration
  19.  
  20. | instructors |
  21. |--------------------------------|
  22. | id serial |
  23. | name varchar(255) not nullable |
  24. | timestamps
  25.  
  26. | cohorts |
  27. |--------------------------------|
  28. | id serial |
  29. | name varchar(255) not nullable |
  30. | timestamps |
  31.  
  32. | students |
  33. |------------------------------------------------------|
  34. | id serial |
  35. | name varchar(255) not nullable |
  36. | cohort_id integer references cohorts.id not nullable |
  37. | timestamps |
  38.  
  39. | instructors_cohorts |
  40. |---------------------------------------------------------------|
  41. | id serial |
  42. | instructors_id integer references instructors.id not nullable |
  43. | cohorts_id integer reference cohorts.id not nullable |
  44. | timestamps
  45.  
  46. * What is the command to run a migration?
  47. * Would a migration run more than once?
  48. * How do you rollback migrations?
  49. * In what order do migrations run?
  50.  
  51. ## How do you create seeds?
  52. * Create seeds for `school-api`. What is the command to create a seed file
  53.  
  54. ### instructors
  55. | id | name |
  56. |----|-----------------|
  57. | 1 | Ada Lovelace |
  58. | 2 | Alan Turing |
  59. | 3 | Grace Hopper |
  60. | 4 | Jon Von Neumann |
  61.  
  62. ### cohorts
  63.  
  64. | id | name |
  65. |----|---------------|
  66. | 1 | Tiger Sharks |
  67. | 2 | Velociraptors |
  68. | 3 | Hawks |
  69. | 4 | Lemurs |
  70.  
  71. ### students
  72.  
  73. | id | name | cohort_id |
  74. |----|---------|-----------|
  75. | 1 | Abe | 2 |
  76. | 2 | Bryan | 2 |
  77. | 3 | Claudia | 1 |
  78. | 4 | Daniel | 3 |
  79. | 5 | Dustin | 4 |
  80. | 6 | Gavin | 1 |
  81. | 7 | Luda | 4 |
  82. | 8 | Mark | 4 |
  83. | 9 | Sunil | 3 |
  84. | 10 | Vika | 3 |
  85.  
  86.  
  87. ### instructors_cohorts
  88.  
  89. | id | instructors_id | cohorts_id |
  90. |----|----------------|------------|
  91. | 1 | 1 | 4 |
  92. | 2 | 2 | 1 |
  93. | 3 | 3 | 3 |
  94. | 4 | 4 | 2 |
  95.  
  96. * Reset the sequence for each table
  97. ```
  98. .then(() => {
  99. // reset sequence
  100. return knex.raw(`SELECT setval('${TABLE_NAME}_id_seq', (SELECT MAX(id) FROM ${TABLE_NAME}));`)
  101. })
  102. ```
  103.  
  104. * What happens when you run seeds more than once?
  105. * How can you fix constraint errors?
  106. * Add on cascade delete
  107. * Create a seed file to delete in the right order
  108. * How do you roll back seeds?
  109. * In what order do seeds run? Does it matter?
  110.  
  111. ## Closing questions
  112. * Where do the migrations folder and seed folder get configured?
Add Comment
Please, Sign In to add comment