开发者

Codeigniter upgradable module logic for database process

开发者 https://www.devze.com 2023-04-03 19:44 出处:网络
i am trying to build my own cms with using Codeigniter. I wrote some modules already. But, in time i made some changes with them.

i am trying to build my own cms with using Codeigniter. I wrote some modules already. But, in time i made some changes with them. Now, if i want to upgrade a module. I send the files with ftp and change database fields with phpmyadmin.

It takes a lot time and high possibility to mis something to change, and for every project i've use this module, i have to repeat these changes again.

Now, I am planning to make an installation system.

my Modules directory structure like below:

/modules
/modules/survey/
/modules/survey/config
/modules/survey/config/autoload.php
/modules/survey/config/config.php
/modules/survey/config/routes.php
/modules/survey/config/install.php
/modules/survey/controllers
/modules/survey/controllers/entry.php...
/modules/survey/models
/modules/survey/models/survey.php...
/modules/survey/views
/modules/survey/views/index.php...

I thought that all modules should have an install.php file in config directory. That is keeping the setting of releated module. Like below:

$config['version']  = 1.1;  //or 1.2, 1.3 etc.
$config['module'] = 'Survey Module';
$config['module_slug'] = 'survey';
$config['module_db_table'] = 'module_survey'; 

I have an installed_modules table already:

id, module, module_slug, version

Now, i am trying to make an installation script. Like below:

Before start , I zip module's files.

1- upload zip file with an installation page to a temp directory 
2- unzip the module in this temp direcorty
3- Find install.php
4- Get modules information from install.php
5- Check if this module already in installed_modules table.
6a) If it's not: I will make a new module_survey table. And copy this temp directory into the real modules directory.
6b) If it's already : I have to change the structure of this table without lossing the data added before. Delete all module files and copy the new ones from temp into the modules directory.
7- When everything done, Delete temp directory.

I stucked in 6a and 6b.

For 6a, How should i create e.x 'module_survey' table. Should i add a $config['db_query'] in install.php like

$config['db_query'] = "CREATE TABLE IF NOT EXISTS `module_survey` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) DEFAULT NULL,
`lang_id` int(11) NOT NULL DEFAULT '1',
`usort` int(3) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 ;
";

and run this query. Or what is your advice here? There maybe not just one table, there should 2 or more with relations each other for different modules.

and For 6b:

I thought, i should create a new temp table like named "temp_module_survey".

old fields =

$oldFields = $this->db->field_data('module_survey');

for new fields = $newFields = $this->db->field_data('temp_module_survey');

compa开发者_JAVA百科re fields which are newly added, which are deleted and which's fieldData has changed. And add new fields to oldTable Delete unnecessary fields from oldTable and update fields which's fieldData has changed.

Then remove temporary Table.

For a summary, What should i do for database changes without lossing the old data.

I hope i could explain.

Thank you.


Phil Sturgeon's codeigniter-migrations can help.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号