From 006406e81367c6a346056b7feb920c661a9db6eb Mon Sep 17 00:00:00 2001 From: Andrey Koleshko Date: Mon, 27 Jan 2020 22:05:13 +0300 Subject: [PATCH 1/2] Fix bundler error in tests --- migration_data.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/migration_data.gemspec b/migration_data.gemspec index 13d6df5..968da50 100644 --- a/migration_data.gemspec +++ b/migration_data.gemspec @@ -22,7 +22,7 @@ Gem::Specification.new do |spec| spec.test_files = spec.files.grep(%r{^(test)/}) spec.require_paths = ["lib"] - spec.add_development_dependency 'bundler', '~> 1.5' + spec.add_development_dependency 'bundler' spec.add_development_dependency 'rake' spec.add_development_dependency 'sqlite3' end From 2bf08250bfd7a11a02067054e7a1a767b637363b Mon Sep 17 00:00:00 2001 From: Andrey Koleshko Date: Mon, 27 Jan 2020 22:10:07 +0300 Subject: [PATCH 2/2] remove schema squasher and dumper --- README.md | 4 -- .../active_record/schema_dumper.rb | 16 ------ lib/migration_data/squash.rb | 49 ------------------- lib/tasks/db.rake | 11 ----- test/rake_task_test.rb | 49 ------------------- test/schema_dumper_test.rb | 20 -------- test/squash_test.rb | 39 --------------- 7 files changed, 188 deletions(-) delete mode 100644 lib/migration_data/active_record/schema_dumper.rb delete mode 100644 lib/migration_data/squash.rb delete mode 100644 lib/tasks/db.rake delete mode 100644 test/rake_task_test.rb delete mode 100644 test/schema_dumper_test.rb delete mode 100644 test/squash_test.rb diff --git a/README.md b/README.md index 5bdef1b..a29a047 100644 --- a/README.md +++ b/README.md @@ -83,10 +83,6 @@ end The helper to load migrations `require_migration` is defined in the `migration_data/testing`. So you should to require it to have access to this convinient require extension. -## Clean old migration - -Use `rake db:migrate:squash` to remove all your old migrations and generate one migration with the current database schema. You don't have to run migrations after this because the generated migration will have the latest database version. - ## Contributing 1. Fork it ( http://github.com/ka8725/migration_data/fork ) diff --git a/lib/migration_data/active_record/schema_dumper.rb b/lib/migration_data/active_record/schema_dumper.rb deleted file mode 100644 index 5b9ed49..0000000 --- a/lib/migration_data/active_record/schema_dumper.rb +++ /dev/null @@ -1,16 +0,0 @@ -module MigrationData - module ActiveRecord - class SchemaDumper < ::ActiveRecord::SchemaDumper - def self.dump(connection=::ActiveRecord::Base.connection, stream=StringIO.new, config = ::ActiveRecord::Base) - new(connection, generate_options(config)).dump(stream) - stream - end - - def dump(stream) - extensions(stream) - tables(stream) - stream - end - end - end -end diff --git a/lib/migration_data/squash.rb b/lib/migration_data/squash.rb deleted file mode 100644 index cbbe880..0000000 --- a/lib/migration_data/squash.rb +++ /dev/null @@ -1,49 +0,0 @@ -require 'migration_data/active_record/schema_dumper' - -module MigrationData - class Squash - def call - remove_all_old_migrations - squash_schema - end - - private - - def squash_schema - File.open(migration_file_name, 'w') do |f| - content = - <<-MIGRATION.gsub(/^ {10}/, '') - class CreateSchema < ActiveRecord::Migration - def change - #{MigrationData::ActiveRecord::SchemaDumper.dump.string.strip.gsub(/^/, ' ')} - end - end - MIGRATION - - f.write(content) - end - end - - def remove_all_old_migrations - migration_dirs.each do |dir| - FileUtils.rm_rf(Dir.glob(File.join(dir, '**/*.rb'))) - end - end - - def current_version - ::ActiveRecord::Migrator.current_version - end - - def migration_dirs - MigrationData::ActiveRecord::Migration.migration_dirs - end - - def migration_dir - MigrationData::ActiveRecord::Migration.migration_dir - end - - def migration_file_name - File.join(migration_dir, "#{current_version}_create_schema.rb") - end - end -end diff --git a/lib/tasks/db.rake b/lib/tasks/db.rake deleted file mode 100644 index 5406807..0000000 --- a/lib/tasks/db.rake +++ /dev/null @@ -1,11 +0,0 @@ -require 'migration_data/squash' - -namespace :db do - namespace :migrate do - desc 'Squashes all current migrations and dumps current schema to the latest one' - task :squash => 'db:load_config' do - ::ActiveRecord::Base.establish_connection - MigrationData::Squash.new.call - end - end -end diff --git a/test/rake_task_test.rb b/test/rake_task_test.rb deleted file mode 100644 index cf048fd..0000000 --- a/test/rake_task_test.rb +++ /dev/null @@ -1,49 +0,0 @@ -require 'test_helper' -require 'migration_data/squash' - -describe 'db:schema:squash' do - let(:rake) { Rake::Application.new } - - def migrate_existing_migrations - CreateTableMigration.new.migrate(:up) - end - - def load_rails_db_env - load 'active_record/railties/databases.rake' - Rake::Task['db:load_config'].invoke - end - - def stub_dependent_tasks - rake.intern(Rake::Task, 'db:load_config') - end - - def prepare_rake_env_for_testing - Rake.application = rake - Rake.application.rake_require('db', [File.expand_path('../../lib/tasks', __FILE__)]) - end - - before do - migrate_existing_migrations - load_rails_db_env - stub_dependent_tasks - prepare_rake_env_for_testing - end - - describe '#invoke' do - it 'squashes the schema to the current migration' do - latest_migration = Rails.root.join('db', 'migrate', '0_create_schema.rb') - FileUtils.rm(latest_migration) if File.exist?(latest_migration) - - Rake::Task['db:migrate:squash'].invoke - assert_equal <<-MIGRATION, File.read(latest_migration) -class CreateSchema < ActiveRecord::Migration - def change - create_table \"users\", force: #{FORCE_MIGRATION} do |t| - t.string \"name\" - end - end -end - MIGRATION - end - end -end diff --git a/test/schema_dumper_test.rb b/test/schema_dumper_test.rb deleted file mode 100644 index c92f436..0000000 --- a/test/schema_dumper_test.rb +++ /dev/null @@ -1,20 +0,0 @@ -require 'test_helper' -require 'migration_data/active_record/schema_dumper' - -describe MigrationData::ActiveRecord::SchemaDumper do - before do - CreateTableMigration.new.migrate(:up) - end - - describe '#dump' do - it 'squashes the schema' do - stream = MigrationData::ActiveRecord::SchemaDumper.dump - assert_equal <<-SCHEMA, stream.string - create_table \"users\", force: #{FORCE_MIGRATION} do |t| - t.string \"name\" - end - - SCHEMA - end - end -end diff --git a/test/squash_test.rb b/test/squash_test.rb deleted file mode 100644 index 7f0eda2..0000000 --- a/test/squash_test.rb +++ /dev/null @@ -1,39 +0,0 @@ -require 'test_helper' -require 'migration_data/squash' - -describe MigrationData::Squash do - let(:squash) { MigrationData::Squash.new } - let(:db_path) { Rails.root.join('db', 'migrate') } - - before do - CreateTableMigration.new.migrate(:up) - end - - describe '#call' do - it 'squashes the schema to the current migration' do - squash.stub(:current_version, '100500') do - squash.call - end - assert_equal <<-MIGRATION, File.read(db_path.join('100500_create_schema.rb')) -class CreateSchema < ActiveRecord::Migration - def change - create_table \"users\", force: #{FORCE_MIGRATION} do |t| - t.string \"name\" - end - end -end - MIGRATION - end - - it 'removes all the old migrations' do - old_migration = db_path.join('100500_old_migration.rb') - File.write(old_migration, '') - - assert_equal true, File.exist?(old_migration) - - squash.call - - assert_equal false, File.exist?(old_migration) - end - end -end