ec-pg 0.1.3 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/ec/pg/migrator.rb +23 -87
- data/lib/ec/pg/version.rb +1 -1
- data/lib/tasks/ec_pg_migrate.rake +8 -39
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: fe77cf4fcd3e6dd213ece5869aa34f8d25114e448b42f428173e2fec8f3160ce
|
|
4
|
+
data.tar.gz: f84123214c7dcf8d6a8abc0c5e676accf604649cb36d2b214b245b978449c2e4
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: b9361a94d4f3719a24a6dfda8240dc1c25bb2ce3c4780a4ff1bfa6cc697fba092028925a2803fa8c9818e00e8c32d705174bf459e8e05890bc6e806134c58f2d
|
|
7
|
+
data.tar.gz: 2b9f3c7caffd8012a41fe60be30ac227c4b98b8e4abcf2c29cfeba016e3f8f629eb7023f63f672c8a6c625689663f86bef9e310046495d7895182c4ce9f539c0
|
data/lib/ec/pg/migrator.rb
CHANGED
|
@@ -5,116 +5,52 @@ module Ec
|
|
|
5
5
|
# Runs ActiveRecord migrations in the context of a specific schema or shard.
|
|
6
6
|
#
|
|
7
7
|
# == Schema migrations
|
|
8
|
-
#
|
|
9
|
-
# Sets search_path to the target schema before running migrations so that
|
|
10
|
-
# Rails creates (and tracks via schema_migrations) tables inside that schema.
|
|
11
|
-
#
|
|
12
|
-
# Migrator.migrate_schema("tenant_abc")
|
|
13
|
-
# Migrator.migrate_schema("tenant_abc", version: 20240101120000)
|
|
14
|
-
# Migrator.rollback_schema("tenant_abc", steps: 2)
|
|
15
|
-
#
|
|
16
|
-
# Migrator.migrate_each_schema(["tenant_a", "tenant_b"])
|
|
17
|
-
#
|
|
18
|
-
# == Shard migrations
|
|
19
|
-
#
|
|
20
|
-
# Connects to the target shard before running migrations.
|
|
21
|
-
#
|
|
22
|
-
# Migrator.migrate_shard(:shard_one)
|
|
23
|
-
# Migrator.rollback_shard(:shard_one, steps: 1)
|
|
24
|
-
#
|
|
25
|
-
# # Migrate all shards derived from configuration.number_of_shards
|
|
26
|
-
# Migrator.migrate_each_shard
|
|
27
|
-
#
|
|
28
|
-
# # Or supply an explicit list
|
|
29
|
-
# Migrator.migrate_each_shard([:shard_one, :shard_two])
|
|
30
|
-
#
|
|
31
8
|
class Migrator
|
|
32
9
|
class << self
|
|
33
|
-
# Runs pending migrations with search_path set to +schema_name+.
|
|
34
|
-
#
|
|
35
|
-
# @param schema_name [String]
|
|
36
|
-
# @param paths [Array<String>] migration paths (defaults to Rails configured paths)
|
|
37
|
-
# @param version [Integer, nil] target version; migrates to latest when nil
|
|
38
|
-
def migrate_schema(connection, schema_name, paths: nil, version: nil)
|
|
39
|
-
Ec::Pg.dprint("Running migration for: #{schema_name}".red)
|
|
40
|
-
connection.schema_search_path = schema_name
|
|
41
|
-
run_migrations(connection, paths: paths, version: version)
|
|
42
|
-
end
|
|
43
10
|
|
|
11
|
+
# Runs pending migrations on all schemas
|
|
12
|
+
#
|
|
44
13
|
def migrate_all_schemas
|
|
45
14
|
Ec::Pg::SchemaMixin.registered_models.each do |model|
|
|
46
15
|
model.schemas.each do |shard, schemas|
|
|
47
16
|
Ec::Pg.switch(shard: shard) do
|
|
48
17
|
schemas.each do |schema|
|
|
49
|
-
Ec::Pg.dprint("migrating model: #{model}, shard: #{shard}, schema: #{schema}")
|
|
50
|
-
|
|
18
|
+
Ec::Pg.dprint("migrating model: #{model}, shard: #{shard}, schema: #{schema}".red)
|
|
19
|
+
migrate_schema(model.connection.pool, schema)
|
|
51
20
|
end
|
|
52
21
|
end
|
|
53
22
|
end
|
|
54
23
|
end
|
|
55
24
|
end
|
|
56
25
|
|
|
57
|
-
#
|
|
58
|
-
#
|
|
59
|
-
# @param schema_names [Array<String>]
|
|
60
|
-
def migrate_each_schema(connection, schema_names, paths: nil, version: nil)
|
|
61
|
-
schema_names.each { |name| migrate_schema(connection, name, paths: paths, version: version) }
|
|
62
|
-
end
|
|
63
|
-
|
|
64
|
-
# Rolls back +steps+ migrations with search_path set to +schema_name+.
|
|
65
|
-
#
|
|
66
|
-
# @param schema_name [String]
|
|
67
|
-
# @param steps [Integer] number of migrations to roll back (default 1)
|
|
68
|
-
def rollback_schema(schema_name, steps: 1, paths: nil)
|
|
69
|
-
SchemaManager.with_schema(schema_name) do
|
|
70
|
-
build_context(paths).rollback(steps)
|
|
71
|
-
end
|
|
72
|
-
end
|
|
73
|
-
|
|
74
|
-
# Runs pending migrations connected to +shard_name+.
|
|
26
|
+
# Rollsback schemas
|
|
75
27
|
#
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
# When +shard_names+ is omitted, derives +:shard_1+ through +:shard_N+
|
|
87
|
-
# from +configuration.number_of_shards+.
|
|
88
|
-
#
|
|
89
|
-
# @param shard_names [Array<Symbol>, nil]
|
|
90
|
-
def migrate_each_shard(shard_names = nil, paths: nil, version: nil)
|
|
91
|
-
(shard_names || default_shards).each do |name|
|
|
92
|
-
migrate_shard(name, paths: paths, version: version)
|
|
28
|
+
def rollback_all_schemas(steps: 1)
|
|
29
|
+
Ec::Pg::SchemaMixin.registered_models.each do |model|
|
|
30
|
+
model.schemas.each do |shard, schemas|
|
|
31
|
+
Ec::Pg.switch(shard: shard) do
|
|
32
|
+
schemas.each do |schema|
|
|
33
|
+
Ec::Pg.dprint("rolling back model: #{model}, shard: #{shard}, schema: #{schema}".red)
|
|
34
|
+
rollback_schema(model.connection.pool, schema, steps)
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
93
38
|
end
|
|
94
39
|
end
|
|
95
40
|
|
|
96
|
-
# Rolls back +steps+ migrations connected to +shard_name+.
|
|
97
|
-
#
|
|
98
|
-
# @param shard_name [Symbol]
|
|
99
|
-
# @param steps [Integer]
|
|
100
|
-
def rollback_shard(shard_name, steps: 1, paths: nil)
|
|
101
|
-
ShardManager.with_shard(shard_name) do
|
|
102
|
-
build_context(paths).rollback(steps)
|
|
103
|
-
end
|
|
104
|
-
end
|
|
105
41
|
|
|
106
42
|
private
|
|
107
43
|
|
|
108
|
-
def
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
connection.pool.migration_context.migrate
|
|
44
|
+
def rollback_schema(pool, schema_name, steps)
|
|
45
|
+
ActiveRecord::Base.establish_connection(pool.db_config)
|
|
46
|
+
pool.schema_search_path = schema_name
|
|
47
|
+
pool.migration_context.rollback(steps)
|
|
113
48
|
end
|
|
114
49
|
|
|
115
|
-
def
|
|
116
|
-
|
|
117
|
-
|
|
50
|
+
def migrate_schema(pool, schema_name)
|
|
51
|
+
ActiveRecord::Base.establish_connection(pool.db_config)
|
|
52
|
+
pool.schema_search_path = schema_name
|
|
53
|
+
pool.migration_context.migrate
|
|
118
54
|
end
|
|
119
55
|
end
|
|
120
56
|
end
|
data/lib/ec/pg/version.rb
CHANGED
|
@@ -4,9 +4,9 @@ namespace :ec_pg do
|
|
|
4
4
|
# ---------------------------------------------------------------------------
|
|
5
5
|
# Migrations
|
|
6
6
|
# ---------------------------------------------------------------------------
|
|
7
|
-
namespace :
|
|
7
|
+
namespace :schemas do
|
|
8
8
|
task preload_models: :environment do
|
|
9
|
-
if ENV['
|
|
9
|
+
if ENV['ECPG_DEBUG']
|
|
10
10
|
ActiveRecord::Base.logger = Logger.new(STDOUT)
|
|
11
11
|
end
|
|
12
12
|
|
|
@@ -15,46 +15,15 @@ namespace :ec_pg do
|
|
|
15
15
|
end
|
|
16
16
|
end
|
|
17
17
|
|
|
18
|
-
desc "Migrate
|
|
19
|
-
task :
|
|
20
|
-
name = args[:name] or abort "Usage: rake 'ec_pg:migrate:schema[schema_name]'"
|
|
21
|
-
Ec::Pg::Migrator.migrate_schema(name)
|
|
22
|
-
puts "Schema '#{name}' migrated."
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
desc "Migrate comma-separated schemas. Usage: rake 'ec_pg:migrate:schemas[tenant_a,tenant_b]'"
|
|
26
|
-
task :schemas => :preload_models do
|
|
18
|
+
desc "Migrate all schemas including public."
|
|
19
|
+
task :migrate => :preload_models do
|
|
27
20
|
Ec::Pg::Migrator.migrate_all_schemas
|
|
28
21
|
end
|
|
29
22
|
|
|
30
|
-
desc "
|
|
31
|
-
task :
|
|
32
|
-
|
|
33
|
-
steps
|
|
34
|
-
Ec::Pg::Migrator.rollback_schema(name, steps: steps)
|
|
35
|
-
puts "Rolled back #{steps} migration(s) in schema '#{name}'."
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
desc "Migrate a single shard. Usage: rake 'ec_pg:migrate:shard[shard_one]'"
|
|
39
|
-
task :shard, [:name] => :environment do |_, args|
|
|
40
|
-
name = args[:name] or abort "Usage: rake 'ec_pg:migrate:shard[shard_name]'"
|
|
41
|
-
Ec::Pg::Migrator.migrate_shard(name.to_sym)
|
|
42
|
-
puts "Shard '#{name}' migrated."
|
|
43
|
-
end
|
|
44
|
-
|
|
45
|
-
desc "Migrate all shards (derived from configuration.number_of_shards, or explicit list)"
|
|
46
|
-
task :shards, [:names] => :environment do |_, args|
|
|
47
|
-
shards = args[:names]&.split(",")&.map { |s| s.strip.to_sym }
|
|
48
|
-
Ec::Pg::Migrator.migrate_each_shard(shards)
|
|
49
|
-
puts "All shards migrated."
|
|
50
|
-
end
|
|
51
|
-
|
|
52
|
-
desc "Roll back a shard. Usage: rake 'ec_pg:migrate:rollback_shard[shard_one]' or '[shard_one,2]'"
|
|
53
|
-
task :rollback_shard, [:name, :steps] => :environment do |_, args|
|
|
54
|
-
name = args[:name] or abort "Usage: rake 'ec_pg:migrate:rollback_shard[name]'"
|
|
55
|
-
steps = args[:steps]&.to_i || 1
|
|
56
|
-
Ec::Pg::Migrator.rollback_shard(name.to_sym, steps: steps)
|
|
57
|
-
puts "Rolled back #{steps} migration(s) in shard '#{name}'."
|
|
23
|
+
desc "Rollback all schemas including public. Usage: rake 'ec_pg:schemas:rollback[2]"
|
|
24
|
+
task :rollback, [:steps] => :preload_models do |_, args|
|
|
25
|
+
steps = args[:steps]&.to_i || 1
|
|
26
|
+
Ec::Pg::Migrator.rollback_all_schemas(steps: steps)
|
|
58
27
|
end
|
|
59
28
|
end
|
|
60
29
|
end
|