I'm currently building a new application which runs on an existing database - which is already in production. This new app is built in Laravel 4 and has a few migrations to make schema changes that are required.
Requirements are to use Percona Toolkits pt-online-schema-change to issue schema changes, however I cannot find anywhere how to use this from within a migration - just the standard CLI interface. I need some way to tie together the schema change within the migration and pt-online-schema-change.
I don't want to loose all the benefits that come with writing migrations or using the Laravel schema builder. I was hoping there is a simple way I'm not seeing to use Percona Toolkit within them. I have thought of using exec() but I'd prefer to avoid this if possible.
When "Googling" periodically over the past few weeks, I could not find anything which resembles using Percona Toolkit within a migration. Surely this is a problem that has been solved already?
If my approach is flawed, please tell me! :)
You probably will be able to execute it like this in your migrations:
public function up()
{
exec('pt-online-schema-change --alter "ADD COLUMN c1 INT" D=sakila,t=actor', $output, $return);
if ( ! $return) {
throw \Exception('Error migrating '.__FILE__);
}
}
Related
In a previous question, I asked about how to write a trigger and gave a decent example of one way to write it in a laravel 5.X migration file. (note: hard/raw coded)
Referenced Question:
https://www.stackoverflow.com/questions/39177303/laravel5-x-database-triggers-and-possible-best-practices
However, as the title states, does anyone know how to write a database routine/subroutine in a laravel migration file??
Or
Does anyone know of a clean way to implement using laravel's methods to create a migration file that will make subroutines or routines within a database?
I speak English, but not that good when it comes to grammar.
Now this question is about Laravel Framework.
Lately I've been developing a project using Laravel, normal day. But then, I started to make a migration
php artisan make:migration create_quizs_table --create=quizs
Created a few create query using php artisan tinker, yes all goes fine.
But then I refresh my migration just for curiosity, php artisan migrate:refresh, and here's what I get when I try to load pages using table quizs, after I:
Created a Quiz Model, App\Quiz.php from artisan,
try Eloquent technique to retrieve simple database ::all() query.
The Error on my browser:
There is an error ... with 'quizzes' something.
At this point, I learned that the plural of quiz is quizzes, not quizs. Is Laravel that strict on that, especially Model, since I noticed, Model use Singular without those s-es such as Quiz, Note, Book etc.
I have to figure out my English just to solve this.
Sadly, I don't have any log as `too much tinkering` corrupt my project.
But that is what I recall clearly, for I learned the plural of quiz from Laravel, thanks I guess. I believe you can recreate the issue too by creating quizs table, with wrong grammar.
I usually work with SF2, and with Doctrine, Entities can be generated automatically and if you build the schema in a Soft like MySQL Workbench you can do Reverse Engineering.
I'm new to Laravel so there is a way to do these both things? I would use Laravel because I've to do a very little project, but I didn't want to write all this code for what we call "Migrations", seems very boring no?
So there is a way to generate this stuff in laravel? Maybe I've to use Doctrine in Laravel for that?
If you want to generate migration files from an existing mysql database you can use a Laravel package called XCMer / larry-four-generator. Apart from reverse engineering it has a bunch of other features.
Larry Four is a Laravel 4 package offering advanced model and
migration generation functionality. Thanks to Larry you can quickly
jot down the data scheme for your idea in an easy DSL and genereate
migrations and models from it with just a single click. Larry can also
analyse your existing data scheme and generate some magic for you too.
Try the original best master branch
https://github.com/XCMer/larry-four-generator/tree/master
or my Extended Models fork offering additional functionality
https://github.com/XCMer/larry-four-generator/tree/Gadoma-extendedmodels
As far as I can tell there is still no way to reverse a schema to migration files, using Laravel only. But you can:
1) Export your MySQL schema
mysqldump -u root -p --no-data dbname > schema.sql
2) Create a migration to use your schema
php artisan migration:make create_schema
3) Boot your full schema
class CreateSchema extends Migration {
public function up()
{
$file = file_get_contents(app_path().'/database/data/full_schema.sql', true);
DB::unprepared($file);
}
public function down()
{
}
}
4) After that, if you need to do any changes to your schema, just create new migrations and make your changes.
Not tested, but should work.
Is there a way to take an existing mysql create table String, and turn it into a db_forge array/script suitable for the create_table() method?, ie basically reverse the dbforge process. It is just quicker to make tables in something like Sequel pro, then copy the sql code to the model.
I already have the mysql code, but i was thinking of using the dbforge to allow different databases to be used.
Do people use the dbforge? or just write the SQL creation code manually?
hi if you want to create tables through codeigniter then use migrations class it can solve your problem. you can create, update and delete table with migrations.
CI Migration class
There is no automated process to do what you ask. You have to convert it manually.
I have found that once I became familiar with the dbforge syntax, I can create tables just as quickly, if not quicker, than using Sequel Pro or another SQL GUI.
Does a utility exist in Codeigniter to automatically generate a migration script when a model or schema changes? If so, where is it?
Unfortunately, no such utility exists. Though it would definitely be a fantastic one. You could write one to monitor changes in db structure, though that might be a monster project. Alternatively, A library could be written to provide that feature.