Advertisement
Guest User

Untitled

a guest
May 15th, 2018
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.92 KB | None | 0 0
  1. ## Entity relation design for reference: https://github.com/pmeaney/expressKnexPostgres-timetracker/blob/master/timetracker_ERD.png
  2.  
  3. ## MIGRATION FILE
  4.  
  5. /*
  6. Table creation order:
  7. locations,
  8. activity_codes,
  9. cost_centers,
  10. employees,
  11. projects,
  12. activities,
  13. timesheets
  14. */
  15. exports.up = function(knex, Promise) {
  16. return Promise.all([
  17.  
  18. knex.schema.createTable('locations', function(table) {
  19. table.increments('location_id').primary();
  20. table.text('location_name');
  21. table.text('location_address');
  22. table.text('location_city');
  23. table.text('location_state');
  24. table.text('location_zip');
  25. table.enu('location_type', ['residential', 'commercial']);
  26. table.timestamps(true, true);// this automatically sets 'created at' and 'updated at' timestamps
  27. }),
  28.  
  29. knex.schema.createTable('activity_codes', function(table) {
  30. table.increments('activity_code').primary();
  31. table.text('activity_type');
  32. table.timestamps(true, true);// this automatically sets 'created at' and 'updated at' timestamps
  33. }),
  34.  
  35. knex.schema.createTable('cost_centers', function(table) {
  36. table.increments('cost_center_id').primary();
  37. table.text('cost_center_name');
  38. table.timestamps(true, true);// this automatically sets 'created at' and 'updated at' timestamps
  39. }),
  40.  
  41. knex.schema.createTable('employees', function(table) {
  42. table.increments('employee_id').primary();
  43. table.text('firstName');
  44. table.text('lastName');
  45. table.text('phone');
  46. table.text('email');
  47. table.integer('pay');
  48. table.timestamps(true, true);// this automatically sets 'created at' and 'updated at' timestamps
  49. }),
  50.  
  51. knex.schema.createTable('projects', function(table) {
  52. table.increments('project_id').primary();
  53. table.integer('location_id').references('locations.location_id');
  54. table.integer('project_mgr_emp_id').references('employees.employee_id');
  55. table.date('project_date_begin');
  56. table.date('project_date_end');
  57. table.timestamps(true, true);// this automatically sets 'created at' and 'updated at' timestamps
  58. }),
  59.  
  60.  
  61. // 1:M activities:timesheets --> can have several timesheets per activity
  62. // ==> allows for multiple employees per pactivity
  63. knex.schema.createTable('activities', function(table) {
  64. table.increments('activity_id').primary();
  65. table.integer('activity_code').references('activity_codes.activity_code');
  66. table.integer('project_id').references('projects.project_id');
  67. table.text('activity_notes');
  68. table.timestamp('activity_datetime_begin');
  69. table.timestamp('activity_datetime_end');
  70.  
  71.  
  72. table.timestamps(true, true);// this automatically sets 'created at' and 'updated at' timestamps
  73. }),
  74.  
  75. // get all timesheets by activity_id and order descending by date
  76. // to, for example, get list of timesheets
  77. knex.schema.createTable('timesheets', function(table) {
  78. table.increments('timesheet_id').primary();
  79. table.integer('activity_id').references('activities.activity_id');
  80. table.integer('authorized_by_emp_id').references('employees.employee_id');
  81. table.integer('cost_center_id').references('cost_centers.cost_center_id');
  82. table.integer('timesheet_for_emp_id').references('employees.employee_id');
  83. table.text('timesheet_notes');
  84. table.timestamp('timesheet_submitted_datetime');
  85. table.timestamp('timesheet_clockin');
  86. table.timestamp('timesheet_clockout');
  87.  
  88. // info on GPS precision: https://en.wikipedia.org/wiki/Decimal_degrees
  89. table.specificType('timesheet_clockin_lat', 'DECIMAL'); //.defaultTo(knex.raw('POINT (37.3875, -122.0575)'))
  90. table.specificType('timesheet_clockin_long', 'DECIMAL'); //.defaultTo(knex.raw('POINT (37.3875, -122.0575)'))
  91. table.specificType('timesheet_clockout_lat', 'DECIMAL'); //.defaultTo(knex.raw('POINT (37.3875, -122.0575)'))
  92. table.specificType('timesheet_clockout_long', 'DECIMAL'); //.defaultTo(knex.raw('POINT (37.3875, -122.0575)'))
  93.  
  94. table.timestamps(true, true);// this automatically sets 'created at' and 'updated at' timestamps
  95. }),
  96.  
  97. ])
  98. };
  99.  
  100. exports.down = function(knex, Promise) {
  101.  
  102. /*
  103. Table creation order:
  104. locations,
  105. activity_codes,
  106. cost_centers,
  107. employees,
  108. projects,
  109. activities,
  110. timesheets
  111. */
  112. return Promise.all([
  113. knex.schema.dropTable('timesheets'),
  114. knex.schema.dropTable('activities'),
  115. knex.schema.dropTable('projects'),
  116. knex.schema.dropTable('employees'),
  117. knex.schema.dropTable('cost_centers'),
  118. knex.schema.dropTable('activity_codes'),
  119. knex.schema.dropTable('locations'),
  120. ]);
  121.  
  122. };
  123.  
  124.  
  125. ## SEED FILE
  126. /*
  127. Table creation order:
  128. locations,
  129. activity_codes,
  130. cost_centers,
  131. employees,
  132. projects,
  133. activities,
  134. timesheets
  135. */
  136.  
  137.  
  138. exports.seed = function(knex, Promise) {
  139. // Deletes ALL existing entries
  140. return knex('timesheets').del()
  141. .then(() => knex('activities').del())
  142. .then(() => knex('projects').del())
  143. .then(() => knex('employees').del())
  144. .then(() => knex('cost_centers').del())
  145. .then(() => knex('activity_codes').del())
  146. .then(() => knex('locations').del())
  147.  
  148. .then(() => { // Using promises allows to catch errors during process
  149. return Promise.all([
  150.  
  151. knex('locations').insert([
  152. { location_id: 1, location_name: 'commercial: Brisbane post office', location_address: "280 Old County Rd", location_city: "brisbane", location_state: "ca", location_zip: "94005", location_type: "commercial"},
  153. { location_id: 2, location_name: 'commercial: Brisbane Hardware & Sply Inc', location_address: "1 Visitacion Ave", location_city: "brisbane", location_state: "ca", location_zip: "94005", location_type: "commercial"},
  154. ]),
  155.  
  156. knex('activity_codes').insert([
  157. { activity_code: 1, activity_type: 'painting'},
  158. { activity_code: 2, activity_type: 'carpentry'},
  159. { activity_code: 3, activity_type: 'electrical'},
  160. { activity_code: 4, activity_type: 'plumbing'},
  161. { activity_code: 5, activity_type: 'landscaping'},
  162. { activity_code: 6, activity_type: 'irrigation'},
  163. ]),
  164.  
  165. /*
  166. cost centers allow me to create different timesheet categories per department
  167. more info on cost center info design: http://www.doa.la.gov/OTS/ERP/blueprint/finance/FI-CO-003%20Presentation.pdf
  168. */
  169.  
  170. knex('cost_centers').insert([
  171. { cost_center_id: 1, cost_center_name: 'operations payroll' }
  172. ]),
  173.  
  174. knex('employees').insert([
  175. { employee_id: 1, firstName: 'George', lastName: 'Jefferson', phone: "123-456-7890", email: "email@gmail.com", pay: "20" },
  176. { employee_id: 2, firstName: 'Bill', lastName: 'Smith', phone: "123-456-7890", email: "email@gmail.com", pay: "20" },
  177. { employee_id: 3, firstName: 'James', lastName: 'Bond', phone: "123-456-7890", email: "email@gmail.com", pay: "20" },
  178. { employee_id: 4, firstName: 'Sam', lastName: 'Anderson', phone: "123-456-7890", email: "email@gmail.com", pay: "20" },
  179. ]),
  180.  
  181. knex('projects').insert([
  182. { project_id: 1, location_id: 1, project_mgr_emp_id: 1, project_date_begin: '2018-01-01', project_date_end: '2018-01-20'},
  183. { project_id: 2, location_id: 2, project_mgr_emp_id: 3, project_date_begin: '2018-01-15', project_date_end: '2018-01-30'},
  184. ]),
  185.  
  186. // note: UTC is 7 hours ahead of PST
  187. // 2018-01-01T00:00:00.000Z
  188. knex('activities').insert([
  189. { activity_id: 1, activity_code: 1, project_id: 1, activity_notes: 'paint with blue until you run out, then switch to red', activity_datetime_begin: '2018-01-01T13:00:00.000Z', activity_datetime_end: '2018-01-01T21:00:00.000Z'},
  190. { activity_id: 2, activity_code: 1, project_id: 2, activity_notes: 'work on building shelf in store', activity_datetime_begin: '2018-02-01T13:00:00.000Z', activity_datetime_end: '2018-02-01T21:00:00.000Z'},
  191. ]),
  192.  
  193. // for coordinates, take first 8 digits supplied
  194. knex('timesheets').insert([
  195. { timesheet_id: 1, activity_id: 1, authorized_by_emp_id: 1, cost_center_id: 1, timesheet_for_emp_id: 2, timesheet_notes: 'ran 15 minutes late', timesheet_submitted_datetime: '2018-01-14T15:00:00.000Z', timesheet_clockin: '2018-01-01T13:15:00.000Z', timesheet_clockout: '2018-01-01T21:00:00.000Z', timesheet_clockin_lat: '37.685246', timesheet_clockin_long: '-122.40277', timesheet_clockout_lat: '37.685116', timesheet_clockout_long: '-122.40140'},
  196. { timesheet_id: 2, activity_id: 2, authorized_by_emp_id: 1, cost_center_id: 1, timesheet_for_emp_id: 3, timesheet_notes: 'all good', timesheet_submitted_datetime: '2018-02-14T15:02:00.000Z', timesheet_clockin: '2018-02-01T13:05:00.000Z', timesheet_clockout: '2018-02-02T21:05:00.000Z', timesheet_clockin_lat: '37.684578', timesheet_clockin_long: '-122.40233', timesheet_clockout_lat: '37.684116', timesheet_clockout_long: '-122.40240'},
  197. ])
  198.  
  199. ]) // end promise.all
  200. .catch(reason => {
  201. console.log('inner error: ', reason);
  202. })
  203.  
  204. }) // end main .then
  205. .catch(reason => {
  206. console.log('outer error:', reason);
  207. }); // last .then
  208. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement