So basically I am making a new sample application that will run using an external Postgres database and a model, both created by another programmer who has given me this project.
I am supposed to create a rails app that will implement the model and database in order to add some mapping functionality. The problem is that I truly do not know how to migrate an existing database using the "db_name.sqldump" file they provided me. I have installed psql, created a new rails app and added the "pg" gem, but I haven't been able to create a table with all the model information.
I am not sure if I am being clear but I truly am pretty much lost here with RoR. Any input would help.
Assuming you've a pg_dump file, import it from the command line using either of psql or pg_restore.
http://www.postgresql.org/docs/current/interactive/app-pgrestore.html
Follow this railscast by Ryan Bates - http://railscasts.com/episodes/342-migrating-to-postgresql
To convert your sqldump to a sqlite db, Follow these guides : -
script to convert mysql dump sql file into format that can be imported into sqlite3 db
http://code-n-fold.com/2011/11/28/convert-mysql-dump-to-sqlite-format/
Related
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 did a homework on Rails using RubyMine.
I need to submit the homework with the database tables . But I can't access database tables file location, because I don't know under the which folder I created with migrate. I have migrated tables at the rubymine terminal
I used postgresql for database.
I will be so happy if you could help me. Thank you from now.
There is a schema.rb file that represents your database under db/ folder. So this is the answer for your first question.
The answer to second question is in order to deploy your code on a different computer, the third-party softwares need to be installed. In your scenario, this third-party is postgres as db server. To deploy your code, just type on your terminal rake db:setup. This command will create your database, run migrations and finally seed your database if db/seed.rb contains something. As a result, after rails s there should be some output.
It is worth to note that Rails is db agnostic framework. If you did not use postgres specific features then you would use mysql or sqlite on different computers/servers. But keep in mind that you should change the adapter from config/database.yml if you want to use another database.
Ödev sunumunda başarılar.
I am new user to ruby on rails. I have some question please give the answer as early as possible
What is the Best way to Create a Database For Your Projects in Rails?
1.Scaffold generator
2.Manually Design Database
After installing the Rails then create a project using rails new project_name then cd project_name then run rake db:create it will create a database.
You can modify database name like by default rails uses a database e.g name_development or name_production you can modify this on go to project/config/database.yml.
The rails new project_name command creates default database adapter e.g sqlite3 you can define this which adapter he use by default while rails project create.
The sqlite is a development database you should change database adapter while going to production like postgresql or mysql or mongodb...
If you need to use mysql by default then run below command
rails new project_name -d mysql
If you need to use postgresql by default then run below command
rails new project_name -d postgres
It will create a database adapter by default which you need.
Note: You can use both for a command like rake new project_name or rails new project_name
You need to the read the Rails Guides carefully for understand basic Rails. This tutorial assumes you have basic Rails knowledge.
Also here are the Rails Commands
Ruby on rails Database setup using scaffold generator or Manually
The scaffold works after create a database.
You can see the active record basics and for basic association you can see the Michael Hartley tutorial this is good tutorial for new RoR programmer.
After all you need to design manually using reference or defining by index foreign key
First of all decide what kind of database are you going to use. (Msql2 or Postgres are mostly used)
Then design the schema on a paper or anywhere else.
And whenever you're sure. Now everything comes down to 2 different things.
You want things to be created automatically. for example: Views, CRUD for database operations in controllers.
You want to control things on your own. Now create model from rails commands, write associations, and begin writing your code and define routes as well.
NOTE: Point 2 is implemented when you're going to create a custom application and you may need operations other than basic crud.
Now, you can tell us. What actually you want to achieve and people will help you.
I am new with Ruby on Rails and have some issues with it. I am writing my application on Rails and i have to install some pre-defined data in my Database to use it in my application. This data is read-only and never change (for example users and roles. There are no use cases to create new users and roles).
So i have to install this data when my application runs first time. I tried to use Migrations to solve this problem, but my manager told me that it is not a right way, cause migrations are usually used to define DB structure changes and not to install any data in DB.
Can you please help me and tell better way to install my pre-defined data using Ruby on Rails?
You'll be able to achieve this using the seeds functionality:
To add initial data after a database is created, Rails has a built-in 'seeds' feature that makes the process quick and easy. This is especially useful when reloading the database frequently in development and test environments. It's easy to get started with this feature: just fill up db/seeds.rb with some Ruby code, and run rake db:seed:
#db/seeds.rb
User.create name: "test", description: "info"
$ rake db:seed
--
If you wanted static data, you'll be able to use a gem such as the config gem -- which gives you the ability to allocate values in config/settings.yml:
This data is then accessible as Settings.company...
You should use seeds. See this rails cast
You could also look at Dibber, that allows you to define your seeds via YML files.
What you are looking to do is seed the database. Rails comes with a db/seeds.rb that you can use to do this. There are also gems such as seed-fu that you could use.
Here is a short tutorial on seeding a database in Rails.
Does anybody know how to create a rails application that use noSql database?
We do rails new app_name -d mysql for using mysql database, I need to know what command I should fire in case of noSql database.how to configure and connect to that database and how to use table from it. I need reference link to study. Can anybody help me out?
Thanks.
As far as I am concerned you will have to create a new Rails project excluding active record:
rails new my_app --skip-active-record
then you can follow the configuration steps from Mongoid:
http://mongoid.org/en/mongoid/docs/installation.html
http://www.mongodb.org/display/DOCS/Rails+3+-+Getting+Started should get you started.