Skip to content

Commit 44dcfa0

Browse files
authored
Merge pull request #374 from formidablejs/feature/db-commands
Feature/db commands
2 parents ccfb35c + 1ca3039 commit 44dcfa0

8 files changed

Lines changed: 62 additions & 10 deletions

File tree

src/Foundation/Console.imba

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export default class Console
3737
'make:tag'
3838
'make:type'
3939
'make:view'
40+
'migrate'
4041
'migrate:down'
4142
'migrate:fresh'
4243
'migrate:latest'

src/Foundation/Console/Command.imba

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ export class Command < BaseCommand
3939

4040
exit!
4141

42+
return
43+
4244
confirmed
4345

4446
else true

src/Foundation/Console/Commands/DbSeedCommand.imba

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,10 @@ export class DbSeedCommand < Command
3737
this.error "Seeder file {seeder} does not exist"
3838

3939
def handle
40-
await self.shouldRun!
40+
if !self.app.console![Symbol.for('#internal')]
41+
await self.shouldRun!
4142

42-
self.usingEnv!
43+
self.usingEnv!
4344

4445
if isTS
4546
require('ts-node').register({
@@ -63,10 +64,12 @@ export class DbSeedCommand < Command
6364

6465
Output.group { newLine: false }, do
6566
results[0].forEach do(seeder)
66-
self.message 'info', "Seeder \x1b[1m[{seeder.substring(root.length)}]\x1b[0m ran successfully."
67+
self.message 'info', "Seeder \x1b[1m[{seeder.substring(root.length + 1)}]\x1b[0m ran successfully."
6768

6869
exit!
6970

71+
return
72+
7073
console.error results
7174

7275
self.exit!
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Prop } from '@formidablejs/console'
2+
import { MigrateUpCommand } from './MigrateUpCommand'
3+
4+
export class MigrateCommand < MigrateUpCommand
5+
6+
get signature
7+
'migrate {?--migration}'
Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,48 @@
1-
import { Prop } from '@formidablejs/console'
1+
import { Prop, Output } from '@formidablejs/console'
22
import { MigrationCommand } from './MigrationCommand'
3+
import { join } from 'path'
4+
import { existsSync } from 'fs-extra'
35

46
export class MigrateFreshCommand < MigrationCommand
57

8+
get isTS
9+
const appPackage = join(process.cwd!, 'package.json')
10+
11+
if !existsSync(appPackage)
12+
return false
13+
14+
const language = require(appPackage).language || 'imba'
15+
16+
language.toLowerCase! == 'typescript'
17+
618
get signature
7-
'migrate:fresh'
19+
'migrate:fresh {?--seed} {?--seeder}'
820

921
get description
1022
'Drop all tables and re-run all migrations'
1123

24+
get props
25+
{
26+
seed: Prop.boolean!.default(false).description('Indicates if db:seed should run after migrations have been ran')
27+
seeder: Prop.string!.default("DatabaseSeeder.{isTS ? 'ts' : 'js'}").description('The specific seeder to run. If not provided, all seeders will be run')
28+
}
29+
1230
def handle
13-
call 'fresh'
31+
const shouldSeed? = this.option('seed')
32+
33+
if shouldSeed?
34+
process.env.CONSOLE_FORMIDABLE_GROUP = JSON.stringify({
35+
newLine: false
36+
})
37+
38+
await call 'fresh', false
39+
40+
const seeder = this.option('seeder', 'DefaultSeeder')
41+
42+
Output.write "Running <dim>db:seed --seeder={seeder}</dim>"
43+
44+
await self.app.console!.run("db:seed --seeder={seeder}")
45+
46+
self.exit!
47+
else
48+
call 'fresh'

src/Foundation/Console/Commands/MigrationCommand.imba

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export class MigrationCommand < Command
1616

1717
language.toLowerCase! == 'typescript'
1818

19-
def call action\string
19+
def call action\string, exitOnEnd\boolean = true
2020
await shouldRun!
2121

2222
self.usingEnv!
@@ -55,10 +55,12 @@ export class MigrationCommand < Command
5555
results[1].forEach do(migration)
5656
self.message 'info', "<fg:green>{action === 'rollback' ? 'Rollback' : 'Migrate'}:</fg:green> {migration}"
5757

58-
exit!
58+
if exitOnEnd
59+
exit!
5960

6061
return
6162

6263
self.write "<fg:red>No migration to run</fg:red>"
6364

64-
exit!
65+
if exitOnEnd
66+
exit!

src/Foundation/ConsoleKernel.imba

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { MakeResolverCommand } from './Console/Commands/MakeResolverCommand'
2121
import { MakeSeederCommand } from './Console/Commands/MakeSeederCommand'
2222
import { MakeTagCommand } from './Console/Commands/MakeTagCommand'
2323
import { MakeViewCommand } from './Console/Commands/MakeViewCommand'
24+
import { MigrateCommand } from './Console/Commands/MigrateCommand'
2425
import { MigrateDownCommand } from './Console/Commands/MigrateDownCommand'
2526
import { MigrateFreshCommand } from './Console/Commands/MigrateFreshCommand'
2627
import { MigrateLatestCommand } from './Console/Commands/MigrateLatestCommand'
@@ -88,6 +89,7 @@ export default class ConsoleKernel
8889
UpCommand
8990

9091
# migration commands
92+
MigrateCommand
9193
MigrateDownCommand
9294
MigrateFreshCommand
9395
MigrateLatestCommand

types/Foundation/Console/Commands/MigrationCommand.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ export class MigrationCommand extends Command {
22
/**
33
@param {string} action
44
*/
5-
call(action: string): Promise<mixed>;
5+
call(action: string, exitOnEnd?: boolean): Promise<mixed>;
66
}
77
import { Command } from "../Command";

0 commit comments

Comments
 (0)