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

偶然一次:

运行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 在一个对象实例化后调用方法。







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

时间: 2024-08-26 13:50:19

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

Spring @ResponseBody 返回乱码 的优雅解决办法

版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] 返回的结果中,中文全部被问号(?)代替的解决办法: *-servlet.xml的部分配置如下: [html] view plain copy <bean id="utf8Charset" class="java.nio.charset.Charset" factory-method="forName"> <constructor-arg value=&quo

Spring注入service为null另类解决办法 工具类 一般类 静态 非controller

系统为SpringMVC框架,在开发的过程中有一些工具类需要调用下由spring管理的service层.但是一进注入不进来,报null异常: 在尝试了网上的一系列方法后,还是没有解决.网上的解决方法主要有以下几种: 1.将工具类申明为spring组件,如@controller @compent 等,在spring自动扫描包设置中将工具类所在的包加进来:  无效 2.new一个service:  无效 而且不符合spring管理: 山穷水尽后,找到了一个另类的解决办法,代码原理还不太清楚,只是大概

Spring入门hello world常见问题及解决办法

1.关于springDispatcherServlet,首先安装springsource-tool-suite工具,这个方便进行web.xml配置时,使用快捷键 alt+/. 教程我写在了新浪博客上面,链接:http://blog.sina.com.cn/s/blog_648e93100102x31t.html 可以参考一下.注意根据自己的IDE版本选择卡到的zip下载. 2.jar包问题: spring版本与JDK版本存在匹配关系.一般来说,spring3.x匹配JDK 1.7,spring

Spring注解无法注入Session监听器解决办法

public class DefaultSessionListener implements HttpSessionListener { public void sessionCreated(HttpSessionEvent arg0) { ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(arg0.getSession().getServletContext());   IM

SpringBoot2.0及Spring 5.0 WebMvcConfigurerAdapter被废弃解决办法

1 直接实现WebMvcConfigurer @Configuration public class WebMvcConfg implements WebMvcConfigurer { @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/index").setViewName("index"); } } 2 直

Learn Rails5.2- Scaffolding and REST

用generator建立一个手脚架 Representational State Transfer (REST).  具像的状态转存. https://en.wikipedia.org/wiki/Representational_state_transfer RESTful风格的简单的理解: 如何根据一个简单的法则来存取数据.法则包括CRUD的原理和对HTTP的明确定义. Learn Rails5.2- Scaffolding and REST 原文地址:https://www.cnblogs.

maven 工程启动找不到 Spring ContextLoaderListener 的解决办法

用maven 工程搭建项目,在搭建好之后,运行时却抛出了这样的错误:  Error configuring application listener of class org.springframework.web.context.ContextLoaderListener  java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener, 这说明根本没有找到我们在 web.xml 里

Jboss AS 7 部署web应用程序时无法初始化spring的bean的解决办法

Jboss AS 7 在部署web应用程序的时候无法初始化spring的bean(在tomcat下边不会出现这个问题) 原因是web应用程序没有导入jboss对spring的支持的jar包 解决方法: 下载 snowdrop-vfs.jar(该文件在jboss-spring-subsystem-as7-nodeps-2.0.0.Final.zip包的module-deployer\org\jboss\snowdrop\main下,jboss-spring-subsystem-as7-nodeps

spring 以Ant Build方式运行build.xml文件,报warning: &#39;includeantruntime&#39; was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds 的解决办法

Buildfile: F:\experience\spring_pdf\sourcecode\example1\build.xml compile: [javac] F:\experience\spring_pdf\sourcecode\example1\build.xml:25: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable