CakePHP Migration Script doesn't update the Table Model - php

I need to add an admin column to my user table in my database. I created the migration script with the following command.
bin/cake bake migration AddAdminToUsers admin:boolean
This mostly did what I wanted, I just changed the default value to false. My Migration script now looks like this.
<?php
use Migrations\AbstractMigration;
class AddAdminToUsers extends AbstractMigration
{
public function change()
{
$table = $this->table('users');
$table->addColumn('admin', 'boolean', [
'default' => false,
'null' => false
]);
$table->update();
}
}
Also, oddly enough, I've tried this several times and each time I'm only able to run this migration script once. I have to delete it and re-bake a new one if I want another one to work.

When you run a migration it marks as migrated and you can not run it one more time unless do the rollback. Rollback will cancel previous migration and you will be able to run it one more time.Here is fully docs for plugin that cakphp using for migrations.

Related

How to manually name Laravel migration so "php artisan migrate" can run it?

I have a side project written in PHP that can automatically generate a migration file based on the information from my audit table. Since the file is created by my application without using the artisan command, I have no idea how to make Laravel to see my migration files.
A sample of a migration file my application generated:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class sampleMigration extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('audit_database_index_column_relations', function(Blueprint $table){
$table->integer('relation_id');
$table->integer('database_index_id');
$table->integer('database_column_id');
$table->integer('sequence_position');
$table->integer('is_deleted');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('audit_database_index_column_relations');
}
}
Regarding what I have attempted, I firsly tried to specify the path linking to my migration file. However, it showed an error that is
Symfony\Component\Debug\Exception\FatalThrowableError : Class '' not found
at C:\Users\L0tusw0w\Desktop\Laravel\testProject\vendor\laravel\framework\src\Illumi
nate\Database\Migrations\Migrator.php:448
444| public function resolve($file)
445| {
446| $class = Str::studly(implode('_', array_slice(explode('_', $file), 4)
));
447|
> 448| return new $class;
449| }
450|
451| /**
452| * Get all of the migration files in a given path.
Exception trace:
1 Illuminate\Database\Migrations\Migrator::resolve("sampleMigration")
C:\Users\L0tusw0w\Desktop\Laravel\testProject\vendor\laravel\framework\src\Illum
inate\Database\Migrations\Migrator.php:186
2 Illuminate\Database\Migrations\Migrator::runUp("C:\Users\L0tusw0w\Desktop\Larave
l\testProject/database/migrations/sampleMigration.php")
C:\Users\L0tusw0w\Desktop\Laravel\testProject\vendor\laravel\framework\src\Illum
inate\Database\Migrations\Migrator.php:162
In my second attempt, I replaced the up() function of the default migration "create_user_table" with my up() function as the example, and as I expected, it executed without any problem! In general, I suppose that the error must be from my way of naming the file.
Therefore, I would like to know how I can name my migration file in a way so that Laravel can detect and run them
Thank you in advance!
The error message you're getting when trying to run your migration actually provides the answer you need in this case. The function is using the name of the file to determine what the name of the class for the migration should be. We read chained functions like this from the inside most paranthesis out so starting at the beginning
explode('_', $file)
The method takes the file name and explodes it into an array, splitting on under scores. So
2014_10_12_100000_create_password_resets_table
becomes
[
"2014",
"10",
"12",
"100000",
"create",
"password",
"resets",
"table",
]
Next, the method slices off the first 4 elements of the array. These elements contain a timestamp of when the migration was created, to determine the order to run the migrations, but have nothing to do with the class name
array_slice(explode('_', $file), 4)
So now we have
[
"create",
"password",
"resets",
"table",
]
next the array gets collapsed back into a string, with underscores separating each word
implode('_', array_slice(explode('_', $file), 4))
giving us
create_password_resets_table
And lastly the Laravel Str::studly helper function converts this new string to studly case to determine the class name
Str::studly(implode('_', array_slice(explode('_', $file), 4)));
CreatePasswordResetsTable
So to create a migration file name, start with a timestamp of when you are creating the file, then add the name of the class in all lowercase, separated by underscores.
Class SampleMigration extends Migration
Would be
2019_12_04_37860000_sample_migration
Special note: Don't skip the timestamp or try to throw whatever you want in those first 4 elements. The order that the files appear in the directory is the order the migrations will be run in, the timestamps ensure that migrations are always run in the right order. Skipping this step could cause migrations that change tables to run before the table is created, or changes running out of order and giving you an unexpected result for the current table.

Dynamically add columns in an existing table on the fly in CakePHP 3

I want to add column in my existing table in CakePHP 3.
My ContactsTable.php file code:
<?php
namespace App\Model\Table;
use Cake\ORM\Table;
use Migrations\AbstractMigration;
class ContactsTable extends Table
{
public function initialize(array $config)
{
$this->addBehavior('Timestamp');
$table = $this->table('contacts');
$table->addColumn('price', 'decimal')->update();
}
}
I have tried as described in CakePHP 3 documentation but I got this error:
Call to a member function addColumn() on a non-object
How do I add columns on-the-fly via the controller?
Code:
<?php
namespace App\Controller;
use Cake\Core\Configure;
use Cake\Network\Exception\NotFoundException;
use Cake\View\Exception\MissingTemplateException;
use Cake\ORM\TableRegistry;
use Cake\Database\Schema\Table;
use Cake\Datasource\ConnectionManager;
use \Migrations\AbstractMigration as AbstractMigration;
use \Phinx\Db\Adapter\MysqlAdapter as MysqlAdapter;
class PagesController extends AppController
{
public function display()
{
$connectionArray = ConnectionManager::get('default')->config();
$connectionArray['pass'] = $connectionArray['password'];
$connectionArray['user'] = $connectionArray['username'];
$connectionArray['name'] = $connectionArray['database'];
$migrationObject = new AbstractMigration(mt_rand());
$migrationObject->setAdapter(new MysqlAdapter($connectionArray));
$tree = $migrationObject->table('tests');
$tree->addColumn('something', 'text')
->update();
}
}
After few hours of Hacking, finally found a way to do it on-the-fly.
Tested in default cakephp 3 (latest - as of today - 2nd June '16)
If you are using a different database adapter, change it to that adapater from MysqlAdapter.
Note to the users:
This is an ugly hack and should be used ONLY if you do not work in
an organization where each migration commit requires peer reference.
mt_rand() must NEVER be used as a version number hack.
There is no canonical way of doing it via the controllers. Update in a datasource MUST always be done modified via migrations - using a proper structure.
Refer to Running Migrations in a non-shell environment and try to create a migrations logs under /config/migrations, that would be more rule-specific-on-the-fly and you will also have logs for peers to review.
Migration plugin also support Running Migrations in a non-shell environment.
Since the release of version 1.2 of the migrations plugin, you can run migrations from a non-shell environment, directly from an app, by using the new Migrations class. This can be handy in case you are developing a plugin installer for a CMS for instance. The Migrations class allows you to run the following commands from the migrations shell: migrate, rollback, markMigrated, status and seed.
Each of these commands has a method defined in the Migrations class.
You can prepare some custom handler which will accept column data from user side and run migration. In this case it could be some form with name and type inputs. Migration will be applied to DB after form with data will be submitted.
Here is how to use it:
use Migrations\Migrations;
$migrations = new Migrations();
// Will return an array of all migrations and their status
$status = $migrations->status();
// Will return true if success. If an error occurred, an exception will be thrown
$migrate = $migrations->migrate();
// Will return true if success. If an error occurred, an exception will be thrown
$rollback = $migrations->rollback();
// Will return true if success. If an error occurred, an exception will be thrown
$markMigrated = $migrations->markMigrated(20150804222900);
// Will return true if success. If an error occurred, an exception will be thrown
$seeded = $migrations->seed();
If you want add new column to product table e.g 'price' and price is a 'decimal' you should go to your project and write this in console:
bin/cake bake migration AddPriceToProducts price:decimal
You can see a new file e.g. Config/Migrations/20160501190410_AddPriceToProducts.php
<?php
use Migrations\AbstractMigration;
class AddPriceToProducts extends AbstractMigration
{
/**
* Change Method.
*
* More information on this method is available here:
* http://docs.phinx.org/en/latest/migrations.html#the-change-method
* #return void
*/
public function change()
{
$table = $this->table('products');
$table->addColumn('price', 'decimal', [
'default' => null,
...
'null' => true,
]);
$table->update();
}
}
and later just launch migrations to add this column to data base, write this in console:
bin/cake migrations migrate

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.

Laravel DB Seeds - Test Data v Sample Data

I'm probably misunderstanding exactly how this works, but what's the best way to accomplish this? I have something in mind but it seems quite hacky.
I have a set of sample data which I use to test my application. This is seeded via the built in seeder in Laravel. This contains things like example users, addresses, documents etc.
I also have a set of default data which should go in production. I currently add this directly in the migration. For example, if I was adding a table for account_roles, I might include the following at the bottom of the migration
$account_admin = array('role' => 'Account Administrator', 'flag' => 'ACCOUNT_ADMIN');
$account_owner = array('role' => 'Account Administrator', 'flag' => 'ACCOUNT_OWNER');
DB::table('account_roles')->insert($account_admin);
DB::table('account_roles')->insert($account_owner);
This way, on production, I just migrate the database to insert any production ready database values, and on staging/development, I can refresh the migrations and then seed the database with sample data.
Is there any other (better) way to do this?
You could run a check on the current environment in your seeder file, and seed as needed
<?php
class DatabaseSeeder extends Seeder {
public function run()
{
Eloquent::unguard();
if (App::environment() === 'production')
{
$this->call('ProductionSeeder');
}
else
{
$this->call('StagingSeeder');
}
}
}

database migration is not working in yii

In Yii framework I used migration just like
./yiic migrate create tbl_demo
it made the migration file where I entered the values for up like
<?php
class m110714_094912_tbl_demo extends CDbMigration
{
public function up()
{
$this-> createTable('{{tbl_demo}}', array(
'id' => 'pk',
'name' => 'VARCHAR \'80\' NOT NULL',
))
}
public function down()
{
echo "m110714_094912_tbl_demo does not support migration down.\n";
return false;
}
/*
// Use safeUp/safeDown to do migration with transaction
public function safeUp()
{
}
public function safeDown()
{
}
*/
}
after entering this it is showing message like
New migration created successfully.
But whenever I am checking mysql database there is no table for tbl_demo is found. I also put all the values of up in safeup but it not made any result.Every thing is working fine but don't know why new table is not creating? Please help me
You should go to the command line and call yiic migrate up and it will ask you if you want to apply the tbl_demo migration and when you type in "yes" it will execute the code. The message New migration created successfully. appears when you execute yiic migrate create -something- not when you apply the migration.
If you want to apply the migration again, you should delete the row regarding "tbl_demo" migration in the "migrations" db table that yiic created to log the migrations.
I just fixed this problem myself.
The answer is, the default /protected/config/console.php comes out of the box configured to use a SQLite database called testdrive.db. You have to configure it to talk to your MySQL database.

Categories