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?
Related
I have several database migrations coded in Laravel that I do not want to apply just yet.
I do not want to rename these files or move them into some other folder. If any of the migrations had been applied previously, moving or renaming them breaks Laravel's ability to roll them back.
I do not want a means of applying only one specific migration at a time.
I want a means of marking the class that is a modification to the migration file itself, not some external marking or process (such as adding rows to the migration table).
Is there a programmatic solution for this?
The answer : Laravel 5 - skip migrations may be of help for you, instead of skipping , choose the migrations you want to do directly .
What I'm referring too exactly is the use of caloskao/migrate-specific
I want shorter names for my migration files.
This project will not have tens of thousands of migrations, therefor 2019_11_05_191747_create_units_table.php is verbose.
I want to have clean migration file names like:
0001_create_users_table.php
0002_create_units_table.php
But the code in vendor\laravel\framework\src\Illuminate\Database\Migrations\Migrator.php is exploding on _ and not liking it.
Symfony\Component\Debug\Exception\FatalThrowableError : Class '' not found
How to get this done?
You can't because you should not change Laravel main core (for update compatibility reasons)
You should use the migrations as intended with "YYYY_MM_DD_HHMMSS_action_table_table_description.php" format.
Well, there is a solution, which would be to create a custom command and do what laravel migration does but with another file format and that's a lot of work for just the name of the migration files.
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'm looking for ideas to properly handle my project's mysql table updates across environments. I've taken a look at the CI DB Forge class and I believe this might help me out a bit. My thoughts are to:
create a new file for each database install, table upgrade or change. The file would contain the raw mysql query to do each relevant task
run the upgrade scripts via hooks before any controllers are loaded
continue loading the project
Is this the correct thinking? This is pretty similar to how Magento handles database upgrades per extensions.
Sounds like you are looking for the Migrations class. This is a fairly new library, and the documentation at this moment is not too good in my opinion.
If you enable this library in application/config/migrations.php and load it, the it will create a database table called migrations. The workflow from there is the following:
Create a new file under application/migrations make sure you name it with sequentially numbered file name like 001_some_descriptive_name.php. The format is important, exactly 3 numbers and at least one _ after them.
In the new file create a class named after the file name, so the 001_some_descriptive_name.php should hold a class called Migration_Some_descriptive_name and extend the CI_Migration class. The class name casing is important, first Migration_ then one uppercase letter then lowercase.
Create a public up and a public down method inside the class
Inside the up method add your migration code, that changes the database. You can use the db forge library or just plain old $this->db->query() calls. Dbforge is more portable if you need to support multiple database systems its probably better to use that.
Inside the down method add the code that would reverse the effects of up. If up adds a column then down should drop that column, if up creates a table, down should drop that table and so on.
Once you finished your migration class bump the migration_version inside the migration config file.
Create a controller load the migration library and call $this->migration->current() this will check the version from the migration database table and run the migration classes up or down methods in order to reach the migration version in the config file you set at step 6. So for example if the database says you are at version 2, and the config says you should be on 5, then it will run the up method of the migrations with 003_..., 004_..., 005_.. filenames in order. If you set lower number then the current the down methods will be called. The database starts the counting from 0 so don't create a 000_... file.
If you feel adventurous you can create a hook that loads the migration class and runs the get_instance()->migration->latest() on every page load so every environment will auto update the db once you deploy a new migration class.
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.