I am trying to set an autoincrement value for the Sqlite database I am currently using. In the future, I will be switching to Postgresql because I will be deploying to heroku and thus will want Postgresql local for development.
Until then, I'm using Sqlite. Is there a way I can write a migration such that it knows which database provider I'm using so that I can have two separate execute statements to set autoincement?
Try calling connection.adapter_name in your migration. It should return either: "PostgreSQL" or "SQLite".
Related
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 recently deployed a website to heroku and I am having problems with the database. I am using ruby on rails and when I first created my local databases I used SQLite. When deploying to heroku I had to change my databases to Postgres and in the process several migration files got removed and changed into a single migration. Now, when I run the postgres database on my localhost, I am still seeing all of the columns in my database, but when I visit the page on heroku one of the columns is missing.
I have checked both my local console and the heroku console and the databases are now different. The local console includes all of the columns in the database and the heroku console is missing one of the columns in the database, but is generating all of the other columns correctly. I have tried running a rake task on heroku and have pushed the most recent changes to heroku.
I tried to to add an additional migration to add the missing column to the database, but whenever I try to rake the migration it tells me that attribute already exists. Any help I can get is appreciated!
The best way to set up your database on heroku is with
rake db:schema:load
From the guides on migrations
Migrations, mighty as they may be, are not the authoritative source
for your database schema. That role falls to either db/schema.rb or an
SQL file which Active Record generates by examining the database. They
are not designed to be edited, they just represent the current state
of the database.
There is no need (and it is error prone) to deploy a new instance of
an app by replaying the entire migration history. It is much simpler
and faster to just load into the database a description of the current
schema.
I'm using pgcrypto gem to encrypt data in certain columns of the database, but in order for this gem to work I have to specify pgcrypto as an adapter in database.yml file. I know that Heroku disregards this file and generates its own when application is pushed to Heroku server, which uses default postgresql adapter. Does anybody know if it's possible to override adapter value, with a configuration variable for example?
Eventually found the solution, which happened to be a quite simple one - I just had to replace the first token in the Heroku's database URL variable, so instead of
postgres://username:password#host:port/dbname
I use
pgcrypto://username:password#host:port/dbname
You can manually create a database connection with
`ActiveRecord::Base.establish_connection(config)
you could also do this on a per-model basis using a mixing.
More info is here Understanding how establish_connection works in ActiveRecord
I am learning RoR using M Hartls Rails tutorial book. I am very much new to the world of databases. I created the application(simple one, just on 3rd chapter) and did my RSpec and made few static pages. I wanted to migrate from sqllite3 to postgres.
I changed the database.yml to postgres deleting the full sqlite3 specifications. Now I run my app it does not work? It says 'PG:' Error.
I need to first understand how the data is stored in sqlite? I searched the db directory and I could not find the development.rb or any database file (probably because I altered the database.yml file)
In this case, I did not enter any data as such,(it still does not work , gives me error) but generally, where does the data get stored? and since I changed the database.yml file to postgres, what will happen to the existing data?
what does rake db:migrate command do?
It would be great if someone gives a simple analogy or explanation is to how this is stored then finding a solution for this problem becomes much easier.
sqlite stores its data in files in a folder on your filesystem, using its own system of storage.
postgres will do a similar thing, but in a different folder, using a different system.
You will generally never touch these folders. You don't need to know where they are or how the dbms (database management system, eg sqlite or postgres) stores the data. All your interaction with your dbms will be done either via the terminal, or the dmbs's shell client (which you launch in the terminal aka the shell), or via a desktop client, or via your rails app's own connection with the dbms.
When you make a new rails project you need to create the database it will use. If you switch to using a different dbms then you will need to create a new database in that dbms for your app to use. Google how to do this.
If you have data in one dbms and you want to bring it into a different dbms then you will need to export it from the first dbms and import it into the second dbms. Google how to do this.
rake db:migrate will run any migrations which haven't been run in your current database (ie the one in your database.yml). Rails creates a table in your database to store which migrations have been run already: this is how it keeps track. It will fail if you haven't created the database yet.
I noticed that Rails creates triggers in the development db but not the test db since it creates the test db from schema.rb instead of running migrations. I have a trigger (as a migration) and need to test that it does the right thing, how can I do that? I tried adding the trigger manually to the test db but that didn't work.
By default rails sets up the test database using the database independant schema.rb, which doesn't understand stuff like triggers.
If you change config.active_record_schema_format to :sql then rails will instead dump the raw SQL that represents the development database's structure and use that to recreate the test database. This dump is placed in development.sql, which is then used instead of schema.rb
This dump is done using the command line tools that your db provides and so will respect features like triggers that active record isn't aware of
If you do prefer Ruby dumps to SQL dumps you might give this gem a try:
https://github.com/jovoto-team/trackless_triggers
It supports dumping triggers and functions for mysql.