ruby 我可以改变Rails创建迁移的方式吗

ulydmbyx  于 2023-03-22  发布在  Ruby
关注(0)|答案(1)|浏览(104)

TL;DR;

在过去(几年前),我在Ruby中使用自定义生成器来改变运行rails g model Users时的创建方式。我今天的问题是,你是否可以改变Rails生成迁移文件的方式。

我想做的

每当我通过Rails的生成函数生成一个迁移时,我几乎总是要进行修改;如果不添加def updef down用于恢复目的;我几乎总是需要更改t.timestamps的位置。我喜欢将时间戳放在表的开头,这样如果我决定向表中添加额外的列(让我们面对它,这似乎总是随着项目的增长而发生),时间戳就不会卡在表的中间。

示例

为了便于举例,假设我运行rails g scaffold Notes body:text
这将生成许多文件和以下迁移文档:

class CreateNotes < ActiveRecord::Migration[7.0]
  def change
    create_table :notes do |t|
      t.text    :body

      t.timestamps
    end
  end
end

在运行db:migrate之后使用以下模式:

create_table "notes", force: :cascade do |t|
    t.text "body"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
end

现在,如果稍后我们想添加一个笔记标题的功能。所以我们运行rails g migration AddTitleToNotes title:string,它给予了我们这样的迁移:

class AddTitleToNotes < ActiveRecord::Migration[7.0]
  def change
    add_column :notes, :title, :string
  end
end

而且,当这次运行db:migrate时,我们的模式被更新为如下所示:

create_table "notes", force: :cascade do |t|
    t.text "body"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "title"
end

现在,这个小例子可能看起来不太糟糕,但是当你开始一个7列的表,然后添加3列后(没有理由我想出了这些数字,哈哈),你会得到第9列和第10列的时间戳(因为id的第一列总是在那里)和第11-13列的新列。
这可能只是一个“我”的问题,但我喜欢保持我的模式易于阅读,数据库也是如此。因此,在这种情况下,我会进入第一个迁移文件并将其更改为:

class CreateNotes < ActiveRecord::Migration[7.0]
  def change
    create_table :notes do |t|
      t.timestamps

      t.text    :body
    end
  end
end

这样,生成的schema将如下所示:

create_table "notes", force: :cascade do |t|
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.text "body"
end

在添加了'title'之后,模式看起来像这样:

create_table "notes", force: :cascade do |t|
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.text "body"
    t.string "title"
end

问题

所以,我的问题是,我能改变Rails创建迁移文件的方式,总是把t.timestamps放在迁移文件的开头,紧接着create_table子句吗?

保存我理智的额外问题

我是否也可以改变Rails更新模式的方式,让它停止添加每次运行新迁移时在模式顶部注解掉的“helper”文本?(同样,这可能是我的问题,但我喜欢干净的模式,我能说什么呢?)
x1米11米1x

# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# This file is the source Rails uses to define your schema when running `bin/rails
# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to
# be faster and is potentially less error prone than running all of your
# migrations from scratch. Old migrations may fail to apply correctly if those
# migrations use external dependencies or application code.
#
# It's strongly recommended that you check this file into your version control system.
t30tvxxf

t30tvxxf1#

这是未经测试的,但从周围的Rails源代码和测试戳…
1.为database.yml添加一个migrations_path
1.将默认的create table template复制到该路径中。
1.根据您的喜好编辑模板。
例如:

default: &default
          adapter: sqlite3
          pool: 5
          timeout: 5000
        development:
          <<: *default
          database: storage/development.sqlite3
          migrations_paths:
            - db/migration_templates

然而,表中列的顺序(几乎)完全无关紧要。未来的模式更改将导致“无序”表,或者您可能必须使用现有的生产数据库。
我建议你去适应它,灵活性是一项非常有用的技能。

相关问题