lib/ridgepole/dsl_parser.rb in ridgepole-0.5.3.beta4 vs lib/ridgepole/dsl_parser.rb in ridgepole-0.6.0.beta
- old
+ new
@@ -20,22 +20,24 @@
:type => type,
:options => options,
}
end
+ # https://github.com/winebarrel/rails/blob/v4.2.1/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb#L274
TYPES = [
:string,
:text,
:integer,
+ :bigint,
:float,
:decimal,
:datetime,
:timestamp,
:time,
:date,
:binary,
- :boolean
+ :boolean,
]
TYPES.each do |column_type|
define_method column_type do |*args|
options = args.extract_options!
@@ -51,13 +53,15 @@
end
def references(*args)
options = args.extract_options!
polymorphic = options.delete(:polymorphic)
+ type = options.delete(:type) || :integer
+
args.each do |col|
- column("#{col}_id", :integer, options)
- column("#{col}_type", :string, polymorphic.is_a?(Hash) ? polymorphic : options) unless polymorphic.nil?
+ column("#{col}_id", type, options)
+ column("#{col}_type", :string, polymorphic.is_a?(Hash) ? polymorphic : options) if polymorphic
end
end
alias :belongs_to :references
end
@@ -118,10 +122,32 @@
:column_name => column_name,
:options => options,
}
end
+ def add_foreign_key(from_table, to_table, options = {})
+ unless options[:name]
+ raise "Foreign key name in `#{from_table}` is undefined"
+ end
+
+ from_table = from_table.to_s
+ to_table = to_table.to_s
+ options[:name] = options[:name].to_s
+ @__definition[from_table] ||= {}
+ @__definition[from_table][:foreign_keys] ||= {}
+ idx = options[:name]
+
+ if @__definition[from_table][:foreign_keys][idx]
+ raise "Foreign Key `#{from_table}(#{idx})` already defined"
+ end
+
+ @__definition[from_table][:foreign_keys][idx] = {
+ :to_table => to_table,
+ :options => options,
+ }
+ end
+
def require(file)
schemafile = (file =~ %r|\A/|) ? file : File.join(@__working_dir, file)
if File.exist?(schemafile)
instance_eval(File.read(schemafile), schemafile)
@@ -145,23 +171,27 @@
end
def parse(dsl, opts = {})
definition, execute = Context.eval(dsl, opts)
check_orphan_index(definition)
-
- if @options[:enable_foreigner]
- Ridgepole::ForeignKey.check_orphan_foreign_key(definition)
- end
-
+ check_orphan_foreign_key(definition)
[definition, execute]
end
private
def check_orphan_index(definition)
definition.each do |table_name, attrs|
if attrs[:indices] and not attrs[:definition]
raise "Table `#{table_name}` to create the index is not defined: #{attrs[:indices].keys.join(',')}"
+ end
+ end
+ end
+
+ def check_orphan_foreign_key(definition)
+ definition.each do |table_name, attrs|
+ if attrs[:foreign_keys] and not attrs[:definition]
+ raise "Table `#{table_name}` to create the foreign key is not defined: #{attrs[:foreign_keys].keys.join(',')}"
end
end
end
end