Category Archives: 数据库

使用SQLite in memory提高测试代码效率

对于需要进行持续集成的大型项目来说,运行测试代码的花费就显得尤为重要,Chris Roos在这篇帖子里介绍了一种通过使用SQLite数据库来 提高测试代码运行效率的方法。 首先你需要安装SQLite3以及sqlite3-ruby gem,然后修改database.yml: test: adapter: sqlite3 database: “:memory:” 但是这样有一个问题,由于SQLite数据库保存在内存中,因此必须在每次执行单元测试之前首先生成数据库的大纲,不过不用担心,Chris Roos已经提供了一段现成的代码,将它加到你的environment.rb就可以了: def in_memory_database?   ENV["RAILS_ENV"] == “test” and   ActiveRecord::Base.connection.class == ActiveRecord::ConnectionAdapters::SQLite3Adapter and   Rails::Configuration.new.database_configuration['test']['database'] == ‘:memory:’ end if in_memory_database?   puts “creating sqlite in memory database”   load “#{RAILS_ROOT}/db/schema.rb” # use db agnostic schema by default   # ActiveRecord::Migrator.up(‘db/migrate’) # use migrations end 不过要注意,以上代码最好加在”# Include your application configuration below”这一行之后,因为说不准后面的配置有可能就会用到数据库。 [...]

Also posted in 性能, 测试 | Leave a comment

Rails插件: Annotate models

简介 Annotate models能够以注释的方式在Model顶部自动生成此Model的大纲(schema)信息,并可以在Model的大纲发生变化后自动更新。 对于拥有许多Model的大型项目来说,在Model相关文件中维护此model的大纲信息就显得尤为重要,你不需要去db/migrate目录下搜寻此 Model相关的Migration文件,也不需要去mysql中敲describe XXX命令,就可以迅速了解此Model对应的表拥有那些字段。 作者:Dave Thomas SVN仓库:http://repo.pragprog.com/svn/Public/plugins/annotate_models 许可:Ruby License 安装 $ ./script/plugin install http://repo.pragprog.com/svn/Public/plugins/annotate_models 使用 $ rake annotate_models 这将会在所有Model文件及Fixture文件的头部添加此Model的大纲信息,添加的大纲信息看起来是下面这个样子: # == Schema Information # Schema version: 1 # # Table name: users # # id :integer(11) not null, primary key # login :string(255) # email :string(255) # crypted_password :string(40) # salt :string(40) # [...]

Also posted in 插件 | Leave a comment

has_many_polymorphs:简化多对多映射

简介 假设我们要开发一个教学系统,那么老师和学生必然是系统不可缺少的两个model,要建立他们之间的映射,我们的数据库可能看起来是下面这个样子: 但是随着系统的开发,我们发现还需要将课程管理也纳入进来,于是数据库就变成了这个样子: 但是这样的数据库模型明显的不够DRY,因为teachers_students和teachers_courses两个表不但字段相似,完成的功能也是相同的,那么可不可以将它们合并呢? 答案是:可以。 使用evan weaver的has_many_polymorphs插件可以很轻松的完成这个任务。 安装 $ script/plugin install -x svn://rubyforge.org/var/svn/fauna/has_many_polymorphs/trunk 使用 对于上例,应用has_many_polymorphs后,我们的数据库看起来是这个样子: 修改app/models/teacher.rb class Teaching < ActiveRecord::Base   belongs_to :teacher   belongs_to :teachable, polymorphic => trues end class Teacher < ActiveRecord::Base   has_many_polymorphs :teachables,      :from => [:students, :courses],      :through => :teachings end 这就搞定了,不需要修改student.rb和course.rb,现在你可以这样使用teacher了: teacher.teachables # 老师教授的所有课程及学生 teacher.students # 老师教授的所有学生 teacher.courses # 老师教授的所有课程 teacher.students[0].teacher [...]

Also posted in 插件 | Leave a comment

Sequel:轻量级的Ruby ORM库

简介 Sequel是一个轻量级的Ruby ORM程序库,支持线程安全,连接池,同时还提供了一个简洁的DSL来操作数据库。 如何你只是想写一个简单的纯Ruby的数据库应用,而不想同庞大的ActiveRecord发生关系,那么Sequel为你提供了另外一个选择。 Sequel目前支持SQLite3, PostgreSQL, MySQL, ODBC以及DBI。 作者: ciconia 项目主页:http://code.google.com/p/ruby-sequel/ 文档: http://sequel.rubyforge.org/ 最新版本:0.1.8 安装 gem install sequel 使用 1. 打开数据库 DB = Sequel(‘sqlite:///my_blog.db’) DB = Sequel(‘postgres://user:password@localhost/my_db’) DB = Sequel(‘mysql://user:password@localhost/my_db’) 2.创建表 DB.create_table :items do # Create a new table   column :name, :text   column :price, :float end 3. 获取记录 items = DB[:items] 4. 添加记录 items << [...]

Also posted in 插件 | Leave a comment

在Rails中创建多对多映射

AWDwR 有一个小节讲到了如何创建多对多映射,不过使用db:migrate使得这个过程变得更为简单,这是来自thembid.com 的一篇指南,原文在这里 。 为了让例子更容易理解一些,我修改了原文的两个表名,并使用了scaffold_resource替代了model,同时在最后给出了两个截图。这里我使用book和author来作为演示的resource,一本书可以有多个作者,一个作者也可能写了多本书,因此,作者和书属于多对多的关系。 首先,让我们创建resource: ruby script/generate scaffold_resource book title:string ruby script/generate scaffold_resource authors name:string 然后,我们为创建映射关系表: ruby script/generate migration create_authors_books 编辑db/migrate/003_create_authors_books.rb: class CreateAuthorsBooks < ActiveRecord::Migration   def self.up     create_table :authors_books, :id => false do |t|       t.column :author_id, :integer       t.column :book_id, :integer     end     add_index :authors_books, [:author_id, :book_id]     add_index :authors_books, [:book_id]   end   def self.down     remove_index :authors_books, [:book_id] [...]

Also posted in 指南 | Leave a comment

db:migrate简明指南

AWDwR 有一个小节讲到了如何创建多对多映射,不过使用db:migrate使得这个过程变得更为简单,这是来自thembid.com 的一篇指南,原文在这里 。 为了让例子更容易理解一些,我修改了原文的两个表名,并使用了scaffold_resource替代了model,同时在最后给出了两个截图。这里我使用book和author来作为演示的resource,一本书可以有多个作者,一个作者也可能写了多本书,因此,作者和书属于多对多的关系。 首先,让我们创建resource: ruby script/generate scaffold_resource book title:string ruby script/generate scaffold_resource authors name:string 然后,我们为创建映射关系表: ruby script/generate migration create_authors_books 编辑db/migrate/003_create_authors_books.rb: class CreateAuthorsBooks < ActiveRecord::Migration   def self.up     create_table :authors_books, :id => false do |t|       t.column :author_id, :integer       t.column :book_id, :integer     end     add_index :authors_books, [:author_id, :book_id]     add_index :authors_books, [:book_id]   end   def self.down     remove_index :authors_books, [:book_id] [...]

Also posted in 指南 | 3 Comments