📜 ⬆️ ⬇️

Rails and Postgres schemas

Introductory


Recently started using Postgres as a DBMS. I am very much attracted to it by the scheme, or as they are often called - the namespace (namespace), this is certainly not the only advantage, and not even the main thing, but only a pleasant trifle. So when my friend and I began to think about my pilot project, naturally, I chose Postgres as the DBMS. But to put it on the rails as you want, it was not so easy for a beginner ...

The first thing I did was create a project on rails:

rails <project name> -d postgresql 


Everything is simple, the base structure of the project is created and the configuration with connection to postgres is created in database.yml:
')
 development: adapter: postgresql encoding: unicode database: app_development pool: 5 username: www password: 123 host: localhost port: 5432 


Yes, for everything to work, you need to remember to connect gem - pg :-)

Problem


I was faced with the fact that I can’t create a migration to create a schema, or rather I can create one, but if after this migration there is another one that wants to create a table in this schema, then the problems begin ... As I understand, all migrations start in one Transactions and therefore it is impossible to create a diagram and immediately create a table in it.

In the manual, I basically did not want to create schemes, there was a desire to automate the process:

 rake db:migrate 

and everything is ready.

What i was trying to do


Added search_path to database.yml: “app, public” - did not help, because at the moment of launching the migraysh that creates the app scheme, it is not there yet and therefore the application cannot connect to the database. As soon as I didn’t approach search_path, it didn’t work out for me.

Then I got the idea to customize rake-task db: migrate. Krutil vveortel, did not work, something yes did not go as you want.

The option that gave me


I decided to close all the migrations by directories, I called one directory app — there I put the migrations for the tables that belong to the app schema, then I created the schema directory — it contains all the migrations that create schemas for the database.
Then I began to think, and how can I start only those migrations that are in the schema folder, and then only all the others. I started looking towards creating my own rake-task, which looks like this:

 # ------------------------------------------------------------- # =Description: # Rake task for creating postgres schemas # ------------------------------------------------------------- namespace :db do desc "Run migration for creating schemas" task :create_schemas => :environment do ActiveRecord::Migration.verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] == "true" : true ActiveRecord::Migrator.migrate("db/migrate/schema", ENV["VERSION"] ? ENV["VERSION"].to_i : nil) end end 


Pretty simple task, what you should pay attention to here:


How it all works


Now for rolling migrations I do the following:
 rake db:create_schemas rake db:migrate 

Conclusion


I started to torture Ruby and RoR quite recently, perhaps there is a more ideal solution to this problem with RoR schemes, but the current version suits me so far, can someone know a different, simpler and more elegant way?

Source: https://habr.com/ru/post/161693/


All Articles