Is there any way to automatically generate Rails API for existing database? I have mysql DB from php project and I need to generate REST API for it.
using rails 4, you could use scaffold, taking care to keep the data models exactly like the old database (ie. same table and column names), then remove the migrations that are generated with the scaffold.
let's say you have a table called Posts, with columns: subject and body.
You could run:
rails g scaffold post subject:string body:text
Then remove the migration from db/migrate.
Now assuming you set up the rails app to access the database correctly via config/database.yml, you should have a json API all set up and ready to use, since rails scaffold generates index.json.jbuilder and show.json.jbuilder for each resource that you scaffold.
You might have to edit the application controller to allow external API requests, but this should only matter if you plan on POSTing to your API:
if you do need to POST, then change this line in the top of your app/controllers/application_controller.rb:
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
to this:
protect_from_forgery with: :null_session
GET requests should work without modifying the application controller.
Forgot to mention, to access these resources, you will use ?format=json as a parameter, so:
http://localhost:3000/posts?format=json
or
http://localhost:3000/posts/1?format=json
Will return the json response for all posts or a single post.
you can use AutomaticApiRest [automaticapirest.info], This project was created by myself last year. Basically, this tool allows to create an API REST of your DB (MySQL) in seconds.
Features
Creation a powerful API REST of your MySQL Data Base in Seconds.
Management of the API in situ, it is not neccesary an extra data base.
Private tables and fields.
Custom queries.
Installation
Download the source or clone the repo.
git clone https://github.com/GeekyTheory/Automatic-API-REST
Place it in /var/www/YourWebPage/ (for Apache).
Open the file config.php and complete all the fields with the server credencials.
Go to domain.com/AutomaticaApiRest
There is a Node.js alternative in which you can use any SQL database(MySQL, MSSQL, SQL Server, Aurora, PostgreSQL, and SQLite). I'm one of the core committers of that project.
Install the CLI tool(xc-cli) globally from npm using the following command:
npm install -g xc-cli
Then you can create the project simply using following command:
xc new <project_name>
Note : It would prompt for database connection details, just enter the connection details and it would do the rest of the code generation. And you have to install GUI tool to work the code generation(Download it from here: https://github.com/xgenecloud/xc-desktop-app/releases)
For more info refer GitHub repo: https://github.com/xgenecloud/xgenecloud
And for documentation refer: http://docs.xgenecloud.com/
Related
Recently, I build a self-managed Gitlab instance via gitlab-development-kit.
I used the database migration to added a payload(string) column to the Users table. And I want to add the 'payload' to the response of requests to '/oauth/token'.
I did a full text search for the string '/oauth/token' in Gitlab's source code and it seems like there are only two ruby file including the string:
You can find these two file here: application.rb and routes.rb
I'm new to ruby and ruby on rails. I'm confused about the via: :options since in the ruby on rails documents, the value of the via always be all or post/get
My point is if the via here is options, how does the /oauth/token api receive a POST or GET request?
I've built a single tenant rails application with all core features ready to ship. But I now want to make it multi-tenant using the apartment gem. Most tutorials I've found all show how to start from scratch. But I find no pointers on how to convert an existing project to have multi-tenancy built in. From my research I found that all models need a tenant ID added in the migration. How do I add this to all existing models easily? Does installing the gem and running the generator suffice? I'm running a rails 5 API application with close to 30 models and using graphql ruby in an Ubuntu 18.04 environment.
Any ideas on how to do this?
Thanks #lacostenycoder for pointing me in the right direction. Here's what I did.
Short answer: Yes, installing the gem, running the generator and creating your tenant model should suffice. Apartment reads your schema and creates schemas for the tenants.
Long answer, my experience:
I Backed up existing data from my database just in-case.
Installed apartment gem
If you are using any PostgreSQL database extensions like I was (pgcrypto, uuid- ssop)you need to understand that the extensions are not automatically loaded into the newly created schema.(look here for more details) do the following:
Follow instructions the github readme to create a shared_extensions schema
Create your first tenant. If it works, all is well. But...
If that doesn’t work, I mean when you create a tenant you get errors like ActiveRecord::StatementInvalid: PG::UndefinedFunction: ERROR: function gen_random_uuid() does not exist
or ActiveRecord::StatementInvalid: PG::UndefinedFunction: ERROR: function uuid_generate_v4() does not exist (more details here, here and here) like it was my case when creating my first tenant, I discovered that once the extensions are enabled on the public schema, they cannot be installed in the shared schema shared_extensions in this case. So you have to change them from the public schema to the shared_extensions schema. get more details here
If you are using uuid-ossp ALTER EXTENSION "uuid-ossp" SET SCHEMA shared_extensions in your rails dbconsole
If you are using pgcrypto ALTER EXTENSION "pgcrypto" SET SCHEMA shared_extensions in your rails dbconsole
The apartment gem will create all models for your tenant once you have it set up correctly and create your first tenant.
For more information on check out some of these github issues on apartment. Here and here.
Took me a day to figure it out and gather that info. I hope it saves you time and headache!
There doesn't appear to be any reason why you can't add this to an existing Rails app. I have not done this myself, nor does there appear to be any specific tutorials on how to migrate an existing app. Follow the gem setup instructions as shown on the README and also see the wiki for additional configurations and instructions. Use TDD wherever possible and expect to make changes to your application to get everything working correctly. Depending on how complex your existing app is will determine how much work this move will be.
You'll likely want to use lvh.me:3000 instead of localhost:3000 as root domain and port in your browser and perhaps use rails s -b lvh.me when starting your local server.
If you want to safely hack through this branch I recommend that you make a full backup of your local development database before you get started so that you can rollback to your stable current version in the event things go horribly wrong. If you're using Postgres for example there's pg_dump and pg_restore
This approach combined with good TDD should get you where you need to be.
I am currently using Bitbucket and am handling a Ruby on Rails repository across users. By default, when one user pushes the repository(default command - git push origin master -entire rails folder), I assume that the db also gets pushed to bit bucket, right?
When a second user downloads the repository off git, shouldn't i expect all the db files to also get downloaded?
Is it necessary for the second user to run rake db migrate commands again after downloading the file?
In the specific case above, I am the second user and I get the following error message upon downloading the repository off BitBucket, while the file run perfectly on the uploaders computer:
ActiveRecord::StatementInvalid in StaticPagesController#home
Could not find table 'users'
I want to make sure that both of us are working on the same DB and are not working in parallel on different datasets.
Data in your database will reside only in database. It will not be in git repository. The repository contains database config files and migration files to create databases on the fly. Again, it doesn't contain data.
If you want to work on the same database, I would look into using Amazon AWS RDS. Setting up RDS is not undoable, but it's not trivial enough for me to expound on exactly how you do that here.
I guess you are new to Rails. The way Rails handle database in development is :
with database structure:
You maintain the structure through migrations file.
Yes, if you pull new code that does contain new migration file, you
need to run rake db:migrate. You will notified if you don't.
with database data:
In development, you can maintain data to test through seed file. You can watch this great screencast here: http://railscasts.com/episodes/179-seed-data
Better, you should use seed_fu gem https://github.com/mbleigh/seed-fu
I'm working on a web site which provides API for external developers.
Whenever the API is updated (i.e. modified parameters, new end points, etc), I have to update the documentation manually.
So I'm looking at how this process can be be done automatically. Here is what I hope to acheive: once my source code is updated and committed to my source control, some sort of an API Registry will be updated. So my documentation can be an app which refers to the registry and shows the viewers with the latest API.
My site is based on Ruby on Rails.
RDoc is automatically generated based on your source code, and it comes default with Rails. Your code needs to be well-documented, but if it is, you can just run
bundle exec rake doc:app
and your app will generate documentation accessible from doc/app/index.html. I would look at that and see if such documentation fits your needs. Links in the "Class and Module Index" sidebar contain lists of all of your methods with expandable views of the source code for them.
I use a custom rake task that places all of that documentation in the Rails public folder for access directly from my website and allows me to add more than one custom page. I imagine it wouldn't be that hard for you to do something similar that only parsed your API files.
Links:
RDoc Syntax Help
Project on Github
I have symfony 1.4 installed and had setup my own user model (table). Now, in order to use sfFacebookConnectPlugin, it asks to use sfGuard plugin, so it can easily integrate existing users with facebook account.
Is there a way to use sfGuardPlugin with existing user model ?
OR
is there a better way to implement facebook connect, such that it recognizes the same user (by email), if exists, or creates a new user in the user table with the fb email id and also creates the session.
Thx !
You can use sfGuardPlugin with an existing model, the schema file will be loaded from the plugin's folder and the table names are namespaced in a way that prevents naming collisions.
All you have to do is install the plugin as per its installation instructions and then build your model. To deploy to production you can either copy the SQL that propel generates (in data/sql) and execute it or use a program like Toad for MySQL to generate diff sql upgrade scripts.
Have you tried and failed? I hope I've addressed your concerns.