I have a Prdoucts table and what I need to do is increase the value of this table's price column by 20% how can I accomplish this using adonis version 5 migrations.
this is what I have tried:
import BaseSchema from '@ioc:Adonis/Lucid/Schema'
export default class extends BaseSchema {
protected tableName = 'products'
public async up() {
this.defer(async (db) => {
const products = await db.from('products').select('*').increment('price','products_price'*20/100)
})
}
public async down() {
this.schema.dropTable(this.tableName)
}
}
From a SQL point of view you just need to run a statement like the following:
update Prdoucts set price = price * 1.2;
In order to run this as part of a adonis migration the documentation says:
Quite often, you will have requirements to run SQL queries other than just creating/altering tables. For example: Migrating data to a newly created table before deleting the old table.
You should define these operations using the this.defer method
As to how to execute the operation there might be a way doing this using the Adonis Query Builder, but probably the easiest is to just run a raw update:
Database
.raw(`update Prdoucts set price = price * 1.2`, [])