lib/activefacts/generator/rails/schema.rb in activefacts-compositions-1.9.22 vs lib/activefacts/generator/rails/schema.rb in activefacts-compositions-1.9.23
- old
+ new
@@ -13,20 +13,26 @@
module ActiveFacts
module Generators
module Rails
class Schema
MM = ActiveFacts::Metamodel unless const_defined?(:MM)
- HEADER = "# Auto-generated from CQL, edits will be lost"
+ HEADER = "# Auto-generated (edits will be lost) using:"
def self.options
({
fks: ['Boolean', "Generate foreign key definitions"],
include_comments: ['Boolean', "Generate a comment for each column showing the absorption path"],
closed_world: ['Boolean', "Set this if your DBMS only allows one null in a unique index (MS SQL)"],
})
end
- def initialize composition, options = {}
+ def self.compatibility
+ # REVISIT: We depend on the surrogate option being enabled if any PK is not Rails-friendly
+ [1, %i{relational}] # one relational composition
+ end
+
+ def initialize constellation, composition, options = {}
+ @constellation = constellation
@composition = composition
@options = options
@option_exclude_fks = [false, 'f', 'n', 'no'].include?(options.delete("fks"))
@option_include_comments = options.delete("include_comments")
@option_closed_world = options.delete("closed_world")
@@ -54,11 +60,12 @@
compact
header =
[
'#',
- "# schema.rb auto-generated for #{@composition.name}",
+ "#{HEADER}",
+ "\# #{([File.basename($0)]+ARGV)*' '}",
'#',
'',
"ActiveRecord::Base.logger = Logger.new(STDOUT)",
"ActiveRecord::Schema.define(version: #{Time.now.strftime('%Y%m%d%H%M%S')}) do",
" enable_extension 'pgcrypto' unless extension_enabled?('pgcrypto')",
@@ -203,14 +210,15 @@
end
end
end
valid_parameters = MM::DataType::TypeParameters[type]
+ size_param = valid_parameters && valid_parameters.include?(:precision) ? :precision : :limit
length_ok = valid_parameters &&
![MM::DataType::TYPE_Real, MM::DataType::TYPE_Integer].include?(type) &&
(valid_parameters.include?(:length) || valid_parameters.include?(:precision))
scale_ok = length_ok && valid_parameters.include?(:scale)
- length_option = length_ok && options[:length] ? ", limit: #{options[:length]}" : ''
+ length_option = length_ok && options[:length] ? ", #{size_param}: #{options[:length]}" : ''
scale_option = scale_ok && options[:scale] ? ", scale: #{options[:scale]}" : ''
null_option = ", null: #{!options[:mandatory]}"
(@option_include_comments ? [" \# #{component.comment}"] : []) +
[%Q{ t.column "#{component.column_name.snakecase}", :#{type_name}#{length_option}#{scale_option}#{null_option}}]