Skip to content

Commit

Permalink
Detect table migrations on migration generation
Browse files Browse the repository at this point in the history
à la php artisan (Similar to laravel's artisan)
  • Loading branch information
SychO9 committed Jul 4, 2021
1 parent cac6e86 commit e443a32
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
18 changes: 15 additions & 3 deletions boilerplate/stubs/migration.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
<?php

<% if (typeof tableName === 'undefined' || ! tableName) { %>
use Illuminate\Database\Schema\Builder;

// HINT: you might want to use a `Flarum\Database\Migration` helper method for simplicity!
// See https://docs.flarum.org/extend/data.html#migrations to learn more about migrations.

return [
'up' => function (Builder $schema) {
// up migration
},
'down' => function (Builder $schema) {
// down migration
}
];
];<% } %><% if (typeof tableName !== 'undefined' && tableName) { %>
use Flarum\Database\Migration;
use Illuminate\Database\Schema\Blueprint;

return Migration::createTable(
'<%= tableName %>',
function (Blueprint $table) {
$table->increments('id');

// created_at & updated_at
$table->timestamps();
}
);
<% } %>
20 changes: 17 additions & 3 deletions src/steps/stubs/backend/migration.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { readdirSync } from 'fs';
import { Store } from 'mem-fs';
import { ParamProvider } from 'src/provider/param-provider';
import { PathProvider } from 'src/provider/path-provider';
import { Editor } from 'mem-fs-editor';
import { ParamProvider } from '../../../provider/param-provider';
import { PathProvider } from '../../../provider/path-provider';
import { Validator } from '../../../utils/validation';
import { getNextMigrationName } from '../../../utils/migration';
import { BasePhpStubStep } from '../php-base';

Expand All @@ -18,11 +20,23 @@ export class GenerateMigrationStub extends BasePhpStubStep {
name: 'name',
type: 'text',
message: 'Migration name/short description',
validate: (s: string) => /^[0-9a-zA-Z_ ]+$/.test(s.trim()) || 'Field is required; alphanumerical characters, underscores, and spaces only!',
validate: Validator.migrationName,
},
],
}

protected async compileParams(fsEditor: Editor, pathProvider: PathProvider, paramProvider: ParamProvider): Promise<Record<string, unknown>> {
const params = await super.compileParams(fsEditor, pathProvider, paramProvider);

const regex = new RegExp(/^create_([A-z0-9_]+)_table$/);

if (regex.test(params.name as string)) {
params.tableName = regex.exec(params.name as string)?.pop();
}

return params;
}

protected async getFileName(fs: Store, pathProvider: PathProvider, _paramProvider: ParamProvider) {
let persistedMigrations: string[];
try {
Expand Down
4 changes: 4 additions & 0 deletions src/utils/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ export class Validator {
public static routeName(s: string) {
return /^([A-z-._0-9]+)$/.test(s.trim()) || 'Invalid path name: only alphanumerical characters allowed and (._-).';
}

public static migrationName(s: string) {
return /^[0-9a-zA-Z_ ]+$/.test(s.trim()) || 'Field is required; alphanumerical characters, underscores, and spaces only!';
}
}

0 comments on commit e443a32

Please sign in to comment.