Autoanswer:
I made a test and observed that if you just run php artisan module:migrate YourModuleName
it just migrates all the new tables, without touching anything else.
Cool!
Autoanswer:
I made a test and observed that if you just run php artisan module:migrate YourModuleName
it just migrates all the new tables, without touching anything else.
Cool!
I have an app that is currently deployed and a module needs to have a new entity. Is there any way to run only the entity specific migration, without running the module migration as a whole?
OK, got it, thanks to @nWidart .
The test complained about tables that haven't been created. So, the function resetDatabase()
was doing nothing.
In order to put the function resetDatabase()
to work, you have to tell the module's service provider where to load the migrations from.
Thus, this gets solved by adding:
$this->loadMigrationsFrom(__DIR__.'/../Database/Migrations');
into your module service provider's boot()
method.
I would edit some typos on the post, but Akismet is flagging it as spam... wtf...?
So the thing is I can't figure out how what I'm missing when I try to create tests for a new custom module I've created and called News. This module has been created with the Asgard's builtin module scaffolder.
My experience with unit testing is quite limited, I've been able to do some basic tests, but perhaps I feel a bit overwhelmed when the application grows. I have time for the project I'm working on, and I'd really like to run unit testing in order to improve stability as well as the rest of benefits that has this kind of development.
Said that, let's explain what's going on:
All suite of tests passes, except when it's the turn of my News
module, where I get this error:
Illuminate\Database\QueryException: SQLSTATE[HY000]: General error: 1 no such table: news__news_translations (SQL: select * from "news__news_translations" where "news__news_translations"."news_id" is null and "news__news_translations"."news_id" is not null)
I've used the builtin BasePageTest
from the Page
module as a base to do mine (I'd use the Blog
module tests, but seems they're slighty different of the rest of the tests on v2) , so the code of my NewsBaseTest
is as following:
<?php
namespace Modules\News\Tests;
use Illuminate\Contracts\Console\Kernel;
use Illuminate\Database\Eloquent\Model;
use Illuminate\View\ViewServiceProvider;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
use Mcamara\LaravelLocalization\LaravelLocalizationServiceProvider;
use Modules\Core\Providers\CoreServiceProvider;
use Modules\News\Providers\NewsServiceProvider;
use Modules\News\Repositories\NewsRepository;
use Nwidart\Modules\LaravelModulesServiceProvider;
use Orchestra\Testbench\TestCase;
abstract class BaseNewsTest extends TestCase
{
protected $news;
public function setUp()
{
parent::setUp();
$this->resetDatabase();
$this->news = app(NewsRepository::class);
}
protected function getPackageProviders($app)
{
return [
LaravelModulesServiceProvider::class,
CoreServiceProvider::class,
ViewServiceProvider::class,
NewsServiceProvider::class,
LaravelLocalizationServiceProvider::class,
];
}
protected function getPackageAliases($app)
{
return [
'Eloquent' => Model::class,
'LaravelLocalization' => LaravelLocalization::class,
];
}
protected function getEnvironmentSetUp($app)
{
$app['path.base'] = __DIR__ . '/..';
$app['config']->set('database.default', 'sqlite');
$app['config']->set('database.connections.sqlite', array(
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
));
$app['config']->set('translatable.locales', ['en', 'fr']);
}
private function resetDatabase()
{
// Relative to the testbench app folder: vendors/orchestra/testbench/src/fixture
$migrationsPath = 'Database/Migrations';
$artisan = $this->app->make(Kernel::class);
// Makes sure the migrations table is created
$artisan->call('migrate', [
'--database' => 'sqlite',
]);
// We empty all tables
$artisan->call('migrate:reset', [
'--database' => 'sqlite',
]);
// Migrate
$artisan->call('migrate', [
'--database' => 'sqlite',
]);
$artisan->call('migrate', [
'--database' => 'sqlite',
'--path' => 'Modules/News/Database/Migrations',
]);
}
}
I'm sure I'm missing something, as to migrate all the tables in my module (both news__news
and news__news_translations
), but I assume (perhaps wrongly) that this piece of code at the end:
$artisan->call('migrate', [
'--database' => 'sqlite',
'--path' => 'Modules/News/Database/Migrations',
]);
Is this piece of code above doing the thing I'm expecting?
What I'm missing?
Sorry to resurrect this topic, but it worked perfectly for my super custom treatment of uploads in special modules, which even supports different media upload for each locale/language
@nWidart
Fantastic, many thanks, I'll try the first option
Hi, just a quick question:
I'd like to make some minor adjustments to the User
module (giving always at least the permission to view the dashboard, even if the administrator forgets to give the rights to do so).
Is there any way to extend/override the default behavior of a module? It'd be a bit overkill to duplicate the module and make my own CustomUsers just for one line of code I have to change...
Same happens with Media
module. I have minor changes to distinguish between audio & video files, but I lose all my changes at each composer-update
.
I need to have the whole module in .gitignore
in order to 'rescue' my modifications, as a workaround.
So, where'd be the best place to apply this changes without breaking the flow / modularity ?
Haha, yes! We both answered at same time...
I investigated a bit and found the solution by myself... (well, actually I found this):
https://github.com/dimsav/laravel-translatable/issues/239
I also thought the updating was doing out-of-the box.
By the way... I know it's time consuming, but would be possible to make a screencast like the one published (the testimonials one), but with translations? All details like this one, can bring confusion to newcomers...
Or make a tutorial? Well, perhaps I'd be able to do by myself, but I may be wrong on some stuff as I don't know a deep knowledge of the CMS innerings...
AutoSolved:
This is not an AsgardCMS issue / question, but from how the relations are made on the Entities.
So, for others looking for this, the TestimonialTranslation
entity should look like this:
<?php namespace Modules\Testimonials\Entities;
use Illuminate\Database\Eloquent\Model;
class TestimonialTranslation extends Model
{
public $timestamps = false;
protected $fillable = [
'testimonial_id',
'title',
'content'
];
protected $table = 'testimonials__testimonial_translations';
protected $touches = ['news']; // THIS enables 'touching' (updates timestamps) on the 'parent' model.
// And we make the relation with the model we want to touch
public function testimonial()
{
return $this->belongsTo(Testimonial::class);
}
}
And that's it!