-
Notifications
You must be signed in to change notification settings - Fork 66
Use go-sqlcmd for script action #113
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
Changes from all commits
2fe2849
ea8533a
efb5753
aa01faf
1155db5
e2805bf
22df3df
74b5666
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,4 @@ | ||
| import * as core from "@actions/core"; | ||
| import * as tc from "@actions/tool-cache"; | ||
| import { AuthorizerFactory } from 'azure-actions-webclient/AuthorizerFactory'; | ||
|
|
||
| import run from "../src/main"; | ||
|
|
@@ -16,9 +15,10 @@ jest.mock('../src/AzureSqlResourceManager'); | |
| jest.mock('../src/Setup'); | ||
|
|
||
| describe('main.ts tests', () => { | ||
| afterEach(() => { | ||
| beforeEach(() => { | ||
| jest.restoreAllMocks(); | ||
| }) | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('gets inputs and executes build and publish action', async () => { | ||
| const resolveFilePathSpy = jest.spyOn(AzureSqlActionHelper, 'resolveFilePath').mockReturnValue('./TestProject.sqlproj'); | ||
|
|
@@ -45,7 +45,7 @@ describe('main.ts tests', () => { | |
| expect(AzureSqlAction).toHaveBeenCalled(); | ||
| expect(detectIPAddressSpy).toHaveBeenCalled(); | ||
| expect(getAuthorizerSpy).not.toHaveBeenCalled(); | ||
| expect(getInputSpy).toHaveBeenCalledTimes(10); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was wrong as the mocks weren't getting cleared correctly before. |
||
| expect(getInputSpy).toHaveBeenCalledTimes(9); | ||
| expect(resolveFilePathSpy).toHaveBeenCalled(); | ||
| expect(addFirewallRuleSpy).not.toHaveBeenCalled(); | ||
| expect(actionExecuteSpy).toHaveBeenCalled(); | ||
|
|
||
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,3 @@ | ||
| import * as fs from 'fs'; | ||
| import * as path from 'path'; | ||
| import * as core from '@actions/core'; | ||
| import * as exec from '@actions/exec'; | ||
|
|
@@ -7,7 +6,6 @@ import AzureSqlActionHelper from './AzureSqlActionHelper'; | |
| import DotnetUtils from './DotnetUtils'; | ||
| import Constants from './Constants'; | ||
| import SqlConnectionConfig from './SqlConnectionConfig'; | ||
| import SqlUtils from './SqlUtils'; | ||
|
|
||
| export enum ActionType { | ||
| DacpacAction, | ||
|
|
@@ -90,15 +88,50 @@ export default class AzureSqlAction { | |
|
|
||
| private async _executeSqlFile(inputs: ISqlActionInputs) { | ||
| core.debug('Begin executing sql script'); | ||
| let scriptContents: string; | ||
| try { | ||
| scriptContents = fs.readFileSync(inputs.sqlFile, "utf8"); | ||
|
|
||
| // sqlcmd should be added to PATH already, we just need to see if need to add ".exe" for Windows | ||
| let sqlCmdPath: string; | ||
| switch (process.platform) { | ||
zijchen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| case "win32": | ||
| sqlCmdPath = "sqlcmd.exe"; | ||
| break; | ||
| case "linux": | ||
| case "darwin": | ||
| sqlCmdPath = "sqlcmd"; | ||
| break; | ||
| default: | ||
| throw new Error(`Platform ${process.platform} is not supported.`); | ||
| } | ||
| catch (e) { | ||
| throw new Error(`Cannot read contents of file ${inputs.sqlFile} due to error '${e.message}'.`); | ||
|
|
||
| // Determine the correct sqlcmd arguments based on the auth type in connectionConfig | ||
| let sqlcmdCall = `"${sqlCmdPath}" -S ${inputs.serverName} -d ${inputs.connectionConfig.Config.database}`; | ||
zijchen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const authentication = inputs.connectionConfig.Config['authentication']; | ||
| switch (authentication?.type) { | ||
| case undefined: | ||
| // No authentication type defaults SQL login | ||
| sqlcmdCall += ` -U "${inputs.connectionConfig.Config.user}"`; | ||
| core.exportVariable(Constants.sqlcmdPasswordEnvVarName, inputs.connectionConfig.Config.password); | ||
| break; | ||
|
|
||
| case 'azure-active-directory-default': | ||
| sqlcmdCall += ` --authentication-method=ActiveDirectoryDefault`; | ||
| break; | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for AD default does the user know they need to set the various environment variables like AZURE_CLIENT_ID and AZURE_TENANT_ID?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll update the README for documentation |
||
| case 'azure-active-directory-password': | ||
| sqlcmdCall += ` --authentication-method=ActiveDirectoryPassword -U "${authentication.options.userName}"`; | ||
| core.exportVariable(Constants.sqlcmdPasswordEnvVarName, authentication.options.password); | ||
| break; | ||
|
|
||
| case 'azure-active-directory-service-principal-secret': | ||
| sqlcmdCall += ` --authentication-method=ActiveDirectoryServicePrincipal -U "${inputs.connectionConfig.Config.user}"`; | ||
| core.exportVariable(Constants.sqlcmdPasswordEnvVarName, authentication.options.clientSecret); | ||
| break; | ||
|
|
||
| default: | ||
| throw new Error(`Authentication type ${authentication.type} is not supported.`); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we could support integrated auth on Windows now, and eventually on Linux when microsoft/go-mssqldb#35 is merged to the driver. |
||
| } | ||
| await SqlUtils.executeSql(inputs.connectionConfig, scriptContents); | ||
|
|
||
| await exec.exec(`${sqlcmdCall} -i "${inputs.sqlFile}" ${inputs.additionalArguments}`); | ||
|
|
||
| console.log(`Successfully executed SQL file on target database.`); | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.