Skip to content

Instantly share code, notes, and snippets.

@intrip
Created January 29, 2021 18:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save intrip/180694fc9a2f1f99481d5f93df74fd51 to your computer and use it in GitHub Desktop.
Save intrip/180694fc9a2f1f99481d5f93df74fd51 to your computer and use it in GitHub Desktop.
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem "activerecord", "6.1"
# gem "rails", path: '.'
gem "mysql2"
end
require "active_record"
require "minitest/autorun"
require "logger"
ActiveRecord::Base.establish_connection(adapter: "mysql2", database: "test1", username: 'root', password: 'root')
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :payments, force: true do |t|
t.decimal :amount, precision: 10, scale: 0, default: 0, null: false
t.json "invoice"
end
end
class Payment < ActiveRecord::Base
end
class ChangeAmountToAddScale < ActiveRecord::Migration[6.0]
def change
add_column :payments, :invoice_id, :integer, as: "JSON_EXTRACT(invoice, '$.id')", stored: true
end
end
class BugTest < Minitest::Test
def test_migration_up
ChangeAmountToAddScale.migrate(:up)
Payment.reset_column_information
# save schema
stream = StringIO.new
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
File.write("./schema.rb", stream.string)
# load from schema
ActiveRecord::Tasks::DatabaseTasks.instance_variable_set("@db_dir", ".")
ActiveRecord::Tasks::DatabaseTasks.reconstruct_from_schema(ActiveRecord::Base.connection_db_config, ActiveRecord::Base.schema_format, nil)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment