Packagecom.memamsa.airdb
Classpublic class Migrator
ImplementsIMigratable

The Migrator accepts schema information for a Modeler sub-class and automatically migrates the database table and column definitions to reflect the schema specified as part of the class definition.

Typically, each Modeler sub-class instantiates a Migrator object, either as a Class constant or static member variable, with information about its Modeler sub-class, table options and a set of directives for column definitions.

Additional directives can be added to the code at any time. When instantiated, each Migrator object compares the schema information for their model using version numbers, and applies the latest directives to bring the schema up-to-date.

The Migrator provides several basic methods for defining columns and creating tables, including short-cut specifiers for foreign key mapping and join table creation. Generally, migrations are non- destructive. This especially allows AIR applications to seamlessly update their schema as part of application updates while keeping user data intact.


Example
A model class for a blog Post that has defined schema over time in terms of three migration directives.
  dynamic class Post extends Modeler {
     private static var migrations:Migrator = new Migrator(
       // model class on which to apply the migration
       Post,
       // global options, id as primary autoincr field, 
       // name table as 'blog_posts' default would be 'posts'
       {id: true, storage: 'blog_posts'},
       // migration directives. 
       [
         function(my:Migrator):void {
           my.createTable(function():void{
             my.column('title', DB.Field.VarChar, {limit: 255});
             my.column('url', DB.Field.VarChar, {limit: 128});
             my.columnTimeStamps();
           });
         },
         function(my:Migrator):void {
           my.belongsTo(User)
         },
          function(my:Migrator):void {
            my.joinTable(Category);
          }
          // New directives go here
       ]
     )
  }
  

See also

Modeler
DB


Public Properties
 PropertyDefined by
  storeName : String
[read-only] Returns the storeName (database table name) corresponding to this Migrator
Migrator
Public Methods
 MethodDefined by
  
Migrator(klass:Class, options:Object, directives:Array)
Migrator
  
belongsTo(klass:Class):void
Specify a foreign key column for another model within the table for this model.
Migrator
  
column(name:String, dataType:uint, options:Object = null):void
Column specifier.
Migrator
  
Short-cut specifier to add created_at and updated_at DATETIME columns
Migrator
  
createTable(block:Function = null):void
Create the database table corresponding to the model.
Migrator
  
dropTable():void
Drop associated table from the database.
Migrator
  
joinTable(klass:Class, joinAttr:Array = null):void
Construct a join table for a has_and_belongs_to_many association.
Migrator
  
migrate(fromVer:uint = 0, toVer:uint = 0):uint
Runs necessary migration directives to bring the schema for the corresponding database table up-to-date.
Migrator
  
removeColumn(name:String):void
Unsupported in SQLite
Migrator
Property detail
storeNameproperty
storeName:String  [read-only]

Returns the storeName (database table name) corresponding to this Migrator

Implementation
    public function get storeName():String

See also

Constructor detail
Migrator()constructor
public function Migrator(klass:Class, options:Object, directives:Array)

Parameters
klass:Class
 
options:Object
 
directives:Array
Method detail
belongsTo()method
public function belongsTo(klass:Class):void

Specify a foreign key column for another model within the table for this model.

Parameters
klass:Class — The class for the other model (which has_many of this model)

See also

column()method 
public function column(name:String, dataType:uint, options:Object = null):void

Column specifier. This method can be used either inside or outside of the function block argument for createTable

Parameters
name:String — Field name for the database column.
 
dataType:uint — The column type.
 
options:Object (default = null) — An object specifying column options or defaults. Here are the pre-defined constants for columns and supported options.
  • DB.Serial - PRIMARY AUTOINCREMENT
  • DB.Integer - default
  • DB.VarChar - limit, default
  • DB.DateTime
  • DB.Text
  • DB.Blob
  • DB.Float

See also

columnTimestamps()method 
public function columnTimestamps():void

Short-cut specifier to add created_at and updated_at DATETIME columns

createTable()method 
public function createTable(block:Function = null):void

Create the database table corresponding to the model. Safely returns if table was already created or exists.

Parameters
block:Function (default = null) — A function block which is executed before the actual creation of the table. This function block can include column definitions which will be applied at once in one CREATE TABLE statement.
dropTable()method 
public function dropTable():void

Drop associated table from the database. Caution: Data will be lost. Use appropriate judgement.

joinTable()method 
public function joinTable(klass:Class, joinAttr:Array = null):void

Construct a join table for a has_and_belongs_to_many association. The join table has two columns corresponding to the foreign_keys of each of the table in the association. The name for the join table is conjugated from the table names being associated.

Parameters
klass:Class — The class for the other model to be associated with.
 
joinAttr:Array (default = null) — (optional) Additional attributes for the join relationship. Specified as an array of fieldSpec arrays.

See also

migrate()method 
public function migrate(fromVer:uint = 0, toVer:uint = 0):uint

Runs necessary migration directives to bring the schema for the corresponding database table up-to-date. The N migration directives specified during instantiation of this Migrator are considered as versions from 0 to N-1

Parameters
fromVer:uint (default = 0) — Starting schema version for this table.
 
toVer:uint (default = 0) — Desired ending version for the schema.

Returns
uint — The final schema version after necessary migration directives have been applied.

See also

removeColumn()method 
public function removeColumn(name:String):void

Unsupported in SQLite

Parameters
name:String