Skip to content

Commit 7e4d784

Browse files
committed
add EnumColumn test, update docs
1 parent 2e40628 commit 7e4d784

File tree

4 files changed

+129
-30
lines changed

4 files changed

+129
-30
lines changed

documentation/Migrations.md

+103-29
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
| table | string | Name of table to create |
1010
| fn | Function | Function containing blueprint object |
1111

12-
```ts
12+
```typescript
1313
await Schema.createTable('users', (blueprint) =>
1414
{
1515
blueprint.integer('id', {allowNull: false, length: 15, signed: false});
@@ -23,7 +23,7 @@ await Schema.createTable('users', (blueprint) =>
2323
| --------- | -------- | --------------------- |
2424
| table | string | Name of table to drop |
2525

26-
```ts
26+
```typescript
2727
await Schema.drop('users');
2828
```
2929

@@ -33,7 +33,7 @@ await Schema.drop('users');
3333
| --------- | -------- | ---------------------------------- |
3434
| table | string | Name of table to drop if it exists |
3535

36-
```ts
36+
```typescript
3737
await Schema.dropIfExists('users');
3838
```
3939

@@ -44,7 +44,7 @@ await Schema.dropIfExists('users');
4444
| table | string | Name of table |
4545
| column | string | Name of column |
4646

47-
```ts
47+
```typescript
4848
await Schema.hasColumn('users', 'id');
4949
```
5050

@@ -54,20 +54,20 @@ await Schema.hasColumn('users', 'id');
5454
| --------- | -------- | ------------- |
5555
| table | string | Name of table |
5656

57-
```ts
57+
```typescript
5858
await Schema.hasTable('users');
5959
```
6060

6161
## Blueprint
6262

6363
#### bigInteger
6464

65-
| Parameter | Type | Description |
66-
| --------- | -------- | -------------- |
67-
| name | string | Column name |
68-
| options | object | Column options |
65+
| Parameter | Type | Description |
66+
| --------- | -------------------- | -------------- |
67+
| name | string | Column name |
68+
| options | IntegerColumnOptions | Column options |
6969

70-
```ts
70+
```typescript
7171
blueprint.bigInteger('id', {allowNull: false, length: 15, signed: false});
7272
```
7373

@@ -77,43 +77,70 @@ blueprint.bigInteger('id', {allowNull: false, length: 15, signed: false});
7777
| --------- | -------- | ----------- |
7878
| name | string | Column name |
7979

80-
```ts
80+
```typescript
8181
blueprint.blob('binaryData');
8282
```
8383

8484
#### char
8585

86-
```ts
86+
| Parameter | Type | Description |
87+
| --------- | -------- | ----------- |
88+
| name | string | Column name |
89+
90+
```typescript
8791
blueprint.char('char');
8892
```
8993

9094
#### date
9195

92-
```ts
96+
| Parameter | Type | Description |
97+
| --------- | -------- | ----------- |
98+
| name | string | Column name |
99+
100+
```typescript
93101
blueprint.date('startDate');
94102
```
95103

96104
#### dateTime
97105

98-
```ts
106+
| Parameter | Type | Description |
107+
| --------- | -------- | ----------- |
108+
| name | string | Column name |
109+
110+
```typescript
99111
blueprint.dateTime('startDateTime');
100112
```
101113

102114
#### decimal
103115

104-
```ts
116+
| Parameter | Type | Description |
117+
| --------- | ------------- | -------------- |
118+
| name | string | Column name |
119+
| options | ColumnOptions | Column options |
120+
121+
```typescript
105122
blueprint.decimal('decimalColumn', {allowNull: false});
106123
```
107124

108125
#### double
109126

110-
```ts
127+
| Parameter | Type | Description |
128+
| --------- | ------------- | -------------- |
129+
| name | string | Column name |
130+
| options | ColumnOptions | Column options |
131+
132+
```typescript
111133
blueprint.double('numberColumn', {length: 15});
112134
```
113135

114136
#### enum
115137

116-
```ts
138+
| Parameter | Type | Description |
139+
| --------- | ----------------- | -------------- |
140+
| name | string | Column name |
141+
| options | EnumColumnOptions | Column options |
142+
143+
```typescript
117144
blueprint.enum('status', {
118145
values: ['pending', 'succeeded', 'failed'],
119146
defaultValue: null
@@ -122,72 +149,119 @@ blueprint.enum('status', {
122149

123150
#### float
124151

125-
```ts
152+
| Parameter | Type | Description |
153+
| --------- | -------- | ----------- |
154+
| name | string | Column name |
155+
156+
```typescript
126157
blueprint.float('floatColumn');
127158
```
128159

129160
#### integer
130161

131-
```ts
162+
```typescript
132163
blueprint.integer('id', {length: 15});
133164
```
134165

135166
#### longText
136167

137-
```ts
168+
| Parameter | Type | Description |
169+
| --------- | -------- | ----------- |
170+
| name | string | Column name |
171+
172+
```typescript
138173
blueprint.longText('description');
139174
```
140175

141176
#### mediumInteger
142177

143-
```ts
178+
| Parameter | Type | Description |
179+
| --------- | -------------------- | -------------- |
180+
| name | string | Column name |
181+
| options | IntegerColumnOptions | Column options |
182+
183+
```typescript
144184
blueprint.mediumInteger('id');
145185
```
146186

147187
#### mediumText
148188

149-
```ts
189+
| Parameter | Type | Description |
190+
| --------- | -------- | ----------- |
191+
| name | string | Column name |
192+
193+
```typescript
150194
blueprint.mediumText('shortDescription');
151195
```
152196

153197
#### smallInteger
154198

155-
```ts
199+
| Parameter | Type | Description |
200+
| --------- | -------------------- | -------------- |
201+
| name | string | Column name |
202+
| options | IntegerColumnOptions | Column options |
203+
204+
```typescript
156205
blueprint.smallInteger('quantity');
157206
```
158207

159208
#### text
160209

161-
```ts
210+
| Parameter | Type | Description |
211+
| --------- | -------- | ----------- |
212+
| name | string | Column name |
213+
214+
```typescript
162215
blueprint.text('description');
163216
```
164217

165218
#### time
166219

167-
```ts
220+
| Parameter | Type | Description |
221+
| --------- | -------- | ----------- |
222+
| name | string | Column name |
223+
224+
```typescript
168225
blueprint.time('startTime');
169226
```
170227

171228
#### timestamp
172229

173-
```ts
230+
| Parameter | Type | Description |
231+
| --------- | -------- | ----------- |
232+
| name | string | Column name |
233+
234+
```typescript
174235
blueprint.timestamp('createdAt');
175236
```
176237

177238
#### tinyInteger
178239

179-
```ts
240+
| Parameter | Type | Description |
241+
| --------- | -------------------- | -------------- |
242+
| name | string | Column name |
243+
| options | IntegerColumnOptions | Column options |
244+
245+
```typescript
180246
blueprint.tinyInteger('active');
181247
```
182248

183249
#### varchar
184250

185-
```ts
251+
| Parameter | Type | Description |
252+
| --------- | -------- | ----------- |
253+
| name | string | Column name |
254+
255+
```typescript
186256
blueprint.varchar('username');
187257
```
188258

189259
#### year
190260

191-
```ts
261+
| Parameter | Type | Description |
262+
| --------- | -------- | ----------- |
263+
| name | string | Column name |
264+
265+
```typescript
192266
blueprint.year('startYear');
193267
```

src/migrations/columns/EnumColumn.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export default class EnumColumn extends Column
3939
expressions.push('NULL');
4040

4141
if (this.default !== undefined) {
42-
expressions.push('DEFAULT', this.default);
42+
expressions.push('DEFAULT', mysql.escape(this.default));
4343
}
4444

4545
return expressions.join(' ').trim();

tests/integration_tests/model/Model.test.ts

+9
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ describe('Model', () =>
5454
expect(users[1].id).toEqual(2);
5555
});
5656

57+
test('delete', async () =>
58+
{
59+
const user = await User.findById(1);
60+
await user.delete();
61+
62+
const users = await User.all();
63+
expect(users.length).toEqual(1);
64+
});
65+
5766
test('findById', async () =>
5867
{
5968
const id = 1;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import EnumColumn from 'migrations/columns/EnumColumn';
2+
import { ColumnType } from 'migrations/constants';
3+
4+
5+
describe('EnumColumn', () =>
6+
{
7+
test('values and default', () =>
8+
{
9+
const column = new EnumColumn('status', ColumnType.ENUM, {
10+
default: 'pending',
11+
values: ['pending', 'succeeded', 'failed']
12+
});
13+
14+
expect(column.toString()).toEqual("`status` ENUM ( 'pending', 'succeeded', 'failed' ) NULL DEFAULT 'pending'");
15+
});
16+
})

0 commit comments

Comments
 (0)