-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2571 from DMPRoadmap/rails5-ar-monkeypatch
Rails5 ActiveRecord monkey patch for SchemaDumper
- Loading branch information
Showing
13 changed files
with
235 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 10 additions & 9 deletions
19
app/services/org/create_last_month_created_plan_service.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 9 additions & 8 deletions
17
app/services/org/create_last_month_exported_plan_service.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# frozen_string_literal: true | ||
|
||
module ActiveRecord | ||
|
||
# The Rails 5.x SchemaDumper includes an `options:` arrgument on table definitions | ||
# that is not DB agnostic. This Monkey Patch comments out the `options:` section. | ||
# | ||
# TODO: Determine if this is still necessary in Rails 6.x+ | ||
class SchemaDumper | ||
|
||
# Method definition taken from the 5.2-stable branch of ActiveRecord: | ||
# https://github.com/rails/rails/blob/5-2-stable/activerecord/lib/active_record/schema_dumper.rb | ||
def table(table, stream) | ||
columns = @connection.columns(table) | ||
begin | ||
tbl = StringIO.new | ||
|
||
# first dump primary key column | ||
pk = @connection.primary_key(table) | ||
|
||
tbl.print " create_table #{remove_prefix_and_suffix(table).inspect}" | ||
|
||
case pk | ||
when String | ||
tbl.print ", primary_key: #{pk.inspect}" unless pk == "id" | ||
pkcol = columns.detect { |c| c.name == pk } | ||
pkcolspec = column_spec_for_primary_key(pkcol) | ||
if pkcolspec.present? | ||
tbl.print ", #{format_colspec(pkcolspec)}" | ||
end | ||
when Array | ||
tbl.print ", primary_key: #{pk.inspect}" | ||
else | ||
tbl.print ", id: false" | ||
end | ||
|
||
# Commenting out Table Options because they are not DB agnostic | ||
# table_options = @connection.table_options(table) | ||
# if table_options.present? | ||
# tbl.print ", #{format_options(table_options)}" | ||
# end | ||
|
||
tbl.puts ", force: :cascade do |t|" | ||
|
||
# then dump all non-primary key columns | ||
columns.each do |column| | ||
raise StandardError, "Unknown type '#{column.sql_type}' for column '#{column.name}'" unless @connection.valid_type?(column.type) | ||
next if column.name == pk | ||
type, colspec = column_spec(column) | ||
tbl.print " t.#{type} #{column.name.inspect}" | ||
tbl.print ", #{format_colspec(colspec)}" if colspec.present? | ||
tbl.puts | ||
end | ||
|
||
indexes_in_create(table, tbl) | ||
|
||
tbl.puts " end" | ||
tbl.puts | ||
|
||
tbl.rewind | ||
stream.print tbl.read | ||
rescue => e | ||
stream.puts "# Could not dump table #{table.inspect} because of following #{e.class}" | ||
stream.puts "# #{e.message}" | ||
stream.puts | ||
end | ||
end | ||
|
||
end | ||
|
||
end |
Oops, something went wrong.