From 0c62b49484c65d6eadfdf8602f507b55497e442f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Coutable?= Date: Tue, 6 Feb 2018 11:13:10 +0100 Subject: Reset column information after the schema is migrated in MigrationsHelpers.schema_migrate_up! MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rémy Coutable --- spec/support/migrations_helpers.rb | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) (limited to 'spec/support/migrations_helpers.rb') diff --git a/spec/support/migrations_helpers.rb b/spec/support/migrations_helpers.rb index 6522d74ba89..ba4a1bee089 100644 --- a/spec/support/migrations_helpers.rb +++ b/spec/support/migrations_helpers.rb @@ -15,18 +15,22 @@ module MigrationsHelpers ActiveRecord::Migrator.migrations(migrations_paths) end - def reset_column_in_migration_models + def clear_schema_cache! ActiveRecord::Base.connection_pool.connections.each do |conn| conn.schema_cache.clear! end + end - described_class.constants.sort.each do |name| - const = described_class.const_get(name) + def reset_column_in_all_models + clear_schema_cache! - if const.is_a?(Class) && const < ActiveRecord::Base - const.reset_column_information - end - end + # Reset column information for the most offending classes **after** we + # migrated the schema up, otherwise, column information could be outdated + ActiveRecord::Base.descendants.each { |klass| klass.reset_column_information } + + # Without that, we get errors because of missing attributes, e.g. + # super: no superclass method `elasticsearch_indexing' for # + ApplicationSetting.define_attribute_methods end def previous_migration @@ -45,7 +49,7 @@ module MigrationsHelpers migration_schema_version) end - reset_column_in_migration_models + reset_column_in_all_models end def schema_migrate_up! @@ -53,7 +57,7 @@ module MigrationsHelpers ActiveRecord::Migrator.migrate(migrations_paths) end - reset_column_in_migration_models + reset_column_in_all_models end def disable_migrations_output -- cgit v1.2.1 From 77c1d87feabc6afcad3c3fce44c6884b54345a43 Mon Sep 17 00:00:00 2001 From: Sean McGivern Date: Thu, 8 Feb 2018 16:31:36 +0000 Subject: Make resetting column information overridable in EE --- spec/support/migrations_helpers.rb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'spec/support/migrations_helpers.rb') diff --git a/spec/support/migrations_helpers.rb b/spec/support/migrations_helpers.rb index ba4a1bee089..06322aa0586 100644 --- a/spec/support/migrations_helpers.rb +++ b/spec/support/migrations_helpers.rb @@ -25,14 +25,19 @@ module MigrationsHelpers clear_schema_cache! # Reset column information for the most offending classes **after** we - # migrated the schema up, otherwise, column information could be outdated - ActiveRecord::Base.descendants.each { |klass| klass.reset_column_information } + # migrated the schema up, otherwise, column information could be + # outdated. We have a separate method for this so we can override it in EE. + ActiveRecord::Base.descendants.each(&method(:reset_column_information)) # Without that, we get errors because of missing attributes, e.g. # super: no superclass method `elasticsearch_indexing' for # ApplicationSetting.define_attribute_methods end + def reset_column_information(klass) + klass.reset_column_information + end + def previous_migration migrations.each_cons(2) do |previous, migration| break previous if migration.name == described_class.name -- cgit v1.2.1 From 5912d9b70b35769b7da468113ea6680ef8cbc309 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Coutable?= Date: Mon, 12 Feb 2018 15:33:39 +0100 Subject: Call .reset_column_in_all_models! before migrating in MigrationsHelpers.schema_migrate_up! MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rémy Coutable --- spec/support/migrations_helpers.rb | 2 ++ 1 file changed, 2 insertions(+) (limited to 'spec/support/migrations_helpers.rb') diff --git a/spec/support/migrations_helpers.rb b/spec/support/migrations_helpers.rb index 06322aa0586..9a151439486 100644 --- a/spec/support/migrations_helpers.rb +++ b/spec/support/migrations_helpers.rb @@ -58,6 +58,8 @@ module MigrationsHelpers end def schema_migrate_up! + reset_column_in_all_models + disable_migrations_output do ActiveRecord::Migrator.migrate(migrations_paths) end -- cgit v1.2.1 From f6357b77ff8aa36b143794b43d42145f5a6bb642 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Coutable?= Date: Mon, 12 Feb 2018 15:31:12 +0100 Subject: Allow to use the latest migration in migration specs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is useful for migration tests that relies on factories and that are very old and/or tedious to modify to not use factories. Signed-off-by: Rémy Coutable --- spec/support/migrations_helpers.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'spec/support/migrations_helpers.rb') diff --git a/spec/support/migrations_helpers.rb b/spec/support/migrations_helpers.rb index 9a151439486..6bf976a2cf9 100644 --- a/spec/support/migrations_helpers.rb +++ b/spec/support/migrations_helpers.rb @@ -45,7 +45,13 @@ module MigrationsHelpers end def migration_schema_version - self.class.metadata[:schema] || previous_migration.version + metadata_schema = self.class.metadata[:schema] + + if metadata_schema == :latest + migrations.last.version + else + metadata_schema || previous_migration.version + end end def schema_migrate_down! -- cgit v1.2.1