15
SchemaPlus enhanced capabilities for schema definition and querying Ronen Barzel & Michał Łomnicki

Schema plus

Embed Size (px)

DESCRIPTION

SchemaPlus gem - ActiveRecord enhanced capabilities for querying and defining database schemas

Citation preview

Page 1: Schema plus

SchemaPlusenhanced capabilities for schema definition and querying

Ronen Barzel & Michał Łomnicki

Page 2: Schema plus

HistorySimon Harris / harukizaemon

redhillonrails_core (2006, 2009)

foreign_key_migrations => automatic_foreign_key

schema_plus (2011)

Page 3: Schema plus

Goalssolid base

well-tested

well-documented

better name :)

Page 4: Schema plus

Plain ActiveRecordcreate_table :comments do |t| t.text :body t.integer :post_id t.integer :author_idendexecute "ALTER TABLE comments ADD FOREIGN KEY (post_id) \ REFERENCES (posts)"execute "ALTER TABLE comments ADD FOREIGN KEY (author_id) \ REFERENCES (users)"add_index :comments, :post_id

Page 5: Schema plus

With schema_pluscreate_table :comments do |t| t.text :body t.integer :post_id, :index => true t.integer :author_id, :references => :usersend

Page 6: Schema plus

auto-index foreign keys# without auto_indext.integer :post_id, :index => true# with auto_indext.integer :post_id

Page 7: Schema plus

indext.string :area_codet.string :local_number, :index => { :unique => true, :with => :area_code }

Page 8: Schema plus

Other featuresglobal, per table and per statement config

expressional indexes

views

Page 9: Schema plus

SchemaAssociations# without schema_associationsclass Post < ActiveRecord::Base belongs_to :author has_many :commentsend

Page 10: Schema plus

SchemaAssociations# without schema_associationsclass Post < ActiveRecord::Base # associations auto-created from foreign keysend

Page 11: Schema plus

SchemaValidationscreate_table :posts do |t| t.integer :author_id, :null => false, :references => :users t.integer :likes_count t.string :content, :limit => 5_000end

Page 12: Schema plus

SchemaValidations# without schema_validationsclass Post < ActiveRecord::Base validates :author, :presence => true validates :likes_count, :numericality => true validates :content, :length => { :maximum => 5_000 }end

Page 13: Schema plus

SchemaValidations# with schema_validationsclass Post < ActiveRecord::Baseend

Page 14: Schema plus

Schema FamilyScary?

...but that's next step for active record pattern

Page 15: Schema plus

Questionshttp://github.com/lomba