Can ActiveRecord create tables outside of a migration?

后端 未结 2 1390
傲寒
傲寒 2020-12-28 19:25

I am working on a non Rails web app, so no migrations script by default.

The Sequel ORM lets me create tables easily in a script:

#!/usr/bin/env rub         


        
2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-28 20:01

    My current understanding is no, all modifications data or schema have to be done through a migration. I have a complete rakefile on github which can be used to perform the migrations outside of Rails.

    Alternatively if it is just an initialisation script the following could be used.

    ActiveRecord::Base.establish_connection(
       :adapter   => 'sqlite3',
       :database  => './lesson1_AR.db'
    )
    
    ActiveRecord::Migration.class_eval do
      create_table :posts do |t|
            t.string  :title
            t.text :body
       end
    
       create_table :people do |t|
          t.string :first_name
          t.string :last_name
          t.string :short_name
       end
    
       create_table :tags do |t|
          t.string :tags
       end 
    end
    

提交回复
热议问题