如何(以及是否)使用初始数据填充Rails应用程序 [英] How (and whether) to populate rails application with initial data

查看:53
本文介绍了如何(以及是否)使用初始数据填充Rails应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Rails应用程序,用户必须登录。因此,为了使该应用程序可用,系统中必须有一个初始用户才能让第一人登录(然后他们可以创建后续用户)用户)。到目前为止,我已经使用迁移将特殊用户添加到数据库中。

I've got a rails application where users have to log in. Therefore in order for the application to be usable, there must be one initial user in the system for the first person to log in with (they can then create subsequent users). Up to now I've used a migration to add a special user to the database.

在询问了这个问题,看来我应该使用db:schema:load而不是运行迁移来在新的开发机器上建立新的数据库。不幸的是,这似乎不包括插入数据的迁移,仅包括设置表,键等的迁移。

After asking this question, it seems that I should be using db:schema:load, rather than running the migrations, to set up fresh databases on new development machines. Unfortunately, this doesn't seem to include the migrations which insert data, only those which set up tables, keys etc.

我的问题是,处理数据的最佳方法是什么这种情况:

My question is, what's the best way to handle this situation:


  1. 有没有办法让d:s:l包括数据插入迁移?

  2. 我应该完全不用迁移来插入数据吗?

  3. 我应该根本不使用数据预先填充数据库吗?我是否应该更新应用程序代码,以使其能够优雅地处理没有用户的情况,并允许在应用程序内部实时创建初始用户帐户?

  4. 还有其他选择吗? :)

  1. Is there a way to get d:s:l to include data-insertion migrations?
  2. Should I not be using migrations at all to insert data this way?
  3. Should I not be pre-populating the database with data at all? Should I update the application code so that it handles the case where there are no users gracefully, and lets an initial user account be created live from within the application?
  4. Any other options? :)


推荐答案

我以为我总结了一些很棒的答案关于这个问题,以及我现在的想法,我已经全部阅读了:)

I thought I'd summarise some of the great answers I've had to this question, together with my own thoughts now I've read them all :)

这里有两个不同的问题:

There are two distinct issues here:


  1. 我应该用特殊的 admin用户预填充数据库吗?还是该应用程序应提供一种在首次使用时进行设置的方法?

  2. 一个人如何用数据预先填充数据库?请注意,无论第1部分的答案如何,这都是一个有效的问题,除了管理员用户以外,还有其他预填充的使用场景。



<对于(1),似乎从应用程序内部设置第一个用户是很多额外的工作,因为从定义上讲,几乎没有使用过该功能。但是,由于它会强制用户设置他们选择的密码,因此它可能会稍微更安全。最好的解决方案是介于这两个极端之间:有一个脚本(或rake任务,或其他任何东西)来设置初始用户。然后可以将脚本设置为在开发过程中自动使用默认密码进行填充,并要求在生产安装/部署过程中输入密码(如果您不希望管理员使用默认密码)。

For (1), it seems that setting up the first user from within the application itself is quite a bit of extra work, for functionality which is, by definition, hardly ever used. It may be slightly more secure, however, as it forces the user to set a password of their choice. The best solution is in between these two extremes: have a script (or rake task, or whatever) to set up the initial user. The script can then be set up to auto-populate with a default password during development, and to require a password to be entered during production installation/deployment (if you want to discourage a default password for the administrator).

对于(2),似乎有许多好的有效解决方案。瑞克任务似乎是一个好方法,并且有一些插件可以使此操作更加容易。只需查看其他一些答案即可查看这些细节:)

For (2), it appears that there are a number of good, valid solutions. A rake task seems a good way, and there are some plugins to make this even easier. Just look through some of the other answers to see the details of those :)

这篇关于如何(以及是否)使用初始数据填充Rails应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆