Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How i can alter a column type without lose the database data in migration using adonis?

I have this class:

class BookUnitQuestionSchema extends Schema {
  up () {
    this.create('book_unit_question', (table) => {
      table.increments()
      table.integer('book_unit_id').references('id').inTable('book_unit')
      table.string('correct_answer_description')
      table.boolean('status').defaultTo(false)
      table.integer('user_id').references('id').inTable('users')
      table.timestamps()
    })
  }

  down () {
    this.drop('book_unit_question')
  }
}

I need to change the data type of the column correct_answer_description to text. If i change my actually up() method to:

table.text('correct_answer_description')

and make a: adonis migration:refresh

All table is recreated and i lose the data in this table.

How i can only change the datatype without lose the data?

I try something like:

this.alter('book_unit_question', (table) => {
  table.text('correct_answer_description')
})

And make a:

adonis migration:run

But i get:

Nothing to migrate

like image 630
bla Avatar asked Mar 03 '23 04:03

bla


1 Answers

You need to create a new migration file like :

adonis make:migration ...

Type modification (new migration file) : .alter()

class BookUnitQuestionSchema extends Schema {
  up() {
    this.alter('book_unit_questions', (table) => {
      table.text('correct_answer_description').notNullable().alter();
    })
  }

  // reverse modification 
  down() {
    this.table('book_unit_questions', (table) => {
      table.string('correct_answer_description').notNullable().alter();
    })
  }
}

and run pending migrations :

> adonis migration:run
like image 195
crbast Avatar answered May 11 '23 10:05

crbast