Created
March 28, 2018 16:18
-
-
Save yihyang/7fed8efa135ca58022fcf5cc2d40bd7f to your computer and use it in GitHub Desktop.
KnexJs Multiple Table In Single Migration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| exports.up = function(knex, Promise) { | |
| return renameProductsTable() | |
| .then(updateRecordsReferences) | |
| .then(updateSummariesReferences); | |
| function renameProductsTable() { | |
| return knex.schema.renameTable('products', 'finished_goods'); | |
| } | |
| function updateRecordsReferences() { | |
| return knex.schema.table('records', function(t) { | |
| t.dropForeign('product_id'); | |
| t.renameColumn('product_id', 'program_id'); | |
| t.foreign('program_id').references('id').inTable('finished_goods').onDelete('cascade'); | |
| }) | |
| } | |
| function updateSummariesReferences() { | |
| return knex.schema.table('summaries', function(t) { | |
| t.dropForeign('product_id'); | |
| t.renameColumn('product_id', 'program_id'); | |
| t.foreign('program_id').references('id').inTable('finished_goods').onDelete('cascade'); | |
| }) | |
| } | |
| }; | |
| exports.down = function(knex, Promise) { | |
| return renamefinished_goodsTable() | |
| .then(updateRecordsReferences) | |
| .then(updateSummariesReferences); | |
| function renameProgramsTable() { | |
| return knex.schema.renameTable('finished_goods', 'products'); | |
| } | |
| function updateRecordsReferences() { | |
| return knex.schema.table('records', function(t) { | |
| t.dropForeign('program_id'); | |
| t.renameColumn('program_id', 'product_id'); | |
| t.foreign('product_id').references('id').inTable('products').onDelete('cascade'); | |
| }) | |
| } | |
| function updateSummariesReferences() { | |
| return knex.schema.table('summaries', function(t) { | |
| t.dropForeign('program_id'); | |
| t.renameColumn('program_id', 'product_id'); | |
| t.foreign('product_id').references('id').inTable('products').onDelete('cascade'); | |
| }) | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment