core icon indicating copy to clipboard operation
core copied to clipboard

Migration

Open stombre opened this issue 5 years ago • 0 comments

Make a system to manage migration?

we could do a up / down system? to apply / migrate data depending of the stage?


// Or connector?
await ilorm.applyMigration()

Schema.version(timestamp or version? or both?, DEF)
  .up((old, new) => {})
  .down((old, new) => {})

ex;

// Always most recent version;
const userSchema = new Schema({
  id: Schema.string(),
  firstName: Schema.string(),
  lastName: Schema.string(),
  age: Schema.number(),
})

// Declare old version of the schema;
userSchema.version(3232323, {
  id: Schema.string(),
  name: Schema.string(),
  age: Schema.string(),
})
.up(UserModel => {
  UserModel.query()
    .stream()
    .on('data', user => {
      const [firstName, lastName] = user.name.split(' ');
      user.firstName = firstName;
      user.lastName = lastName;
      user.age = parseInt(user.previous.age)
      user.save();
    });
})
.down(UserModel => {
  UserModel.query()
    .stream()
    .on('data', user => {
       user.name = `${user.firstName} ${user.lastName}`;
       user.age = `${user.previous.age}`
       user.save();
     });
});

Keep in mind;

  • We probably want a way to up / down, and change multiple model at once.
  • We want to apply up and down for similar version schema on different model at the same time.

The way to proceed is probably; for each schema version;

  1. Create new column / alter old column (for keeping data)
  2. Run up on every schema of the given version of the schema
  3. Drop old column

stombre avatar Sep 04 '20 11:09 stombre