-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
/
SchemaDropCommand.ts
52 lines (48 loc) · 1.71 KB
/
SchemaDropCommand.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
import { DataSource } from "../data-source/DataSource"
import * as yargs from "yargs"
import chalk from "chalk"
import { PlatformTools } from "../platform/PlatformTools"
import path from "path"
import process from "process"
import { CommandUtils } from "./CommandUtils"
/**
* Drops all tables of the database from the given dataSource.
*/
export class SchemaDropCommand implements yargs.CommandModule {
command = "schema:drop"
describe =
"Drops all tables in the database on your default dataSource. " +
"To drop table of a concrete connection's database use -c option."
builder(args: yargs.Argv) {
return args.option("dataSource", {
alias: "d",
describe:
"Path to the file where your DataSource instance is defined.",
demandOption: true,
})
}
async handler(args: yargs.Arguments) {
let dataSource: DataSource | undefined = undefined
try {
dataSource = await CommandUtils.loadDataSource(
path.resolve(process.cwd(), args.dataSource as string),
)
dataSource.setOptions({
synchronize: false,
migrationsRun: false,
dropSchema: false,
logging: ["query", "schema"],
})
await dataSource.initialize()
await dataSource.dropDatabase()
await dataSource.destroy()
console.log(
chalk.green("Database schema has been successfully dropped."),
)
} catch (err) {
if (dataSource) await dataSource.destroy()
PlatformTools.logCmdErr("Error during schema drop:", err)
process.exit(1)
}
}
}