Skip to content

Commit

Permalink
chore: adjusted migration creation
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeerickson committed Apr 19, 2019
1 parent 16d2b75 commit 08c9b98
Show file tree
Hide file tree
Showing 14 changed files with 98 additions and 63 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).


## [1.1.2] - 2019-04-18

### Fixed

- Fix issue when creating migrations and tablename is not supplied
- Added migration name parsing to determine migration class name when --model or --tablename not supplied
- Added more tests to cover migration adjustments

## [1.1.0] - 2019-04-17

### Added
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ laravel-craftsman craft:all Contact \
--model App/Models/Contact \
--tablename contacts \
--rows 50 \
--fields first_name:string@30:nullable, last_name:string@50:nullable, email:string@80:nullable:unique
--fields first_name:string@30:nullable,last_name:string@50:nullable,email:string@80:nullable:unique
```

| Command | Name / Option | Description |
Expand Down Expand Up @@ -104,6 +104,8 @@ laravel-craftsman craft:all Contact \
| | --tablename, -t | Tablename used in database (will set $tablename in Model) |
| | | _If not supplied, default table will be pluralized model name_ |
| | --fields, -f | List of fields (option) _see syntax below_ |
| | | **🚨 If you have spaces separating fields, you must surround** |
| | | **fields list in quotes** |
| | --down, -d | Include down methods (skipped by default) |
| | --template, -t | Path to custom template (override config file) |
| | --overwrite, -w | Overwrite existing class |
Expand Down
57 changes: 34 additions & 23 deletions app/Commands/CraftMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use App\CraftsmanFileSystem;
use Carbon\Carbon;
use Codedungeon\PHPMessenger\Facades\Messenger;
use Illuminate\Support\Str;
use LaravelZero\Framework\Commands\Command;

Expand Down Expand Up @@ -67,33 +66,45 @@ public function handle()
{
$migrationName = $this->argument('name');
$model = $this->option('model');
$tablename = $this->option('tablename');
$fields = $this->option('fields');

if (strlen($model) === 0) {
Messenger::log("");
Messenger::error("Migrations require model name (--model)\n", "ERROR");
return;
} else {
$tablename = $this->option('tablename');
$fields = $this->option('fields');

if (strlen($tablename) === 0) {
// CreateProductUsersTable
// CreateProductsNutritionalFactsTable

if (strlen($tablename) === 0 || (is_null($tablename))) {
if (strlen($model) === 0) {
$parts = explode("_", $migrationName);
array_shift($parts);
array_pop($parts);
$tablename = implode($parts, "_");
$model = str_replace("_", "", Str::title($tablename));
} else {
$parts = explode("/", $model);
$tablename = Str::plural(array_pop($parts));
}
$data = [
"model" => $model,
"name" => $migrationName,
"tablename" => $tablename,
"fields" => $fields,
"down" => $this->option('down'),
"overwrite" => $this->option('overwrite'),
];

// timestamp to be prepended to name
$dt = Carbon::now()->format('Y_m_d_His');
$migrationFilename = $dt."_".$migrationName;

$this->fs->createFile('migration', $migrationFilename, $data);
} else {
if (strlen($model) === 0) {
$model = str_replace("_", "", Str::title($tablename));
}
}


$data = [
"model" => $model,
"name" => $migrationName,
"tablename" => $tablename,
"fields" => $fields,
"down" => $this->option('down'),
"overwrite" => $this->option('overwrite'),
];

// timestamp to be prepended to name
$dt = Carbon::now()->format('Y_m_d_His');
$migrationFilename = $dt."_".$migrationName;

$this->fs->createFile('migration', $migrationFilename, $data);

}
}
2 changes: 1 addition & 1 deletion app/CraftsmanFileSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ public function createFile($type = null, $filename = null, $data = [])
$mustache = new Mustache_Engine();

$vars["model_path"] = str_replace("/", "\\", $vars["model_path"]);

$template_data = $mustache->render($template, $vars);

try {
Expand Down
31 changes: 0 additions & 31 deletions app/Exceptions/CraftsmanFileSystemException.php

This file was deleted.

Binary file modified builds/laravel-craftsman
Binary file not shown.
2 changes: 1 addition & 1 deletion builds/templates/migration.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use {{model_path}};

class Create{{model}}sTable extends Migration
class Create{{model}}Table extends Migration
{
public function up()
{
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "codedungeon/laravel-craftsman",
"version": "1.1.1",
"version": "1.1.2",
"description": "Laravel Craftsman",
"keywords": [
"framework",
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "laravel-craftsman",
"version": "1.1.1",
"build": "129",
"version": "1.1.2",
"build": "131",
"description": "Laravel Craftsman",
"main": "index.js",
"directories": {
Expand Down
2 changes: 1 addition & 1 deletion templates/migration.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use {{model_path}};

class Create{{model}}sTable extends Migration
class Create{{model}}Table extends Migration
{
public function up()
{
Expand Down
1 change: 1 addition & 0 deletions tests/Feature/CraftAllTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public function should_execute_craft_all_command()
$this->fs->rmdir("app/Http");
$this->fs->rmdir("app/Models");
$this->fs->rmdir("app/Test");
$this->fs->rmdir("resources/views/posts");
$this->fs->rmdir("database/migrations");
$this->fs->rmdir("database/factories");
$this->fs->rmdir("database/seeds");
Expand Down
39 changes: 38 additions & 1 deletion tests/Feature/CraftMigrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function setUp(): void
/** @test */
public function should_execute_default_craft_migration_command()
{
$class = "CreateTestsTable";
$class = "CreateTestTable";
$model = "App/Models/Test";
$migrationName = "create_tests_table";

Expand Down Expand Up @@ -121,6 +121,43 @@ public function should_build_complex_field_data()
$this->fs->rmdir("database/migrations");
}

/** @test */
public function should_create_migration_without_tablename()
{
$migrationName = "create_product_contacts_table";

$this->artisan("craft:migration {$migrationName}")
->assertExitCode(0);

$this->assertMigrationFileExists($migrationName);

$migrationFilename = $this->fs->getLastFilename("database/migrations", $migrationName);
$data = file_get_contents($migrationFilename);

$this->assertStringContainsString("CreateProductContactsTable", $data);

$this->fs->rmdir("database/migrations");

}

/** @test */
public function should_create_migration_without_model()
{
$migrationName = "create_product_contacts_table";

$this->artisan("craft:migration {$migrationName} --tablename product_contacts")
->assertExitCode(0);

$this->assertMigrationFileExists($migrationName);

$migrationFilename = $this->fs->getLastFilename("database/migrations", $migrationName);
$data = file_get_contents($migrationFilename);

$this->assertStringContainsString("CreateProductContactsTable", $data);

$this->fs->rmdir("database/migrations");
}


// check to see if migration file was created. Since the filename is changed (adding timestamp)
// we can only validate the core migration ($migrationName) is actually created
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/CraftModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,6 @@ public function should_craft_model_using_custom_template()

$this->assertFileContainsString($filename, "class {$model} extends Model");

// $this->fs->rmdir("app/Models");
$this->fs->rmdir("app/Models");
}
}
7 changes: 7 additions & 0 deletions tests/Unit/CraftsmanFileSystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ public function setUp(): void
parent::setUp();
}

public function tearDown(): void
{
parent::tearDown(); // TODO: Change the autogenerated stub

$this->fs->rmdir("database/migrations");
}

public static function tearDownAfterClass(): void
{
parent::tearDownAfterClass();
Expand Down

0 comments on commit 08c9b98

Please sign in to comment.