activerecord.rb
2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
require "active_record"
# for debugging
ActiveRecord::Base.logger = $logger
# rails does this in activerecord/lib/active_record/railtie.rb
ActiveRecord::Base.default_timezone = :utc
ActiveRecord::Base.time_zone_aware_attributes = true
# migrations
ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"
ActiveRecord::Base.raise_in_transactional_callbacks = true if ActiveRecord::VERSION::STRING.start_with?("4.2.")
if defined?(Apartment)
class Rails
def self.env
ENV["RACK_ENV"]
end
end
tenants = ["tenant1", "tenant2"]
Apartment.configure do |config|
config.tenant_names = tenants
config.database_schema_file = false
config.excluded_models = ["Product", "Store", "Animal", "Dog", "Cat"]
end
class Tenant < ActiveRecord::Base
searchkick index_prefix: -> { Apartment::Tenant.current }
end
tenants.each do |tenant|
begin
Apartment::Tenant.create(tenant)
rescue Apartment::TenantExists
# do nothing
end
Apartment::Tenant.switch!(tenant)
ActiveRecord::Migration.create_table :tenants, force: true do |t|
t.string :name
t.timestamps null: true
end
Tenant.reindex
end
Apartment::Tenant.reset
end
ActiveRecord::Migration.create_table :products do |t|
t.string :name
t.integer :store_id
t.boolean :in_stock
t.boolean :backordered
t.integer :orders_count
t.decimal :found_rate
t.integer :price
t.string :color
t.decimal :latitude, precision: 10, scale: 7
t.decimal :longitude, precision: 10, scale: 7
t.text :description
t.text :alt_description
t.timestamps null: true
end
ActiveRecord::Migration.create_table :stores do |t|
t.string :name
end
ActiveRecord::Migration.create_table :regions do |t|
t.string :name
t.text :text
end
ActiveRecord::Migration.create_table :speakers do |t|
t.string :name
end
ActiveRecord::Migration.create_table :animals do |t|
t.string :name
t.string :type
end
ActiveRecord::Migration.create_table :skus, id: :uuid do |t|
t.string :name
end
ActiveRecord::Migration.create_table :songs do |t|
t.string :name
end
ActiveRecord::Migration.create_table :bands do |t|
t.string :name
end
class Product < ActiveRecord::Base
belongs_to :store
end
class Store < ActiveRecord::Base
has_many :products
end
class Region < ActiveRecord::Base
end
class Speaker < ActiveRecord::Base
end
class Animal < ActiveRecord::Base
end
class Dog < Animal
end
class Cat < Animal
end
class Sku < ActiveRecord::Base
end
class Song < ActiveRecord::Base
end
class Band < ActiveRecord::Base
default_scope { where(name: "Test") }
end