Skip to content

Commit

Permalink
feat: implement migrator to execute schema files
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Oct 13, 2019
1 parent ba751e6 commit 1c95b65
Show file tree
Hide file tree
Showing 10 changed files with 983 additions and 25 deletions.
28 changes: 27 additions & 1 deletion adonis-typings/migrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,39 @@
declare module '@ioc:Adonis/Lucid/Migrator' {
import { SchemaConstructorContract } from '@ioc:Adonis/Lucid/Schema'

/**
* Migration node returned by the migration source
* implementation
*/
export type MigrationNode = {
absPath: string,
name: string,
source: SchemaConstructorContract,
}

/**
* Options accepted by migrator constructor
*/
export type MigratorOptions = {
direction: 'up',
connectionName?: string,
dryRun?: boolean,
} | {
direction: 'down',
batch: number,
connectionName?: string,
dryRun?: boolean,
}

/**
* Shape of the migrator
*/
export interface MigratorContract {
migrate (): Promise<void>
dryRun: boolean
direction: 'up' | 'down'
status: 'completed' | 'skipped' | 'pending'
migratedFiles: string[]
migratedQueries: { [file: string]: string[] }
run (): Promise<void>
}
}
2 changes: 1 addition & 1 deletion adonis-typings/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ declare module '@ioc:Adonis/Lucid/Schema' {
export type DeferCallback = (client: QueryClientContract) => void | Promise<void>

export interface SchemaConstructorContract {
disableTransactions: boolean
new (db: QueryClientContract, file: string, dryRun: boolean): SchemaContract
}

Expand All @@ -23,7 +24,6 @@ declare module '@ioc:Adonis/Lucid/Schema' {
db: QueryClientContract
schema: SchemaBuilder
file: string
disableTransactions: boolean

now (precision?: number): RawContract & ExcutableQueryBuilderContract<any>
defer: (cb: DeferCallback) => void
Expand Down
28 changes: 14 additions & 14 deletions src/Database/QueryBuilder/Chainable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export abstract class Chainable implements ChainableContract {
}

if (!alias) {
return alias
return columns
}

return { [alias]: this.$transformValue(columns) }
Expand Down Expand Up @@ -123,7 +123,7 @@ export abstract class Chainable implements ChainableContract {
* Add a `where` clause
*/
public where (key: any, operator?: any, value?: any): this {
if (value) {
if (value !== undefined) {
this.$knexBuilder.where(key, operator, this.$transformValue(value))
} else if (operator) {
this.$knexBuilder.where(key, this.$transformValue(operator))
Expand All @@ -142,7 +142,7 @@ export abstract class Chainable implements ChainableContract {
* Add a `or where` clause
*/
public orWhere (key: any, operator?: any, value?: any): this {
if (value) {
if (value !== undefined) {
this.$knexBuilder.orWhere(key, operator, this.$transformValue(value))
} else if (operator) {
this.$knexBuilder.orWhere(key, this.$transformValue(operator))
Expand All @@ -164,7 +164,7 @@ export abstract class Chainable implements ChainableContract {
* Adding `where not` clause
*/
public whereNot (key: any, operator?: any, value?: any): this {
if (value) {
if (value !== undefined) {
this.$knexBuilder.whereNot(key, operator, this.$transformValue(value))
} else if (operator) {
this.$knexBuilder.whereNot(key, this.$transformValue(operator))
Expand All @@ -179,7 +179,7 @@ export abstract class Chainable implements ChainableContract {
* Adding `or where not` clause
*/
public orWhereNot (key: any, operator?: any, value?: any): this {
if (value) {
if (value !== undefined) {
this.$knexBuilder.orWhereNot(key, operator, this.$transformValue(value))
} else if (operator) {
this.$knexBuilder.orWhereNot(key, this.$transformValue(operator))
Expand Down Expand Up @@ -448,7 +448,7 @@ export abstract class Chainable implements ChainableContract {
* Add an inner join clause
*/
public innerJoin (table: any, first: any, operator?: any, second?: any): this {
if (second) {
if (second !== undefined) {
this.$knexBuilder.innerJoin(table, first, operator, this.$transformRaw(second))
} else if (operator) {
this.$knexBuilder.innerJoin(table, first, this.$transformRaw(operator))
Expand All @@ -463,7 +463,7 @@ export abstract class Chainable implements ChainableContract {
* Add a left join clause
*/
public leftJoin (table: any, first: any, operator?: any, second?: any): this {
if (second) {
if (second !== undefined) {
this.$knexBuilder.leftJoin(table, first, operator, this.$transformRaw(second))
} else if (operator) {
this.$knexBuilder.leftJoin(table, first, this.$transformRaw(operator))
Expand All @@ -478,7 +478,7 @@ export abstract class Chainable implements ChainableContract {
* Add a left outer join clause
*/
public leftOuterJoin (table: any, first: any, operator?: any, second?: any): this {
if (second) {
if (second !== undefined) {
this.$knexBuilder.leftOuterJoin(table, first, operator, this.$transformRaw(second))
} else if (operator) {
this.$knexBuilder.leftOuterJoin(table, first, this.$transformRaw(operator))
Expand All @@ -493,7 +493,7 @@ export abstract class Chainable implements ChainableContract {
* Add a right join clause
*/
public rightJoin (table: any, first: any, operator?: any, second?: any): this {
if (second) {
if (second !== undefined) {
this.$knexBuilder.rightJoin(table, first, operator, this.$transformRaw(second))
} else if (operator) {
this.$knexBuilder.rightJoin(table, first, this.$transformRaw(operator))
Expand All @@ -508,7 +508,7 @@ export abstract class Chainable implements ChainableContract {
* Add a right outer join clause
*/
public rightOuterJoin (table: any, first: any, operator?: any, second?: any): this {
if (second) {
if (second !== undefined) {
this.$knexBuilder.rightOuterJoin(table, first, operator, this.$transformRaw(second))
} else if (operator) {
this.$knexBuilder.rightOuterJoin(table, first, this.$transformRaw(operator))
Expand All @@ -523,7 +523,7 @@ export abstract class Chainable implements ChainableContract {
* Add a full outer join clause
*/
public fullOuterJoin (table: any, first: any, operator?: any, second?: any): this {
if (second) {
if (second !== undefined) {
this.$knexBuilder.fullOuterJoin(table, first, operator, this.$transformRaw(second))
} else if (operator) {
this.$knexBuilder.fullOuterJoin(table, first, this.$transformRaw(operator))
Expand All @@ -538,7 +538,7 @@ export abstract class Chainable implements ChainableContract {
* Add a cross join clause
*/
public crossJoin (table: any, first: any, operator?: any, second?: any): this {
if (second) {
if (second !== undefined) {
this.$knexBuilder.crossJoin(table, first, operator, this.$transformRaw(second))
} else if (operator) {
this.$knexBuilder.crossJoin(table, first, this.$transformRaw(operator))
Expand Down Expand Up @@ -569,7 +569,7 @@ export abstract class Chainable implements ChainableContract {
* use raw queries in this case.
*/
public having (key: any, operator?: any, value?: any): this {
if (value) {
if (value !== undefined) {
this.$knexBuilder.having(key, operator, this.$transformValue(value))
} else if (operator) {
this.$knexBuilder.having(key, this.$transformValue(operator))
Expand All @@ -587,7 +587,7 @@ export abstract class Chainable implements ChainableContract {
* use raw queries in this case.
*/
public orHaving (key: any, operator?: any, value?: any): this {
if (value) {
if (value !== undefined) {
this.$knexBuilder.orHaving(key, operator, this.$transformValue(value))
} else if (operator) {
this.$knexBuilder.orHaving(key, this.$transformValue(operator))
Expand Down
2 changes: 1 addition & 1 deletion src/Migrator/MigrationSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class MigrationSource {
return resolve(files.sort().map((file) => {
return {
absPath: join(path, file),
name: file.replace(RegExp(`${extname(file)}$`), ''),
name: join(directoryPath, file.replace(RegExp(`${extname(file)}$`), '')),
source: require(join(path, file)),
}
}))
Expand Down
Loading

0 comments on commit 1c95b65

Please sign in to comment.