How to Export a Rails Model to JSON Schema

How can I export a rails model to json schema?

Formtastic: http://github.com/justinfrench/formtastic has a mechanism for building forms from the models. Maybe you could base your code on theirs.

Check in lib/formtastic.rb line 474 or so.

Railscast for formtastic: http://railscasts.com/episodes/184-formtastic-part-1

Generating a JSON schema from a Ruby on rails model (class)

I don't know if it's enough for your needs, but there is a gem that does something like that: https://github.com/salesking/json_schema_builder

How to pretty format JSON output in Ruby on Rails

Use the pretty_generate() function, built into later versions of JSON. For example:

require 'json'
my_object = { :array => [1, 2, 3, { :sample => "hash"} ], :foo => "bar" }
puts JSON.pretty_generate(my_object)

Which gets you:

{
"array": [
1,
2,
3,
{
"sample": "hash"
}
],
"foo": "bar"
}

Extract rails db to JSON file

Use "yaml_db" gem. https://github.com/ludicast/yaml_db

rake db:data:dump

to dump data
(Data is stored in a seperate file after dumping)
and

rake db:data:load

to load data to other database

Export Rails model validations to hash

Perhaps you could overwrite the validates function to do what you want?

Tool to generate JSON schema from JSON data

Seeing that this question is getting quite some upvotes, I add new information (I am not sure if this is new, but I couldn't find it at the time)

  • The home of JSON Schema
  • An implementation of JSON Schema validation for Python
  • Related hacker news discussion
  • A json schema generator in python, which is what I was looking for.

How to export the entire active records in a schema to a csv file

def backup
models = ActiveRecord::Base.connection.tables
all_data = Hash.new
models.map do |model_name|
table_data = []
model_name = model_name.split("")
model_name.pop
model_name = model_name.join("")
model_name.camelize.constantize.all.map do |data|
table_data.push(data)
end
all_data[model_name.camelize] = table_data
end
send_data export_csv(all_data), filename: "Backup - #{Date.today}.csv" and return
end
def export_csv(data)
csvfile = CSV.generate(headers: true) do |csv|
data.each do |key, value|
csv << [key]
attributes = key.camelize.constantize.column_names
csv << attributes
value.each do |val|
csv << val.attributes.values_at(*attributes)
end
csv << ['eot']
end
end
return csvfile
end

I found the solution and a way to export all tables inside a single csv file.

Generate a schema.json with graphql-ruby

Don't know if there is anyone who wants to know about this.
But apparently, you can do it with GraphQL::RakeTask.
I'll leave some links here as well.

https://graphql-ruby.org/api-doc/1.9.3/GraphQL/RakeTask#invoking-the-task-from-ruby
https://github.com/rmosolgo/graphql-ruby/blob/master/lib/graphql/rake_task.rb

structure_dump and structure_load

First, I'll link to some Rails documentation which may be more helpful. This documentation is about the default Rails rake tasks which utilize the methods you've asked about:

Types of Rails schema dumps (Rails edge guides)

I assume you are familiar with Rails' db/schema.rb file. That file is essentially a big "migration" file, which contains some Ruby code which can be run, which will create all the necessary tables and indices in your database. This is what happens when you run rake db:schema:load.

We can use rake db:schema:dump to essentially take the state of the database and translate it directly into a schema.rb file. We might need to do this if we don't have any migration files, and we don't have an existing schema.rb file. In some cases (not very many), ActiveRecord has trouble properly "translating" the database schema directly into Ruby code. ActiveRecord is unable to translate certain features/options, including, but not limited to:

  • Triggers
  • Stored Procedures
  • CHECK Constraints

structure_dump and it's accompanying rake task db:structure:dump exist to try to solve this problem. Instead of asking ActiveRecord's database adapter to dump to a schema.rb file containing Ruby code, it instead uses the database's underlying management tools to dump to a structure.sql file, containing a big list of SQL statements. It's typically easier to "represent" the database schema in raw SQL instead of having to translate it into Ruby code first. The resulting structure.sql file can now be used to recreate the database schema/structure in one of two ways:

  • Run the contents of db/structure.sql in an SQL prompt, or in some way which is directly evaluated by your database management system

  • Run the rake task db:structure:load. This will read from db/structure.sql and evaluate it's contents as raw SQL, but through ActiveRecord - very similar to db:schema:load.

NOTE: In Rails 6.1, db:structure:dump and db:structure:load are deprecated. In 6.1 and later, the schema type is configurable in your environment configuration:

config.active_record.schema_format = :sql

This will switch the normal db:schema:load and db:schema:dump to use SQL formats instead.


TLDR: Sometimes it's hard for ActiveRecord to dump a schema directly to Ruby code, so structure_dump allows dumping to raw SQL. That SQL can be run to recreate the database schema the same way that a schema.rb is run to recreate the database schema. You can load it into the database as raw SQL or by running the rake task db:structure:load. In Rails 6.1, this is changing.



Related Topics



Leave a reply



Submit