Class: Migration

Migration

new Migration(version, models)

Migration model contains several migration tasks.

Parameters:
Name Type Description
version Number

The migration's version number.

models Models

The current models module.

Methods

activate()

Let's all models know we are in migration mode.

This sets this migration as the active migration on all models, which proxies all of their methods to this migration so we can create migration tasks for them if executed.

addProperty(property, existingProperty)

Adds a property to an existing model edit task, or creates an edit task if it does not yet exists.

If there is a model create task, the property is ignored as a create task already creates all model's properties.

Parameters:
Name Type Description
property Property

The property to be added.

existingProperty Property

Optionally, a property with the same name if it already exists.

addTask(self, methodName, existingParams)

Adds a generic task to the migration.

Parameters:
Name Type Description
self Object

The object on which to call the method.

methodName String

The name of the method to call.

existingParams Dictionary

The arguments to be passed to the method.

Returns:

This promise resolves after the method is finished.

Type
Promise

createEditTask(existingModel)

Creates a task to edit a model.

Editing a model alters the model's table, either adding, removing or changing columns.

Parameters:
Name Type Description
existingModel Model

The model to be edited.

createModel(existingModel)

Adds a create model task for the given model. If a create model task already exists, an error is thrown.

Parameters:
Name Type Description
existingModel Model

The model to be created.

deactivate()

This deactives the migration state on all models, see Migration#activate.

destroyModel(existingModel)

Adds a destroy model task of the given model in existingModel.

If an edit task exists for the model, the edit task is removed from the migration.

This method throws an error if a destroy model task for this model already exists.

Parameters:
Name Type Description
existingModel Model

The model to destroy.

findTaskByName(name, model)

Finds a specific migration task with name for model.

Parameters:
Name Type Description
name String

The name of the task.

model Model

The model associated to the task.

go(direction)

Executes the migration in the given direction.

This method first calls Migration#up or Migration#down (depending on the migration). In these methods the migration tasks are created.

If any new models are added, we create forward references to newly created models and call the migration in the opposite direction to revert changes. Then we call the migration-specific method again (with forward references in place). Forward references are neccesary so that we can reference this.models.User in any task even if it has not been created yet.

Then we sort all the tasks based on their dependencies.

Then we start a database transaction, and execute all the separate tasks. If an error occurs, we rollback and an error is thrown. Once all tasks are executed we commit the transaction.

Lastly, we create a Schema instance for this migration's version number.

Parameters:
Name Type Description
direction up | down

Either "up" or "down".

Returns:
Type
Promise

removeProperty(property)

Adds a remove property task to an existing edit model task, or creates an edit model task.

If a destroy model task exists, removing the property is ignored because the model is already queued for destruction.

Parameters:
Name Type Description
property Property

The property to be removed.

soft()

Executes a migration without actually applying anything to the database.

Returns:
Type
Promise

sortTasks()

To Do:
  • Refactor this. Sorts all tasks so they get executed in the correct order as some tasks are dependant on other tasks. Brute-force style.

Do you have any questions or comments on this page? Start a discussion below.

comments powered by Disqus
This is the footer.