windwalker/rad-development-bundle

开发包包含种子器和SQL同步工具。

1.0.5 2017-04-29 10:27 UTC

This package is auto-updated.

Last update: 2024-09-21 20:31:00 UTC


README

一些有用的命令行工具,用于在开发过程中操作数据库。

目录

通过Composer安装

cd /to/your/joomla

composer create-project windwalker/rad-development-bundle libraries/windwalker-bundles/DevelopmentBundle 1.*

命令

Windwalker Console - version: 2.1
------------------------------------------------------------

# system commands ...

  seed         The data seeder help you create fake data.
    import       Import seeders.
    clear        Clear seeders.

  sql          SQL sync & diff tools.
    backup       Backup sql.
    col          Column operation
    export       Export sql.
    import       Import a sql file.
    profile      Profiles.
    restore      Restore to pervious point.
    table        Model operation.

SQL同步

Windwalker Sqlsync是一个强大的SQL比较和差异工具,帮助开发者更新他们的SQL模式。

为什么不使用迁移工具?

实际上,我们目前正在为Joomla和Windwalker开发迁移工具,但迁移对Joomla CMS来说并不合适,有时我们可能需要将Joomla文章、模块、扩展和菜单同步到您的生产服务器。迁移工具很难做到这一点。

因此,使用Sqlsync工具将帮助您比较本地和远程机器之间的SQL模式,只需在本地运行export,并通过git跟踪所有模式YAML文件。然后将文件推送到远程服务器并运行import,所有模式将更新到远程。

目前Sqlsync在列名更改方面较弱,您可以使用钩子来处理。

导出和导入SQL模式

php bin/windwalker sql export

php bin/windwalker sql import

模式将导出到resources/sqlsync/default/schema.yml

跟踪表

跟踪信息存储在resources/sqlsync/default/track.yml

默认为

table:
    '#__assets': all
    '#__associations': all
    '#__banner_clients': all
    '#__banner_tracks': all
    '#__banners': all
    '#__categories': all
    '#__contact_details': all
    '#__content': all
    '#__content_frontpage': all
    '#__content_rating': all
    '#__content_types': all
    '#__contentitem_tag_map': all
    '#__core_log_searches': all
    '#__extensions': all
    '#__finder_filters': cols
    '#__finder_links': cols
    '#__finder_links_terms0': cols
    '#__finder_links_terms1': cols
    '#__finder_links_terms2': cols
    '#__finder_links_terms3': cols
    '#__finder_links_terms4': cols
    '#__finder_links_terms5': cols
    '#__finder_links_terms6': cols
    '#__finder_links_terms7': cols
    '#__finder_links_terms8': cols
    '#__finder_links_terms9': cols
    '#__finder_links_termsa': cols
    '#__finder_links_termsb': cols
    '#__finder_links_termsc': cols
    '#__finder_links_termsd': cols
    '#__finder_links_termse': cols
    '#__finder_links_termsf': cols
    '#__finder_taxonomy': cols
    '#__finder_taxonomy_map': cols
    '#__finder_terms': cols
    '#__finder_terms_common': all
    '#__finder_tokens': cols
    '#__finder_tokens_aggregate': cols
    '#__finder_types': cols
    '#__languages': all
    '#__menu': all
    '#__menu_types': all
    '#__messages': all
    '#__messages_cfg': all
    '#__modules': all
    '#__modules_menu': all
    '#__newsfeeds': all
    '#__overrider': all
    '#__postinstall_messages': all
    '#__redirect_links': all
    '#__schemas': all
    '#__session': cols
    '#__tags': all
    '#__template_styles': all
    '#__ucm_base': all
    '#__ucm_content': all
    '#__ucm_history': all
    '#__update_sites': all
    '#__update_sites_extensions': all
    '#__updates': cols
    '#__user_keys': all
    '#__user_notes': all
    '#__user_profiles': all
    '#__user_usergroup_map': all
    '#__usergroups': all
    '#__users': all
    '#__utf8_conversion': all
    '#__viewlevels': all

有3个跟踪规则

  • all - 跟踪所有数据,在导入和导出时始终刷新数据。
  • cols - 只跟踪表列,不刷新数据。
  • none - 忽略此表

所有未在track.yml中列出的表都将为none

同步跟踪表

如果您安装了一个新组件,数据库中可能添加了多个表,您可以运行

php bin/windwalker sql table sync

将所有未跟踪的表自动添加到track.yml

状态

此命令可以帮助您检查表跟踪信息。

php bin/windwalker sql status

p-2016-04-05-006

配置文件

您可以使用以下方式更改配置文件

# List profile
php bin/windwalker sql profile

# Create & checkout profile
php bin/windwalker sql profile create test
php bin/windwalker sql profile checkout test

现在您可以将模式导出到其他配置文件,这非常类似于git分支。

快速导入和导出到配置文件

您不需要总是检出配置文件,将配置文件作为命令的参数添加。

# Single profile
php bin/windwalker sql export test

# Multiple profiles
php bin/windwalker sql export default foo bar test

# Ignore checking prompt
php bin/windwalker sql export default foo bar test -y

# Export all profiles
php bin/windwalker sql export --all -y

此操作支持export / import两个命令。

重命名列

修改模式文件中的From属性。

columns:
  oldname: { Field: newname, ... , From: [oldname, oldname2] }

From属性中的所有旧名称都将转换为newname

目前Sqlsync在列名更改方面较弱,尽量避免执行此操作。

导出和导入钩子

将这些文件添加到配置文件文件夹。

pre-export.php
post-export.php
pre-import.php
post-import.php

然后简单地编写您的脚本做些事情。

// resources/sqlsync/default/pre-import.php

if (!JFactory::getConfig()->get('debug'))
{
    throw new \Exception('STOP importing, please enable debug mode to do any DB operations.');
}

// Or do some advanced DB actions, for instance, rename column or remove indexes.
JFactory::getDbo()->setQuery('ALTER TABLE ...')->execute();

种子器

Windwalker开发包提供简单的种子器和模拟工具,帮助您生成假数据。

打开resources/seeders/DatabaseSeeder.php,您将看到默认的DatabaseSeeder

resources/seeders/ProductSeeder.php中添加一个新的种子器类

use Faker\Factory;
use Windwalker\Data\Data;
use Windwalker\DataMapper\DataMapper;

class ProductSeeder extends \DevelopmentBundle\Seeder\AbstractSeeder
{
	public function doExecute()
	{
		$faker = Factory::create();
		$mapper = new DataMapper('#__mycomponent_products');
		$categories = (new DataMapper('#__categories'))->findColumn('id', ['extension' => 'com_mycomponent']);
		$userIds = (new DataMapper('#__users'))->id;

		foreach (range(1, 200) as $i)
		{
			$data = new Data;
			
			$user_id = JFactory::getUser()->id;

			$data['catid']        = $faker->randomElement($categories);
			$data['title']        = $faker->sentence(rand(3, 5));
			$data['alias']        = JFilterOutput::stringURLSafe($data['title']);
			$data['temperature']  = $faker->randomElement(['normal', 'refrigeration', 'freeze']);
			$data['price']        = rand(5000, 10000)/100;
			$data['location']     = $faker->country;
			$data['description']  = $faker->paragraph(5);
			$data['image']        = $faker->imageUrl();
			$data['state']        = $faker->randomElement([1, 1, 1, 1, 1, 0]);
			$data['created']      = $faker->dateTimeThisMonth->format('Y-m-d H:i:s');
			$data['created_by']   = $faker->randomElement($userIds);
			$data['modified']     = $faker->dateTimeThisMonth->format('Y-m-d H:i:s');
			$data['modified_by']  = $faker->randomElement($userIds);
			$data['params']       = '';

			$mapper->createOne($data);

			$this->command->out('.', false);
		}

		$this->command->out();
	}
	
	public function doClear()
	{
		$this->truncate('#__mycomponent_products');
	}
}

并将此类设置为DatabaseSeeder,您必须按依赖关系对种子器进行排序。

// ...

	public function doExecute()
	{
		$this->execute(new CategorySeeder);

		$this->execute(new ProductSeeder);

		$this->execute(new OrderSeeder);
	}

	public function doClear()
	{
		$this->clear(new CategorySeeder);

		$this->clear(new ProductSeeder);

		$this->clear(new OrderSeeder);
	}

现在运行种子器

php bin/windwalker seed import

或清除它

php bin/windwalker seed export