lib/hanami/model/migrator/sqlite_adapter.rb in hanami-model-1.0.1 vs lib/hanami/model/migrator/sqlite_adapter.rb in hanami-model-1.0.2

- old
+ new

@@ -1,7 +1,8 @@ require 'pathname' require 'hanami/utils' +require 'English' module Hanami module Model class Migrator # SQLite3 Migrator @@ -89,23 +90,39 @@ end # @since 0.4.0 # @api private def dump_structure - system "sqlite3 #{escape(path)} .schema > #{escape(schema)}" + execute "sqlite3 #{escape(path)} .schema > #{escape(schema)}" end # @since 0.4.0 # @api private def load_structure - system "sqlite3 #{escape(path)} < #{escape(schema)}" if schema.exist? + execute "sqlite3 #{escape(path)} < #{escape(schema)}" if schema.exist? end # @since 0.4.0 # @api private + # + # rubocop:disable Metrics/AbcSize + # rubocop:disable Metrics/MethodLength def dump_migrations_data - system %(sqlite3 #{escape(path)} .dump | grep '^INSERT INTO "#{migrations_table}"' >> #{escape(schema)}) + execute "sqlite3 #{escape(path)} .dump" do |stdout| + begin + contents = stdout.read.split($INPUT_RECORD_SEPARATOR) + contents = contents.grep(/^INSERT INTO "#{migrations_table}"/) + + ::File.open(schema, ::File::CREAT | ::File::BINARY | ::File::WRONLY | ::File::APPEND) do |file| + file.write(contents.join($INPUT_RECORD_SEPARATOR)) + end + rescue => exception + raise MigrationError.new(exception.message) + end + end end + # rubocop:enable Metrics/MethodLength + # rubocop:enable Metrics/AbcSize end end end end