SlideShare a Scribd company logo
1 of 50
Download to read offline
ActiveRecord, Standalone
Migrations, and aRel
Alex Tironati
Overview
● Part 1 - ActiveRecord/Standalone Migrations
○ The Problem
○ Motivation
○ The Solution
○ Implementation
● Part 2 - Arel vs. AR
○ ActiveRecord Basics
○ ARel tables
○ Select and Join queries
○ Using a QueryBuilder
○ Bringing it home
Part 1
Standalone Database Migrations
in non-Rails Projects
The Problem
The Underlying Problem
● We have no process in place for managing
the database schemas for our Java projects
● This is the best
we could do
before:
Is that good enough?
Motivation
● We should be able to keep track of the schema
changes over time
● We should have a standard way to change the schema
across all databases
● We shouldn’t have to go through SQLPro to make those
changes
● We should have an easy way to recreate the current
schema from scratch
● We should be able to run those changes automatically,
after they are defined
What magical wonder-framework of Poseidon does all
this shit?
?
Oh that’s right
Implementation
● Add a .env file
● Make a project-level db folder
● Create a Gemfile and add:
○ gem 'active_record_migrations'
○ gem 'mysql2'
○ gem 'dotenv'
● Create a migration file for each table in your database:
○ rake db:new_migration name='create_note_tables'
○ Remember to include index definitions
○ Make sure all of the fields match your current schema exactly
○ run ‘rake db:migrate’ to test a lot locally
● When the migrations are written, go to new schema_migrations table
Implementation
● Create schema_migrations on staging and prod instances
● Duplicate entries (timestamps) from local schema_migrations table to
remote instances so that they match (the migrations are in order)
● run migrations on remote instance:
○ rake db:migrate RAILS_ENV=staging
● This will generate a file in db/schema.rb
○ Be sure to commit and push this to Github every time you run
migrations
● Update your Capistrano build to automatically run migrations for the
right environment on deploy
.env
Migration Example
Ugly way to make bigint primary key
Schema.rb
Capistrano task
Part 2
ActiveRecord vs. aRel
RailsConf 2014 - Advanced aRel: When
ActiveRecord Just Isn’t Enough
● Cameron Dutro
● Works on Twitter’s International
Engineering team
● https://www.youtube.com/watch?
v=ShPAxNcLm3o&index=14&list=PLE7tQUdR
KcyZ5jfnbS_osIoWzK_FrwKz5
Rails using SQL
Rails ¾ Improvements
Issues
● Just wrapping SQL in Rails methods
● You have to know underlying language
● No syntax checking
● Who likes matching question marks?
○ They’re confusing, man
● Not object-oriented
Arelized
Benefits
● Don’t have to know SQL
● Ruby syntax checking
● Chainable, composable
● No question marks
● Easier to read
ActiveRecord
● Database abstraction
● Persistence layer
○ Database rows as Ruby objects
● Domain Logic
○ Validations
● Associations
Arel
● “Relational Algebra” for Ruby
● Used to build queries,
generates ASTs
● Applies query optimizations
● Enables Chaining (subtrees)
● Not well documented
● Knows nothing about models or database, does not retrieve
data
○ That’s ActiveRecord’s job
○ Arel is all like “IDGAF” about yo models, db, or data
What’s an AST?
Arel Table
Arel-Helpers Gem
● gem install arel-helpers
Active Record Relations and .to_sql
Arelized
Select
More ActiveRecord vs. Arel
In this
case AR
is better/
cleaner
.not_eq(nil)
Reminder of the Data Model
Joins
Outer Joins Only Possible in Arel
Ugly Arel Version
Outer Joins Only Possible in Arel
Query Builder/Builder Pattern
Constructing a Query - Post Query Bldr
Constructing a Query cont.
Example in our own code!
Translated to SQL
The End

More Related Content

What's hot

Quarterly Technology Briefing, Manchester, UK September 2013
Quarterly Technology Briefing, Manchester, UK September 2013Quarterly Technology Briefing, Manchester, UK September 2013
Quarterly Technology Briefing, Manchester, UK September 2013
Thoughtworks
 

What's hot (19)

XML Quick Fixes
XML Quick FixesXML Quick Fixes
XML Quick Fixes
 
Implementing GraphQL API in Elixir – Victor Deryagin
Implementing GraphQL API in Elixir – Victor DeryaginImplementing GraphQL API in Elixir – Victor Deryagin
Implementing GraphQL API in Elixir – Victor Deryagin
 
Overcome a Frontier
Overcome a FrontierOvercome a Frontier
Overcome a Frontier
 
OSMC 2014: From monitoringsucks to monitoringlove (and back) | Kris Buytaert
OSMC 2014: From monitoringsucks to monitoringlove (and back) | Kris BuytaertOSMC 2014: From monitoringsucks to monitoringlove (and back) | Kris Buytaert
OSMC 2014: From monitoringsucks to monitoringlove (and back) | Kris Buytaert
 
Serverless microservices in the wild
Serverless microservices in the wildServerless microservices in the wild
Serverless microservices in the wild
 
ARB_gl_spirv: bringing SPIR-V to Mesa OpenGL (FOSDEM 2018)
ARB_gl_spirv: bringing SPIR-V to Mesa OpenGL (FOSDEM 2018)ARB_gl_spirv: bringing SPIR-V to Mesa OpenGL (FOSDEM 2018)
ARB_gl_spirv: bringing SPIR-V to Mesa OpenGL (FOSDEM 2018)
 
Reasonable app development
Reasonable app developmentReasonable app development
Reasonable app development
 
Why I don’t want to develop iOS apps in Objective C
Why I don’t want to develop iOS apps in Objective CWhy I don’t want to develop iOS apps in Objective C
Why I don’t want to develop iOS apps in Objective C
 
Elm - never get a runtime error anymore. Almost.
Elm - never get a runtime error anymore. Almost.Elm - never get a runtime error anymore. Almost.
Elm - never get a runtime error anymore. Almost.
 
PuppetConf 2017: Using Puppet Enterprise APIs with PowerShell- Jason Johnson,...
PuppetConf 2017: Using Puppet Enterprise APIs with PowerShell- Jason Johnson,...PuppetConf 2017: Using Puppet Enterprise APIs with PowerShell- Jason Johnson,...
PuppetConf 2017: Using Puppet Enterprise APIs with PowerShell- Jason Johnson,...
 
PuppetConf 2017: No Server Left Behind - Miguel Di Ciurcio Filho, Instruct
PuppetConf 2017: No Server Left Behind - Miguel Di Ciurcio Filho, InstructPuppetConf 2017: No Server Left Behind - Miguel Di Ciurcio Filho, Instruct
PuppetConf 2017: No Server Left Behind - Miguel Di Ciurcio Filho, Instruct
 
Helpful into to Rx
Helpful into to RxHelpful into to Rx
Helpful into to Rx
 
Load impact insights webinar
Load impact insights webinarLoad impact insights webinar
Load impact insights webinar
 
Cassovary
CassovaryCassovary
Cassovary
 
The dev ops code has no servers
The dev ops code has no serversThe dev ops code has no servers
The dev ops code has no servers
 
How To Tweak Angular 2 Performance
How To Tweak Angular 2 PerformanceHow To Tweak Angular 2 Performance
How To Tweak Angular 2 Performance
 
Angular Libraries & NPM
 Angular Libraries & NPM Angular Libraries & NPM
Angular Libraries & NPM
 
Quarterly Technology Briefing, Manchester, UK September 2013
Quarterly Technology Briefing, Manchester, UK September 2013Quarterly Technology Briefing, Manchester, UK September 2013
Quarterly Technology Briefing, Manchester, UK September 2013
 
Introduction to K6
Introduction to K6Introduction to K6
Introduction to K6
 

Similar to Active record, standalone migrations, and working with Arel

Deployments with rails
Deployments with railsDeployments with rails
Deployments with rails
Gourav Tiwari
 
Benchmarking for postgresql workloads in kubernetes
Benchmarking for postgresql workloads in kubernetesBenchmarking for postgresql workloads in kubernetes
Benchmarking for postgresql workloads in kubernetes
DoKC
 
Aws uk ug #8 not everything that happens in vegas stay in vegas
Aws uk ug #8   not everything that happens in vegas stay in vegasAws uk ug #8   not everything that happens in vegas stay in vegas
Aws uk ug #8 not everything that happens in vegas stay in vegas
Peter Mounce
 

Similar to Active record, standalone migrations, and working with Arel (20)

Snowflake Automated Deployments / CI/CD Pipelines
Snowflake Automated Deployments / CI/CD PipelinesSnowflake Automated Deployments / CI/CD Pipelines
Snowflake Automated Deployments / CI/CD Pipelines
 
Kubernetes: The Very Hard Way
Kubernetes: The Very Hard WayKubernetes: The Very Hard Way
Kubernetes: The Very Hard Way
 
Create a One Click Migration (OCM) process to Automate Repeatable Infrastruct...
Create a One Click Migration (OCM) process to Automate Repeatable Infrastruct...Create a One Click Migration (OCM) process to Automate Repeatable Infrastruct...
Create a One Click Migration (OCM) process to Automate Repeatable Infrastruct...
 
Project management frameworks for software developing
Project management frameworks for software developingProject management frameworks for software developing
Project management frameworks for software developing
 
Next.js with drupal, the good parts
Next.js with drupal, the good partsNext.js with drupal, the good parts
Next.js with drupal, the good parts
 
Deployments with rails
Deployments with railsDeployments with rails
Deployments with rails
 
Concurrency - Why it's hard ?
Concurrency - Why it's hard ?Concurrency - Why it's hard ?
Concurrency - Why it's hard ?
 
Continuous Deployment Applied at MyHeritage
Continuous Deployment Applied at MyHeritageContinuous Deployment Applied at MyHeritage
Continuous Deployment Applied at MyHeritage
 
Gobblin @ NerdWallet (Nov 2015)
Gobblin @ NerdWallet (Nov 2015)Gobblin @ NerdWallet (Nov 2015)
Gobblin @ NerdWallet (Nov 2015)
 
Db migrations equal pain
Db migrations equal painDb migrations equal pain
Db migrations equal pain
 
Flux architecture and Redux - theory, context and practice
Flux architecture and Redux - theory, context and practiceFlux architecture and Redux - theory, context and practice
Flux architecture and Redux - theory, context and practice
 
Why Concurrency is hard ?
Why Concurrency is hard ?Why Concurrency is hard ?
Why Concurrency is hard ?
 
Benchmarking for postgresql workloads in kubernetes
Benchmarking for postgresql workloads in kubernetesBenchmarking for postgresql workloads in kubernetes
Benchmarking for postgresql workloads in kubernetes
 
Liquibase få kontroll på dina databasförändringar
Liquibase   få kontroll på dina databasförändringarLiquibase   få kontroll på dina databasförändringar
Liquibase få kontroll på dina databasförändringar
 
Aws uk ug #8 not everything that happens in vegas stay in vegas
Aws uk ug #8   not everything that happens in vegas stay in vegasAws uk ug #8   not everything that happens in vegas stay in vegas
Aws uk ug #8 not everything that happens in vegas stay in vegas
 
Monitoring your VM's at Scale
Monitoring your VM's at ScaleMonitoring your VM's at Scale
Monitoring your VM's at Scale
 
Improve the deployment process step by step
Improve the deployment process step by stepImprove the deployment process step by step
Improve the deployment process step by step
 
Apache Airflow in the Cloud: Programmatically orchestrating workloads with Py...
Apache Airflow in the Cloud: Programmatically orchestrating workloads with Py...Apache Airflow in the Cloud: Programmatically orchestrating workloads with Py...
Apache Airflow in the Cloud: Programmatically orchestrating workloads with Py...
 
Devoxx : being productive with JHipster
Devoxx : being productive with JHipsterDevoxx : being productive with JHipster
Devoxx : being productive with JHipster
 
Road to sbt 1.0 paved with server
Road to sbt 1.0   paved with serverRoad to sbt 1.0   paved with server
Road to sbt 1.0 paved with server
 

Recently uploaded

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 

Recently uploaded (20)

AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 

Active record, standalone migrations, and working with Arel