Skip to content

Commit

Permalink
fix: pretty sql queries with propert formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Apr 28, 2020
1 parent d0c5c80 commit 77f6043
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 26 deletions.
14 changes: 9 additions & 5 deletions commands/Migration/Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,16 @@ export default abstract class MigrationsBase extends BaseCommand {
*/
private prettyPrintSql (file: MigratedFileNode, connectionName: string) {
console.log(file.migration.name)
prettyPrint({
connection: connectionName,
queries: file.queries.map((sql) => {
return { sql, bindings: [] }
}),
file.queries.map((sql) => {
prettyPrint({
connection: connectionName,
sql: sql,
ddl: true,
bindings: [],
})
console.log()
})
console.log()
}

/**
Expand Down
22 changes: 21 additions & 1 deletion src/Helpers/prettyPrint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,25 @@
import kleur from 'kleur'
import { inspect } from 'util'

/**
* Returns method for the DDL query
*/
function getDDLMethod (sql: string) {
if (sql.startsWith('create')) {
return 'create'
}

if (sql.startsWith('alter')) {
return 'alter'
}

if (sql.startsWith('drop')) {
return 'drop'
}

return 'unknown'
}

/**
* Colorizes the sql query based upon the method
*/
Expand Down Expand Up @@ -67,7 +86,8 @@ export function prettyPrint (queryLog: any) {
/**
* Colorize query and bindings
*/
output += colorizeQuery(color, queryLog.method, queryLog.sql)
const method = queryLog.method || queryLog.ddl ? getDDLMethod(queryLog.sql) : queryLog.method
output += colorizeQuery(color, method, queryLog.sql)
output += color.gray(` ${inspect(queryLog.bindings)}`)

/**
Expand Down
20 changes: 0 additions & 20 deletions src/Schema/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,25 +66,6 @@ export class Schema implements SchemaContract {
.map((schema) => (schema as SchemaBuilder).toQuery())
}

/**
* Returns the DDL method for the SQL query
*/
private getDDLMethod (sql: string) {
if (sql.startsWith('create')) {
return 'create'
}

if (sql.startsWith('alter')) {
return 'alter'
}

if (sql.startsWith('drop')) {
return 'drop'
}

return 'unknown'
}

/**
* Returns reporter instance
*/
Expand All @@ -99,7 +80,6 @@ export class Schema implements SchemaContract {
return {
connection: this.db.connectionName,
inTransaction: this.db.isTransaction,
method: this.getDDLMethod(sql.sql),
ddl: true,
...sql,
}
Expand Down

0 comments on commit 77f6043

Please sign in to comment.