switchman 1.2.7 → 1.2.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 80227d17c7587ce99df75ee7d181e2ff40cdbeb0
4
- data.tar.gz: ac327051b373ac44f3a8e01bbfeec23891405bc8
3
+ metadata.gz: 62a38cba848a274d99ab64d7e6de6a9d3cf25d8a
4
+ data.tar.gz: fd23f4718d778dd580703478253658610c7bc2ba
5
5
  SHA512:
6
- metadata.gz: 08f9fb72a1518b792bf7ebbad331b2191a1ed72e41d72850bb17a182dc1690fc31bbc78c8cb98ea2f52308e9ce20b3e13ece5b0bdbf8e727511de482c9fcfc4e
7
- data.tar.gz: d2d7610a4f11918adcd79c1ab232f76fa01c212c4d9bd86962b039a60bf5f84797907fb3f08814105fd9c8130930d0aadc2c83f1ca541960bc5f1226a876fc75
6
+ metadata.gz: ce4357fb9fb1898b19c2dae8696ab457022bad518b871e38a06d03a64e3ef17fa137e7dceda600ba5acbfcd622004b35bd3fcfda6d23be4d7541e88da9ba873b
7
+ data.tar.gz: f8e25efd8028727f282b9d37598f6caa0dc83e8d39645e888bf9e7741a1658519c5623638a41c8628a22630d036966bde3a53f1eb91534b0cae7d281cac9ff39
@@ -82,16 +82,16 @@ module Switchman
82
82
 
83
83
  def save(*args)
84
84
  @shard_set_in_stone = true
85
- self.class.shard(shard).scoping { super }
85
+ self.class.shard(shard, :implicit).scoping { super }
86
86
  end
87
87
 
88
88
  def save!(*args)
89
89
  @shard_set_in_stone = true
90
- self.class.shard(shard).scoping { super }
90
+ self.class.shard(shard, :implicit).scoping { super }
91
91
  end
92
92
 
93
93
  def destroy
94
- self.class.shard(shard).scoping { super }
94
+ self.class.shard(shard, :implicit).scoping { super }
95
95
  end
96
96
 
97
97
  def clone
@@ -15,11 +15,10 @@ module Switchman
15
15
 
16
16
  def create(settings = {})
17
17
  raise "database servers should be set up in database.yml" unless ::Rails.env.test?
18
- id = 1
19
- while database_servers[id.to_s]; id += 1; end
20
- server = DatabaseServer.new({ :id => id.to_s }.merge(settings))
18
+ @id ||= 0
19
+ @id += 1
20
+ server = DatabaseServer.new({ :id => @id.to_s }.merge(settings))
21
21
  server.instance_variable_set(:@fake, true)
22
- raise "database server #{server.id} already exists" if database_servers[server.id]
23
22
  database_servers[server.id] = server
24
23
  end
25
24
 
@@ -95,11 +95,17 @@ module Switchman
95
95
  shards << @shard1 unless @shard1.database_server == Shard.default.database_server
96
96
  shards.each do |shard|
97
97
  shard.activate do
98
- # this is how AR does it in fixtures.rb
98
+ # this is how AR does it in database_statements.rb
99
99
  if ::Rails.version < '4'
100
- ::ActiveRecord::Base.connection.increment_open_transactions
101
- ::ActiveRecord::Base.connection.transaction_joinable = false
102
- ::ActiveRecord::Base.connection.begin_db_transaction
100
+ conn = ::ActiveRecord::Base.connection
101
+ # support nested transactions around (groups of) specs (e.g. for once-ler)
102
+ if conn.open_transactions == 0
103
+ conn.transaction_joinable = false
104
+ conn.begin_db_transaction
105
+ else
106
+ conn.create_savepoint
107
+ end
108
+ conn.increment_open_transactions
103
109
  else
104
110
  ::ActiveRecord::Base.connection.begin_transaction joinable: false
105
111
  end
@@ -115,9 +121,12 @@ module Switchman
115
121
  shards.each do |shard|
116
122
  shard.activate do
117
123
  if ::Rails.version < '4'
118
- if ::ActiveRecord::Base.connection.open_transactions != 0
119
- ::ActiveRecord::Base.connection.rollback_db_transaction
120
- ::ActiveRecord::Base.connection.decrement_open_transactions
124
+ conn = ::ActiveRecord::Base.connection
125
+ conn.decrement_open_transactions
126
+ if conn.open_transactions == 0
127
+ conn.rollback_db_transaction
128
+ else
129
+ conn.rollback_to_savepoint
121
130
  end
122
131
  else
123
132
  ::ActiveRecord::Base.connection.rollback_transaction if ::ActiveRecord::Base.connection.transaction_open?
@@ -1,3 +1,3 @@
1
1
  module Switchman
2
- VERSION = "1.2.7"
2
+ VERSION = "1.2.8"
3
3
  end
@@ -0,0 +1,23 @@
1
+ # SQLite version 3.x
2
+ # gem install sqlite3
3
+ #
4
+ # Ensure the SQLite 3 gem is defined in your Gemfile
5
+ # gem 'sqlite3'
6
+ development:
7
+ adapter: postgresql
8
+ database: switchman_development
9
+
10
+ # Warning: The database defined as "test" will be erased and
11
+ # re-generated from your development database when you run "rake".
12
+ # Do not set this db to the same as development or production.
13
+ test:
14
+ adapter: sqlite3
15
+ database: db/test.sqlite3
16
+ pool: 5
17
+ timeout: 5000
18
+
19
+ production:
20
+ adapter: sqlite3
21
+ database: db/production.sqlite3
22
+ pool: 5
23
+ timeout: 5000
Binary file
@@ -0,0 +1,67 @@
1
+ # encoding: UTF-8
2
+ # This file is auto-generated from the current state of the database. Instead
3
+ # of editing this file, please use the migrations feature of Active Record to
4
+ # incrementally modify your database, and then regenerate this schema definition.
5
+ #
6
+ # Note that this schema.rb definition is the authoritative source for your
7
+ # database schema. If you need to create the application database on another
8
+ # system, you should be using db:schema:load, not running all the migrations
9
+ # from scratch. The latter is a flawed and unsustainable approach (the more migrations
10
+ # you'll amass, the slower it'll run and the greater likelihood for issues).
11
+ #
12
+ # It's strongly recommended that you check this file into your version control system.
13
+
14
+ ActiveRecord::Schema.define(version: 20140219183820) do
15
+
16
+ # These are extensions that must be enabled in order to support this database
17
+ enable_extension "plpgsql"
18
+
19
+ create_table "appendages", force: true do |t|
20
+ t.integer "user_id", limit: 8
21
+ t.integer "value"
22
+ t.datetime "created_at"
23
+ t.datetime "updated_at"
24
+ end
25
+
26
+ create_table "digits", force: true do |t|
27
+ t.integer "appendage_id", limit: 8
28
+ t.integer "value"
29
+ t.datetime "created_at"
30
+ t.datetime "updated_at"
31
+ end
32
+
33
+ create_table "features", force: true do |t|
34
+ t.integer "owner_id", limit: 8
35
+ t.string "owner_type"
36
+ t.integer "value"
37
+ t.datetime "created_at"
38
+ t.datetime "updated_at"
39
+ end
40
+
41
+ create_table "mirror_users", force: true do |t|
42
+ t.integer "user_id", limit: 8
43
+ t.datetime "created_at"
44
+ t.datetime "updated_at"
45
+ end
46
+
47
+ create_table "roots", force: true do |t|
48
+ t.integer "user_id", limit: 8
49
+ t.datetime "created_at"
50
+ t.datetime "updated_at"
51
+ end
52
+
53
+ create_table "switchman_shards", force: true do |t|
54
+ t.string "name"
55
+ t.string "database_server_id"
56
+ t.boolean "default", default: false, null: false
57
+ end
58
+
59
+ create_table "users", force: true do |t|
60
+ t.string "name"
61
+ t.integer "mirror_user_id", limit: 8
62
+ t.datetime "created_at"
63
+ t.datetime "updated_at"
64
+ t.integer "parent_id", limit: 8
65
+ end
66
+
67
+ end
@@ -0,0 +1,87 @@
1
+  (1.0ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL) 
2
+  (1.3ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
3
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
4
+ Migrating to CreateSwitchmanShards (20130328212039)
5
+  (0.0ms) begin transaction
6
+  (0.4ms) CREATE TABLE "switchman_shards" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255), "database_server_id" varchar(255), "default" boolean DEFAULT 'f' NOT NULL) 
7
+ SQL (0.2ms) INSERT INTO "schema_migrations" ("version") VALUES (?) [["version", "20130328212039"]]
8
+  (0.8ms) commit transaction
9
+ Migrating to CreateDefaultShard (20130328224244)
10
+  (0.1ms) begin transaction
11
+ Switchman::Shard Exists (0.1ms) SELECT 1 AS one FROM "switchman_shards" WHERE "switchman_shards"."default" = 't' LIMIT 1
12
+ SQL (0.3ms) INSERT INTO "switchman_shards" ("default") VALUES (?) [["default", true]]
13
+ Switchman::Shard Load (0.1ms) SELECT "switchman_shards".* FROM "switchman_shards" WHERE "switchman_shards"."default" = 't' LIMIT 1
14
+  (0.2ms) begin transaction [shard 1 deploy]
15
+ SQL (5023.8ms) INSERT INTO "schema_migrations" ("version") VALUES (?) [["version", "20130328224244"]] [shard 1 deploy]
16
+ SQLite3::BusyException: database is locked: INSERT INTO "schema_migrations" ("version") VALUES (?)
17
+  (0.1ms) rollback transaction [shard 1 deploy]
18
+  (6.5ms) rollback transaction
19
+  (1.3ms) SELECT * FROM unnest(current_schemas(false))
20
+ PG::UndefinedTable: ERROR: relation "switchman_shards" does not exist
21
+ LINE 5: WHERE a.attrelid = '"switchman_shards"'::regc...
22
+ ^
23
+ : SELECT a.attname, format_type(a.atttypid, a.atttypmod),
24
+ pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod
25
+ FROM pg_attribute a LEFT JOIN pg_attrdef d
26
+ ON a.attrelid = d.adrelid AND a.attnum = d.adnum
27
+ WHERE a.attrelid = '"switchman_shards"'::regclass
28
+ AND a.attnum > 0 AND NOT a.attisdropped
29
+ ORDER BY a.attnum
30
+
31
+  (2.0ms) CREATE TABLE "schema_migrations" ("version" character varying(255) NOT NULL)
32
+  (1.2ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
33
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
34
+ Migrating to CreateSwitchmanShards (20130328212039)
35
+  (0.1ms) BEGIN
36
+  (3.7ms) CREATE TABLE "switchman_shards" ("id" bigserial primary key, "name" character varying(255), "database_server_id" character varying(255), "default" boolean DEFAULT 'f' NOT NULL)
37
+ SQL (0.5ms) INSERT INTO "schema_migrations" ("version") VALUES ($1) [["version", "20130328212039"]]
38
+  (0.4ms) COMMIT
39
+ Migrating to CreateDefaultShard (20130328224244)
40
+  (0.3ms) BEGIN
41
+ Switchman::Shard Exists (0.5ms) SELECT 1 AS one FROM "switchman_shards" WHERE "switchman_shards"."default" = 't' LIMIT 1
42
+ SQL (0.5ms) INSERT INTO "switchman_shards" ("default") VALUES ($1) RETURNING "id" [["default", true]]
43
+ Switchman::Shard Load (0.2ms) SELECT "switchman_shards".* FROM "switchman_shards" WHERE "switchman_shards"."default" = 't' LIMIT 1
44
+ SQL (0.2ms) INSERT INTO "schema_migrations" ("version") VALUES ($1) [["version", "20130328224244"]]
45
+  (0.3ms) COMMIT
46
+ Migrating to CreateUsers (20130403132607)
47
+  (0.1ms) BEGIN
48
+  (1.8ms) CREATE TABLE "users" ("id" bigserial primary key, "name" character varying(255), "mirror_user_id" bigint, "created_at" timestamp, "updated_at" timestamp)
49
+ SQL (0.1ms) INSERT INTO "schema_migrations" ("version") VALUES ($1) [["version", "20130403132607"]]
50
+  (0.4ms) COMMIT
51
+ Migrating to CreateAppendages (20130411202442)
52
+  (0.1ms) BEGIN
53
+  (1.7ms) CREATE TABLE "appendages" ("id" bigserial primary key, "user_id" bigint, "value" integer, "created_at" timestamp, "updated_at" timestamp)
54
+ SQL (0.1ms) INSERT INTO "schema_migrations" ("version") VALUES ($1) [["version", "20130411202442"]]
55
+  (0.4ms) COMMIT
56
+ Migrating to CreateMirrorUsers (20130411202551)
57
+  (0.1ms) BEGIN
58
+  (1.6ms) CREATE TABLE "mirror_users" ("id" bigserial primary key, "user_id" bigint, "created_at" timestamp, "updated_at" timestamp)
59
+ SQL (0.2ms) INSERT INTO "schema_migrations" ("version") VALUES ($1) [["version", "20130411202551"]]
60
+  (0.3ms) COMMIT
61
+ Migrating to CreateDigits (20131022202028)
62
+  (0.1ms) BEGIN
63
+  (1.6ms) CREATE TABLE "digits" ("id" bigserial primary key, "appendage_id" bigint, "value" integer, "created_at" timestamp, "updated_at" timestamp)
64
+ SQL (0.2ms) INSERT INTO "schema_migrations" ("version") VALUES ($1) [["version", "20131022202028"]]
65
+  (0.3ms) COMMIT
66
+ Migrating to CreateFeatures (20131206172923)
67
+  (0.1ms) BEGIN
68
+  (1.9ms) CREATE TABLE "features" ("id" bigserial primary key, "owner_id" bigint, "owner_type" character varying(255), "value" integer, "created_at" timestamp, "updated_at" timestamp)
69
+ SQL (0.2ms) INSERT INTO "schema_migrations" ("version") VALUES ($1) [["version", "20131206172923"]]
70
+  (0.3ms) COMMIT
71
+ Migrating to AddParentIdToUsers (20140123154135)
72
+  (0.1ms) BEGIN
73
+  (0.2ms) ALTER TABLE "users" ADD COLUMN "parent_id" bigint
74
+ SQL (0.2ms) INSERT INTO "schema_migrations" ("version") VALUES ($1) [["version", "20140123154135"]]
75
+  (0.3ms) COMMIT
76
+ Migrating to CreateRoots (20140219183820)
77
+  (0.1ms) BEGIN
78
+  (1.7ms) CREATE TABLE "roots" ("id" bigserial primary key, "user_id" bigint, "created_at" timestamp, "updated_at" timestamp)
79
+ SQL (0.4ms) INSERT INTO "schema_migrations" ("version") VALUES ($1) [["version", "20140219183820"]]
80
+  (0.4ms) COMMIT
81
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
82
+  (1.0ms) SELECT * FROM unnest(current_schemas(false))
83
+ Switchman::Shard Load (0.3ms) SELECT "switchman_shards".* FROM "switchman_shards" WHERE "switchman_shards"."default" = 't' LIMIT 1
84
+  (0.2ms) SELECT * FROM unnest(current_schemas(false))
85
+ Switchman::Shard Load (0.7ms) SELECT "switchman_shards".* FROM "switchman_shards" WHERE "switchman_shards"."database_server_id" = 'test' AND "switchman_shards"."name" IS NULL ORDER BY "switchman_shards"."id" ASC LIMIT 1
86
+ Switchman::Shard Load (0.4ms) SELECT "switchman_shards".* FROM "switchman_shards" WHERE "switchman_shards"."database_server_id" = 'production' AND "switchman_shards"."name" IS NULL ORDER BY "switchman_shards"."id" ASC LIMIT 1
87
+  (0.7ms) SELECT COUNT(*) FROM "users" [shard 1 master]
@@ -2,19 +2,73 @@ require "spec_helper"
2
2
 
3
3
  module Switchman
4
4
  describe RSpecHelper do
5
- # strategically place these before we include the module
6
- before(:all) do
7
- Shard.default.should be_a(DefaultShard)
8
- end
5
+ context "sharding" do
6
+ # strategically place these before we include the module
7
+ before(:all) do
8
+ Shard.default.should be_a(DefaultShard)
9
+ end
10
+
11
+ after(:all) do
12
+ Shard.default.should be_a(DefaultShard)
13
+ end
9
14
 
10
- after(:all) do
11
- Shard.default.should be_a(DefaultShard)
15
+ include RSpecHelper
16
+
17
+ it "should make the default shard a real shard" do
18
+ Shard.default.should be_a(Shard)
19
+ end
12
20
  end
13
21
 
14
- include RSpecHelper
22
+ context "transactions" do
23
+ include RSpecHelper
24
+
25
+ def begin_transaction(conn)
26
+ if ::Rails.version < '4'
27
+ conn.transaction_joinable = false
28
+ conn.begin_db_transaction
29
+ conn.increment_open_transactions
30
+ else
31
+ conn.begin_transaction joinable: false
32
+ end
33
+ end
34
+
35
+ def rollback_transaction(conn)
36
+ if ::Rails.version < '4'
37
+ conn.decrement_open_transactions
38
+ if conn.open_transactions == 0
39
+ conn.rollback_db_transaction
40
+ else
41
+ conn.rollback_to_savepoint
42
+ end
43
+ else
44
+ conn.rollback_transaction
45
+ end
46
+ end
47
+
48
+ before :all do
49
+ @shard2.activate do
50
+ begin_transaction(::ActiveRecord::Base.connection)
51
+ User.create!
52
+ end
53
+ end
54
+
55
+ it "should support nested transactions" do
56
+ @shard2.activate do |shard|
57
+ User.count.should == 1 # we get the user from the before :all
58
+ User.create! # should only last for the duration of this spec
59
+ conn = ::ActiveRecord::Base.connection
60
+ conn.open_transactions.should eql 2
61
+ end
62
+ end
15
63
 
16
- it "should make the default shard a real shard" do
17
- Shard.default.should be_a(Shard)
64
+ prepend_after :all do
65
+ @shard2.activate do
66
+ User.count.should == 1
67
+ conn = ::ActiveRecord::Base.connection
68
+ conn.open_transactions.should eql 1 # RSpecHelper shouldn't have rolled back the before :all one above
69
+ rollback_transaction(::ActiveRecord::Base.connection)
70
+ end
71
+ end
18
72
  end
19
73
  end
20
74
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: switchman
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.7
4
+ version: 1.2.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cody Cutrer
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2014-07-01 00:00:00.000000000 Z
13
+ date: 2014-07-07 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: railties
@@ -66,20 +66,6 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: 1.0.5
69
- - !ruby/object:Gem::Dependency
70
- name: debugger
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - ">="
74
- - !ruby/object:Gem::Version
75
- version: '0'
76
- type: :development
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - ">="
81
- - !ruby/object:Gem::Version
82
- version: '0'
83
69
  - !ruby/object:Gem::Dependency
84
70
  name: mysql2
85
71
  requirement: !ruby/object:Gem::Requirement
@@ -183,6 +169,7 @@ files:
183
169
  - spec/dummy/app/models/user.rb
184
170
  - spec/dummy/config/application.rb
185
171
  - spec/dummy/config/boot.rb
172
+ - spec/dummy/config/database.yml
186
173
  - spec/dummy/config/database.yml.example
187
174
  - spec/dummy/config/environment.rb
188
175
  - spec/dummy/config/environments/development.rb
@@ -194,6 +181,7 @@ files:
194
181
  - spec/dummy/config/initializers/wrap_parameters.rb
195
182
  - spec/dummy/config/routes.rb
196
183
  - spec/dummy/config.ru
184
+ - spec/dummy/db/development.sqlite3
197
185
  - spec/dummy/db/migrate/20130403132607_create_users.rb
198
186
  - spec/dummy/db/migrate/20130411202442_create_appendages.rb
199
187
  - spec/dummy/db/migrate/20130411202551_create_mirror_users.rb
@@ -201,6 +189,8 @@ files:
201
189
  - spec/dummy/db/migrate/20131206172923_create_features.rb
202
190
  - spec/dummy/db/migrate/20140123154135_add_parent_id_to_users.rb
203
191
  - spec/dummy/db/migrate/20140219183820_create_roots.rb
192
+ - spec/dummy/db/schema.rb
193
+ - spec/dummy/log/development.log
204
194
  - spec/dummy/Rakefile
205
195
  - spec/dummy/script/rails
206
196
  - spec/lib/action_controller/caching_spec.rb
@@ -257,6 +247,7 @@ test_files:
257
247
  - spec/dummy/app/models/user.rb
258
248
  - spec/dummy/config/application.rb
259
249
  - spec/dummy/config/boot.rb
250
+ - spec/dummy/config/database.yml
260
251
  - spec/dummy/config/database.yml.example
261
252
  - spec/dummy/config/environment.rb
262
253
  - spec/dummy/config/environments/development.rb
@@ -268,6 +259,7 @@ test_files:
268
259
  - spec/dummy/config/initializers/wrap_parameters.rb
269
260
  - spec/dummy/config/routes.rb
270
261
  - spec/dummy/config.ru
262
+ - spec/dummy/db/development.sqlite3
271
263
  - spec/dummy/db/migrate/20130403132607_create_users.rb
272
264
  - spec/dummy/db/migrate/20130411202442_create_appendages.rb
273
265
  - spec/dummy/db/migrate/20130411202551_create_mirror_users.rb
@@ -275,6 +267,8 @@ test_files:
275
267
  - spec/dummy/db/migrate/20131206172923_create_features.rb
276
268
  - spec/dummy/db/migrate/20140123154135_add_parent_id_to_users.rb
277
269
  - spec/dummy/db/migrate/20140219183820_create_roots.rb
270
+ - spec/dummy/db/schema.rb
271
+ - spec/dummy/log/development.log
278
272
  - spec/dummy/Rakefile
279
273
  - spec/dummy/script/rails
280
274
  - spec/lib/action_controller/caching_spec.rb