Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support foreign key constraints #132

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions lib/yaml_db/serialization_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,49 @@ def self.after_table(io, table)

end

def self.class_exists?(class_name)
klass = Module.const_get(class_name)
return klass.is_a?(Class)
rescue NameError
return false
end

def self.tables
ActiveRecord::Base.connection.tables.reject { |table| ['schema_info', 'schema_migrations'].include?(table) }.sort
tables = ActiveRecord::Base.connection.tables.reject do |table|
['schema_info', 'schema_migrations'].include?(table)
end
result_tables = tables.select do |table|
!class_exists?(table.classify)
end
tables -= result_tables
sort_tables = Array.new
while tables.length > 0
tables.delete_if do |table|
table_class = table.classify.constantize

find_undefined = table_class.reflect_on_all_associations(:belongs_to).find do |reflection|
if reflection.options.include?(:class_name)
reflection_table_name = reflection.options[:class_name].constantize.table_name

if reflection_table_name != table
!sort_tables.include?(reflection.options[:class_name].constantize.table_name)
else
false
end
else
!sort_tables.include?(reflection.name.to_s.classify.constantize.table_name)
end
end

if find_undefined
false
else
sort_tables << table
true
end
end
end
sort_tables + result_tables
end

def self.dump_table(io, table)
Expand All @@ -179,7 +220,6 @@ def self.table_column_names(table)
ActiveRecord::Base.connection.columns(table).map { |c| c.name }
end


def self.each_table_page(table, records_per_page=1000)
total_count = table_record_count(table)
pages = (total_count.to_f / records_per_page).ceil - 1
Expand Down
2 changes: 1 addition & 1 deletion yaml_db.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Gem::Specification.new do |s|

s.required_ruby_version = ">= 1.8.7"

s.add_runtime_dependency "rails", ">= 3.0", "< 5.2"
s.add_runtime_dependency "rails", ">= 3.0"
s.add_runtime_dependency "rake", ">= 0.8.7"

s.add_development_dependency "bundler", "~> 1.14"
Expand Down