如何将SQL架构导入Rails

时间:2023-01-13 18:06:36

Say for example I've got an SQL schema that is ready to go. How would I import it into my Rails app so that I use my prepared database instead of all those funny migrations.

比方说,我有一个准备好的SQL模式。我如何将它导入我的Rails应用程序,以便我使用我准备好的数据库而不是所有那些有趣的迁移。

EDIT: You have all misunderstood my question so far. I'm asking if I had a working database application in say PostgresQL. How would I use this as the basis of my Rails application?

编辑:到目前为止,你们都误解了我的问题。我在问PostgresQL是否有一个有效的数据库应用程序。我如何使用它作为我的Rails应用程序的基础?

5 个解决方案

#1


There's no requirement that you use migrations, "funny" or otherwise. Just start creating models from your tables. The Rails authors are smart enough to recognise the need to support "legacy schemas".

没有要求您使用“有趣”或其他方式的迁移。只需从表中开始创建模型即可。 Rails的作者非常聪明,认识到需要支持“遗留模式”。

Note that if your primary keys aren't called id then you'll need to define primary keys (see p316 of "Agile Web Development With Rails 3rd edition"):

请注意,如果主键未被称为id,则需要定义主键(请参阅“使用Rails第3版进行Agile Web开发”的第316页):

class LegacyBook < ActiveRecord::Base
  self.primary_key = "isbn"
end

Similarly, if your foreign key names do not follow the AR conventional default style, you'll need to explicitly define them in your relationship definitions.

同样,如果您的外键名称不遵循AR常规默认样式,则需要在关系定义中明确定义它们。

Out of the box, ActiveRecord doesn't support composite primary keys yet: it kind of assumes something more like 5th Normal Form (PK just a sort of arbitrary number with no meaning in the business domain). There is at least one gem, appropriately named composite_primary_keys (gem install in the usual way) but it may not support AR 2.3 yet (I see v2.2.2 when I gem list --remote composite). There's some discussion on Google Groups.

开箱即用,ActiveRecord还不支持复合主键:它假设更像第五范式(PK只是一种在业务领域没有任何意义的任意数字)。至少有一个gem,适当地命名为composite_primary_keys(以通常的方式安装gem),但它可能还不支持AR 2.3(我看到v2.2.2当我的gem列表--remote composite)。有关Google网上论坛的一些讨论。

#2


This should do it

这应该做到这一点

  def self.up
    execute <<EOF
begin;
  SQL HERE
commit;
EOF
  end

#3


I would dump the current database into a rails schema, and then use that to generate the migration files. This way you can have more control over your database, in a rails fashion. For this, you should:

我会将当前数据库转储到rails模式,然后使用它来生成迁移文件。通过这种方式,您可以以轨道方式更好地控制数据库。为此,你应该:

  1. setup your database configuration file (config/database.yml)
  2. 设置数据库配置文件(config / database.yml)

  3. run the rake db:schema:dump which will create the schema file db/schema.rb
  4. 运行rake db:schema:dump,它将创建模式文件db / schema.rb

  5. create your migration file/files using the content of your schema.rb
  6. 使用schema.rb的内容创建迁移文件/文件

-

 #db/migrate/001_create_database.rb
 class CreateDatabase < ActiveRecord::Migration
   def self.up
     # the content of schema.rb
   end

   def self.down
     # drop all the tables
   end
 end

This way you can take advantage of migrations later on, when developing your app.

这样,您可以在以后开发应用时利用迁移。

#4


Providing your database tables follow the Rails' ActiveRecord naming conventions then you should be good to go. Run the SQL to create the database objects. Then you can generate your Rails models in the normal fashion, but skipping the creation of a migration file.

提供数据库表遵循Rails的ActiveRecord命名约定,那么你应该很高兴。运行SQL以创建数据库对象。然后,您可以以正常方式生成Rails模型,但跳过创建迁移文件。

For example:

script/generate --skip-migration User name:string, age:integer

#5


edit your config/database.yml file like so:

编辑你的config / database.yml文件,如下所示:

development:
  database: [your legacy database name]
  host: localhost
  adapter: postgres

you may have to include username and password keys as well, I usually don't for postgres. You'll want to set up a test database in postgres as well.

您可能还必须包含用户名和密码密钥,我通常不会为postgres。您还需要在postgres中设置测试数据库。

Once you've configured the development database in this way you'll be able to:

以这种方式配置开发数据库后,您将能够:

> rake db:schema:dump 

and create your test database using

并使用创建测试数据库

> rake db:schema:load RAILS_ENV=test 

with the resulting file.

使用生成的文件。

#1


There's no requirement that you use migrations, "funny" or otherwise. Just start creating models from your tables. The Rails authors are smart enough to recognise the need to support "legacy schemas".

没有要求您使用“有趣”或其他方式的迁移。只需从表中开始创建模型即可。 Rails的作者非常聪明,认识到需要支持“遗留模式”。

Note that if your primary keys aren't called id then you'll need to define primary keys (see p316 of "Agile Web Development With Rails 3rd edition"):

请注意,如果主键未被称为id,则需要定义主键(请参阅“使用Rails第3版进行Agile Web开发”的第316页):

class LegacyBook < ActiveRecord::Base
  self.primary_key = "isbn"
end

Similarly, if your foreign key names do not follow the AR conventional default style, you'll need to explicitly define them in your relationship definitions.

同样,如果您的外键名称不遵循AR常规默认样式,则需要在关系定义中明确定义它们。

Out of the box, ActiveRecord doesn't support composite primary keys yet: it kind of assumes something more like 5th Normal Form (PK just a sort of arbitrary number with no meaning in the business domain). There is at least one gem, appropriately named composite_primary_keys (gem install in the usual way) but it may not support AR 2.3 yet (I see v2.2.2 when I gem list --remote composite). There's some discussion on Google Groups.

开箱即用,ActiveRecord还不支持复合主键:它假设更像第五范式(PK只是一种在业务领域没有任何意义的任意数字)。至少有一个gem,适当地命名为composite_primary_keys(以通常的方式安装gem),但它可能还不支持AR 2.3(我看到v2.2.2当我的gem列表--remote composite)。有关Google网上论坛的一些讨论。

#2


This should do it

这应该做到这一点

  def self.up
    execute <<EOF
begin;
  SQL HERE
commit;
EOF
  end

#3


I would dump the current database into a rails schema, and then use that to generate the migration files. This way you can have more control over your database, in a rails fashion. For this, you should:

我会将当前数据库转储到rails模式,然后使用它来生成迁移文件。通过这种方式,您可以以轨道方式更好地控制数据库。为此,你应该:

  1. setup your database configuration file (config/database.yml)
  2. 设置数据库配置文件(config / database.yml)

  3. run the rake db:schema:dump which will create the schema file db/schema.rb
  4. 运行rake db:schema:dump,它将创建模式文件db / schema.rb

  5. create your migration file/files using the content of your schema.rb
  6. 使用schema.rb的内容创建迁移文件/文件

-

 #db/migrate/001_create_database.rb
 class CreateDatabase < ActiveRecord::Migration
   def self.up
     # the content of schema.rb
   end

   def self.down
     # drop all the tables
   end
 end

This way you can take advantage of migrations later on, when developing your app.

这样,您可以在以后开发应用时利用迁移。

#4


Providing your database tables follow the Rails' ActiveRecord naming conventions then you should be good to go. Run the SQL to create the database objects. Then you can generate your Rails models in the normal fashion, but skipping the creation of a migration file.

提供数据库表遵循Rails的ActiveRecord命名约定,那么你应该很高兴。运行SQL以创建数据库对象。然后,您可以以正常方式生成Rails模型,但跳过创建迁移文件。

For example:

script/generate --skip-migration User name:string, age:integer

#5


edit your config/database.yml file like so:

编辑你的config / database.yml文件,如下所示:

development:
  database: [your legacy database name]
  host: localhost
  adapter: postgres

you may have to include username and password keys as well, I usually don't for postgres. You'll want to set up a test database in postgres as well.

您可能还必须包含用户名和密码密钥,我通常不会为postgres。您还需要在postgres中设置测试数据库。

Once you've configured the development database in this way you'll be able to:

以这种方式配置开发数据库后,您将能够:

> rake db:schema:dump 

and create your test database using

并使用创建测试数据库

> rake db:schema:load RAILS_ENV=test 

with the resulting file.

使用生成的文件。