Skip to content

Commit

Permalink
fix(schema): schema.now must return knex query builder instance
Browse files Browse the repository at this point in the history
Since, the schema calls are directly handled by knex, we must
return knex.raw instance for it to handle it properly
  • Loading branch information
thetutlage committed Dec 11, 2019
1 parent 49fd215 commit 22b7fed
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 5 deletions.
1 change: 1 addition & 0 deletions adonis-typings/querybuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,7 @@ declare module '@ioc:Adonis/Lucid/DatabaseQueryBuilder' {
*/
interface RawContract {
client: QueryClientContract,
toKnex (): knex.Raw,
wrap (before: string, after: string): this
}

Expand Down
4 changes: 2 additions & 2 deletions adonis-typings/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
declare module '@ioc:Adonis/Lucid/Schema' {
import { QueryClientContract, ExcutableQueryBuilderContract } from '@ioc:Adonis/Lucid/Database'
import { RawContract } from '@ioc:Adonis/Lucid/DatabaseQueryBuilder'
import { SchemaBuilder } from 'knex'
import { SchemaBuilder, Raw } from 'knex'

export type DeferCallback = (client: QueryClientContract) => void | Promise<void>

Expand All @@ -25,7 +25,7 @@ declare module '@ioc:Adonis/Lucid/Schema' {
schema: SchemaBuilder
file: string

now (precision?: number): RawContract & ExcutableQueryBuilderContract<any>
now (precision?: number): Raw
defer: (cb: DeferCallback) => void
up (): Promise<void> | void
down (): Promise<void> | void
Expand Down
7 changes: 7 additions & 0 deletions src/Database/QueryBuilder/Raw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,11 @@ export class RawQueryBuilder implements RawContract {
this.$knexBuilder.wrap(before, after)
return this
}

/**
* Returns reference to knex raw query
*/
public toKnex () {
return this.$knexBuilder
}
}
4 changes: 2 additions & 2 deletions src/Schema/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ export class Schema implements SchemaContract {
*/
public now (precision?: number) {
return precision
? this.db.raw(`CURRENT_TIMESTAMP(${precision})`)
: this.db.raw('CURRENT_TIMESTAMP')
? this.db.raw(`CURRENT_TIMESTAMP(${precision})`).toKnex()
: this.db.raw('CURRENT_TIMESTAMP').toKnex()
}

/**
Expand Down
23 changes: 22 additions & 1 deletion test/migrations/schema.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ test.group('Schema', (group) => {
assert.deepEqual(queries, [knexSchema])
})

test('get raw query builder using now method', async (assert) => {
test('get knex raw query builder using now method', async (assert) => {
class UsersSchema extends getBaseSchema() {
public up () {
this.schema.createTable('users', (table) => {
Expand Down Expand Up @@ -208,4 +208,25 @@ test.group('Schema', (group) => {
assert.isFalse(hasUsers)
assert.isFalse(hasAccounts)
})

test('use now helper to define default timestamp', async (assert) => {
class UsersSchema extends getBaseSchema() {
public up () {
this.schema.createTable('users', (table) => {
table.increments('id')
table.timestamp('created_at').defaultTo(this.now())
})
}
}

const schema = new UsersSchema(db.connection(), 'users.ts', true)
const queries = await schema.execUp()

const knexSchema = db.connection().schema.createTable('users', (table) => {
table.increments('id')
table.timestamp('created_at').defaultTo(db.connection().getWriteClient().fn.now())
}).toQuery()

assert.deepEqual(queries, [knexSchema])
})
})

0 comments on commit 22b7fed

Please sign in to comment.