Why is laravel migration failing silently? - php

I am converting a 12 year old frameworkless php app into a Laravel app. The old app had two separate user tables which I have merged. Merging them requires massaging the data. I created a migration to massage the data in one of my tables.
My up() function looks like this:
public function up()
{
$users = User::all();
foreach($users as $user) {
if ($user->staff_id = '0') {
$user->role = '4';
} elseif ($user->role != '1') {
$user->role = '3';
}
$user->save();
}
}
I had run a similar function in a migration moments previously which ran fine. However this one produced the following output:
myusername at local in ~/Sites/tgdp/trunk
> mamp-php artisan migrate
myusername at local in ~/Sites/tgdp/trunk
>
And when I looked in my migrations table and at the User table, it was obvious the migration had not been run.
So, to recap. No error thrown. No "Nothing to Migrate." No success response. No effect on the database. * Edit: No errors listed in the logs.
Any idea why this might have happened?

So, it turns out that the problem was that looping through and saving all of those users was very memory intensive. The solution was to give php limitless access to memory. Like so:
php -d memory_limit=-1 artisan migrate
Once I did that the code ran fine.

Related

Laravel unit test not saving to database

I am trying to test some things in laravel but I cant even get past the most basic part.
I want to get the results from the database. Do some things. Add them back to another table and then asset that they exist in the new table and are correctly altered.
However, it is not storing to my database. This is what I have so far
public function getUser($id)
{
return DB::raw("SELECT * FROM users WHERE id = $id");
}
public function testGetUsers()
{
$users = [97];
foreach($users as $key => $value) {
$response = $this->getUser($key);
DB::raw("INSERT INTO user_tested (user_id, response) VALUES ($key, $response)");
}
}
When I run the artisan command in the terminal I only get the warning:
get users → This test did not perform any assertions /tests/Unit/testUsers.php:17
Which I expect because there were no assertions. However the code should still have run, no? Nothing is saved in the database.
also, if someone can tell me how to log out to the terminal whilst doing this that would be really helpful in debugging.

laravel scout: How to update index in controller

Here is my question. I want to update the scout index saved in storage in my controller. Any ideas how to do it?
I am using tntsearch package. I know I can do artisan command in command prompt with $ php artisan scout:import App\\Models\\Paper
But I'm working on a website that everyone can submit their journals in it and I need a powerful search engine on my website. So in this situation, I need to update the index every time a journal submitted. So that everyone can be able to search the journals.
I manage to do a part of this task by making a provider TNTSearchScoutServiceProvider.
here is TNTSearchScoutServiceProvider:
class TNTSearchScoutServiceProvider extends \TeamTNT\Scout\TNTSearchScoutServiceProvider
{
public function boot()
{
$this->app[EngineManager::class]->extend('tntsearch', function ($app) {
$tnt = new TNTSearch();
$driver = config('database.default');
$config = config('scout.tntsearch') + config("database.connections.{$driver}");
$tnt->loadConfig($config);
$tnt->setDatabaseHandle(app('db')->connection()->getPdo());
$this->setFuzziness($tnt);
$this->setAsYouType($tnt);
return new TNTSearchEngine($tnt);
});
// To allow us run commands if we're not running in the console
$this->commands([
ImportCommand::class,
]);
}
}
After adding this provider to config/app.php. In the controller I am using the provider like this:
Artisan::call('tntsearch:import', ['model' => 'App\Models\Paper']);
But this throwes this error:
unlink(C:\wamp64\www\mywbsite\storage/papers.index): Resource temporarily unavailable
Here is what I accomplish so far:
although it throws the error,but I can only get the last updated row in search results and the oldest rows doesn't show up in the search results.
So what are your suggestions? Is it a better way to do this? Or I should check out the site every day and run the artisan commands so that the table can be indexed?
I finally managed to solve this problem:
to update the index in storage you just make a new obj from TNTindexer class; First, you create that index and after that, you select the columns you want to update with query() method. then run() the indexer.Before that make sure to load the configuration. here is the method that I write in the controller:
protected function add_to_search(){
$indexer = new TNTIndexer;
$driver = config('database.default');
$config = config('scout.tntsearch') + config("database.connections.{$driver}");
$indexer->loadConfig($config);
$indexer->createIndex('paper.index');
$indexer->query('SELECT id,title,description,abstract,keywords FROM papers;');
$indexer->run();
}
this way the index always updated through a controller.

Use Laravel's migration to manage db schema from a single config file

I want to use Laravel's migration to manage my database. But I need to have a single configuration file where I store the schema. Something like this :
{
user: {
"id":"increments",
"name":"string",
"timestamps":"timestamps()"
}
}
And when I change this file again to the text below
{
user: {
"id":"increments",
"name":"string",
"password":"string",
"timestamps":"timestamps()"
}
}
I want to be able to run a command and have the database be changed without losing any data or creating an additional config file.
Can I achieve this using laravel migrations or if you know of any other solution that can help me and I would be able to use that on laravel without losing any of laravel's database management tools, please comment.
Thank you.
php artisan make:migration add_password_to_user ( are you sure the table is called user and not users? )
In your new migration
public function up()
{
Schema::table('user', function($table) {
$table->string('password');
});
}
Don't forget the rollback
public function down()
{
Schema::table('user', function($table) {
$table->dropColumn('password');
});
}
The table should be users if you have the model User .. otherwise make sure to have protected $table = 'user'; on the model.
https://laravel.com/docs/5.3/migrations
The --table and --create options may also be used to indicate the name of the table and whether the migration will be creating a new table. These options simply pre-fill the generated migration stub file with the specified table:
php artisan make:migration create_users_table --create=users
php artisan make:migration add_votes_to_users_table --table=users

How to perform a delete operation on a Model?

Coming from a Ruby on Rails experience where you load up the rails console to delete a user or all users. I am new to Laravel 5 and I am looking for something similar to delete a user already in the sqlite3 database.
I see where people are talking about User::find(1)->delete(); to delete a user but where to you put that and run in? Is there a console to perform a delete task in? I would like to know how to delete a user without dropping the table. I do not want to soft delete.
You can put this code for example in controller.
You can use
$user = User::find($id);
$user->delete();
if you don't use SoftDeletingTrait trait or
$user = User::find($id);
$user->forceDelete();
if you do, and you want to really remove user from database, not just hide it from results.
More you can read at Laravel page
in Laravel 5 you can use the destroy method.
$user->destroy($id);
and, sure, you have a command line to do so.
$ php artisan tinker
and you can run for example
>> $var = new App\User;
>> $user= $user->find($id);
>> $user->destroy();
Several ways to do this.
If your controller defines the user as an argument:
public function destroy(User $user)
{
return $user->delete();
}
You can also delete any user by $id:
User::destroy ($id);
Assuming you're wrapping these routes with some security.
Edit: Corrected spelling
You can use bellow example to delete data with multiple
parameters......
>
> tableName::where('field_1','=',$para1)
> ->where('field_2,'=',$para2)
> ->delete();
This still works with laravel 7, i use tinker command line and the delete() method:
php artisan tinker
Now i can run commands directly:
> App\User::find($id)->delete();

Laravel migration transaction

When developing i'm having so many issues with migrations in laravel.
I create a migration. When i finish creating it, there's a small error by the middle of the migration (say, a foreign key constraint) that makes "php artisan migrate" fail. He tells me where the error is, indeed, but then migrate gets to an unconsistent state, where all the modifications to the database made before the error are made, and not the next ones.
This makes that when I fix the error and re-run migrate, the first statement fails, as the column/table is already created/modified. Then the only solution I know is to go to my database and "rollback" everything by hand, which is way longer to do.
migrate:rollback tries to rollback the previous migrations, as the current was not applied succesfully.
I also tried to wrap all my code into a DB::transaction(), but it still doesn't work.
Is there any solution for this? Or i just have to keep rolling things back by hand?
edit, adding an example (not writing Schema builder code, just some kind of pseudo-code):
Migration1:
Create Table users (id, name, last_name, email)
Migration1 executed OK. Some days later we make Migration 2:
Create Table items (id, user_id references users.id)
Alter Table users make_some_error_here
Now what will happen is that migrate will call the first statement and will create the table items with his foreign key to users. Then when he tries to apply the next statement it will fail.
If we fix the make_some_error_here, we can't run migrate because the table "items" it's created. We can't rollback (nor refresh, nor reset), because we can't delete the table users since there's a foreign key constraint from the table items.
Then the only way to continue is to go to the database and delete the table items by hand, to get migrate in a consistent state.
It is not a Laravel limitation, I bet you use MYSQL, right?
As MYSQL documentation says here
Some statements cannot be rolled back. In general, these include data
definition language (DDL) statements, such as those that create or
drop databases, those that create, drop, or alter tables or stored
routines.
And we have a recommendation of Taylor Otwell himself here saying:
My best advice is to do a single operation per migration so that your
migrations stay very granular.
-- UPDATE --
Do not worry!
The best practices say:
You should never make a breaking change.
It means, in one deployment you create new tables and fields and deploy a new release that uses them. In a next deployment, you delete unused tables and fields.
Now, even if you'll get a problem in either of these deployments, don't worry if your migration failed, the working release uses the functional data structure anyway. And with the single operation per migration, you'll find a problem in no time.
I'm using MySql and I'm having this problem.
My solution depends that your down() method does exactly what you do in the up() but backwards.
This is what i go:
try{
Schema::create('table1', function (Blueprint $table) {
//...
});
Schema::create('tabla2', function (Blueprint $table) {
//...
});
}catch(PDOException $ex){
$this->down();
throw $ex;
}
So here if something fails automatically calls the down() method and throws again the exception.
Instead of using the migration between transaction() do it between this try
Like Yevgeniy Afanasyev highlighted Taylor Otwell as saying (but an approach I already took myself): have your migrations only work on specific tables or do a specific operation such as adding/removing a column or key. That way, when you get failed migrations that cause inconsistent states like this, you can just drop the table and attempt the migration again.
I’ve experienced exactly the issue you’ve described, but as of yet haven’t found a way around it.
Just remove the failed code from the migration file and generate a new migration for the failed statement. Now when it fails again the creation of the database is still intact because it lives in another migration file.
Another advantage of using this approach is, that you have more control and smaller steps while reverting the DB.
Hope that helps :D
I think the best way to do it is like shown in the documentation:
DB::transaction(function () {
DB::table('users')->update(['votes' => 1]);
DB::table('posts')->delete();
});
See: https://laravel.com/docs/5.8/database#database-transactions
I know it's an old topic, but there was activity a month ago, so here are my 2 cents.
This answer is for MySql 8 and Laravel 5.8
MySql, since MySql 8, introduced atomic DDL: https://dev.mysql.com/doc/refman/8.0/en/atomic-ddl.html
Laravel at the start of migration checks if the schema grammar supports migrations in a transaction and if it does starts it as such.
The problem is that the MySql schema grammar has it set to false. We can extend the Migrator, MySql schema grammar and MigrationServiceProvider, and register the service provider like so:
<?php
namespace App\Console;
use Illuminate\Database\Migrations\Migrator as BaseMigrator;
use App\Database\Schema\Grammars\MySqlGrammar;
class Migrator extends BaseMigrator {
protected function getSchemaGrammar( $connection ) {
if ( get_class( $connection ) === 'Illuminate\Database\MySqlConnection' ) {
$connection->setSchemaGrammar( new MySqlGrammar );
}
if ( is_null( $grammar = $connection->getSchemaGrammar() ) ) {
$connection->useDefaultSchemaGrammar();
$grammar = $connection->getSchemaGrammar();
}
return $grammar;
}
}
<?php
namespace App\Database\Schema\Grammars;
use Illuminate\Database\Schema\Grammars\MySqlGrammar as BaseMySqlGrammar;
class MySqlGrammar extends BaseMySqlGrammar {
public function __construct() {
$this->transactions = config( "database.transactions", false );
}
}
<?php
namespace App\Providers;
use Illuminate\Database\MigrationServiceProvider as BaseMigrationServiceProvider;
use App\Console\Migrator;
class MigrationServiceProvider extends BaseMigrationServiceProvider {
/**
* Register the migrator service.
* #return void
*/
protected function registerMigrator() {
$this->app->singleton( 'migrator', function( $app ) {
return new Migrator( $app[ 'migration.repository' ], $app[ 'db' ], $app[ 'files' ] );
} );
$this->app->singleton(\Illuminate\Database\Migrations\Migrator::class, function ( $app ) {
return $app[ 'migrator' ];
} );
}
<?php
return [
'providers' => [
/*
* Laravel Framework Service Providers...
*/
App\Providers\MigrationServiceProvider::class,
],
];
Of course, we have to add transactions to our database config...
DISCLAIMER - Haven't tested yet, but looking only at the code it should work as advertised :) Update to follow when I test...
Most of the answers overlook a very important fact about a very simple way to structure your development against this. If one were to make all migrations reversible and add as much of the dev testing data as possible through seeders, then when artisan migrate fails on the dev environment one can correct the error and then do
php artisan migrate:fresh --seed
Optionally coupled with a :rollback to test rolling back.
For me personally artisan migrate:fresh --seed is the second most used artisan command after artisan tinker.

Categories