/
drop-tables.spec.ts
121 lines (96 loc) · 4.41 KB
/
drop-tables.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
* @adonisjs/lucid
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/// <reference path="../../adonis-typings/index.ts" />
import { test } from '@japa/runner'
import { join } from 'path'
import { ApplicationContract } from '@ioc:Adonis/Core/Application'
import { Connection } from '../../src/Connection'
import { QueryClient } from '../../src/QueryClient'
import { fs, getConfig, setup, cleanup, setupApplication } from '../../test-helpers'
let app: ApplicationContract
test.group('Query client | drop tables', (group) => {
group.setup(async () => {
app = await setupApplication()
await setup()
})
group.teardown(async () => {
await cleanup(['temp_posts', 'temp_users', 'table_that_should_not_be_dropped', 'ignore_me'])
await cleanup()
await fs.cleanup()
})
test('drop all tables', async ({ assert }) => {
await fs.fsExtra.ensureDir(join(fs.basePath, 'temp'))
const connection = new Connection('primary', getConfig(), app.logger)
connection.connect()
await connection.client!.schema.createTableIfNotExists('temp_users', (table) => {
table.increments('id')
})
await connection.client!.schema.createTableIfNotExists('temp_posts', (table) => {
table.increments('id')
table.integer('temp_users_id').unsigned().references('id').inTable('temp_users')
})
const client = new QueryClient('dual', connection, app.container.use('Adonis/Core/Event'))
await client.dialect.dropAllTables(['public'])
assert.isFalse(await connection.client!.schema.hasTable('temp_users'))
assert.isFalse(await connection.client!.schema.hasTable('temp_posts'))
assert.isFalse(await connection.client!.schema.hasTable('users'))
assert.isFalse(await connection.client!.schema.hasTable('uuid_users'))
assert.isFalse(await connection.client!.schema.hasTable('follows'))
assert.isFalse(await connection.client!.schema.hasTable('friends'))
assert.isFalse(await connection.client!.schema.hasTable('countries'))
assert.isFalse(await connection.client!.schema.hasTable('skills'))
assert.isFalse(await connection.client!.schema.hasTable('skill_user'))
assert.isFalse(await connection.client!.schema.hasTable('posts'))
assert.isFalse(await connection.client!.schema.hasTable('comments'))
assert.isFalse(await connection.client!.schema.hasTable('profiles'))
assert.isFalse(await connection.client!.schema.hasTable('identities'))
await connection.disconnect()
})
test('drop all tables should not throw when there are no tables', async ({ assert }) => {
await fs.fsExtra.ensureDir(join(fs.basePath, 'temp'))
const connection = new Connection('primary', getConfig(), app.logger)
connection.connect()
const client = new QueryClient('dual', connection, app.container.use('Adonis/Core/Event'))
try {
await client.dropAllTables()
await client.dropAllTables()
} catch (err) {
assert.fail(err)
}
await connection.disconnect()
})
test('drop all tables except those defined in ignoreTables', async ({ assert }) => {
await fs.fsExtra.ensureDir(join(fs.basePath, 'temp'))
const config = getConfig()
config.wipe = {}
config.wipe.ignoreTables = ['table_that_should_not_be_dropped', 'ignore_me']
const connection = new Connection('primary', config, app.logger)
connection.connect()
await connection.client!.schema.createTableIfNotExists('temp_users', (table) => {
table.increments('id')
})
await connection.client!.schema.createTableIfNotExists('temp_posts', (table) => {
table.increments('id')
})
await connection.client!.schema.createTableIfNotExists(
'table_that_should_not_be_dropped',
(table) => table.increments('id')
)
await connection.client!.schema.createTableIfNotExists('ignore_me', (table) =>
table.increments('id')
)
const client = new QueryClient('dual', connection, app.container.use('Adonis/Core/Event'))
await client.dialect.dropAllTables(['public'])
assert.isFalse(await connection.client!.schema.hasTable('temp_users'))
assert.isFalse(await connection.client!.schema.hasTable('temp_posts'))
assert.isTrue(await connection.client!.schema.hasTable('table_that_should_not_be_dropped'))
assert.isTrue(await connection.client!.schema.hasTable('ignore_me'))
await connection.disconnect()
})
})