From 1ff3b898edc4345d94ee04031450198c33a8c3c2 Mon Sep 17 00:00:00 2001 From: Carlos Palhares Date: Mon, 5 Feb 2024 15:33:28 -0300 Subject: [PATCH] Minimize conflicts (#240) 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 --- packages/edgestitch/Gemfile.lock | 2 +- packages/edgestitch/docs/CHANGELOG.md | 7 ++++- .../gemfiles/rails_6_0.gemfile.lock | 2 +- .../gemfiles/rails_6_1.gemfile.lock | 2 +- .../gemfiles/rails_7_0.gemfile.lock | 2 +- .../edgestitch/lib/edgestitch/mysql/dump.rb | 28 ++++++++++++++++--- packages/edgestitch/lib/edgestitch/version.rb | 2 +- .../spec/dummy/config/application.rb | 2 +- .../spec/edgestitch/mysql/dump_spec.rb | 26 +++++++++++++++++ 9 files changed, 62 insertions(+), 11 deletions(-) create mode 100644 packages/edgestitch/spec/edgestitch/mysql/dump_spec.rb diff --git a/packages/edgestitch/Gemfile.lock b/packages/edgestitch/Gemfile.lock index 064e68bf..16d1ca22 100644 --- a/packages/edgestitch/Gemfile.lock +++ b/packages/edgestitch/Gemfile.lock @@ -11,7 +11,7 @@ PATH PATH remote: . specs: - edgestitch (0.3.0) + edgestitch (0.4.0) GEM remote: https://rubygems.org/ diff --git a/packages/edgestitch/docs/CHANGELOG.md b/packages/edgestitch/docs/CHANGELOG.md index 780478f8..e3cbf89f 100644 --- a/packages/edgestitch/docs/CHANGELOG.md +++ b/packages/edgestitch/docs/CHANGELOG.md @@ -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) @@ -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 \ No newline at end of file +- Supports all rails >= 6.0 and all ruby >= 2.7 diff --git a/packages/edgestitch/gemfiles/rails_6_0.gemfile.lock b/packages/edgestitch/gemfiles/rails_6_0.gemfile.lock index 8c01f704..a26fc30a 100644 --- a/packages/edgestitch/gemfiles/rails_6_0.gemfile.lock +++ b/packages/edgestitch/gemfiles/rails_6_0.gemfile.lock @@ -11,7 +11,7 @@ PATH PATH remote: .. specs: - edgestitch (0.3.0) + edgestitch (0.4.0) GEM remote: https://rubygems.org/ diff --git a/packages/edgestitch/gemfiles/rails_6_1.gemfile.lock b/packages/edgestitch/gemfiles/rails_6_1.gemfile.lock index b16779b4..e6c11ca6 100644 --- a/packages/edgestitch/gemfiles/rails_6_1.gemfile.lock +++ b/packages/edgestitch/gemfiles/rails_6_1.gemfile.lock @@ -11,7 +11,7 @@ PATH PATH remote: .. specs: - edgestitch (0.3.0) + edgestitch (0.4.0) GEM remote: https://rubygems.org/ diff --git a/packages/edgestitch/gemfiles/rails_7_0.gemfile.lock b/packages/edgestitch/gemfiles/rails_7_0.gemfile.lock index cc5ac455..1a63a190 100644 --- a/packages/edgestitch/gemfiles/rails_7_0.gemfile.lock +++ b/packages/edgestitch/gemfiles/rails_7_0.gemfile.lock @@ -11,7 +11,7 @@ PATH PATH remote: .. specs: - edgestitch (0.3.0) + edgestitch (0.4.0) GEM remote: https://rubygems.org/ diff --git a/packages/edgestitch/lib/edgestitch/mysql/dump.rb b/packages/edgestitch/lib/edgestitch/mysql/dump.rb index cb6fde75..84c6af93 100644 --- a/packages/edgestitch/lib/edgestitch/mysql/dump.rb +++ b/packages/edgestitch/lib/edgestitch/mysql/dump.rb @@ -11,6 +11,8 @@ 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) @@ -18,7 +20,7 @@ class Dump # # @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+/, "") @@ -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) @@ -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) ) @@ -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 diff --git a/packages/edgestitch/lib/edgestitch/version.rb b/packages/edgestitch/lib/edgestitch/version.rb index 24130f10..d69130a8 100644 --- a/packages/edgestitch/lib/edgestitch/version.rb +++ b/packages/edgestitch/lib/edgestitch/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module Edgestitch - VERSION = "0.3.0" + VERSION = "0.4.0" end diff --git a/packages/edgestitch/spec/dummy/config/application.rb b/packages/edgestitch/spec/dummy/config/application.rb index 856c9dfc..0a623853 100644 --- a/packages/edgestitch/spec/dummy/config/application.rb +++ b/packages/edgestitch/spec/dummy/config/application.rb @@ -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 diff --git a/packages/edgestitch/spec/edgestitch/mysql/dump_spec.rb b/packages/edgestitch/spec/edgestitch/mysql/dump_spec.rb new file mode 100644 index 00000000..95d462bb --- /dev/null +++ b/packages/edgestitch/spec/edgestitch/mysql/dump_spec.rb @@ -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