influitive / apartment

Database multi-tenancy for Rack (and Rails) applications
2.67k stars 463 forks source link

Excluded Models still get created in tenant schemas #567

Closed bdensmore closed 5 years ago

bdensmore commented 5 years ago

Steps to reproduce

Apartment::Tenant.create(name)

Expected behavior

Expect Excluded models not to be created in tenant schemas. User and Subdomain models are set as excluded but when a new tenant is created it's creating the users and subdomains table in the tenants schema. So they exist in both "public" and "demo" tenants. They should be in "public" only.

Actual behavior

Excluded models are being created in tenant schemas

Creating Tenant demo
   (1.2ms)  CREATE SCHEMA "demo"
  ↳ app/models/subdomain.rb:8
  SQL (0.7ms)  CREATE EXTENSION IF NOT EXISTS "plpgsql"
  ↳ db/schema.rb:16
   (0.5ms)  DROP TABLE IF EXISTS "subdomains" CASCADE
  ↳ db/schema.rb:18
   (28.1ms)  CREATE TABLE "subdomains" ("id" bigserial primary key, "created_at" timestamp NOT NULL, "updated_at" timestamp NOT NULL, "name" character varying)
  ↳ db/schema.rb:18
   (1.9ms)  DROP TABLE IF EXISTS "users" CASCADE
  ↳ db/schema.rb:24
   (36.1ms)  CREATE TABLE "users" ("id" bigserial primary key, "email" character varying DEFAULT '' NOT NULL, "encrypted_password" character varying DEFAULT '' NOT NULL, "reset_password_token" character varying, "reset_password_sent_at" timestamp, "remember_created_at" timestamp, "created_at" timestamp NOT NULL, "updated_at" timestamp NOT NULL, "subdomain" character varying, "role" character varying)
  ↳ db/schema.rb:24
   (10.0ms)  CREATE UNIQUE INDEX  "index_users_on_email_and_subdomain" ON "users"  ("email", "subdomain")
  ↳ db/schema.rb:24
   (10.1ms)  CREATE UNIQUE INDEX  "index_users_on_reset_password_token" ON "users"  ("reset_password_token")
  ↳ db/schema.rb:24
   (21.1ms)  CREATE TABLE "schema_migrations" ("version" character varying NOT NULL PRIMARY KEY)
  ↳ db/schema.rb:13
   (1.0ms)  SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
  ↳ db/schema.rb:13
   (1.2ms)  INSERT INTO "schema_migrations" (version) VALUES (20180921204126)
  ↳ db/schema.rb:13
   (0.6ms)  INSERT INTO "schema_migrations" (version) VALUES
(20180921182324),
(20180921183809),
(20180921184654),
(20180921202352);

System configuration

require 'apartment/elevators/subdomain'

#
# Apartment Configuration
#
Apartment.configure do |config|
  config.excluded_models = ['User', 'Subdomain']
  config.tenant_names = lambda { Subdomain.pluck :name }
end

Rails.application.config.middleware.use Apartment::Elevators::Subdomain
Apartment::Elevators::Subdomain.excluded_subdomains = ['www', 'admin'] 
milosmiric commented 5 years ago

Per documentation it's stated that it will happen.

Rails will always access the 'public' tenant when accessing these models, but note that tables will be created in all schemas. This may not be ideal, but its done this way because otherwise rails wouldn't be able to properly generate the schema.rb file.

nhh commented 4 years ago

Any ideas how this could be achieved? I feel not good about leaving admin tables within the tenant schemes.

tmedford commented 4 years ago

@nhh Did you ever figure out a way?

nhh commented 4 years ago

@tmedford Hey, sorry unfortunately not! We completely switched our hosting model. Not building a saas anymore 🤪 Kind of „managed“ hosting now. Each instance with its own server.