加载一个结构。sql通过rake进入rails数据库

时间:2023-01-21 00:16:49

rake db:schema:load will load a schema.rb file into a rails database. Is there a way to load a structure.sql file into the database through rake or do I just need to do this manually?

rake db:schema:load将加载一个schema。将rb文件放入rails数据库。是否有加载结构的方法?sql文件通过rake进入数据库,还是我需要手动完成?

6 个解决方案

#1


61  

Use rake db:structure:load, which will load db/structure.sql.

使用rake db:structure:load,它将加载db/structure.sql。

[Update]

(更新)

If you want to load another file, you can specify its path via

如果要加载另一个文件,可以通过以下方式指定其路径

  • SCHEMA environment variable (Rails 5.0 or later)
  • 模式环境变量(Rails 5.0或更高版本)
  • DB_STRUCTURE environment variable (Rails 4.x)
  • DB_STRUCTURE环境变量(Rails 4.x)

For example, run

例如,运行

rake db:structure:load SCHEMA=db/another.sql

or

rake db:structure:load DB_STRUCTURE=db/another.sql

#2


24  

Just use

只使用

rake db:setup

which will use either schema.rb or structure.sql depending on your configuration.

它将使用两种模式。rb或结构。sql取决于您的配置。

#3


4  

Use the database's own SQL load mechanism.

使用数据库自己的SQL加载机制。

For Postgres this should work (at least if the database exists and you don't need a password):

对于Postgres,这应该是可行的(至少如果数据库存在并且不需要密码):

psql -d databaseName < db/structure.sql

On Heroku where rake db:setup doesn't work as you can't create a database like that you can do this:

在Heroku上的rake db:setup不工作,因为你不能创建一个这样的数据库,你可以这样做:

heroku pg:psql < db/structure.sql

#4


2  

Once you load your schema, you can try:

一旦加载了模式,您可以尝试:

rails dbconsole < structure.sql

OR

rails db < structure.sql

#5


2  

Sometimes you want to load a file that is not the default db/structure.sql.

有时,您希望加载的文件不是默认的db/structure.sql。

For rails 4.2 and earlier, use

对于rails 4.2和更早的版本,请使用

DB_STRUCTURE=some_file.sql rake db:structure:load

DB_STRUCTURE = some_file。sql rake db:结构:负载

As of rails 5 use

使用rails 5

SCHEMA=some_file.sql rake db:structure:load

模式= some_file。sql rake db:结构:负载

#6


1  

Make your seeds.rb file like:

让你的种子。rb文件:

unless Rails.env.production?
  connection = ActiveRecord::Base.connection

  sql = File.read('db/structure.sql')
  statements = sql.split(/;$/)
  statements.pop  # the last empty statement

  ActiveRecord::Base.transaction do
    statements.each do |statement|
      connection.execute(statement)
    end
  end
end

Source.

源。

#1


61  

Use rake db:structure:load, which will load db/structure.sql.

使用rake db:structure:load,它将加载db/structure.sql。

[Update]

(更新)

If you want to load another file, you can specify its path via

如果要加载另一个文件,可以通过以下方式指定其路径

  • SCHEMA environment variable (Rails 5.0 or later)
  • 模式环境变量(Rails 5.0或更高版本)
  • DB_STRUCTURE environment variable (Rails 4.x)
  • DB_STRUCTURE环境变量(Rails 4.x)

For example, run

例如,运行

rake db:structure:load SCHEMA=db/another.sql

or

rake db:structure:load DB_STRUCTURE=db/another.sql

#2


24  

Just use

只使用

rake db:setup

which will use either schema.rb or structure.sql depending on your configuration.

它将使用两种模式。rb或结构。sql取决于您的配置。

#3


4  

Use the database's own SQL load mechanism.

使用数据库自己的SQL加载机制。

For Postgres this should work (at least if the database exists and you don't need a password):

对于Postgres,这应该是可行的(至少如果数据库存在并且不需要密码):

psql -d databaseName < db/structure.sql

On Heroku where rake db:setup doesn't work as you can't create a database like that you can do this:

在Heroku上的rake db:setup不工作,因为你不能创建一个这样的数据库,你可以这样做:

heroku pg:psql < db/structure.sql

#4


2  

Once you load your schema, you can try:

一旦加载了模式,您可以尝试:

rails dbconsole < structure.sql

OR

rails db < structure.sql

#5


2  

Sometimes you want to load a file that is not the default db/structure.sql.

有时,您希望加载的文件不是默认的db/structure.sql。

For rails 4.2 and earlier, use

对于rails 4.2和更早的版本,请使用

DB_STRUCTURE=some_file.sql rake db:structure:load

DB_STRUCTURE = some_file。sql rake db:结构:负载

As of rails 5 use

使用rails 5

SCHEMA=some_file.sql rake db:structure:load

模式= some_file。sql rake db:结构:负载

#6


1  

Make your seeds.rb file like:

让你的种子。rb文件:

unless Rails.env.production?
  connection = ActiveRecord::Base.connection

  sql = File.read('db/structure.sql')
  statements = sql.split(/;$/)
  statements.pop  # the last empty statement

  ActiveRecord::Base.transaction do
    statements.each do |statement|
      connection.execute(statement)
    end
  end
end

Source.

源。