Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated database connection handling via pg_dump. #128

Open
wants to merge 6 commits into
base: development
Choose a base branch
from

Conversation

vdragsic
Copy link
Contributor

@vdragsic vdragsic commented Apr 4, 2014

Connection arguments for pg_dump command are built from Rails.configuration.database_configuration` for current environment.

This reflects only on PostgresqlSchemaFromSqlAdapter.

# read database config
db_config = Rails.configuration.database_configuration.fetch Rails.env
db_name = db_config['database']
db_host = db_config['socket'] ? File.dirname(db_config['socket']) : db_config['host'] || 'localhost'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [108/80]

@jonsgreen
Copy link
Contributor

+1

@NielsKSchjoedt
Copy link

From what I can see here, the problem with the excluded models is not really being fixed by leaving out those models when dumping/restoring. Now your FK constraints won't be prepended by the schema unless they point to a table in a different schema, than the one you dump from. Let me give an example:

Let's say you have an excluded model called user which resides in your default schema called wine_blog. You create your FK's so that they alway point to wine_blog.users. Then when you run Apartment::Database.create('food_blog'), it will do the following:

--
-- PostgreSQL database dump
--

--
-- Name: food_blog; Type: SCHEMA; Schema: -; Owner: -
--

CREATE SCHEMA food_blog;


--
-- Name: SCHEMA food_blog; Type: COMMENT; Schema: -; Owner: -
--

COMMENT ON SCHEMA  IS 'standard public schema';


SET search_path = food_blog;

(...)

--
-- Name: posts_user_id_fk; Type: FK CONSTRAINT; Schema: food_blog; Owner: -
--

ALTER TABLE ONLY posts
    ADD CONSTRAINT posts_user_id_fk FOREIGN KEY (users_id) REFERENCES users(id);

Now we have a problem, because when creating the new schema food_blog, it will suddenly reference food_blog.users as described. Where as what you really would want is something like:

ALTER TABLE ONLY posts
    ADD CONSTRAINT posts_user_id_fk FOREIGN KEY (users_id) REFERENCES wine_blog.users(id);

However that is never going to happen, because the wine_blog is your default_schema which is being used for dumping in the first place.

Now to get the right behavior, you would have to move all excluded models into a different schema (e.g. public), because then you would see public.users when dumping from your default wine_blog schema. The problem with this however would be, that migrations making changes to users would break, if they, besides the public schema, not also resides as "dummy"-tables in the _blog schemas.

To wrap up, the best idea is probably to leave the excluded models in all schemas, then make sure, that your FK's referring to excluded models, refer to the excluded model tables residing in the public persisted_schema, NOT in the default_schema.

Do you follow me?

@jenseng
Copy link

jenseng commented Sep 24, 2014

as a workaround for those waiting on this PR, pg_dump and friends support a variety of environment variables (e.g. PGUSER, PGPORT, etc.). when coupled with something like dotenv you can still keep all your db config in one place (e.g. database.yml could also pull from env vars)

# schema. In that case FK on 'tenant.tableA' references to
# non-existing row in 'tenant.tableB', but should ref. to
# 'public.tableB', that's why tables from excluded models shouldn't
# be copied to tenant schemes.)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made an alternative to this: #182

…into development

Conflicts:
	apartment.gemspec
	lib/apartment/adapters/postgresql_adapter.rb
marksiemers pushed a commit to AbleHealth/apartment that referenced this pull request Jan 13, 2021
marksiemers pushed a commit to AbleHealth/apartment that referenced this pull request Jan 13, 2021
marksiemers pushed a commit to AbleHealth/apartment that referenced this pull request Jan 13, 2021
marksiemers pushed a commit to AbleHealth/apartment that referenced this pull request Jan 13, 2021
marksiemers pushed a commit to AbleHealth/apartment that referenced this pull request Apr 13, 2021
Prepare Release - 2.8.1

**Implemented enhancements:**

**Fixed bugs:**

- New version raises an error with ActiveSupport::LogSubscriber [influitive#128](rails-on-services/apartment#128)
- Weird logs when tenant fails to create [influitive#127](<rails-on-services/apartment#127>)

**Closed issues:**

- Removed travis and slim configured circleci [influitive#130](rails-on-services/apartment#130)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants