-
Notifications
You must be signed in to change notification settings - Fork 15
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
Introduce new command to run manual scripts #233
Draft
samirsilwal
wants to merge
5
commits into
master
Choose a base branch
from
feat/manual-scripts
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7c07c92
Introduce new commnand to run manual scripts
samirsilwal 5033816
Add and update test for run script
samirsilwal e1a4750
Remove use of any
samirsilwal 9893665
Add validation for --file flag to script
samirsilwal 6f80db4
Add test for validator function
samirsilwal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import { Command, flags } from '@oclif/command'; | ||
import { bold, red, magenta, cyan } from 'chalk'; | ||
|
||
import { runScriptAPI } from '../api'; | ||
import { dbLogger } from '../util/logger'; | ||
import { loadConfig, resolveConnections } from '..'; | ||
import { validateScriptFileName } from '../util/fs'; | ||
import { printLine, printError, printInfo } from '../util/io'; | ||
import OperationResult from '../domain/operation/OperationResult'; | ||
|
||
class RunScript extends Command { | ||
static description = 'Run the provided manual scripts.'; | ||
|
||
static flags = { | ||
'dry-run': flags.boolean({ description: 'Dry run script.', default: false }), | ||
only: flags.string({ | ||
helpValue: 'CONNECTION_ID(s)', | ||
description: 'Filter provided connection(s). Comma separated ids eg: id1,id2' | ||
}), | ||
file: flags.string({ | ||
required: true, | ||
helpValue: 'Script Name', | ||
parse: validateScriptFileName, | ||
description: 'Name of the manual SQL/JS/TS script' | ||
}), | ||
'connection-resolver': flags.string({ | ||
helpValue: 'PATH', | ||
description: 'Path to the connection resolver.' | ||
}), | ||
config: flags.string({ | ||
char: 'c', | ||
description: 'Custom configuration file.' | ||
}) | ||
}; | ||
|
||
/** | ||
* Started event handler. | ||
*/ | ||
onStarted = async (result: OperationResult) => { | ||
await printLine(bold(` ▸ ${result.connectionId}`)); | ||
|
||
await printInfo(' [✓] Manual script run - started'); | ||
}; | ||
|
||
/** | ||
* Success handler. | ||
*/ | ||
onSuccess = async (result: OperationResult) => { | ||
const log = dbLogger(result.connectionId); | ||
const [num, list] = result.data; | ||
const alreadyUpToDate = num && list.length === 0; | ||
|
||
log('Up to date: ', alreadyUpToDate); | ||
|
||
await printInfo(` [✓] Manual script run - completed (${result.timeElapsed}s)`); | ||
|
||
if (alreadyUpToDate) { | ||
await printInfo(' Already up to date.\n'); | ||
|
||
return; | ||
} | ||
|
||
// Completed migrations. | ||
for (const item of list) { | ||
await printLine(cyan(` - ${item}`)); | ||
} | ||
|
||
await printInfo(` Ran ${list.length} scripts.\n`); | ||
}; | ||
|
||
/** | ||
* Failure handler. | ||
*/ | ||
onFailed = async (result: OperationResult) => { | ||
await printLine(bold(red(` [✓] Manual script run - Failed\n`))); | ||
}; | ||
|
||
/** | ||
* CLI command execution handler. | ||
* | ||
* @returns {Promise<void>} | ||
*/ | ||
async run(): Promise<void> { | ||
const { flags: parsedFlags } = this.parse(RunScript); | ||
const isDryRun = parsedFlags['dry-run']; | ||
const config = await loadConfig(parsedFlags.config); | ||
|
||
const connections = await resolveConnections(config, parsedFlags['connection-resolver']); | ||
|
||
if (isDryRun) await printLine(magenta('\n• DRY RUN STARTED\n')); | ||
|
||
const results = await runScriptAPI(config, connections, { | ||
...parsedFlags, | ||
onStarted: this.onStarted, | ||
onSuccess: this.onSuccess, | ||
onFailed: this.onFailed | ||
}); | ||
|
||
const failedCount = results.filter(({ success }) => !success).length; | ||
|
||
if (failedCount === 0) { | ||
if (isDryRun) await printLine(magenta('• DRY RUN ENDED\n')); | ||
|
||
return process.exit(0); | ||
} | ||
|
||
printError(`Error: Script failed for ${failedCount} connection(s).`); | ||
|
||
if (isDryRun) await printLine(magenta('\n• DRY RUN ENDED\n')); | ||
|
||
process.exit(-1); | ||
} | ||
} | ||
|
||
export default RunScript; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Knex } from 'knex'; | ||
|
||
import { RunScriptParams } from './RunScriptParams'; | ||
import OperationContext from './operation/OperationContext'; | ||
|
||
export interface RunScriptContext extends OperationContext { | ||
params: RunScriptParams; | ||
migrateFunc: ( | ||
trx: Knex.Transaction, | ||
files: string[], | ||
connectionId: string, | ||
runSQLScripts: (trx: Knex.Transaction, filteredScripts: string[]) => Promise<void> | ||
) => Promise<(number | string[])[]>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import OperationParams from './operation/OperationParams'; | ||
|
||
export interface RunScriptParams extends OperationParams { | ||
file?: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's not name things as manual here. Just getScriptPath should be good.