dev-master
9999999-devDatabase migrations for the Elefant CMS
MIT
The Requires
database migrations app cms elefant
Database migrations for the Elefant CMS
This app provides a database migrations framework for the Elefant CMS, including a simple API for automating migrations as well as a series of command line options for viewing and applying/reverting changes., (*1)
Status: Beta, (*2)
Copy the migrate
app folder into apps/migrate
and run the following
command to import the database schema:, (*3)
./elefant import-db apps/migrate/conf/install_mysql.sql
Note: Replace the database driver with the appropriate one for your site., (*4)
From the command line:, (*5)
# update to the latest revision ./elefant migrate/up myapp\\MyModel # update to the specified revision ./elefant migrate/up myapp\\MyModel 20130922012345 # revert all revisions ./elefant migrate/down myapp\\MyModel # revert to the specified revision ./elefant migrate/down myapp\\MyModel 20130922012345 # list all available migrations ./elefant migrate/list # list all versions of a migration ./elefant migrate/versions myapp\\MyModel # check which version is current ./elefant migrate/current myapp\\MyModel # check if the current version is the latest ./elefant migrate/is-latest myapp\\MyModel # generate a new migration class ./elefant migrate/generate myapp\\MyModel
Here is a basic migration that creates a table on migrate/up
and drops
it on migrate/down
., (*6)
table () ->column ('id', 'integer', array ('primary' => true)) ->column ('title', 'string', array ('limit' => 72)) ->column ('created', 'datetime', array ('null' => false)) ->column ('body', 'text', array ('null' => false)) ->index (array ('created')) ->create (); } public function down () { return $this->drop (); } } ?>
This migration defines a table with four columns, which will translate
to the following CREATE TABLE
statement:, (*7)
CREATE TABLE #prefix#mymodel ( id integer primary key, title char(72), created datetime not null, body text not null );
A few things to note:, (*8)
MODEL_DATETIME
that extend
a base \Migration
class.migrations
folder, and must be named to match
the class name.$table
property, or pass the
name explicitly to methods like table()
and drop()
.#prefix#
in database names will be replaced with the value of the prefix
setting from the [Database]
section of Elefant's global configuration.up()
and down()
which apply or revert
the changes for the migration, respectively. Each method should return
true or false depending on whether they succeeded.We recommend using dates of the form YYYYMMDDHHIISS
to keep your migrations
in sequential order. The app doesn't enforce a particular naming scheme
however, so you are free to name them using any combination of letters and
numbers, just be aware that it will sort them alphanumerically so you should
name them accordingly to apply them in the right order., (*9)
These methods are inherited from \Migration
for you to use. Methods that
execute SQL statements will return true or false, and $this->error
will
contain any error messages., (*10)
add_column($name, $type = 'char', $options = array ())
Add a column by altering an existing table. Note that SQLite will always add
the column to the end of the table, even if 'after' => 'colname'
is passed
as an option., (*11)
column($name, $type = 'char', $options = array ())
Adds a column to a CREATE TABLE
definition. Returns $this
so you can chain
several column()
calls together, followed by create()
to execute the query., (*12)
The column type value can be anything supported by your database of choice.
string
is also an alias for char
., (*13)
Options include:, (*14)
auto-increment
- The column should auto-incrementcomment
- A comment to add to the columndefault
- The default value of the columnlimit
- The character limit of the columnnull
- The column may or may not be nullprimary
- The column is a primary keysigned
- The column is signed (may contain negative values)unique
- The column value must be uniqueunsigned
- The column values are unsigned (may contain only positive values)create()
Executes a CREATE TABLE
statement based on the previous calls to table()
and column()
., (*15)
driver()
Returns the PDO driver name for the database connection., (*16)
drop($table = null)
Executes a DROP TABLE
statement., (*17)
drop_column($name)
Drop a column from the table. Note: Not supported in SQLite., (*18)
index($fields)
Add an index to the table definition with the specified list of field names., (*19)
rename_column($old, $new, $type = 'char', $options = array ())
Rename an existing column. Note: Not supported in SQLite., (*20)
run($sql, $params = array ())
Execute a direct SQL statement., (*21)
table($table = null)
Initializes a new CREATE TABLE
chain. Returns $this
so you can chain
several column()
calls to it, followed by create()
to execute the query., (*22)
Database migrations for the Elefant CMS
MIT
database migrations app cms elefant