Codeigniter可升级数据库进程模块逻辑

发布于 2024-12-03 16:28:02 字数 2526 浏览 1 评论 0原文

我正在尝试使用 Codeigniter 构建自己的 cms。 我已经写了一些模块。但是,随着时间的推移,我和他们一起做了一些改变。 现在,如果我想升级一个模块。我使用 ftp 发送文件并使用 phpmyadmin 更改数据库字段。

这需要花费大量时间并且很有可能会错过要更改的内容,并且对于我使用此模块的每个项目,我都必须再次重复这些更改。

现在,我打算做一个安装系统。

我的模块目录结构如下:

/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...

我认为所有模块都应该在 config 目录中有一个 install.php 文件。即保留相关模块的设置。如下所示:

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

我已经有一个installed_modules表:

id, module, module_slug, version

现在,我正在尝试制作一个安装脚本。如下所示:

在开始之前,我压缩模块的文件。

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.

我陷入了6a和6b。

对于 6a,我应该如何创建前“module_survey”表。 我应该在 install.php 中添加 $config['db_query']

$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 ;
";

并运行此查询吗?或者你在这里有什么建议?也许不只是一张表,应该有两张或更多张表,不同的模块彼此有关系。

对于 6b:

我想,我应该创建一个新的临时表,例如名为“temp_module_survey”。

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

对于新字段= $newFields = $this->db->field_data('temp_module_survey');

比较哪些字段是新添加的,哪些字段是删除的,哪些字段的fieldData发生了变化。 和 向旧表添加新字段 从oldTable中删除不需要的字段 并更新 fieldData 已更改的字段。

然后删除临时表。

总结一下,在不丢失旧数据的情况下,我应该如何处理数据库更改。

我希望我能解释一下。

谢谢。

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');

compare 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

那支青花 2024-12-10 16:28:02

Phil Sturgeon 的 codeigniter-migrations 可以提供帮助。

Phil Sturgeon's codeigniter-migrations can help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文