customink / activerecord-aurora-serverless-adapter

ActiveRecord Adapter for Amazon Aurora Serverless
https://technology.customink.com/blog/2020/01/03/migrate-your-rails-app-from-heroku-to-aws-lambda/
MIT License
66 stars 7 forks source link

ActiveRecord Phase #1 - Test Harness & Schema Load #5

Closed metaskills closed 4 years ago

metaskills commented 4 years ago

Goal

This is the first of a two part series to pass all of ActiveRecord's MySQL test cases. To do this we need to have a test harness sytem and minimal amount of code to load the ActiveRecord test schema(s). The quest to full-stack Lambda for all is close!

Screen Shot 2019-12-21 at 7 31 04 PM

Prior Art & Architecture

Thankfully due to my previous ActiveRecord adapter experience I had two open source projects to pull from.

Aws::RDSDataService::Client Facade

My TinyTDS gem for FreeTDS is heavily inspired by the Mysql2 library. Both's interface pattern relies on two key objects, a Client and Result. To ensure our adapter requires the least work possible, we need to replace all Mysql2 adapter interfaces (the @connection object) with a facade backed by the Aws::RDSDataService::Client.

ActiveRecord Test Harness

Most 3rd party adapter gems (Oracle, SQLServer, etc) like to run the full ActiveRecord test suite along with their own unit or integration tests. This is kind of tricky but ActiveRecord does a really good job supporting it and I copied a lot of code I wrote in the SQL Server adapter over to this gem. Highlights:

Aurora Serverless Test DBs

In our initial work via (#2) we used CDK to create our Aurora Serverless test database. During this work I discovered two major chagnes were needed.

Native Gems?

The Aws::RDSDataService::Client uses AWS HTTP client code to talk to Aurora Serverless via the Data API. This requires no native gems to use. However, our adapter does inherit from the core Mysql2Adapter and doing so requires this code to be run.

gem "mysql2", ">= 0.4.4"
require "mysql2"

To avoid requiring and building code we do not need. This project includes a small Rubygems hack which keeps the Mysql2 gem from being really loaded.

Current Test Status?

I have not run the full ActiveRecord test suite. For now I have setup the bin/test file to only run one test in ActiveRecord's adapter_test.rb case. The goal is to ensure we are loading the schema successfully which happens prior to the first test. Which we have done 🎉

However, my first attempt at this whole file we passed most of the tests. So I suspect Phase II is going to go really smoothly and I bet we are passing ~75% of ActiveRecord's tests now! Other details include:

PostgreSQL?

Aurora Serverless does support PostgreSQL-compatible editions. However, I have no interest in hacking two native gem adapters now or likely in the future. I have however done a lot of work to allow this to easily happen if some person with a lot of gumption decided to contribute to this adapter. Here are a few points if you are interested in the topic.