Repository navigation
Migration Methods
All migrations inherent from the Ruckusing_BaseMigration class, so they must be prefixed with $this (e.g. $this->addColumn().
The available methods are (brief list below, with detailed usageg further down):
createDatabasedropDatabase
createTabledropTablerenameTable
addColumnremoveColumnrenameColumnchangeColumn
addIndexremoveIndex
executeselectOneselectAll
There are two database-level operations, createDatabase and dropDatabase.
Migrations that manipulate databases on this high of a level are used rarely.
This command is slightly useless since normally you would be running your migrations against an existing database (created and setup with whatever your traditional RDMBS creation methods are). However, if you wanted to create another database from a migration, this method is available:
Method Call: createDatabase
Parameters
-
name: Name of the new database
Example:
<?php
$this->createDatabase("my_project");
?>To completely remove a database and all of its tables (and data!).
Method Call: dropDatabase
Parameters
-
name: Name of the existing database
Example:
<?php
$this->dropDatabase("my_project");
?>This method is probably the most complex of all methods, but also one of the most widely used.
Method Call: createTable
Parameters
-
name: Name of the new table -
options: (Optional) An associative array of options for creating the new table. Supported option key/value pairs are:-
id: Boolean - whether or not the framework should automatically generate a primary key. For MySQL the column will be calledidand be of type integer with auto-incrementing. -
options: A string representing finalization parameters that will be passed verbatim to the tail of the create table command. Often this is used to specify the storage engine for MySQL, e.g. 'options' => 'Engine=InnoDB'
-
Assumptions Ultimately this method delegates to the appropriate RDMBS adapter and the MySQL adapter makes some important assumptions about the structure of the table.
The database migration framework offers a rich facility for creating, removing and renaming tables.
A call to $this->createTable(...) actually returns a TableDefinition object.
This method of the framework is one of the very few which actually returns a
result that you must interact with (as and end user).
The steps for creating a new table are:
- Create the table with a name and any optional options and store the return value for later use:
<?php
$users = $this->createTable("users");
?>- Add columns to the table definition:
<?php
$users->column("first_name", "string");
$users->column("last_name", "string");
?>- Call
finish()to actually create the table with the definition and its columns:
<?php
$users->finish();
?>By default, the table type will be what your database defaults too. To specify
a different table type (e.g. InnoDB), pass a key of options into the
$options array, e.g.
Example A: Create a new InnoDB table called users.
<?php
$this->createTable('users', array('options' => 'Engine=InnoDB'));
?>- This command also assumes that you want an
idcolumn. This column does not need to be specified, it will be auto-generated, unless explicitly told not to via theidkey in$optionsarray.
Example B: Create a new table called users but do not automatically make
a primary key.
<?php
$this->createTable('users', array('id' => false));
?>The primary key column will be created with attributes of
int(11) unsigned auto_increment.
Example C: To specify your own primary key called 'guid':
<?php
$t = $this->createTable(
'users',
array(
'id' => false,
'options' => 'Engine=InnoDB',
)
);
$t->column(
'guid',
'string',
array(
'primary_key' => true,
'limit' => 64,
)
);
$t->finish();
?>Tables can be removed by using the dropTable method call. As might be
expected, removing a table also removes all of its columns and any indexes.
Method Call: dropTable
Arguments::
-
tableName: The name of the table to remove.
Tables can be renamed using the renameTable method.
Method Call: renameTable
Arguments::
-
tableName: The existing name of the table. -
newName: The new name of the table.
For the complete documentation on adding new columns, please see Adding Columns.
Removing a database column is very simple, but keep in mind that any index associated with that column will also be removed.
Method call: removeColumn
Arguments:
-
tableName: The name of the table from which the column will be removed. -
columnName: The column to be removed.
Example A:: Remove the age column from the users table.
<?php
$this->removeColumn("users", "age");
?>Database columns can be renamed (assuming the underlying RDMBS/adapter supports it).
Method call: renameColumn
Arguments:
-
tableName: The name of table from which the column is to be renamed. -
columnName: The existing name of the column. -
newColumnName: The new name of the column.
Example A: From the users table, rename first_name to fname
<?php
$this->renameColumn("users", "first_name", "fname");
?>The type, defaults or NULL support for existing columns can be modified. If
you want to just rename a column then use the renameColumn method. This
method takes a generalized type for the column's type and also an array of
options which affects the column definition. For the available types and
options, see the documentation on adding new columns, AddingColumns.
Method Call: changeColumn
Arguments:
-
tableName: The name of the table from which the column will be altered. -
columnName: The name of the column to change. -
type: The desired generalized type of the column. -
options: (Optional) An associative array of options for the column definition.
Example A: From the users table, change the length of the first_name
column to 128.
<?php
$this->changeColumn("users", "first_name", "string", array('limit' => 128) );
?>Indexes can be created and removed using the framework methods.
Method Call: addIndex
Arguments:
-
table: The name of the table to add the index to. -
column: The column to create the index on. If this is a string, then it is presumed to be the name of the column, and the index will be a single-column index. If it is an array, then it is presumed to be a list of columns name and the index will then be a multi-column index, on the columns specified. -
options: (Optional) An associative array of options to control the index generation. Keys / Value pairs:-
unique: values:trueorfalse. Whether or not create a unique index for this column. Defaults tofalse. -
name: values: user defined. The name of the index. If not specified, a default name will be generated based on the table and column name.
-
Known Issues / Workarounds: MySQL is currently limited to 64 characters for identifier names. When add_index is used without specifying the name of the index, Ruckusing will generate a suitable name based on the table name and the column(s) being index. For example, if there is a users table and an index is being generated on the username column then the generated index name would be: idx_users_username . If one is attempting to add a multi-column index then its very possible that the generated name would be longer than MySQL's limit of 64 characters. In such situations Ruckusing will raise an error suggesting you use a custom index name via the name option parameter. See Example C.
Example A: Create an index on the email column in the users table.
<?php
$this->addIndex("users", "email");
?>Example B: Create a unqiue index on the ssn column in the users table.
<?php
$this->addIndex("users", "ssn", array('unique' => true)));
?>Example C: Create an index on the blog_id column in the posts table,
but specify a specific name for the index.
<?php
$this->addIndex("posts", "blog_id", array('name' => 'index_on_blog_id'));
?>Example D: Create a multi-column index on the email and ssn columns
in the users table.
<?php
$this->addIndex("users", array('email', 'ssn') );
?>Easy enough. If the index was created using the sibling to this method (addIndex)
then one would need to just specify the same arguments to that method
(but calling removeIndex).
**Method Call: ** removeIndex
Arguments:
-
tableName: The name of the table to remove the index from. -
columnName: The name of the column from which to remove the index from. -
options: (Optional) An associative array of options to control the index removal process. Key / Value pairs:-
name: values: user defined. The name of the index to remove. If not specified, a default name will be generated based on the table and column name. If during the index creation process (using theaddIndexmethod) and anameis specified then you will need to do the same here and specify the same name. Otherwise, the default name that is generated will likely not match with the actual name of the index.
-
Example A: Remove the (single-column) index from the users table on the
email column.
<?php
$this->removeIndex("users", "email");
?>Example B: Remove the (multi-column) index from the users table on the
email and ssn columns.
<?php
$this->removeIndex("users", array("email", "ssn") );
?>Example C: Remove the (single-column) named index from the users table on
the email column.
<?php
$this->removeIndex(
"users",
"email",
array('name' => "index_on_email_column")
);
?>Arbitrary query execution is available via a set of methods.
The execute() method is intended for queries which do not return any data,
e.g. INSERT, UPDATE or DELETE.
Example A: Update all rows give some criteria
<?php
$this->execute("UPDATE foo SET name = 'bar' WHERE .... ");
?>For queries that return results, e.g. SELECT queries, then use either
selectOne or selectAll depending on what you are returning.
Both of these methods return an associative array with each element of the array being itself another associative array of the column names and their values.
selectOne() is intended for queries where you are expecting a single result
set, and selectAll() is intended for all other cases (where you might not
necessarily know how many rows you will be getting).
NOTE: Since these methods take raw SQL queries as input, they might not necessarily be portable across all RDBMS.
Example A: (selectOne) Get the sum of of a column
<?php
$result = $this->selectOne("SELECT SUM(total_price) AS total_price FROM orders");
if($result) {
echo "Your revenue is: " . $result['total_price'];
}
?>Example B: (selectAll) Get all rows and iterate over each one, performing
some operation:
<?php
$result = $this->selectAll("SELECT email, first_name, last_name FROM users WHERE created_at >= SUBDATE( NOW(), INTERVAL 7 DAY)");
if($result) {
echo "New customers: (" . count($result) . ")\n";
foreach($result as $row) {
printf("(%s) %s %s\n", $row['email'], $row['first_name'], $row['last_name']);
}
}
?>