Skip to content

Commit e6cd6b7

Browse files
committed
cleanup the code
1 parent e7d182f commit e6cd6b7

File tree

3 files changed

+44
-104
lines changed

3 files changed

+44
-104
lines changed

dist/index.js

Lines changed: 22 additions & 53 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main.ts

Lines changed: 21 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ async function downloadSqldef(command: string, version: string): Promise<string>
6262
const toolPath = await tc.cacheDir(extractedPath, command, version);
6363
const binaryPath = path.join(toolPath, command);
6464

65-
await fs.promises.chmod(binaryPath, 0o755);
65+
fs.chmodSync(binaryPath, 0o755);
6666

6767
return binaryPath;
6868
}
@@ -102,7 +102,7 @@ function getCommandConfig(command: string): CommandConfig {
102102
if (user) config.args.push("-u", user);
103103
// Use environment variable for password (works with empty passwords)
104104
// This avoids command line parsing issues with -p flag
105-
if (password !== undefined) {
105+
if (password != null) {
106106
config.env = { ...config.env, MYSQL_PWD: password };
107107
}
108108
if (database) config.args.push(database);
@@ -187,7 +187,6 @@ async function runSqldef(binaryPath: string, config: CommandConfig): Promise<str
187187
}
188188
Object.assign(execEnv, config.env);
189189

190-
// Log the command for debugging
191190
const sanitizedArgs = args.map((arg, index) => {
192191
// Hide password value for security
193192
if (index > 0 && args[index - 1] === "-P") {
@@ -197,30 +196,21 @@ async function runSqldef(binaryPath: string, config: CommandConfig): Promise<str
197196
});
198197
core.debug(`Running command: ${binaryPath} ${sanitizedArgs.join(" ")}`);
199198

200-
// Try to capture all output including stderr
201-
let exitCode = 0;
202-
try {
203-
exitCode = await exec.exec(binaryPath, args, {
204-
env: execEnv,
205-
silent: false, // Change to false to see output
206-
ignoreReturnCode: true,
207-
listeners: {
208-
stdout: (data: Buffer) => {
209-
output += data.toString();
210-
},
211-
stderr: (data: Buffer) => {
212-
stderr += data.toString();
213-
},
199+
const exitCode = await exec.exec(binaryPath, args, {
200+
env: execEnv,
201+
silent: false,
202+
ignoreReturnCode: true,
203+
listeners: {
204+
stdout: (data: Buffer) => {
205+
output += data.toString();
214206
},
215-
});
216-
} catch (execError) {
217-
// If exec itself fails, log the error
218-
core.error(`Exec failed: ${execError}`);
219-
throw execError;
220-
}
207+
stderr: (data: Buffer) => {
208+
stderr += data.toString();
209+
},
210+
},
211+
});
221212

222213
if (exitCode !== 0) {
223-
// Log stderr for debugging before throwing
224214
if (stderr) {
225215
core.error(`Command stderr: ${stderr}`);
226216
}
@@ -230,7 +220,6 @@ async function runSqldef(binaryPath: string, config: CommandConfig): Promise<str
230220
throw new Error(`Command failed with exit code ${exitCode}`);
231221
}
232222

233-
// Return combined output for successful runs
234223
return output + stderr;
235224
}
236225

@@ -263,7 +252,7 @@ async function createComment(body: string, command: string, versionOutput: strin
263252
// Find previous comment by searching for the HTML comment ID
264253
const previousComment = comments.find((comment) => comment.user?.type === "Bot" && comment.body?.includes(htmlCommentId));
265254

266-
const title = `SQLDef Migration Preview`;
255+
const title = "SQLDef Migration Preview";
267256

268257
const infoLine = `Migration is performed by \`${command} ${versionOutput}\` with the schema file \`${schemaFile}\``;
269258

@@ -300,7 +289,7 @@ This comment was created by [sqldef-preview-action](https://github.com/sqldef/sq
300289
async function run(): Promise<void> {
301290
try {
302291
const command = core.getInput("command", { required: true });
303-
const version = core.getInput("version") || "v3.0.0";
292+
const version = core.getInput("version");
304293
const schemaFile = core.getInput("schema-file", { required: true });
305294
const baselineSchemaFile = core.getInput("baseline-schema-file");
306295

@@ -309,18 +298,17 @@ async function run(): Promise<void> {
309298
const binaryPath = await downloadSqldef(command, version);
310299
core.info(`Downloaded ${command} to ${binaryPath}`);
311300

312-
// Verify the binary works by running --version
313301
core.info(`Verifying ${command} binary...`);
314302
let versionOutput = "";
315303
await exec.exec(binaryPath, ["--version"], {
316304
silent: false,
317305
listeners: {
318306
stdout: (data: Buffer) => {
319-
versionOutput += data.toString();
307+
versionOutput += data.toString().trim();
320308
},
321309
},
322310
});
323-
core.info(`${command} version: ${versionOutput.trim()}`);
311+
core.info(`${command} version: ${versionOutput}`);
324312

325313
const config = getCommandConfig(command);
326314

@@ -349,24 +337,7 @@ async function run(): Promise<void> {
349337
baselineConfig.args = baselineConfig.args.map((arg) => (arg === schemaFile ? actualBaselineFile : arg));
350338

351339
core.info("Applying baseline schema to database");
352-
// Debug: Log environment variables being set
353-
if (baselineConfig.env) {
354-
const sanitizedEnv = { ...baselineConfig.env };
355-
// Mask any password values for security
356-
if ("MYSQL_PWD" in sanitizedEnv) {
357-
sanitizedEnv.MYSQL_PWD = sanitizedEnv.MYSQL_PWD ? "***" : "(empty)";
358-
}
359-
if ("PGPASSWORD" in sanitizedEnv) {
360-
sanitizedEnv.PGPASSWORD = sanitizedEnv.PGPASSWORD ? "***" : "(empty)";
361-
}
362-
core.debug(`Environment variables: ${JSON.stringify(sanitizedEnv)}`);
363-
}
364-
try {
365-
await runSqldef(binaryPath, baselineConfig);
366-
} catch (error) {
367-
core.error(`Failed to apply baseline schema: ${error}`);
368-
throw error;
369-
}
340+
await runSqldef(binaryPath, baselineConfig);
370341
} else {
371342
core.info("No baseline schema found, skipping baseline application");
372343
}
@@ -379,13 +350,13 @@ async function run(): Promise<void> {
379350
core.info(output);
380351
// Create comment for PR events
381352
if (context.eventName === "pull_request") {
382-
await createComment(output, command, versionOutput.trim(), schemaFile);
353+
await createComment(output, command, versionOutput, schemaFile);
383354
}
384355
} else {
385356
core.info("No schema changes detected");
386357
// Create comment for PR events
387358
if (context.eventName === "pull_request") {
388-
await createComment("No schema changes detected.", command, versionOutput.trim(), schemaFile);
359+
await createComment("No schema changes detected.", command, versionOutput, schemaFile);
389360
}
390361
}
391362

0 commit comments

Comments
 (0)