-
-
Notifications
You must be signed in to change notification settings - Fork 191
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support data migrations #53
Comments
@nrempel I was think of something like this.table('users', function () {
})
this.db(function * (database) {
yield database.from('users').select('name')
}) |
That looks 👌 |
🎉 |
So if we want to migrate data and then remove a field we should do? this.table('users', function () {
// add the new columns
})
this.db(function * (database) {
// migrate the data
})
this.table('users', function () {
// remove the old column
}) |
That looks correct to me @RomainLanz. You could also break it out into several migration files too if you want. @thetutlage I've tested this in my system and everything looks to be working correctly. 👍 |
@thetutlage so far this is working really well. One awkward thing is working with transactions.
I wonder if it's possible to facilitate the use of transactions? Something like:
Or possibly even (not sure if this is possible):
|
Both are possible http://adonisjs.com/docs/3.0/query-builder#database-transactions |
Amazing! thanks. |
this.db will allow to execute database actions using the database provider Closes #53
@thetutlage has this functionality been removed in Adonis 4? I'm struggling to get the migration to wait for some data to be migrated. I want to do this:
I'm not sure the best way to do this. If I just use an |
Mind sharing some code :) |
@thetutlage - I've created a simple example, which show my struggle. Example 1: Using an async up () {
// Create a new table
this.create('photos', (table) => {
this.increments()
this.string('url')
})
// Move some data
const photos = await use('App/Models/User').query().where('photo_url', '>', '')
const photosData = photos.map((record) => {
return {
url: record.photo_url
})
await use('App/Models/Photo').createMany(photosData)
// Modify another table
this.table('users', (table) => {
table.dropColumn('photo_url')
})
} Example 2: I move the data migration into the closure that will modify the second table. This waits until the first table is created, but the second table never ends up getting modified, and the migration script never terminates itself. It completes the migration, but the command doesn't terminate. up () {
// Create a new table
this.create('photos', (table) => {
this.increments()
this.string('url')
})
// Modify another table
this.table('users', async (table) => {
// Move some data
const photos = await use('App/Models/User').query().where('photo_url', '>', '')
const photosData = photos.map((record) => {
return {
url: record.photo_url
})
await use('App/Models/Photo').createMany(photosData)
// now modify the table
table.dropColumn('photo_url')
})
} Example 3: I put the data migration in it's own closure (using a async up () {
// Create a new table
this.create('photos', (table) => {
this.increments()
this.string('url')
})
// Put the data migration in a closure, so it'll be queued with the other migrations
this.table('photos', async (table) => {
// Move some data
const photos = await use('App/Models/User').query().where('photo_url', '>', '')
const photosData = photos.map((record) => {
return {
url: record.photo_url
})
await use('App/Models/Photo').createMany(photosData)
})
// Modify another table
this.table('users', (table) => {
table.dropColumn('photo_url')
})
} |
@thetutlage - Any thoughts on this? If it's not possible, then I can sort out another way. Thanks! |
@dusbuss Sorry for getting late. Also I recommend creating new issues, since at times closed issues are missed. I have add a new commit, which let you run arbitrary code in the same sequence as you have wrote code. Here's the commit for that and related docs. Would you mind trying it by installing from Github and lemme know, then I will publish it on npm |
@thetutlage - awesome! I just installed, and it works like a dream! Thank you! |
I installed it on MySQL Workbench 8.0 but it don't show any catalog in tool, but the plugin has been installed, how can I work with him? Catalog menu/sub menu is not showing in tools |
Currently, Adonis only supports schema migrations which only modify the schema of the database.
It would be convenient to also be able to run data migrations which modify the contents of the database.
I can already do this by using the
Database
provider in a migration but it seems out of place and I can't useco
.The text was updated successfully, but these errors were encountered: