Skip to content

Commit

Permalink
Minimize conflicts (#240)
Browse files Browse the repository at this point in the history
Minimize merge conflicts

Based on:

https://github.com/lfittl/activerecord-clean-db-structure

- Extract sanitize_migration_timestamps to mysql
- Minimize conflicts in schema migrations
- Always load the defaults for the current version being tested
  • Loading branch information
xjunior authored Feb 5, 2024
1 parent a2435cb commit 1ff3b89
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 11 deletions.
2 changes: 1 addition & 1 deletion packages/edgestitch/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ PATH
PATH
remote: .
specs:
edgestitch (0.3.0)
edgestitch (0.4.0)

GEM
remote: https://rubygems.org/
Expand Down
7 changes: 6 additions & 1 deletion packages/edgestitch/docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## [0.4.0] - 2023-01-25

- Minimize merge conflcits on structure-self.sql [#240](https://github.com/powerhome/power-tools/pull/240)
- Let bundler manage LOAD_PATH alone [#111](https://github.com/powerhome/power-tools/pull/111)

## [0.3.0] - 2023-01-25

- Allow disabling rails tasks enhancing by @xjunior in [#84](https://github.com/powerhome/power-tools/pull/84)
Expand All @@ -10,4 +15,4 @@
## [0.1.0] - 2022-12-27

- First release of Edgestitch, extracted from NitroMysql.
- Supports all rails >= 6.0 and all ruby >= 2.7
- Supports all rails >= 6.0 and all ruby >= 2.7
2 changes: 1 addition & 1 deletion packages/edgestitch/gemfiles/rails_6_0.gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ PATH
PATH
remote: ..
specs:
edgestitch (0.3.0)
edgestitch (0.4.0)

GEM
remote: https://rubygems.org/
Expand Down
2 changes: 1 addition & 1 deletion packages/edgestitch/gemfiles/rails_6_1.gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ PATH
PATH
remote: ..
specs:
edgestitch (0.3.0)
edgestitch (0.4.0)

GEM
remote: https://rubygems.org/
Expand Down
2 changes: 1 addition & 1 deletion packages/edgestitch/gemfiles/rails_7_0.gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ PATH
PATH
remote: ..
specs:
edgestitch (0.3.0)
edgestitch (0.4.0)

GEM
remote: https://rubygems.org/
Expand Down
28 changes: 24 additions & 4 deletions packages/edgestitch/lib/edgestitch/mysql/dump.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ module Mysql
# Wrapper for the mysqldump tool to dump specific tables and migration data
#
class Dump
# @private
#
# Sanitizes a DDL code with some opinionated preferences:
# * Constraints starting with `_fk` will start with `fk`
# * Clear empty lines (with empty spaces even)
# * Reorder constraints (@see Edgestitch::Mysql::StructureConstraintOrderMunger)
#
# @param sql [String] the DDL code to sanitize
# @return String the same DDL sanitized
def self.sanitize_sql(sql)
def self.sanitize_create_table(sql)
comment_instructions_regex = %r{^/\*![0-9]{5}\s+[^;]+;\s*$}

cleanups = sql.gsub(/\s+AUTO_INCREMENT=\d+/, "")
Expand All @@ -29,6 +31,24 @@ def self.sanitize_sql(sql)
::Edgestitch::Mysql::StructureConstraintOrderMunger.munge(cleanups)
end

# @private
#
# Sanitizes a DML code to insert migration timestamps with opinionated preferences:
# * One timestamp per line
# * Except for the first line, they all start with a comma
# * Semicolomn is in a separate line from the last one
#
# The above preferences come from the idea of minimizing merge conflicts, or make
# them easily handled by git automatically
#
# @param sql [String] the DML code to sanitize
# @return String the same DML sanitized
def self.sanitize_migration_timestamps(sql)
sql.gsub("VALUES ", "VALUES\n")
.gsub(",", "\n,")
.gsub(";", "\n;")
end

#
# @param config [ActiveRecord::DatabaseConfigurations::DatabaseConfig] rails database configuration
def initialize(config)
Expand All @@ -49,7 +69,7 @@ def initialize(config)
def export_tables(tables)
return if tables.empty?

self.class.sanitize_sql(
self.class.sanitize_create_table(
execute("--compact", "--skip-lock-tables", "--no-data", "--set-gtid-purged=OFF",
"--column-statistics=0", *tables)
)
Expand All @@ -65,13 +85,13 @@ def export_tables(tables)
# @return String the INSERT statements.
def export_migrations(migrations)
migrations.in_groups_of(50, false).map do |versions|
execute(
self.class.sanitize_migration_timestamps execute(
"--compact", "--skip-lock-tables", "--set-gtid-purged=OFF",
"--no-create-info", "--column-statistics=0",
"schema_migrations",
"-w", "version IN (#{versions.join(',')})"
)
end.join.gsub("VALUES ", "VALUES\n").gsub(",", ",\n")
end.join
end

private
Expand Down
2 changes: 1 addition & 1 deletion packages/edgestitch/lib/edgestitch/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Edgestitch
VERSION = "0.3.0"
VERSION = "0.4.0"
end
2 changes: 1 addition & 1 deletion packages/edgestitch/spec/dummy/config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

module Dummy
class Application < Rails::Application
config.load_defaults 6.0
config.load_defaults Rails.version[0..2]

config.active_record.schema_format = :sql
end
Expand Down
26 changes: 26 additions & 0 deletions packages/edgestitch/spec/edgestitch/mysql/dump_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe Edgestitch::Mysql::Dump do
describe ".sanitize_migration_timestamps" do
it "breaks the insert down in different lines" do
insert = "INSERT INTO schema_migrations VALUES ('20240102123421'),('20240102223421')," \
"('20240102323421'),('20240102423421'),('20240102523421');"

sanitized = Edgestitch::Mysql::Dump.sanitize_migration_timestamps(insert)

# rubocop:disable Rails/SquishedSQLHeredocs
expect(sanitized).to eql <<~SQL.strip
INSERT INTO schema_migrations VALUES
('20240102123421')
,('20240102223421')
,('20240102323421')
,('20240102423421')
,('20240102523421')
;
SQL
# rubocop:enable Rails/SquishedSQLHeredocs
end
end
end

0 comments on commit 1ff3b89

Please sign in to comment.