Ruby 如何从迁移文件生成架构文件?

Ruby 如何从迁移文件生成架构文件?,ruby,activerecord,Ruby,Activerecord,我有一个简单的ruby项目,它使用ActiveRecord作为ORM(没有Rails)。我为所有表创建了一些迁移文件,现在我正在搜索如何在Rails中使用它们。下面是一个例子: class CreateCategoriesTable < ActiveRecord::Migration def up create_table :categories do |t| t.integer :id, null: false t.string :name, null

我有一个简单的ruby项目,它使用ActiveRecord作为ORM(没有Rails)。我为所有表创建了一些迁移文件,现在我正在搜索如何在Rails中使用它们。下面是一个例子:

class CreateCategoriesTable < ActiveRecord::Migration
  def up
    create_table :categories do |t|
      t.integer :id, null: false
      t.string :name, null: false
    end
  end

  def down
    drop_table :categories
  end
end
但是,如果我有db(它是文件中的sqlite db),此迁移会导致异常(表已经存在)。因此,我如何运行所有迁移(或者如何生成架构文件,然后如何运行它?)只有在需要时才运行迁移,例如,第一次,然后只有在某些内容发生更改时才运行迁移?

可能对您有用

迁移的命名方案实际上相当重要。在名为*schema\u migrations*的表中跟踪已运行的迁移。以下是postgres的一个例子:

 Table "public.schema_migrations"
 Column  |          Type          | Modifiers 
---------+------------------------+-----------
 version | character varying(255) | not null
 Indexes:
 "unique_schema_migrations" UNIQUE, btree (version)

development=# select * from schema_migrations;
    version     
----------------
20130206231627
(1 row)
此外,schema.rb可以跟踪其当前版本

ActiveRecord::Schema.define(:version => 20130206231627) do
  ...
end
可能对你有用

迁移的命名方案实际上相当重要。在名为*schema\u migrations*的表中跟踪已运行的迁移。以下是postgres的一个例子:

 Table "public.schema_migrations"
 Column  |          Type          | Modifiers 
---------+------------------------+-----------
 version | character varying(255) | not null
 Indexes:
 "unique_schema_migrations" UNIQUE, btree (version)

development=# select * from schema_migrations;
    version     
----------------
20130206231627
(1 row)
此外,schema.rb可以跟踪其当前版本

ActiveRecord::Schema.define(:version => 20130206231627) do
  ...
end