Compare commits
4 Commits
16b27a0d1c
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 9906b291f1 | |||
| 2a458f74d6 | |||
| 9d65a4e29d | |||
| 8be82f5c49 |
2
Gemfile
2
Gemfile
@@ -49,6 +49,8 @@ group :development, :test do
|
|||||||
|
|
||||||
# Omakase Ruby styling [https://github.com/rails/rubocop-rails-omakase/]
|
# Omakase Ruby styling [https://github.com/rails/rubocop-rails-omakase/]
|
||||||
gem "rubocop-rails-omakase", require: false
|
gem "rubocop-rails-omakase", require: false
|
||||||
|
|
||||||
|
gem 'dotenv-rails'
|
||||||
end
|
end
|
||||||
|
|
||||||
group :development do
|
group :development do
|
||||||
|
|||||||
@@ -103,6 +103,9 @@ GEM
|
|||||||
irb (~> 1.10)
|
irb (~> 1.10)
|
||||||
reline (>= 0.3.8)
|
reline (>= 0.3.8)
|
||||||
dotenv (3.1.8)
|
dotenv (3.1.8)
|
||||||
|
dotenv-rails (3.1.8)
|
||||||
|
dotenv (= 3.1.8)
|
||||||
|
railties (>= 6.1)
|
||||||
drb (2.2.3)
|
drb (2.2.3)
|
||||||
ed25519 (1.4.0)
|
ed25519 (1.4.0)
|
||||||
erb (5.0.1)
|
erb (5.0.1)
|
||||||
@@ -360,6 +363,7 @@ DEPENDENCIES
|
|||||||
brakeman
|
brakeman
|
||||||
capybara
|
capybara
|
||||||
debug
|
debug
|
||||||
|
dotenv-rails
|
||||||
importmap-rails
|
importmap-rails
|
||||||
jbuilder
|
jbuilder
|
||||||
kamal
|
kamal
|
||||||
|
|||||||
14
README.md
14
README.md
@@ -12,6 +12,18 @@ $ psql -U postgres
|
|||||||
postgres=# ALTER USER <username> CREATEDB;
|
postgres=# ALTER USER <username> CREATEDB;
|
||||||
```
|
```
|
||||||
|
|
||||||
6. `rails db:migrate`
|
6. `rails db:migrate`, then create a new org:
|
||||||
|
`rails tenant:create['Demo Corp','demo']` and `rails tenant:migrate`
|
||||||
|
|
||||||
|
```sh
|
||||||
|
rails tenant:create[name,subdomain] # Create a new tenant (organization)
|
||||||
|
rails tenant:migrate # Migrate all tenant schemas
|
||||||
|
|
||||||
|
rails tenant:migrate_one[subdomain] # Migrate a specific tenant schema
|
||||||
|
rails tenant:rollback[step] # Rollback all tenant schemas by N steps (default: 1)
|
||||||
|
rails tenant:status # Show migration status for all tenants
|
||||||
|
rails tenant:drop[subdomain] # Drop a tenant schema and organization
|
||||||
|
```
|
||||||
|
|
||||||
7. `rails server`
|
7. `rails server`
|
||||||
8. Open localhost:3000
|
8. Open localhost:3000
|
||||||
|
|||||||
7
app/models/contact.rb
Normal file
7
app/models/contact.rb
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
class Contact < ApplicationRecord
|
||||||
|
has_many :phone_numbers
|
||||||
|
belongs_to :user
|
||||||
|
|
||||||
|
validates :name, presence: true
|
||||||
|
validates :email, format: { with: URI::MailTo::EMAIL_REGEXP }, allow_blank: true
|
||||||
|
end
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
class Current < ActiveSupport::CurrentAttributes
|
class Current < ActiveSupport::CurrentAttributes
|
||||||
attribute :session
|
attribute :session
|
||||||
delegate :user, to: :session, allow_nil: true
|
delegate :user, to: :session, allow_nil: true
|
||||||
|
attribute :organization, :user
|
||||||
end
|
end
|
||||||
|
|||||||
3
app/models/note.rb
Normal file
3
app/models/note.rb
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
class Note < ApplicationRecord
|
||||||
|
belongs_to :project
|
||||||
|
end
|
||||||
29
app/models/organization.rb
Normal file
29
app/models/organization.rb
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
class Organization < ApplicationRecord
|
||||||
|
validates :name, presence: true
|
||||||
|
validates :subdomain, presence: true, uniqueness: true
|
||||||
|
|
||||||
|
after_create :create_schema
|
||||||
|
before_destroy :drop_schema
|
||||||
|
has_many :users, dependent: :destroy
|
||||||
|
has_many :projects, through: :users
|
||||||
|
|
||||||
|
def schema_name
|
||||||
|
"org_#{id.to_s.gsub('-', '_')}"
|
||||||
|
end
|
||||||
|
|
||||||
|
def with_schema(&block)
|
||||||
|
SchemaManager.with_schema(id, &block)
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def create_schema
|
||||||
|
SchemaManager.create_schema(id)
|
||||||
|
end
|
||||||
|
|
||||||
|
def drop_schema
|
||||||
|
SchemaManager.drop_schema(id)
|
||||||
|
end
|
||||||
|
end
|
||||||
11
app/models/phone_number.rb
Normal file
11
app/models/phone_number.rb
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
class PhoneNumber < ApplicationRecord
|
||||||
|
belongs_to :contact
|
||||||
|
enum :type, {
|
||||||
|
mobile: 0,
|
||||||
|
home: 1,
|
||||||
|
work: 2,
|
||||||
|
fax: 3,
|
||||||
|
other: 4
|
||||||
|
}, _prefix: true
|
||||||
|
validates :number, presence: true, format: { with: /\A\+?[0-9\s\-()]+\z/, message: "must be a valid phone number" }
|
||||||
|
end
|
||||||
8
app/models/project.rb
Normal file
8
app/models/project.rb
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
class Project < ApplicationRecord
|
||||||
|
belongs_to :user
|
||||||
|
belongs_to :project_category
|
||||||
|
has_many :contacts, through: :user
|
||||||
|
has_many :tasks
|
||||||
|
has_many :notes
|
||||||
|
enum business_type: { type1: 0, type2: 1 }
|
||||||
|
end
|
||||||
3
app/models/project_category.rb
Normal file
3
app/models/project_category.rb
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
class ProjectCategory < ApplicationRecord
|
||||||
|
has_many :projects
|
||||||
|
end
|
||||||
67
app/models/schema_manager.rb
Normal file
67
app/models/schema_manager.rb
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
# app/models/schema_manager.rb
|
||||||
|
class SchemaManager
|
||||||
|
class << self
|
||||||
|
def create_schema(organization_id)
|
||||||
|
schema_name = schema_name_for(organization_id)
|
||||||
|
|
||||||
|
ActiveRecord::Base.connection.execute("CREATE SCHEMA IF NOT EXISTS #{schema_name}")
|
||||||
|
|
||||||
|
with_schema(organization_id) do
|
||||||
|
# Run all existing migrations in this schema
|
||||||
|
migration_context = ActiveRecord::MigrationContext.new(Rails.application.paths["db/migrate"].first)
|
||||||
|
migration_context.migrate
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def drop_schema(organization_id)
|
||||||
|
schema_name = schema_name_for(organization_id)
|
||||||
|
ActiveRecord::Base.connection.execute("DROP SCHEMA IF EXISTS #{schema_name} CASCADE")
|
||||||
|
end
|
||||||
|
|
||||||
|
def with_schema(organization_id)
|
||||||
|
schema_name = schema_name_for(organization_id)
|
||||||
|
original_schema = ActiveRecord::Base.connection.schema_search_path
|
||||||
|
|
||||||
|
begin
|
||||||
|
ActiveRecord::Base.connection.schema_search_path = schema_name
|
||||||
|
yield
|
||||||
|
ensure
|
||||||
|
ActiveRecord::Base.connection.schema_search_path = original_schema
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def schema_name_for(organization_id)
|
||||||
|
"org_#{organization_id.to_s.gsub('-', '_')}"
|
||||||
|
end
|
||||||
|
|
||||||
|
# Migrate all existing organization schemas
|
||||||
|
def migrate_all_schemas
|
||||||
|
migration_context = ActiveRecord::MigrationContext.new(Rails.application.paths["db/migrate"].first)
|
||||||
|
|
||||||
|
Organization.find_each do |org|
|
||||||
|
puts "Migrating schema for #{org.name}..."
|
||||||
|
org.with_schema do
|
||||||
|
migration_context.migrate
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# List all organization schemas
|
||||||
|
def list_schemas
|
||||||
|
result = ActiveRecord::Base.connection.execute(
|
||||||
|
"SELECT schema_name FROM information_schema.schemata
|
||||||
|
WHERE schema_name LIKE 'org_%'"
|
||||||
|
)
|
||||||
|
result.map { |row| row['schema_name'] }
|
||||||
|
end
|
||||||
|
|
||||||
|
# Check if schema exists
|
||||||
|
def schema_exists?(organization_id)
|
||||||
|
schema_name = schema_name_for(organization_id)
|
||||||
|
result = ActiveRecord::Base.connection.execute(
|
||||||
|
"SELECT EXISTS(SELECT 1 FROM information_schema.schemata WHERE schema_name = '#{schema_name}')"
|
||||||
|
)
|
||||||
|
result.first['exists']
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
9
app/models/task.rb
Normal file
9
app/models/task.rb
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
class Task < ApplicationRecord
|
||||||
|
belongs_to :creator, class_name: 'User'
|
||||||
|
belongs_to :reporter, class_name: 'User', optional: true
|
||||||
|
belongs_to :reportee, class_name: 'User', optional: true
|
||||||
|
belongs_to :project
|
||||||
|
enum priority: { low: 0, medium: 1, high: 2 }
|
||||||
|
enum status: { open: 0, in_progress: 1, closed: 2 }
|
||||||
|
enum type: { bug: 0, feature: 1, chore: 2 }
|
||||||
|
end
|
||||||
@@ -1,6 +1,9 @@
|
|||||||
class User < ApplicationRecord
|
class User < ApplicationRecord
|
||||||
has_secure_password
|
has_secure_password
|
||||||
has_many :sessions, dependent: :destroy
|
has_many :sessions, dependent: :destroy
|
||||||
|
belongs_to :organization, optional: false
|
||||||
|
has_many :projects
|
||||||
|
has_many :contacts
|
||||||
|
|
||||||
normalizes :email_address, with: ->(e) { e.strip.downcase }
|
normalizes :email_address, with: ->(e) { e.strip.downcase }
|
||||||
end
|
end
|
||||||
|
|||||||
21
db/migrate/20250611160914_create_organizations.rb
Normal file
21
db/migrate/20250611160914_create_organizations.rb
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
class CreateOrganizations < ActiveRecord::Migration[8.0]
|
||||||
|
def change
|
||||||
|
# Enable pgcrypto extension for UUID support
|
||||||
|
enable_extension 'pgcrypto' if connection.adapter_name == 'PostgreSQL'
|
||||||
|
|
||||||
|
create_table :organizations, id: :uuid do |t|
|
||||||
|
t.string :name
|
||||||
|
t.string :subdomain
|
||||||
|
t.string :email_domain
|
||||||
|
t.string :logo_url
|
||||||
|
t.string :address
|
||||||
|
t.string :website
|
||||||
|
t.string :phone_number
|
||||||
|
|
||||||
|
t.timestamps
|
||||||
|
end
|
||||||
|
|
||||||
|
add_index :organizations, :subdomain, unique: true
|
||||||
|
add_reference :users, :organization, null: false, foreign_key: true, type: :uuid
|
||||||
|
end
|
||||||
|
end
|
||||||
11
db/migrate/20250611163105_create_project_categories.rb
Normal file
11
db/migrate/20250611163105_create_project_categories.rb
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
class CreateProjectCategories < ActiveRecord::Migration[8.0]
|
||||||
|
def change
|
||||||
|
create_table :project_categories do |t|
|
||||||
|
t.string :title
|
||||||
|
t.text :description
|
||||||
|
t.integer :order
|
||||||
|
|
||||||
|
t.timestamps
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
13
db/migrate/20250611163700_create_contacts.rb
Normal file
13
db/migrate/20250611163700_create_contacts.rb
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
class CreateContacts < ActiveRecord::Migration[8.0]
|
||||||
|
def change
|
||||||
|
create_table :contacts do |t|
|
||||||
|
t.string :name
|
||||||
|
t.string :email
|
||||||
|
t.string :address
|
||||||
|
t.float :geo_long
|
||||||
|
t.float :geo_lat
|
||||||
|
|
||||||
|
t.timestamps
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
11
db/migrate/20250611172549_create_phone_numbers.rb
Normal file
11
db/migrate/20250611172549_create_phone_numbers.rb
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
class CreatePhoneNumbers < ActiveRecord::Migration[8.0]
|
||||||
|
def change
|
||||||
|
create_table :phone_numbers do |t|
|
||||||
|
t.string :phone_number
|
||||||
|
t.integer :type
|
||||||
|
t.references :contact, null: false, foreign_key: true
|
||||||
|
|
||||||
|
t.timestamps
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
14
db/migrate/20250611172728_create_projects.rb
Normal file
14
db/migrate/20250611172728_create_projects.rb
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
class CreateProjects < ActiveRecord::Migration[8.0]
|
||||||
|
def change
|
||||||
|
create_table :projects do |t|
|
||||||
|
t.string :title
|
||||||
|
t.string :website
|
||||||
|
t.string :email
|
||||||
|
t.integer :business_type
|
||||||
|
t.references :user, null: false, foreign_key: true
|
||||||
|
t.references :project_category, null: false, foreign_key: true
|
||||||
|
|
||||||
|
t.timestamps
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
15
db/migrate/20250611173502_create_tasks.rb
Normal file
15
db/migrate/20250611173502_create_tasks.rb
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
class CreateTasks < ActiveRecord::Migration[8.0]
|
||||||
|
def change
|
||||||
|
create_table :tasks do |t|
|
||||||
|
t.string :title
|
||||||
|
t.text :description
|
||||||
|
t.integer :priority
|
||||||
|
t.integer :status
|
||||||
|
t.integer :type
|
||||||
|
t.date :due_date
|
||||||
|
t.references :user, null: false, foreign_key: true
|
||||||
|
|
||||||
|
t.timestamps
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
13
db/migrate/20250611174428_create_notes.rb
Normal file
13
db/migrate/20250611174428_create_notes.rb
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
class CreateNotes < ActiveRecord::Migration[8.0]
|
||||||
|
def change
|
||||||
|
create_table :notes do |t|
|
||||||
|
t.string :title
|
||||||
|
t.text :description
|
||||||
|
t.boolean :pinned
|
||||||
|
t.boolean :archived
|
||||||
|
t.references :project, null: false, foreign_key: true
|
||||||
|
|
||||||
|
t.timestamps
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
88
db/schema.rb
generated
88
db/schema.rb
generated
@@ -10,9 +10,74 @@
|
|||||||
#
|
#
|
||||||
# It's strongly recommended that you check this file into your version control system.
|
# It's strongly recommended that you check this file into your version control system.
|
||||||
|
|
||||||
ActiveRecord::Schema[8.0].define(version: 2025_06_11_155737) do
|
ActiveRecord::Schema[8.0].define(version: 2025_06_11_174428) do
|
||||||
# These are extensions that must be enabled in order to support this database
|
# These are extensions that must be enabled in order to support this database
|
||||||
enable_extension "pg_catalog.plpgsql"
|
enable_extension "pg_catalog.plpgsql"
|
||||||
|
enable_extension "pgcrypto"
|
||||||
|
|
||||||
|
create_table "contacts", force: :cascade do |t|
|
||||||
|
t.string "name"
|
||||||
|
t.string "email"
|
||||||
|
t.string "address"
|
||||||
|
t.float "geo_long"
|
||||||
|
t.float "geo_lat"
|
||||||
|
t.datetime "created_at", null: false
|
||||||
|
t.datetime "updated_at", null: false
|
||||||
|
end
|
||||||
|
|
||||||
|
create_table "notes", force: :cascade do |t|
|
||||||
|
t.string "title"
|
||||||
|
t.text "description"
|
||||||
|
t.boolean "pinned"
|
||||||
|
t.boolean "archived"
|
||||||
|
t.bigint "project_id", null: false
|
||||||
|
t.datetime "created_at", null: false
|
||||||
|
t.datetime "updated_at", null: false
|
||||||
|
t.index ["project_id"], name: "index_notes_on_project_id"
|
||||||
|
end
|
||||||
|
|
||||||
|
create_table "organizations", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
|
||||||
|
t.string "name"
|
||||||
|
t.string "subdomain"
|
||||||
|
t.string "email_domain"
|
||||||
|
t.string "logo_url"
|
||||||
|
t.string "address"
|
||||||
|
t.string "website"
|
||||||
|
t.string "phone_number"
|
||||||
|
t.datetime "created_at", null: false
|
||||||
|
t.datetime "updated_at", null: false
|
||||||
|
t.index ["subdomain"], name: "index_organizations_on_subdomain", unique: true
|
||||||
|
end
|
||||||
|
|
||||||
|
create_table "phone_numbers", force: :cascade do |t|
|
||||||
|
t.string "phone_number"
|
||||||
|
t.integer "type"
|
||||||
|
t.bigint "contact_id", null: false
|
||||||
|
t.datetime "created_at", null: false
|
||||||
|
t.datetime "updated_at", null: false
|
||||||
|
t.index ["contact_id"], name: "index_phone_numbers_on_contact_id"
|
||||||
|
end
|
||||||
|
|
||||||
|
create_table "project_categories", force: :cascade do |t|
|
||||||
|
t.string "title"
|
||||||
|
t.text "description"
|
||||||
|
t.integer "order"
|
||||||
|
t.datetime "created_at", null: false
|
||||||
|
t.datetime "updated_at", null: false
|
||||||
|
end
|
||||||
|
|
||||||
|
create_table "projects", force: :cascade do |t|
|
||||||
|
t.string "title"
|
||||||
|
t.string "website"
|
||||||
|
t.string "email"
|
||||||
|
t.integer "business_type"
|
||||||
|
t.bigint "user_id", null: false
|
||||||
|
t.bigint "project_category_id", null: false
|
||||||
|
t.datetime "created_at", null: false
|
||||||
|
t.datetime "updated_at", null: false
|
||||||
|
t.index ["project_category_id"], name: "index_projects_on_project_category_id"
|
||||||
|
t.index ["user_id"], name: "index_projects_on_user_id"
|
||||||
|
end
|
||||||
|
|
||||||
create_table "sessions", force: :cascade do |t|
|
create_table "sessions", force: :cascade do |t|
|
||||||
t.bigint "user_id", null: false
|
t.bigint "user_id", null: false
|
||||||
@@ -23,13 +88,34 @@ ActiveRecord::Schema[8.0].define(version: 2025_06_11_155737) do
|
|||||||
t.index ["user_id"], name: "index_sessions_on_user_id"
|
t.index ["user_id"], name: "index_sessions_on_user_id"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
create_table "tasks", force: :cascade do |t|
|
||||||
|
t.string "title"
|
||||||
|
t.text "description"
|
||||||
|
t.integer "priority"
|
||||||
|
t.integer "status"
|
||||||
|
t.integer "type"
|
||||||
|
t.date "due_date"
|
||||||
|
t.bigint "user_id", null: false
|
||||||
|
t.datetime "created_at", null: false
|
||||||
|
t.datetime "updated_at", null: false
|
||||||
|
t.index ["user_id"], name: "index_tasks_on_user_id"
|
||||||
|
end
|
||||||
|
|
||||||
create_table "users", force: :cascade do |t|
|
create_table "users", force: :cascade do |t|
|
||||||
t.string "email_address", null: false
|
t.string "email_address", null: false
|
||||||
t.string "password_digest", null: false
|
t.string "password_digest", null: false
|
||||||
t.datetime "created_at", null: false
|
t.datetime "created_at", null: false
|
||||||
t.datetime "updated_at", null: false
|
t.datetime "updated_at", null: false
|
||||||
|
t.uuid "organization_id", null: false
|
||||||
t.index ["email_address"], name: "index_users_on_email_address", unique: true
|
t.index ["email_address"], name: "index_users_on_email_address", unique: true
|
||||||
|
t.index ["organization_id"], name: "index_users_on_organization_id"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
add_foreign_key "notes", "projects"
|
||||||
|
add_foreign_key "phone_numbers", "contacts"
|
||||||
|
add_foreign_key "projects", "project_categories"
|
||||||
|
add_foreign_key "projects", "users"
|
||||||
add_foreign_key "sessions", "users"
|
add_foreign_key "sessions", "users"
|
||||||
|
add_foreign_key "tasks", "users"
|
||||||
|
add_foreign_key "users", "organizations"
|
||||||
end
|
end
|
||||||
|
|||||||
90
lib/tasks/tenant.rake
Normal file
90
lib/tasks/tenant.rake
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
namespace :tenant do
|
||||||
|
desc "Migrate all tenant schemas"
|
||||||
|
task migrate: :environment do
|
||||||
|
# First migrate the public schema (organizations table, etc.)
|
||||||
|
puts "Migrating public schema..."
|
||||||
|
ActiveRecord::MigrationContext.new(Rails.application.paths["db/migrate"].first).migrate
|
||||||
|
|
||||||
|
# Then migrate all tenant schemas
|
||||||
|
puts "Migrating all tenant schemas..."
|
||||||
|
SchemaManager.migrate_all_schemas
|
||||||
|
|
||||||
|
puts "All schemas migrated successfully!"
|
||||||
|
end
|
||||||
|
|
||||||
|
desc "Migrate specific tenant schema"
|
||||||
|
task :migrate_one, [:subdomain] => :environment do |task, args|
|
||||||
|
subdomain = args[:subdomain]
|
||||||
|
organization = Organization.find_by!(subdomain: subdomain)
|
||||||
|
|
||||||
|
puts "Migrating schema for #{organization.name}..."
|
||||||
|
organization.with_schema do
|
||||||
|
ActiveRecord::MigrationContext.new(Rails.application.paths["db/migrate"].first).migrate
|
||||||
|
end
|
||||||
|
puts "Migration complete!"
|
||||||
|
end
|
||||||
|
|
||||||
|
desc "Rollback all tenant schemas"
|
||||||
|
task :rollback, [:step] => :environment do |task, args|
|
||||||
|
step = (args[:step] || 1).to_i
|
||||||
|
|
||||||
|
Organization.find_each do |org|
|
||||||
|
puts "Rolling back #{step} step(s) for #{org.name}..."
|
||||||
|
org.with_schema do
|
||||||
|
ActiveRecord::MigrationContext.new(Rails.application.paths["db/migrate"].first).rollback(step)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
desc "Show migration status for all tenants"
|
||||||
|
task status: :environment do
|
||||||
|
Organization.find_each do |org|
|
||||||
|
puts "\n#{org.name} (#{org.subdomain}):"
|
||||||
|
org.with_schema do
|
||||||
|
migration_context = ActiveRecord::MigrationContext.new(Rails.application.paths["db/migrate"].first)
|
||||||
|
migration_context.migrations_status.each do |status, version, name|
|
||||||
|
puts " #{status.center(8)} #{version} #{name}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
desc "Create a new tenant"
|
||||||
|
task :create, [:name, :subdomain] => :environment do |task, args|
|
||||||
|
name = args[:name]
|
||||||
|
subdomain = args[:subdomain]
|
||||||
|
|
||||||
|
if name.blank? || subdomain.blank?
|
||||||
|
puts "Usage: rails tenant:create[name,subdomain]"
|
||||||
|
puts "Example: rails tenant:create['Acme Corp','acme']"
|
||||||
|
exit 1
|
||||||
|
end
|
||||||
|
|
||||||
|
organization = Organization.create!(name: name, subdomain: subdomain)
|
||||||
|
puts "Created organization: #{organization.name}"
|
||||||
|
puts "Schema: #{organization.schema_name}"
|
||||||
|
puts "Access at: http://#{subdomain}.localhost:3000"
|
||||||
|
end
|
||||||
|
|
||||||
|
desc "Drop a tenant schema"
|
||||||
|
task :drop, [:subdomain] => :environment do |task, args|
|
||||||
|
subdomain = args[:subdomain]
|
||||||
|
|
||||||
|
if subdomain.blank?
|
||||||
|
puts "Usage: rails tenant:drop[subdomain]"
|
||||||
|
exit 1
|
||||||
|
end
|
||||||
|
|
||||||
|
organization = Organization.find_by!(subdomain: subdomain)
|
||||||
|
|
||||||
|
print "Are you sure you want to drop #{organization.name}? (y/N): "
|
||||||
|
response = STDIN.gets.chomp.downcase
|
||||||
|
|
||||||
|
if response == 'y' || response == 'yes'
|
||||||
|
organization.destroy
|
||||||
|
puts "Dropped organization: #{organization.name}"
|
||||||
|
else
|
||||||
|
puts "Cancelled"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
15
test/fixtures/contacts.yml
vendored
Normal file
15
test/fixtures/contacts.yml
vendored
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
|
||||||
|
|
||||||
|
one:
|
||||||
|
name: MyString
|
||||||
|
email: MyString
|
||||||
|
address: MyString
|
||||||
|
geo_long: 1.5
|
||||||
|
geo_lat: 1.5
|
||||||
|
|
||||||
|
two:
|
||||||
|
name: MyString
|
||||||
|
email: MyString
|
||||||
|
address: MyString
|
||||||
|
geo_long: 1.5
|
||||||
|
geo_lat: 1.5
|
||||||
15
test/fixtures/notes.yml
vendored
Normal file
15
test/fixtures/notes.yml
vendored
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
|
||||||
|
|
||||||
|
one:
|
||||||
|
title: MyString
|
||||||
|
description: MyText
|
||||||
|
pinned: false
|
||||||
|
archived: false
|
||||||
|
project: one
|
||||||
|
|
||||||
|
two:
|
||||||
|
title: MyString
|
||||||
|
description: MyText
|
||||||
|
pinned: false
|
||||||
|
archived: false
|
||||||
|
project: two
|
||||||
19
test/fixtures/organizations.yml
vendored
Normal file
19
test/fixtures/organizations.yml
vendored
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
|
||||||
|
|
||||||
|
one:
|
||||||
|
name: MyString
|
||||||
|
subdomain: MyString
|
||||||
|
email_domain: MyString
|
||||||
|
logo_url: MyString
|
||||||
|
address: MyString
|
||||||
|
website: MyString
|
||||||
|
phone_number: MyString
|
||||||
|
|
||||||
|
two:
|
||||||
|
name: MyString
|
||||||
|
subdomain: MyString
|
||||||
|
email_domain: MyString
|
||||||
|
logo_url: MyString
|
||||||
|
address: MyString
|
||||||
|
website: MyString
|
||||||
|
phone_number: MyString
|
||||||
11
test/fixtures/phone_numbers.yml
vendored
Normal file
11
test/fixtures/phone_numbers.yml
vendored
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
|
||||||
|
|
||||||
|
one:
|
||||||
|
phone_number: MyString
|
||||||
|
type: 1
|
||||||
|
contact: one
|
||||||
|
|
||||||
|
two:
|
||||||
|
phone_number: MyString
|
||||||
|
type: 1
|
||||||
|
contact: two
|
||||||
11
test/fixtures/project_categories.yml
vendored
Normal file
11
test/fixtures/project_categories.yml
vendored
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
|
||||||
|
|
||||||
|
one:
|
||||||
|
title: MyString
|
||||||
|
description: MyText
|
||||||
|
order: 1
|
||||||
|
|
||||||
|
two:
|
||||||
|
title: MyString
|
||||||
|
description: MyText
|
||||||
|
order: 1
|
||||||
17
test/fixtures/projects.yml
vendored
Normal file
17
test/fixtures/projects.yml
vendored
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
|
||||||
|
|
||||||
|
one:
|
||||||
|
title: MyString
|
||||||
|
website: MyString
|
||||||
|
email: MyString
|
||||||
|
business_type: 1
|
||||||
|
user: one
|
||||||
|
project_category: one
|
||||||
|
|
||||||
|
two:
|
||||||
|
title: MyString
|
||||||
|
website: MyString
|
||||||
|
email: MyString
|
||||||
|
business_type: 1
|
||||||
|
user: two
|
||||||
|
project_category: two
|
||||||
19
test/fixtures/tasks.yml
vendored
Normal file
19
test/fixtures/tasks.yml
vendored
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
|
||||||
|
|
||||||
|
one:
|
||||||
|
title: MyString
|
||||||
|
description: MyText
|
||||||
|
priority: 1
|
||||||
|
status: 1
|
||||||
|
type: 1
|
||||||
|
due_date: 2025-06-11
|
||||||
|
user: one
|
||||||
|
|
||||||
|
two:
|
||||||
|
title: MyString
|
||||||
|
description: MyText
|
||||||
|
priority: 1
|
||||||
|
status: 1
|
||||||
|
type: 1
|
||||||
|
due_date: 2025-06-11
|
||||||
|
user: two
|
||||||
7
test/models/contact_test.rb
Normal file
7
test/models/contact_test.rb
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
require "test_helper"
|
||||||
|
|
||||||
|
class ContactTest < ActiveSupport::TestCase
|
||||||
|
# test "the truth" do
|
||||||
|
# assert true
|
||||||
|
# end
|
||||||
|
end
|
||||||
7
test/models/note_test.rb
Normal file
7
test/models/note_test.rb
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
require "test_helper"
|
||||||
|
|
||||||
|
class NoteTest < ActiveSupport::TestCase
|
||||||
|
# test "the truth" do
|
||||||
|
# assert true
|
||||||
|
# end
|
||||||
|
end
|
||||||
7
test/models/organization_test.rb
Normal file
7
test/models/organization_test.rb
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
require "test_helper"
|
||||||
|
|
||||||
|
class OrganizationTest < ActiveSupport::TestCase
|
||||||
|
# test "the truth" do
|
||||||
|
# assert true
|
||||||
|
# end
|
||||||
|
end
|
||||||
7
test/models/phone_number_test.rb
Normal file
7
test/models/phone_number_test.rb
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
require "test_helper"
|
||||||
|
|
||||||
|
class PhoneNumberTest < ActiveSupport::TestCase
|
||||||
|
# test "the truth" do
|
||||||
|
# assert true
|
||||||
|
# end
|
||||||
|
end
|
||||||
7
test/models/project_category_test.rb
Normal file
7
test/models/project_category_test.rb
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
require "test_helper"
|
||||||
|
|
||||||
|
class ProjectCategoryTest < ActiveSupport::TestCase
|
||||||
|
# test "the truth" do
|
||||||
|
# assert true
|
||||||
|
# end
|
||||||
|
end
|
||||||
7
test/models/project_test.rb
Normal file
7
test/models/project_test.rb
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
require "test_helper"
|
||||||
|
|
||||||
|
class ProjectTest < ActiveSupport::TestCase
|
||||||
|
# test "the truth" do
|
||||||
|
# assert true
|
||||||
|
# end
|
||||||
|
end
|
||||||
7
test/models/task_test.rb
Normal file
7
test/models/task_test.rb
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
require "test_helper"
|
||||||
|
|
||||||
|
class TaskTest < ActiveSupport::TestCase
|
||||||
|
# test "the truth" do
|
||||||
|
# assert true
|
||||||
|
# end
|
||||||
|
end
|
||||||
Reference in New Issue
Block a user