码迷,mamicode.com
首页 > 编程语言 > 详细

Learn Rails5.2- ActiveRecord: Migration , spring的使用(不兼容的解决办法)

时间:2018-06-17 13:29:24      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:记录   启动   ali   app   datetime   object   help   目录   cpu   

偶然一次: 

运行rails generate停止不动,网上查找答案,可能是bundle update 之后 spring 版本变化了,和正在运行的 spring 实例不兼容。

 

Spring导致的同样的原因: 

rails g migration后,窗口显示了生成迁移文件,但文件树结构上没有显示这个文件。

rails console停止不动,打不开控制台。 

 

解决办法:

关闭spring, spring stop 

??,如果谷歌上查找问题,中文描述问题找不到好的结果,改用英文描述问题,然后搜索。 

 

 


 

https://github.com/rails/spring

 

Spring Application Preloader 

 

一个预加载程序,通过保持你的程序在后端运行来加速开发。所以当你要跑一个测试,迁移时,你无需每次都boot it。

这是rails4.1带来的功能。

bin/spring status 看状态。

bin/spring stop 停止使用,遇到不兼容时候。

 


 

 

Features 

 

  •  完全自动化,无需明确start, stop后端进程
  •  每次运行时,reload你的app
  •  当configs/initializers/gem dependencies变化时会重新开始restarts 你的app

 

 

 


 

 

Compatibility兼容

 

文档ruby2.5未显示兼容,rails5.2未显示兼容。

rails5.2在rails new后会默认安装Spring gem

 

推荐命令都在bin/目录下执行。 


 

Usage 

 

新键一个rails,运行rails g scaffold post name:string然后运行一个测试:

$ time bin/rake test test/controllers/posts_controller_test.rb

Running via Spring preloader in process 75673
Run options: --seed 64649
# Running:
.......
Finished in 1.798020s, 3.8932 runs/s, 5.0055 assertions/s.
7 runs, 9 assertions, 0 failures, 0 errors, 0 skips
bin/rake test test/controllers/posts_controller_test.rb 

0.13s user 0.02s system 4% cpu 3.091 total

 

再测的话速度就会加快: 

0.13s user 0.02s system 26% cpu 0.566 total 

这是因为当修改程序文件或测试文件,变化会被pick up,在下次运行时后端进程无需再启动。

这个工作效果和代码reloading一样,让你在开发时,刷新浏览器可以立即看到变化。

不过当configs/initializers/gem dependencies变化时会完全restarts 你的app
 

当terminal关闭时,Spring会自动关闭。

如果遇到兼容问题,可以手动关闭bin/spring stop 

 

可以在代码中检测,spring是否开启:

> if defined?(Spring)
>   puts "dd"
> end
输出dd

 


 

 

Removal 

 

  • Unspring 你的bin/executables: bin/spring binstub --remove --all
  • 或者取代gem spring

 

 

 


 

 

Deployment

 

在产品环境下,不能安装Spring.

为了防止被安装,使用--without development test参数给bundle install命令

$ bundle install --without development test

??个人理解: 不安装development, test环境下的gem.

 

 


 


问题定位Troubleshooting:

在单独的窗口运行:spring server ,会产生日志并输出。


 

 


 

Migrations  

如果手动删除数据库,再运行rails db:migrate会重新迁移。

$ rm db/development.sqlite3
$ rails db:migrate

rails db:rollback会回滚一次

rails db:version会看当前的版本,每次操作都有唯一的版本号。版本号是timestamp。

 

$ls db/migrate/ 可以列出版本列表。 

 

可以使用指定的迁移,数字0代表开始,所有表格连内部数据都被删除了。

$ rails db:migrate VERSION=0 


config/database.yml可以看到不同环境的数据库配置。

开发环境使用sqlite3
生产环境需要使用MySQL或者PostgreSQL

进入数据库:

$ sqlite3 db/development.sqlite3
SQLite version 3.8.5 2014-08-15 22:37:57
Enter ".help" for usage hints.
sqlite> .tables
products schema_migrations
sqlite> .schema products
CREATE TABLE "products" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar, "price" decimal(7,2), "weight" integer, "in_stock" boolean, "expiration_date" date, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL, "height" integer);

解析:

表格schema_migrations被用于迁移的版本管理。这个表格在第一次迁移时被Rails执行。 

 


Creating Index
https://en.wikipedia.org/wiki/Database_index 
Database index 是一个数据结构,它可以加速从数据库中取回需要的数据。
代价是额外的存储空间来保存索引的数据结构。	 
add_index :products, :name


自动增加的Fields
  • id:integer:给记录增加唯一的??,在SQL中,相当于NOT NULL AUTO_INCREMENT
  • created_at和updated_at:datetime.


Callbacks 

http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html
一个ActiveRecord object的生命周期使用hooks。
before_validation/after_validation
before_save/before_create等

after_initialize 在一个对象实例化后调用方法。






Learn Rails5.2- ActiveRecord: Migration , spring的使用(不兼容的解决办法)

标签:记录   启动   ali   app   datetime   object   help   目录   cpu   

原文地址:https://www.cnblogs.com/chentianwei/p/9192548.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!