Skip to content

Commit 864a6d0

Browse files
committed
Add migration for initial schema.
Easier to get started with migrations than copy/pasting schema from the docs.
1 parent 6076271 commit 864a6d0

2 files changed

Lines changed: 197 additions & 0 deletions

File tree

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
use Migrations\AbstractMigration;
5+
6+
class InitialSchema extends AbstractMigration
7+
{
8+
/**
9+
* Up Method.
10+
*
11+
* More information on this method is available here:
12+
* https://book.cakephp.org/phinx/0/en/migrations.html#the-up-method
13+
* @return void
14+
*/
15+
public function up()
16+
{
17+
$this->table('articles')
18+
->addColumn('user_id', 'integer', [
19+
'default' => null,
20+
'limit' => null,
21+
'null' => false,
22+
])
23+
->addColumn('title', 'string', [
24+
'default' => null,
25+
'limit' => 255,
26+
'null' => false,
27+
])
28+
->addColumn('slug', 'string', [
29+
'default' => null,
30+
'limit' => 191,
31+
'null' => false,
32+
])
33+
->addColumn('body', 'text', [
34+
'default' => null,
35+
'limit' => null,
36+
'null' => true,
37+
])
38+
->addColumn('published', 'boolean', [
39+
'default' => false,
40+
'limit' => null,
41+
'null' => true,
42+
])
43+
->addColumn('created', 'datetime', [
44+
'default' => null,
45+
'limit' => null,
46+
'null' => true,
47+
])
48+
->addColumn('modified', 'datetime', [
49+
'default' => null,
50+
'limit' => null,
51+
'null' => true,
52+
])
53+
->addIndex(
54+
[
55+
'slug',
56+
],
57+
['unique' => true]
58+
)
59+
->addIndex(
60+
[
61+
'user_id',
62+
]
63+
)
64+
->create();
65+
66+
$this->table('articles_tags', ['id' => false, 'primary_key' => ['article_id', 'tag_id']])
67+
->addColumn('article_id', 'integer', [
68+
'default' => null,
69+
'limit' => null,
70+
'null' => false,
71+
])
72+
->addColumn('tag_id', 'integer', [
73+
'default' => null,
74+
'limit' => null,
75+
'null' => false,
76+
])
77+
->addIndex(
78+
[
79+
'tag_id',
80+
]
81+
)
82+
->addIndex(
83+
[
84+
'article_id',
85+
]
86+
)
87+
->create();
88+
89+
$this->table('tags')
90+
->addColumn('title', 'string', [
91+
'default' => null,
92+
'limit' => 191,
93+
'null' => true,
94+
])
95+
->addColumn('created', 'datetime', [
96+
'default' => null,
97+
'limit' => null,
98+
'null' => true,
99+
])
100+
->addColumn('modified', 'datetime', [
101+
'default' => null,
102+
'limit' => null,
103+
'null' => true,
104+
])
105+
->addIndex(
106+
[
107+
'title',
108+
],
109+
['unique' => true]
110+
)
111+
->create();
112+
113+
$this->table('users')
114+
->addColumn('email', 'string', [
115+
'default' => null,
116+
'limit' => 255,
117+
'null' => false,
118+
])
119+
->addColumn('password', 'string', [
120+
'default' => null,
121+
'limit' => 255,
122+
'null' => false,
123+
])
124+
->addColumn('created', 'datetime', [
125+
'default' => null,
126+
'limit' => null,
127+
'null' => true,
128+
])
129+
->addColumn('modified', 'datetime', [
130+
'default' => null,
131+
'limit' => null,
132+
'null' => true,
133+
])
134+
->create();
135+
136+
$this->table('articles')
137+
->addForeignKey(
138+
'user_id',
139+
'users',
140+
'id',
141+
[
142+
'update' => 'NO_ACTION',
143+
'delete' => 'NO_ACTION',
144+
]
145+
)
146+
->update();
147+
148+
$this->table('articles_tags')
149+
->addForeignKey(
150+
'tag_id',
151+
'tags',
152+
'id',
153+
[
154+
'update' => 'NO_ACTION',
155+
'delete' => 'NO_ACTION',
156+
]
157+
)
158+
->addForeignKey(
159+
'article_id',
160+
'articles',
161+
'id',
162+
[
163+
'update' => 'NO_ACTION',
164+
'delete' => 'NO_ACTION',
165+
]
166+
)
167+
->update();
168+
}
169+
170+
/**
171+
* Down Method.
172+
*
173+
* More information on this method is available here:
174+
* https://book.cakephp.org/phinx/0/en/migrations.html#the-down-method
175+
* @return void
176+
*/
177+
public function down()
178+
{
179+
$this->table('articles')
180+
->dropForeignKey(
181+
'user_id'
182+
)->save();
183+
184+
$this->table('articles_tags')
185+
->dropForeignKey(
186+
'tag_id'
187+
)
188+
->dropForeignKey(
189+
'article_id'
190+
)->save();
191+
192+
$this->table('articles')->drop()->save();
193+
$this->table('articles_tags')->drop()->save();
194+
$this->table('tags')->drop()->save();
195+
$this->table('users')->drop()->save();
196+
}
197+
}
6.39 KB
Binary file not shown.

0 commit comments

Comments
 (0)