actual_db_schema 0.6.0 → 0.7.1

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
  SHA256:
3
- metadata.gz: 116a62e0bbc1d2f43ad5e7654f732807bb6807520f26f33bfde84889601bb895
4
- data.tar.gz: 63c36388ed0efa1d19b9daec936206b6580e22b1b04b25a0db3f058047c34b56
3
+ metadata.gz: eac7885f8774402bb8f0532caf66d4304df61be50ef0c22f1fd8f6ef325b8330
4
+ data.tar.gz: 8c90d4101e41941a4c247f6af2695e050f450a46435b659c99e1d5fe8fd71929
5
5
  SHA512:
6
- metadata.gz: ef27b62051c6183b68a35018b0ed7f402f25f8de2f3d3da8f842b914c13516a49b00ab57687a513bca04b664fc682b40c93c75929e414dfea514c2e0065a68b9
7
- data.tar.gz: 7a7f0ce3ef0ce09a5a0ecf90ff0aec292ae6f9ab58050ce946dd849123b36162398592a9249ea9ec4acecbe9cfe2031737cbe16c5c0b5ef89989ebb48237fee9
6
+ metadata.gz: 794038e111d31a3e99b1a48c3c3a0603498e6492e8b2a8f050d4e9e481debe047d908ff5cae5940789c96c3d76da13a10a841c55834275392e0d08076b4b6b52
7
+ data.tar.gz: 342ba459e1705c42a64d0f476c50054724a48b212bbba79a82f85a1a4118ac342bac7d0ebab3158bcab58f5d6068c1e4f344f813a6464091e20545815ff09dc3
data/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
+ ## [0.7.1] - 2024-03-19
2
+
3
+ - add csv as a dependency since Ruby 3.3 has removed it from the standard library
4
+
5
+ ## [0.7.0] - 2024-01-18
6
+
7
+ - db:phantom_migrations displays the branch in which the phantion migration was run
8
+
1
9
  ## [0.6.0] - 2024-01-03
2
10
 
3
11
  - Added db:phantom_migrations task to display phantom migrations
data/Gemfile.lock CHANGED
@@ -1,9 +1,10 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- actual_db_schema (0.6.0)
4
+ actual_db_schema (0.7.1)
5
5
  activerecord (>= 6.0.0)
6
6
  activesupport (>= 6.0.0)
7
+ csv
7
8
 
8
9
  GEM
9
10
  remote: https://rubygems.org/
@@ -92,6 +93,7 @@ GEM
92
93
  concurrent-ruby (1.2.2)
93
94
  connection_pool (2.4.1)
94
95
  crass (1.0.6)
96
+ csv (3.2.8)
95
97
  date (3.3.3)
96
98
  debug (1.8.0)
97
99
  irb (>= 1.5.0)
data/README.md CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  Does switching between branches in your Rails app mess up the DB schema?
6
6
 
7
- Keep the DB schema actual across branches in your Rails project. Just install `actual_db_schema` gem and run `db:migrate` in branches as usual. It automatically rolls back the *phantom migrations* (non-relevant to the current branch). No additional steps are needed.
7
+ Keep the DB schema actual across branches in your Rails project. Just install `actual_db_schema` gem and run `db:migrate` in branches as usual. It automatically rolls back the *phantom migrations* (non-relevant to the current branch). No additional steps are needed. It works with both `schema.rb` and `structure.sql`.
8
8
 
9
9
  ## Why ActualDbSchema
10
10
 
@@ -28,9 +28,9 @@ With `actual_db_schema` gem you don't need to care about that anymore. It saves
28
28
 
29
29
  ### How it solves the issue
30
30
 
31
- This gem stores all run migrations with their code in the `tmp/migrations` folder. Whenever you perform a schema dump, it rolls back the *phantom migrations*.
31
+ This gem stores all run migrations with their code in the `tmp/migrated` folder. Whenever you perform a schema dump, it rolls back the *phantom migrations*.
32
32
 
33
- The *phantom migrations* list is the difference between the migrations you've executed (in the `tmp/migrations` folder) and the current ones (in the `db/migrate` folder).
33
+ The *phantom migrations* list is the difference between the migrations you've executed (in the `tmp/migrated` folder) and the current ones (in the `db/migrate` folder).
34
34
 
35
35
  Therefore, all you do is run rails `db:migrate` in your current branch. `actual_db_schema` will ensure the DB schema is up-to-date. You'll never have an inaccurate `schema.rb` file again.
36
36
 
@@ -63,7 +63,15 @@ The gem offers the following rake tasks that can be manually run according to yo
63
63
 
64
64
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
65
65
 
66
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
66
+ To install this gem onto your local machine, run `bundle exec rake install`.
67
+
68
+ To release a new version do the following in the order:
69
+
70
+ - update the version number in `version.rb`;
71
+ - update the CHANGELOG;
72
+ - `bundle install` to update `Gemfile.lock`;
73
+ - make the commit and push;
74
+ - run `bundle exec rake release`. This will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
67
75
 
68
76
  ## Contributing
69
77
 
@@ -38,6 +38,7 @@ Gem::Specification.new do |spec|
38
38
  # Uncomment to register a new dependency of your gem
39
39
  spec.add_runtime_dependency "activerecord", ">= 6.0.0"
40
40
  spec.add_runtime_dependency "activesupport", ">= 6.0.0"
41
+ spec.add_runtime_dependency "csv"
41
42
 
42
43
  spec.add_development_dependency "appraisal"
43
44
  spec.add_development_dependency "debug"
@@ -20,18 +20,59 @@ module ActualDbSchema
20
20
  puts "Below is a list of irrelevant migrations executed in unmerged branches."
21
21
  puts "To bring your database schema up to date, the migrations marked as \"up\" should be rolled back."
22
22
  puts "\ndatabase: #{ActiveRecord::Base.connection_db_config.database}\n\n"
23
- puts %(#{"Status".center(8)} #{"Migration ID".ljust(14)} Migration File)
24
- puts "-" * 50
23
+ puts header.join(" ")
24
+ puts "-" * separator_width
25
+ end
26
+
27
+ def separator_width
28
+ header.map(&:length).sum + (header.size - 1) * 2
29
+ end
30
+
31
+ def header
32
+ @header ||=
33
+ [
34
+ "Status".center(8),
35
+ "Migration ID".ljust(14),
36
+ "Branch".ljust(branch_column_width),
37
+ "Migration File".ljust(16)
38
+ ]
25
39
  end
26
40
 
27
41
  def table
28
42
  context.migrations_status.each do |status, version|
29
- migration = indexed_phantom_migrations[version]
30
- next unless migration
31
-
32
- puts %(#{status.center(8)} #{version.to_s.ljust(14)} #{migration.filename.gsub("#{Rails.root}/", "")})
43
+ line = line_for(status, version)
44
+ puts line if line
33
45
  end
34
46
  end
47
+
48
+ def line_for(status, version)
49
+ migration = indexed_phantom_migrations[version]
50
+ return unless migration
51
+
52
+ [
53
+ status.center(8),
54
+ version.to_s.ljust(14),
55
+ branch_for(version).ljust(branch_column_width),
56
+ migration.filename.gsub("#{Rails.root}/", "")
57
+ ].join(" ")
58
+ end
59
+
60
+ def branch_for(version)
61
+ metadata.fetch(version, {})[:branch] || "unknown"
62
+ end
63
+
64
+ def metadata
65
+ @metadata ||= ActualDbSchema::Store.instance.read
66
+ end
67
+
68
+ def longest_branch_name
69
+ @longest_branch_name ||=
70
+ metadata.values.map { |v| v[:branch] }.compact.max_by(&:length) || "unknown"
71
+ end
72
+
73
+ def branch_column_width
74
+ longest_branch_name.length
75
+ end
35
76
  end
36
77
  end
37
78
  end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActualDbSchema
4
+ # Git helper
5
+ class Git
6
+ def self.current_branch
7
+ `git rev-parse --abbrev-ref HEAD`.strip
8
+ rescue Errno::ENOENT
9
+ "unknown"
10
+ end
11
+ end
12
+ end
@@ -6,7 +6,7 @@ module ActualDbSchema
6
6
  module MigrationProxy
7
7
  def migrate(direction)
8
8
  super(direction)
9
- FileUtils.copy(filename, ActualDbSchema.migrated_folder.join(basename)) if direction == :up
9
+ ActualDbSchema::Store.instance.write(filename) if direction == :up
10
10
  end
11
11
  end
12
12
  end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActualDbSchema
4
+ # Stores the migrated files into the tmp folder
5
+ class Store
6
+ include Singleton
7
+
8
+ Item = Struct.new(:version, :timestamp, :branch)
9
+
10
+ def write(filename)
11
+ basename = File.basename(filename)
12
+ FileUtils.copy(filename, folder.join(basename))
13
+ record_metadata(filename)
14
+ end
15
+
16
+ def read
17
+ return {} unless File.exist?(store_file)
18
+
19
+ CSV.read(store_file).map { |line| Item.new(*line) }.index_by(&:version)
20
+ end
21
+
22
+ private
23
+
24
+ def record_metadata(filename)
25
+ version = File.basename(filename).scan(/(\d+)_.*\.rb/).first.first
26
+ CSV.open(store_file, "a") do |csv|
27
+ csv << [
28
+ version,
29
+ Time.current.iso8601,
30
+ Git.current_branch
31
+ ]
32
+ end
33
+ end
34
+
35
+ def folder
36
+ ActualDbSchema.migrated_folder
37
+ end
38
+
39
+ def store_file
40
+ folder.join("metadata.csv")
41
+ end
42
+ end
43
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActualDbSchema
4
- VERSION = "0.6.0"
4
+ VERSION = "0.7.1"
5
5
  end
@@ -1,6 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "active_record/migration"
4
+ require "csv"
5
+ require_relative "actual_db_schema/git"
6
+ require_relative "actual_db_schema/store"
4
7
  require_relative "actual_db_schema/version"
5
8
  require_relative "actual_db_schema/patches/migration_proxy"
6
9
  require_relative "actual_db_schema/patches/migrator"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: actual_db_schema
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrei Kaleshka
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-01-03 00:00:00.000000000 Z
11
+ date: 2024-03-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: 6.0.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: csv
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: appraisal
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -122,9 +136,11 @@ files:
122
136
  - lib/actual_db_schema/commands/base.rb
123
137
  - lib/actual_db_schema/commands/list.rb
124
138
  - lib/actual_db_schema/commands/rollback.rb
139
+ - lib/actual_db_schema/git.rb
125
140
  - lib/actual_db_schema/patches/migration_context.rb
126
141
  - lib/actual_db_schema/patches/migration_proxy.rb
127
142
  - lib/actual_db_schema/patches/migrator.rb
143
+ - lib/actual_db_schema/store.rb
128
144
  - lib/actual_db_schema/version.rb
129
145
  - lib/railtie.rb
130
146
  - lib/tasks/db.rake