codeigniter project database upgrade scripts - php

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.

Related

Laravel 5.3 Writing a DB routine/subroutine in a migration file?

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?

Migrations + model changes in Yii

I want to get some details about recommended work process for Yii. Imagine you already have some database and some model for it. And in one day you need to add a new field to the model. In Django, you can just modify models.py file and then run manage.py makemigrations && manage.py migrate - it will analyze changes, create migration file and apply the changes to the database. But what I should do in Yii?
I see only following way from the docs and manuals:
Create empty migration
Try to write necessary changes in Yii-migration syntax (it may be not so obvious for altering column and adding foreign keys, more difficult than just writing SQL queries).
Run yiic migrate
Generate Model code using Gii for new database structure and copy-paste new fields to your existing Model file.
From my point of view, it leads to lot of useless work by creating migration in addition to modifying Model. So, instead of just modifying model like in Django, I have to use strange migration syntax in Yii and then modify model manually. It it really the way it supposed to work? Isn't it possible to simplify it somehow?
I'm using below approach for like 5-6 month and its work perfect:
create new folder inside models folder name it entities.
generate all models you need using gii and
generate all models you need using gii
a) in model path field use new folder, "entities" instead of models folder
b) in model class field, add "Entity" as model name postfix
now in models folder, make new PHP class and named it for example "Gift" and extends it from "GiftEntity"
add new folder, "entities" in preload imported classes.
now, when you make new migration and change your models in db, use gii to regenerate your entity models "GiftEntity", and all your codes in extended model "Gift" are untouched.

Renaming a MySQL table in a Zend/Doctrine PHP app fails?

I have a PHP backend application which is used both by an administration interface and a game. I know my way around PHP but I'm not familiar with Zend and Doctrine.
There's just two tables in MySQL (for schools and for students) and I want to change their name prefixes, because they refer to an earlier release of the game.
In short: I renamed the tables, I modified all PHP references to use the new names. There is not a trace of the old name anymore in all the Zend and Doctrine folders. But now the web application fails because it is still looking for the old names.
Here's the error:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'db1.neverland_schools' doesn't exist
So what happened is I changed the table name from neverland_schools to adventure_schools. It still looks for the old table name.
What I suspect is this being cache. But I don't have command line access to the server, nor experience with talking to Doctrine via command line.
If cache is the problem, would there be a way to reset the cache from PHP?
And if something else is the problem, I'd love to know.
Thanks!
If you have base class defined You must change name of schools and students base class. Path for base classess is in your config, or bootstrap file.
If not have you tried setting the table name explicitly, as in the first line
of the method below?
class Adventure_Schools extends Doctrine_Record {
public function setTableDefinition () {
$this->setTableName('adventure_schools');
...
You can delete the cache data :
you can emoty the tempory folder of the server
delete Doctrine cache
$deleted = $cacheDriver->deleteAll();
Maybe you should empty the proxy folder also.
You can verify that you changed the table name in the metadta depending on the driver.
if you are using the yaml or xml maybe the plin text serch doesn't take in ccount the file formt.
anotation
// entities/Product.php
/**
* #Entity **#Table(name="products")**
**/
xml
<entity name="Product" table="products">
yaml
# config/yaml/Product.dcm.yml
Product:
type: entity
table: products

Generating data objects in Zend Framework

Is there any way to create data objects for MySQL database tables using Zend_Tool?
What I am trying to achieve is to create setter/getter methods and column variable of any table in some class by using command line zf utility. (like we can do it in pear)
You would need to create your own providers for Zend_Tool.
Essentially, you create a class that extends Zend_Tool_Project_Provider_Abstract. Every public method becomes an action available within the zf.sh tool.
You also need to register your new provider with Zend_Tool. Do this by running:
run zf --setup storage-directory
run zf --setup config-file
edit ~/.zf.ini and add php.include_path and basicloader.classes.0 entries that point to your new provider's path and classname respectively.
Further details: http://akrabat.com/zend-framework/akrabat_db_schema_manager-zend-framework-database-migrations/ & https://github.com/akrabat/Akrabat/blob/master/zf1/Akrabat/Tool/DatabaseSchemaProvider.php

Better use of models and migration in symfony

Hey.
I'm having a hard time migrating changes I've done i my config/doctrine/schema.yml file.
I added the column age to the user table. Then I did a php symfony doctrine:generate-migrations-diff followed by php symfony doctrine:migrate .
Looking in my database, the column age is now added, without deleting any data.
But, my /lib/model/doctrine/base/BaseUser.class.php is not changed, there is no age field or functions for age . So I also did the command php symfony doctrine:build-model . Finally the model is updated/migrated too.
So I wonder, is this the only way? Seems like a lot of work, and I'm afraid to miss something each time doing it.
Could I go right into phpmyadmin, add changes in the database there and just do a php symfony doctrine:build-schema , and like that skip the migration part (two commands).
Also when the comes to use of models, am I right that /lib/model/doctrine/User.class.php is where I can make functions and such for my User "data class"? Like, making a function isFemale . If not, where would that kind of function be?
This might be a bad question, but why is the model layer inside the /lib/doctrine path? As far as I have learned, you keep modules inside apps, where you create your view and controller. Why should the model be outside. Like this I can make models without attached controller and view?
Thanks.
Why should the model be outside
Because models can be used everywhere in your project, in example, in different applications and modules.
Could I go right into phpmyadmin, add changes in the database there and just do a php symfony doctrine:build-schema , and like that skip the migration part (two commands).
Of course you can, but migrations are a good approach to track your schema when deploying to production or working in team.
Here how I use doctrine migrations (simple use-case):
Add a column age to my User model in schema.yml
./symfony doctrine:generate-migrations-diff. Migration class(-es) have been generated.
./symfony doctrine:migrate. Column age successfully added to table.
./symfony doctrine:build --all-classes. Build forms/filters/models
That's it. The main idea is that doctrine:generate-migrations-diff class:
Gathers information about all your models' structure (php-representation of schema.yml)
Compares your schema.yml and info from (1)
Generates migration classes based on difference
Also when the comes to use of models, am I right that /lib/model/doctrine/User.class.php is where I can make functions and such for my User "data class"? Like, making a function isFemale . If not, where would that kind of function be?
Yes, you can add such method to User model because it's about users.

Categories