Before any AI: a mountable engine, a dummy app with several tenants and fake data, and Postgres with pgvector. Boring on purpose, because everything after depends on it.
Context
In part 0 I pitched DataTeller: a Rails engine where an LLM fills a structured query that the engine validates and compiles, instead of writing SQL. Nothing runs yet. This part builds the ground everything else stands on.
If you followed the DataPorter series, the scaffolding will look familiar, with one difference. In DataPorter I tested without a dummy app. Here I keep it on purpose: the dummy app is the test bed for tenant isolation, for the evaluation suite and for retrieval experiments. It needs to be a realistic multi-tenant database from day one.
The problem
An evaluation suite, isolation specs and retrieval experiments all need data with a shape:
- several tenants, of different sizes, so that a cross-tenant leak can visibly change a number;
- relations that are not all one hop away from the tenant, because real schemas aren't;
- a few columns that look sensitive, to test hiding them later;
- predictable values and a repeatable dataset, while keeping dates relative to each tenant's local day.
And I want vectors in the same database from the start, because retrieval is a core goal of the project.
What we're building
- A mountable engine with an embedded dummy app (
spec/dummy), RSpec, RuboCop and CI. - A small domain:
Account(the tenant),Customer,Product,Order,LineItem,Shipment. - Deterministic fake seeds for three accounts.
- Postgres extensions:
vector,pg_trgm,unaccent.
Implementation
Step 1: the engine
rails plugin new data_teller --mountable --dummy-path=spec/dummy -d postgresql -T --skip-git -T skips Minitest (I use RSpec), and --dummy-path=spec/dummy puts the dummy app where RSpec expects it. The generator leaves TODOs in the gemspec and a CI that assumes Minitest, so a first cleanup commit fixes the gemspec, adds rspec-rails and gives CI a test job with a Postgres service.
One line in rails_helper.rb has to change, because the engine's environment lives in the dummy app:
require_relative 'dummy/config/environment' Step 2: a domain with a tenant
Account is the tenant. Customer, Product and Order carry an account_id. LineItem and Shipment deliberately don't:
# No account_id on purpose: tenant is reached through order, as in many real schemas. create_table :line_items do |t| t.references :order, null: false, foreign_key: true t.references :product, null: false, foreign_key: true t.integer :quantity, null: false t.integer :unit_price_cents, null: false t.timestamps end That forces every dataset to declare how it reaches the tenant (line_items → order → account), and the compiler, later, to filter every table it touches, not just the first one. Better to have that case in the test bed from the start.
customers.email and customers.phone exist for another reason: they look like the kind of column an install generator should hide by default.
The tenant itself has one detail worth a comment:
create_table :accounts do |t| t.string :name, null: false t.string :time_zone, null: false, default: "UTC" t.timestamps end "Tomorrow" depends on whose tomorrow it is. A shop in Tokyo and a shop in Paris don't share one. That's why Account has a time_zone column, and why the three seeded accounts live in Paris, New York and Tokyo: for most of the day, New York and Tokyo are not even on the same date. When the engine turns "tomorrow" into a date range, it will do it in the tenant's time zone, never the server's. That's the ship_on range in the SQL of part 0.
Step 3: deterministic fake seeds
All data in the dummy app is fake, and the seed file says so in its first line. It refuses to run in production, uses a fixed random seed, and has no dependency (no Faker):
# FAKE DATA — development and test only. Deterministic (fixed RNG seed) so eval # expectations stay stable. Never run against a real database. abort("Refusing to seed fake data in production") if Rails.env.production? rng = Random.new(42) Three accounts of different sizes: Acme with 120 orders, Globex with 50, Initech with 15. In total: 185 orders.
The counts are deliberately unequal. If a later isolation test accidentally reads orders from multiple tenants, the resulting count can be distinguished from the expected count for an individual account. And with equal sizes, a query scoped to the wrong tenant would return the right count.
This spec doesn't test the compiler or tenant isolation yet. It simply protects the fixture structure that those tests will rely on:
it "creates accounts of distinct sizes so a cross-tenant leak changes counts" do counts = Account.all.map { |a| a.orders.count } expect(counts.uniq.size).to eq(counts.size) end it "is deterministic" do first = Order.order(:id).pluck(:status, :total_cents) load Rails.root.join("db/seeds.rb") expect(Order.order(:id).pluck(:status, :total_cents)).to eq(first) end One detail about determinism: the fixed RNG seed makes the generated data repeatable for a given calendar day. Dates are deliberately anchored to each account's local today, so they move forward over time. This keeps relative questions such as "tomorrow" meaningful while preserving the same data distribution and structure.
Step 4: pgvector
Locally, on Arch, pacman -S pgvector was enough. For anyone without it, a docker-compose.yml starts the same pgvector image on port 5433. Then a migration enables what the project will need:
class EnableVectorExtension < ActiveRecord::Migration[7.2] def change # Needed from the retrieval milestone (embeddings); enabled early so CI and dev fail fast if missing. enable_extension "vector" # Used later for fuzzy value matching and French-insensitive search. enable_extension "pg_trgm" enable_extension "unaccent" end end At this point, there are no embeddings or vector searches in the application yet. The goal is simply to have the extension available from the start, so the development environment and CI catch a missing dependency before retrieval is implemented.
pg_trgm will be useful for similarity-based text matching, while unaccent will help with accent-insensitive searches, particularly for French text.
CI has to use an image that ships with these extensions; otherwise, migrations can pass locally and fail in CI:
services: postgres: image: pgvector/pgvector:pg17 Decisions & tradeoffs
| Decision | Chosen | Rejected | Why |
|---|---|---|---|
| Test bed | Dummy app inside the gem | A separate toy app | One repo, one CI, the same fixtures for isolation and evaluation |
| Fake data | Hand-written deterministic seeds | Faker | No dependency, predictable expectations |
| pgvector | Local install, plus a docker-compose.yml for anyone without it | Docker only | Less friction day to day; the compose file and CI use the same pgvector image |
Testing it
At this point there are five examples: a version smoke test and four seed specs, the two above plus one that reaches the tenant through order for line items and shipments, and one that checks order totals against their line items.
They are not impressive, and that's fine: they pin down the fixtures every later spec will lean on. They don't prove tenant isolation yet; they make sure the test environment is ready to exercise it.
bundle exec rspec and bin/rubocop are both green.
Gotchas
-
--skip-gitalso skips.gitignore. My first commits containedGemfile.lockandtmp/local_secret.txt. The secret is a throwaway development secret of a throwaway app, but it shouldn't be in git. I fixed it with a separate commit rather than rewriting history, so the evidence is still in the log. Lesson: if you skip git in a generator, add the.gitignoreyourself. - RuboCop assumed Ruby 2.7 until I set
TargetRubyVersion. It also needsschema.rbexcluded, since that file is generated. - Constants in a seed file warn when the file is loaded twice, which my spec does. Local variables fixed it.
- CI broke on my first push, and not because of my code.
ruby-version: ruby-3.4.8failed withUnavailable version 3.4.8 for ruby on ubuntu-26.04: the runner image couldn't resolve that exact patch version. Pinning the minor series ("3.4") let the setup action select an available patch. I would not have caught it without pushing early.
Recap
- A mountable engine with a dummy app that is the project's test bed.
- A multi-tenant domain where the tenant is sometimes two hops away.
- Reproducible, clearly labelled fake data, with tenants of different sizes and dates relative to their local time.
- pgvector,
pg_trgmandunaccentenabled locally and in CI.
Next up
Part 2 is the most important design step of the project: the query format, the JSON the LLM fills in. Six decisions, taken one at a time, and the reasons for each.
This is part 1 of the series "Building a Secure AI Data Access Engine for Rails". Previous: part 0 | Next: coming soon
Source code: DataTeller on GitHub. The repository is public, but the project is still a work in progress and is not yet ready to be used in an application. The current main branch has evolved beyond the snapshot shown in this article.
