Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract function to generate migration #10796

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions docs/migrations.md
Expand Up @@ -134,6 +134,26 @@ export class PostRefactoringTIMESTAMP implements MigrationInterface {
}
```

### Generating migration outside of CLI

You can also generate migrations outside of the CLI by using the `MigrationGenerateCommand.genMigration` static method:
```typescript
public static async genMigration(
dataSource: DataSource,
dryrun: boolean,
check: boolean,
pretty: boolean,
exitProcess: boolean,
timestamp: number,
extension: string,
fullPath: string,
)
```

Arguments to these methods match the CLI options.

```typescript

## Running and reverting migrations

Once you have a migration to run on production, you can run them using a CLI command:
Expand Down
85 changes: 54 additions & 31 deletions src/commands/MigrationGenerateCommand.ts
Expand Up @@ -71,20 +71,42 @@ export class MigrationGenerateCommand implements yargs.CommandModule {
const fullPath = args.path.startsWith("/")
? args.path
: path.resolve(process.cwd(), args.path)
const filename = timestamp + "-" + path.basename(fullPath) + extension

let dataSource: DataSource | undefined = undefined
const dataSource = await CommandUtils.loadDataSource(
path.resolve(process.cwd(), args.dataSource as string),
)
dataSource.setOptions({
synchronize: false,
migrationsRun: false,
dropSchema: false,
logging: false,
})
await dataSource.initialize()
await MigrationGenerateCommand.genMigration(
dataSource,
args.dryrun,
args.check,
args.pretty,
args.exitProcess !== false,
timestamp,
extension,
fullPath,
)
}

public static async genMigration(
dataSource: DataSource,
dryrun: boolean,
check: boolean,
pretty: boolean,
exitProcess: boolean,
timestamp: number,
extension: string,
fullPath: string,
) {
try {
dataSource = await CommandUtils.loadDataSource(
path.resolve(process.cwd(), args.dataSource as string),
)
dataSource.setOptions({
synchronize: false,
migrationsRun: false,
dropSchema: false,
logging: false,
})
await dataSource.initialize()
const filename =
timestamp + "-" + path.basename(fullPath) + extension

const upSqls: string[] = [],
downSqls: string[] = []
Expand All @@ -94,7 +116,7 @@ export class MigrationGenerateCommand implements yargs.CommandModule {
.createSchemaBuilder()
.log()

if (args.pretty) {
if (pretty) {
sqlInMemory.upQueries.forEach((upQuery) => {
upQuery.query = MigrationGenerateCommand.prettifyQuery(
upQuery.query,
Expand Down Expand Up @@ -138,7 +160,7 @@ export class MigrationGenerateCommand implements yargs.CommandModule {
}

if (!upSqls.length) {
if (args.check) {
if (check) {
console.log(
chalk.green(`No changes in database schema were found`),
)
Expand All @@ -151,26 +173,27 @@ export class MigrationGenerateCommand implements yargs.CommandModule {
)
process.exit(1)
}
} else if (!args.path) {
} else if (!fullPath) {
console.log(chalk.yellow("Please specify a migration path"))
process.exit(1)
}

const fileContent = args.outputJs
? MigrationGenerateCommand.getJavascriptTemplate(
path.basename(fullPath),
timestamp,
upSqls,
downSqls.reverse(),
)
: MigrationGenerateCommand.getTemplate(
path.basename(fullPath),
timestamp,
upSqls,
downSqls.reverse(),
)
const fileContent =
extension === ".js"
? MigrationGenerateCommand.getJavascriptTemplate(
path.basename(fullPath),
timestamp,
upSqls,
downSqls.reverse(),
)
: MigrationGenerateCommand.getTemplate(
path.basename(fullPath),
timestamp,
upSqls,
downSqls.reverse(),
)

if (args.check) {
if (check) {
console.log(
chalk.yellow(
`Unexpected changes in database schema were found in check mode:\n\n${chalk.white(
Expand All @@ -181,7 +204,7 @@ export class MigrationGenerateCommand implements yargs.CommandModule {
process.exit(1)
}

if (args.dryrun) {
if (dryrun) {
console.log(
chalk.green(
`Migration ${chalk.blue(
Expand All @@ -201,7 +224,7 @@ export class MigrationGenerateCommand implements yargs.CommandModule {
)} has been generated successfully.`,
),
)
if (args.exitProcess !== false) {
if (exitProcess !== false) {
process.exit(0)
}
}
Expand Down