lorint / brick

Auto-generate models, views, controllers, and routes in a Rails app based on database structure

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Question about multiple schemas in postgres sql db

AlaaZenji opened this issue · comments

When having multiple schemas in the same db, brick will add the name of the schema to the model's name in this format <schema_name>::<table_name>. Is there a way to bypass that and generate table names without those schema names before them?

Heya Alaa --

Been looking into this one pretty closely to see what is possible that would also be reliable and maintainable. It can get tricky if you try to blanketly disregard schemas in Postgres. So to keep things consistent, I see a couple approaches -- either set the Postgres search_path to include all your desired schemas, or to pick one "main" one and have Brick not put models for that schema into their own module. I think the latter offers the most consistency.

One reason schemas exist is to avoid naming conflicts that could arise when migrating a set of table structure from somewhere else into an existing database. If there happens to be table name clashes then it's no big deal when the new stuff is placed into its own different schema. Quite similar to the way that modules namespace things in Ruby, schemas end up namespacing things in Postres. So it makes good sense to have these align.

A new feature I'll add to Brick is the ability to set a default schema, like this:

Brick.default_schema = 'sales'

Then all tables in that sales schema will not end up creating models inside a module. And tables from other schemas will still have their models namespaced under modules. Probably one more day or so of testing this thing, and it will be released.

Thanks for reaching out!

-Lorin