phinx迁移sqlite内存phpunit

ndh0cuux  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(439)

使用sqlite内存的phinx迁移在0.9.2中似乎不起作用,我有一个非常简单的应用程序,只有一个表(产品)。运行迁移后,产品表不存在:

  1. use Symfony\Component\Yaml\Yaml;
  2. use Phinx\Config\Config;
  3. use Phinx\Migration\Manager;
  4. use Symfony\Component\Console\Input\StringInput;
  5. use Symfony\Component\Console\Output\NullOutput;
  6. $pdo = new PDO('sqlite::memory:', null, null, [
  7. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
  8. ]);
  9. $settings = Yaml::parseFile('../phinx.yml');
  10. $settings['environments']['testing'] = [
  11. 'adapter' => 'sqlite',
  12. 'connection' => $pdo
  13. ];
  14. $config = new Config($settings);
  15. $manager = new Manager($config, new StringInput(' '), new NullOutput());
  16. $manager->migrate('testing');
  17. $manager->seed('testing');
  18. $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
  19. // This line creates an exception of table doesn't exist
  20. $pdo->query("SELECT * FROM product");

最后一行查询生成以下异常的产品表:
pdoexception:sqlstate[hy000]:常规错误:1第43行没有这样的表:product in/home/vagrant/code/ecommerce/public/index.php
为了完整起见,以下是与mysql开发环境完美配合的产品迁移:

  1. use Phinx\Migration\AbstractMigration;
  2. class Product extends AbstractMigration
  3. {
  4. public function change()
  5. {
  6. $table = $this->table('product');
  7. $table->addColumn('name', 'string', ['limit' => 100, 'null' => false])
  8. ->addColumn('price', 'integer')
  9. ->create();
  10. }
  11. }
wooyq4lh

wooyq4lh1#

通常从命令行使用phinx时,phinx\config的configfilepath属性被正确设置为phinx.yml的完整路径
但是在phinx文档中的示例中(http://docs.phinx.org/en/latest/commands.html#using-phpunit的phinx)为了创建用于phpunit测试的sqlite内存数据库,它使用php数组,因为pdo示例必须手动输入。
由于没有设置phinx.yml的路径,phinx\config的replacetokens方法通过调用以下命令创建phinx\u config\u dir:

  1. $tokens['%%PHINX_CONFIG_DIR%%'] = dirname($this->getConfigFilePath());

phinx使用%%phinx\u config\u dir%%计算其迁移和种子文件夹的位置,当没有使用phinx.yml时,这将不再有效。
解决方案是在手动创建配置类时提供一个路径:

  1. $config = new Config($settings, './');

相关问题