|
| 1 | +# typed: false |
| 2 | +# frozen_string_literal: true |
| 3 | + |
| 4 | +require 'rails_helper' |
| 5 | +require 'lazy_migrate/migration' |
| 6 | + |
| 7 | +RSpec.describe LazyMigrate::Client do |
| 8 | + let(:rails_root) { Rails.root } |
| 9 | + |
| 10 | + let(:create_books_migration_status) { |
| 11 | + LazyMigrate::Migration.new(status: "up", version: 20200804231712, name: "Create books", has_file: true, current: false) |
| 12 | + } |
| 13 | + |
| 14 | + let(:add_author_migration_status_status) { 'up' } |
| 15 | + let(:add_author_migration_status) { |
| 16 | + LazyMigrate::Migration.new(status: add_author_migration_status_status, version: 20200804234040, name: "Add book author", has_file: true, current: false) |
| 17 | + } |
| 18 | + |
| 19 | + let(:add_page_count_migration_status) { |
| 20 | + LazyMigrate::Migration.new(status: "down", version: 20200804234057, name: "Add book page count", has_file: true, current: false) |
| 21 | + } |
| 22 | + |
| 23 | + let(:add_rating_migration_status) { |
| 24 | + LazyMigrate::Migration.new(status: "up", version: 20200804234111, name: "Add book rating", has_file: true, current: true) |
| 25 | + } |
| 26 | + |
| 27 | + let(:migrations) { |
| 28 | + [ |
| 29 | + add_rating_migration_status, |
| 30 | + add_page_count_migration_status, |
| 31 | + add_author_migration_status, |
| 32 | + create_books_migration_status, |
| 33 | + ] |
| 34 | + } |
| 35 | + |
| 36 | + let(:migrator_adapter) { LazyMigrate::MigratorAdapterFactory.create_migrator_adapter } |
| 37 | + |
| 38 | + let(:new_version) { 30900804234040 } |
| 39 | + |
| 40 | + def find_support_folder |
| 41 | + File.join(File.dirname(File.dirname(__FILE__)), 'support') |
| 42 | + end |
| 43 | + |
| 44 | + before do |
| 45 | + # prepare the db directory |
| 46 | + support_folder = find_support_folder |
| 47 | + db_dir = ActiveRecord::Tasks::DatabaseTasks.db_dir |
| 48 | + |
| 49 | + schema_filename = File.join(db_dir, 'schema.rb') |
| 50 | + FileUtils.cp(File.join(support_folder, 'mock_schema.rb'), schema_filename) |
| 51 | + |
| 52 | + migrate_dir = File.join(db_dir, 'migrate') |
| 53 | + FileUtils.rm_rf(migrate_dir) |
| 54 | + FileUtils.cp_r(File.join(support_folder, 'mock_migrations/default/.'), migrate_dir) |
| 55 | + |
| 56 | + ActiveRecord::Migration.drop_table(:books) if ActiveRecord::Base.connection.table_exists?(:books) |
| 57 | + ActiveRecord::Migration.create_table "books", force: :cascade do |t| |
| 58 | + t.string "name" |
| 59 | + t.datetime "created_at", null: false |
| 60 | + t.datetime "updated_at", null: false |
| 61 | + end |
| 62 | + |
| 63 | + ActiveRecord::SchemaMigration.delete_all |
| 64 | + |
| 65 | + migrations.sort { |m| m.version }.each do |migration| |
| 66 | + if migration.status == 'up' |
| 67 | + ActiveRecord::SchemaMigration.create(version: migration.version) |
| 68 | + end |
| 69 | + end |
| 70 | + end |
| 71 | + |
| 72 | + after do |
| 73 | + db_dir = ActiveRecord::Tasks::DatabaseTasks.db_dir |
| 74 | + |
| 75 | + FileUtils.rm(File.join(db_dir, 'schema.rb')) |
| 76 | + FileUtils.rm_rf(File.join(db_dir, 'migrate')) |
| 77 | + end |
| 78 | + |
| 79 | + describe '.database_name' do |
| 80 | + it "returns without error" do |
| 81 | + expect { described_class.send(:database_name) }.not_to raise_error |
| 82 | + end |
| 83 | + end |
| 84 | +end |
0 commit comments