Skip to content

Commit

Permalink
improvement: improving seeds and migrations flow
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Aug 14, 2020
1 parent d818980 commit 4a6acad
Show file tree
Hide file tree
Showing 14 changed files with 295 additions and 228 deletions.
2 changes: 1 addition & 1 deletion adonis-typings/migrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ declare module '@ioc:Adonis/Lucid/Migrator' {
export type MigratedFileNode = {
status: 'completed' | 'error' | 'pending'
queries: string[]
migration: FileNode<unknown>
file: FileNode<unknown>
batch: number
}

Expand Down
6 changes: 2 additions & 4 deletions adonis-typings/seeds.ts → adonis-typings/seeder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/

declare module '@ioc:Adonis/Lucid/Seeder' {
import { QueryClientContract } from '@ioc:Adonis/Lucid/Database'
import { QueryClientContract, FileNode } from '@ioc:Adonis/Lucid/Database'

/**
* Shape of seeder class
Expand All @@ -25,11 +25,9 @@ declare module '@ioc:Adonis/Lucid/Seeder' {
* Shape of file node returned by the run method
*/
export type SeederFileNode = {
absPath: string
name: string
source: SeederConstructorContract
status: 'pending' | 'completed' | 'failed' | 'ignored'
error?: any
file: FileNode<unknown>
}

const BaseSeeder: SeederConstructorContract
Expand Down
35 changes: 15 additions & 20 deletions commands/DbSeed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
* file that was distributed with this source code.
*/

// import { join } from 'path'
import { inject } from '@adonisjs/fold'
import { SeederFileNode } from '@ioc:Adonis/Lucid/Seeder'
import { BaseCommand, Kernel, flags } from '@adonisjs/ace'
Expand Down Expand Up @@ -92,27 +91,27 @@ export default class DbSeed extends BaseCommand {
* Execute command
*/
public async handle(): Promise<void> {
const client = this.db.connection(this.connection || this.db.primaryConnectionName)
this.connection = this.connection || this.db.primaryConnectionName
const connection = this.db.getRawConnection(this.connection)

/**
* Ensure the define connection name does exists in the
* config file
*/
if (!client) {
if (!connection) {
this.logger.error(
`${this.connection} is not a valid connection name. Double check config/database file`
`"${connection}" is not a valid connection name. Double check config/database file`
)
return
}

const { SeedsRunner } = await import('../src/SeedsRunner')
const seedsPath = this.application.seedsPath()
const runner = new SeedsRunner(seedsPath, process.env.NODE_ENV === 'development')
const runner = new SeedsRunner(this.db, this.application, this.connection)

/**
* List of available files
*/
const files = await runner.listSeeders()
const files = await runner.getList()

/**
* List of selected files. Initially, all files are selected and one can
Expand All @@ -131,11 +130,7 @@ export default class DbSeed extends BaseCommand {
selectedFileNames = await this.prompt.multiple(
'Select files to run',
files.map((file) => {
return {
disabled: file.status === 'ignored',
name: file.name,
hint: file.status === 'ignored' ? '(Enabled only in development environment)' : '',
}
return { name: file.name }
})
)
}
Expand All @@ -147,17 +142,17 @@ export default class DbSeed extends BaseCommand {
const sourceFile = files.find(({ name }) => fileName === name)
if (!sourceFile) {
this.printLogMessage({
name: fileName,
file: {
name: fileName,
absPath: fileName,
getSource: () => {},
},
status: 'failed',
error: new Error(
'Invalid file path. Pass relative path from the "database/seeds" directory'
),
source: {} as any,
absPath: fileName,
error: new Error('Invalid file path. Pass relative path from the application root'),
})
} else {
await runner.run(sourceFile, client)
this.printLogMessage(sourceFile)
const response = await runner.run(sourceFile)
this.printLogMessage(response)
}
}

Expand Down
6 changes: 3 additions & 3 deletions commands/Migration/Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export default abstract class MigrationsBase extends BaseCommand {
: 'reverted'
: 'error'

console.log(`${arrow} ${this.colors[color](message)} ${file.migration.name}`)
console.log(`${arrow} ${this.colors[color](message)} ${file.file.name}`)
}

/**
Expand Down Expand Up @@ -97,7 +97,7 @@ export default abstract class MigrationsBase extends BaseCommand {
* Pretty print sql queries of a file
*/
private prettyPrintSql(file: MigratedFileNode, connectionName: string) {
console.log(file.migration.name)
console.log(file.file.name)
file.queries.map((sql) => {
prettyPrint({
connection: connectionName,
Expand Down Expand Up @@ -140,7 +140,7 @@ export default abstract class MigrationsBase extends BaseCommand {
* Starting to process a new migration file
*/
migrator.on('migration:start', (file) => {
processedFiles.add(file.migration.name)
processedFiles.add(file.file.name)
this.printLogMessage(file, migrator.direction)
})

Expand Down
5 changes: 3 additions & 2 deletions commands/Migration/Rollback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default class Migrate extends MigrationsBase {
/**
* Custom connection for running migrations.
*/
@flags.string({ description: 'Define a custom database connection' })
@flags.string({ description: 'Define a custom database connection', alias: 'c' })
public connection: string

/**
Expand Down Expand Up @@ -66,7 +66,6 @@ export default class Migrate extends MigrationsBase {
*/
public async handle(): Promise<void> {
this.connection = this.connection || this.db.primaryConnectionName
const connection = this.db.getRawConnection(this.connection)

const continueMigrations =
!this.application.inProduction || this.force || (await this.takeProductionConstent())
Expand All @@ -78,6 +77,8 @@ export default class Migrate extends MigrationsBase {
return
}

const connection = this.db.getRawConnection(this.connection)

/**
* Ensure the define connection name does exists in the
* config file
Expand Down
6 changes: 3 additions & 3 deletions commands/Migration/Run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default class Migrate extends MigrationsBase {
/**
* Custom connection for running migrations.
*/
@flags.string({ description: 'Define a custom database connection' })
@flags.string({ description: 'Define a custom database connection', alias: 'c' })
public connection: string

/**
Expand Down Expand Up @@ -58,8 +58,6 @@ export default class Migrate extends MigrationsBase {
*/
public async handle(): Promise<void> {
this.connection = this.connection || this.db.primaryConnectionName
const connection = this.db.getRawConnection(this.connection)

const continueMigrations =
!this.application.inProduction || this.force || (await this.takeProductionConstent())

Expand All @@ -70,6 +68,8 @@ export default class Migrate extends MigrationsBase {
return
}

const connection = this.db.getRawConnection(this.connection)

/**
* Ensure the define connection name does exists in the
* config file
Expand Down
2 changes: 1 addition & 1 deletion commands/Migration/Status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default class Status extends MigrationsBase {
/**
* Define custom connection
*/
@flags.string({ description: 'Define a custom database connection' })
@flags.string({ description: 'Define a custom database connection', alias: 'c' })
public connection: string

/**
Expand Down
2 changes: 1 addition & 1 deletion npm-audit.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ <h5 class="card-title">
<div class="card">
<div class="card-body">
<h5 class="card-title">
August 14th 2020, 7:11:45 am
August 14th 2020, 3:24:21 pm
</h5>
<p class="card-text">Last updated</p>
</div>
Expand Down
9 changes: 4 additions & 5 deletions src/Migrator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import {
} from '@ioc:Adonis/Lucid/Database'

import { SchemaConstructorContract } from '@ioc:Adonis/Lucid/Schema'

import { MigrationSource } from './MigrationSource'

/**
Expand Down Expand Up @@ -355,15 +354,15 @@ export class Migrator extends EventEmitter implements MigratorContract {
this.migratedFiles[migration.name] = {
status: 'pending',
queries: [],
migration: migration,
file: migration,
batch: batch + 1,
}
}
})

const filesToMigrate = Object.keys(this.migratedFiles)
for (let name of filesToMigrate) {
await this.executeMigration(this.migratedFiles[name].migration)
await this.executeMigration(this.migratedFiles[name].file)
}
}

Expand Down Expand Up @@ -401,14 +400,14 @@ export class Migrator extends EventEmitter implements MigratorContract {
this.migratedFiles[migration.name] = {
status: 'pending',
queries: [],
migration: migration,
file: migration,
batch: file.batch,
}
})

const filesToMigrate = Object.keys(this.migratedFiles)
for (let name of filesToMigrate) {
await this.executeMigration(this.migratedFiles[name].migration)
await this.executeMigration(this.migratedFiles[name].file)
}
}

Expand Down
56 changes: 56 additions & 0 deletions src/SeedsRunner/SeedersSource.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* @adonisjs/lucid
*
* (c) Harminder Virk <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

/// <reference path="../../adonis-typings/index.ts" />

import { ApplicationContract } from '@ioc:Adonis/Core/Application'
import { ConnectionConfig, FileNode } from '@ioc:Adonis/Lucid/Database'
import { sourceFiles } from '../utils'

/**
* Seeders source exposes the API to read the seeders from disk for a given connection.
*/
export class SeedersSource {
constructor(private config: ConnectionConfig, private app: ApplicationContract) {}

/**
* Returns an array of files inside a given directory. Relative
* paths are resolved from the project root
*/
private async getDirectoryFiles(directoryPath: string): Promise<FileNode<unknown>[]> {
const { files } = await sourceFiles(this.app.appRoot, directoryPath)
return files
}

/**
* Returns an array of seeders paths for a given connection. If paths
* are not defined, then `database/seeders` fallback is used
*/
private getSeedersPaths(): string[] {
const directories = (this.config.seeders || {}).paths
return directories && directories.length ? directories : ['./database/seeders']
}

/**
* Returns an array of files for the defined seed directories
*/
public async getSeeders() {
const seedersPaths = this.getSeedersPaths()
const directories = await Promise.all(
seedersPaths.map((directoryPath) => {
return this.getDirectoryFiles(directoryPath)
})
)

return directories.reduce((result, directory) => {
result = result.concat(directory)
return result
}, [])
}
}
Loading

0 comments on commit 4a6acad

Please sign in to comment.