目录结构
database.yml
1 development: 2 adapter: sqlite3 3 database: db/test.db 4 pool: 5 5 timeout: 5000
001_schema.rb
1 require 'active_record' 2 class Schema <ActiveRecord::Migration 3 def self.up 4 create_table :customers, force: true do |t| 5 t.string :name 6 t.string :address 7 8 t.timestamps 9 end 10 end 11 12 def self.down 13 drop_table :customers 14 end 15 end
customer.rb
1 class Customer <ActiveRecord::Base 2 3 4 end
ar.rb
1 require 'rubygems' 2 require 'active_record' 3 require 'yaml' 4 require 'logger' 5 6 ActiveRecord::Base.logger = Logger.new(STDOUT) 7 dbconfig = YAML::load(IO.read('config/database.yml')) 8 ActiveRecord::Base.establish_connection(dbconfig['development']) 9 10 load 'models/customer.rb'
Gemfile
1 source 'https://gems.ruby-china.org' 2 gem 'activerecord' 3 gem 'sqlite3' 4 gem 'rake'
Rakefile
1 load 'ar.rb' 2 require 'active_record' 3 4 task :default => :migrate 5 6 desc 'Run migrations' 7 task :migrate do 8 ActiveRecord::Migrator.migrate('db/migrate', ENV['VERSION'] ? ENV['VERSION'].to_i : nil) 9 end
使用说明
在程序目录先执行 bundle install
1 在ruby目录执行 命令:
1 rudy-Pc :: ~/ruby » rake 2 D, [2016-06-15T14:36:24.712037 #6726] DEBUG -- : (4.4ms) CREATE TABLE "schema_migrations" ("version" varchar NOT NULL) 3 D, [2016-06-15T14:36:24.712258 #6726] DEBUG -- : (0.1ms) select sqlite_version(*) 4 D, [2016-06-15T14:36:24.716823 #6726] DEBUG -- : (4.2ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version") 5 D, [2016-06-15T14:36:24.717531 #6726] DEBUG -- : ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations" 6 I, [2016-06-15T14:36:24.720448 #6726] INFO -- : Migrating to Schema (1) 7 D, [2016-06-15T14:36:24.720794 #6726] DEBUG -- : (0.0ms) begin transaction 8 == 1 Schema: migrating ======================================================== 9 -- create_table(:customers, {:force=>true}) 10 DEPRECATION WARNING: `#timestamps` was called without specifying an option for `null`. In Rails 5, this behavior will change to `null: false`. You should manually specify `null: true` to prevent the behavior of your existing migrations from changing. (called from block in up at /home/rudy/ruby/db/migrate/001_schema.rb:8) 11 D, [2016-06-15T14:36:24.722126 #6726] DEBUG -- : (0.2ms) CREATE TABLE "customers" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar, "address" varchar, "created_at" datetime, "updated_at" datetime) 12 -> 0.0012s 13 == 1 Schema: migrated (0.0013s) =============================================== 14 15 D, [2016-06-15T14:36:24.726539 #6726] DEBUG -- : SQL (0.1ms) INSERT INTO "schema_migrations" ("version") VALUES (?) [["version", "1"]] 16 D, [2016-06-15T14:36:24.731421 #6726] DEBUG -- : (4.7ms) commit transaction
2 在ruby目录创建 active_record.rb
1 load 'ar.rb' 2 1.upto(10) do |x| 3 customer = Customer.new 4 customer.name ="fak#{x}" 5 customer.address = 'beijing' 6 customer.save 7 end
我在rubymine中直接右键单击文件,选择 run active_record
/home/rudy/.rbenv/versions/2.2.3/bin/ruby -e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) /home/rudy/ruby/active_record.rb D, [2016-06-15T15:03:22.677823 #7646] DEBUG -- : (0.1ms) begin transaction D, [2016-06-15T15:03:22.684157 #7646] DEBUG -- : SQL (0.2ms) INSERT INTO "customers" ("name", "address", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["name", "fak1"], ["address", "beijing"], ["created_at", "2016-06-15 07:03:22.682053"], ["updated_at", "2016-06-15 07:03:22.682053"]] D, [2016-06-15T15:03:22.691053 #7646] DEBUG -- : (6.6ms) commit transaction D, [2016-06-15T15:03:22.691285 #7646] DEBUG -- : (0.0ms) begin transaction D, [2016-06-15T15:03:22.691801 #7646] DEBUG -- : SQL (0.1ms) INSERT INTO "customers" ("name", "address", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["name", "fak2"], ["address", "beijing"], ["created_at", "2016-06-15 07:03:22.691356"], ["updated_at", "2016-06-15 07:03:22.691356"]] D, [2016-06-15T15:03:22.698569 #7646] DEBUG -- : (6.6ms) commit transaction D, [2016-06-15T15:03:22.698767 #7646] DEBUG -- : (0.0ms) begin transaction D, [2016-06-15T15:03:22.699331 #7646] DEBUG -- : SQL (0.1ms) INSERT INTO "customers" ("name", "address", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["name", "fak3"], ["address", "beijing"], ["created_at", "2016-06-15 07:03:22.698849"], ["updated_at", "2016-06-15 07:03:22.698849"]] D, [2016-06-15T15:03:22.702730 #7646] DEBUG -- : (3.2ms) commit transaction D, [2016-06-15T15:03:22.702950 #7646] DEBUG -- : (0.1ms) begin transaction D, [2016-06-15T15:03:22.703435 #7646] DEBUG -- : SQL (0.1ms) INSERT INTO "customers" ("name", "address", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["name", "fak4"], ["address", "beijing"], ["created_at", "2016-06-15 07:03:22.703015"], ["updated_at", "2016-06-15 07:03:22.703015"]] D, [2016-06-15T15:03:22.706785 #7646] DEBUG -- : (3.2ms) commit transaction D, [2016-06-15T15:03:22.706989 #7646] DEBUG -- : (0.0ms) begin transaction D, [2016-06-15T15:03:22.707486 #7646] DEBUG -- : SQL (0.1ms) INSERT INTO "customers" ("name", "address", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["name", "fak5"], ["address", "beijing"], ["created_at", "2016-06-15 07:03:22.707050"], ["updated_at", "2016-06-15 07:03:22.707050"]] D, [2016-06-15T15:03:22.710844 #7646] DEBUG -- : (3.2ms) commit transaction D, [2016-06-15T15:03:22.711062 #7646] DEBUG -- : (0.0ms) begin transaction D, [2016-06-15T15:03:22.711585 #7646] DEBUG -- : SQL (0.1ms) INSERT INTO "customers" ("name", "address", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["name", "fak6"], ["address", "beijing"], ["created_at", "2016-06-15 07:03:22.711146"], ["updated_at", "2016-06-15 07:03:22.711146"]] D, [2016-06-15T15:03:22.714980 #7646] DEBUG -- : (3.2ms) commit transaction D, [2016-06-15T15:03:22.715185 #7646] DEBUG -- : (0.0ms) begin transaction D, [2016-06-15T15:03:22.715699 #7646] DEBUG -- : SQL (0.1ms) INSERT INTO "customers" ("name", "address", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["name", "fak7"], ["address", "beijing"], ["created_at", "2016-06-15 07:03:22.715263"], ["updated_at", "2016-06-15 07:03:22.715263"]] D, [2016-06-15T15:03:22.718966 #7646] DEBUG -- : (3.1ms) commit transaction D, [2016-06-15T15:03:22.719175 #7646] DEBUG -- : (0.0ms) begin transaction D, [2016-06-15T15:03:22.719661 #7646] DEBUG -- : SQL (0.1ms) INSERT INTO "customers" ("name", "address", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["name", "fak8"], ["address", "beijing"], ["created_at", "2016-06-15 07:03:22.719240"], ["updated_at", "2016-06-15 07:03:22.719240"]] D, [2016-06-15T15:03:22.722971 #7646] DEBUG -- : (3.1ms) commit transaction D, [2016-06-15T15:03:22.723230 #7646] DEBUG -- : (0.1ms) begin transaction D, [2016-06-15T15:03:22.723924 #7646] DEBUG -- : SQL (0.2ms) INSERT INTO "customers" ("name", "address", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["name", "fak9"], ["address", "beijing"], ["created_at", "2016-06-15 07:03:22.723325"], ["updated_at", "2016-06-15 07:03:22.723325"]] D, [2016-06-15T15:03:22.727346 #7646] DEBUG -- : (3.2ms) commit transaction D, [2016-06-15T15:03:22.727552 #7646] DEBUG -- : (0.0ms) begin transaction D, [2016-06-15T15:03:22.728065 #7646] DEBUG -- : SQL (0.1ms) INSERT INTO "customers" ("name", "address", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["name", "fak10"], ["address", "beijing"], ["created_at", "2016-06-15 07:03:22.727617"], ["updated_at", "2016-06-15 07:03:22.727617"]] D, [2016-06-15T15:03:22.731302 #7646] DEBUG -- : (3.0ms) commit transaction Process finished with exit code 0