Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
100f5826
Commit
100f5826
authored
Jan 03, 2014
by
Mark
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
migration guide improved
parent
ed11750c
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
42 additions
and
30 deletions
+42
-30
migration.md
docs/guide/migration.md
+42
-30
No files found.
docs/guide/migration.md
View file @
100f5826
...
@@ -73,21 +73,25 @@ method returns `false` to indicate that the migration cannot be reverted.
...
@@ -73,21 +73,25 @@ method returns `false` to indicate that the migration cannot be reverted.
As an example, let's show the migration about creating a news table.
As an example, let's show the migration about creating a news table.
```
php
```
php
use
yii\db\Schema
;
class
m101129_185401_create_news_table
extends
\yii\db\Migration
class
m101129_185401_create_news_table
extends
\yii\db\Migration
{
{
public
function
up
()
public
function
up
()
{
{
$this
->
db
->
createCommand
()
->
createTable
(
'tbl_news'
,
[
$this
->
createTable
(
'tbl_news'
,
[
'id'
=>
'pk'
,
'id'
=>
'pk'
,
'title'
=>
'string(128)
NOT NULL'
,
'title'
=>
Schema
::
TYPE_STRING
.
'
NOT NULL'
,
'content'
=>
'text'
,
'content'
=>
Schema
::
TYPE_TEXT
,
])
->
execute
()
;
]);
}
}
public
function
down
()
public
function
down
()
{
{
$this
->
d
b
->
createCommand
()
->
dropTable
(
'tbl_news'
)
->
execute
(
);
$this
->
d
ropTable
(
'tbl_news'
);
}
}
}
}
```
```
...
@@ -110,38 +114,40 @@ Transactional Migrations
...
@@ -110,38 +114,40 @@ Transactional Migrations
While performing complex DB migrations, we usually want to make sure that each
While performing complex DB migrations, we usually want to make sure that each
migration succeed or fail as a whole so that the database maintains the
migration succeed or fail as a whole so that the database maintains the
consistency and integrity. In order to achieve this goal, we can exploit
consistency and integrity. In order to achieve this goal, we can exploit
DB transactions.
DB transactions. We could use special methods
`safeUp`
and
`safeDown`
for these purposes.
We could explicitly start a DB transaction and enclose the rest of the DB-related
code within the transaction, like the following:
```
php
```
php
use
yii\db\Schema
;
class
m101129_185401_create_news_table
extends
\yii\db\Migration
class
m101129_185401_create_news_table
extends
\yii\db\Migration
{
{
public
function
u
p
()
public
function
safeU
p
()
{
{
$transaction
=
$this
->
getDbConnection
()
->
beginTransaction
();
$this
->
createTable
(
'tbl_news'
,
[
try
'id'
=>
'pk'
,
{
'title'
=>
Schema
::
TYPE_STRING
.
' NOT NULL'
,
$this
->
db
->
createCommand
()
->
createTable
(
'tbl_news'
,
[
'content'
=>
Schema
::
TYPE_TEXT
,
'id'
=>
'pk'
,
]);
'title'
=>
'string NOT NULL'
,
'content'
=>
'text'
,
$this
->
createTable
(
'tbl_user'
,
[
])
->
execute
();
'id'
=>
'pk'
,
$transaction
->
commit
();
'login'
=>
Schema
::
TYPE_STRING
.
' NOT NULL'
,
}
'password'
=>
Schema
::
TYPE_STRING
.
' NOT NULL'
,
catch
(
Exception
$e
)
]);
{
}
echo
"Exception: "
.
$e
->
getMessage
()
.
"
\n
"
;
$transaction
->
rollback
();
public
function
safeDown
()
return
false
;
{
}
$this
->
dropTable
(
'tbl_news);
$this->dropTable('
tbl_user
'
);
}
}
// ...similar code for down()
}
}
```
```
When your code uses more then one query it is recommended to use
`safeUp`
and
`safeDown`
.
> Note: Not all DBMS support transactions. And some DB queries cannot be put
> Note: Not all DBMS support transactions. And some DB queries cannot be put
> into a transaction. In this case, you will have to implement `up()` and
> into a transaction. In this case, you will have to implement `up()` and
> `down()`, instead. And for MySQL, some SQL statements may cause
> `down()`, instead. And for MySQL, some SQL statements may cause
...
@@ -301,7 +307,7 @@ are located within the module's `migrations` directory, we can use the following
...
@@ -301,7 +307,7 @@ are located within the module's `migrations` directory, we can use the following
command:
command:
```
```
yii migrate/up --migrationPath=
ext
.forum.migrations
yii migrate/up --migrationPath=
@app.modules
.forum.migrations
```
```
...
@@ -314,8 +320,14 @@ or we may want to use a customized migration template. We can do so by modifying
...
@@ -314,8 +320,14 @@ or we may want to use a customized migration template. We can do so by modifying
the console application's configuration file like the following,
the console application's configuration file like the following,
```
php
```
php
TBD
'controllerMap'
=>
[
'migrate'
=>
[
'class'
=>
'yii\console\MigrateController'
,
'migrationTable'
=>
'my_custom_migrate_table'
,
],
]
```
```
Now if we run the
`migrate`
command, the above configurations will take effect
Now if we run the
`migrate`
command, the above configurations will take effect
without requiring us to enter the command line options every time.
without requiring us to enter the command line options every time. Other command options
can be also configured this way.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment