facing a postgres error while running rake migration - ruby-on-rails

I am getting the following uuid error while running a rails app with postgres as backend. Can someone help me out with which dependency is needed.
[root#localhost webapp]# rake db:migrate
(in /root/mysite/webapp)
== CreateContributors: migrating =============================================
-- create_table(:contributors, {:id=>false})
-> 0.0121s
-- execute("alter table contributors add primary key (id)")
NOTICE: ALTER TABLE / ADD PRIMARY KEY will create implicit index "contributors_pkey" for table "contributors"
-> 0.0797s
-- execute("alter table contributors alter column id set default uuid_generate_v1()::varchar")
rake aborted!
An error has occurred, this and all later migrations canceled:
PGError: ERROR: function uuid_generate_v1() does not exist
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
: alter table contributors alter column id set default uuid_generate_v1()::varchar

The uuid_generate_v1() function is part of the uuid-ossp package and you have to install that into PostgreSQL before you can use it. You should have a file called uuid-ossp.sql in your PostgreSQL contrib directory. You can install the package with:
$ psql -d your_database < /the/path/to/uuid-ossp.sql
You'll probably want to run that as the database super user.

Related

Rails test DB constantly losing my PostgreSQL functions

My project uses a few custom PostgreSQL stored functions for some features that would be a pain in raw SQL or ActiveRecord. Every now and then I will run the RSpec test suite, and find that all my stored functions have been blown away. Re-running the migrations to create them fixes the problem, but "rake db:structure:load" does NOT.
I am deeply confused. I never drop either the dev or test database unless this happens, but my functions are like Schrodinger's PL/pgSQL. I am REALLY hoping this never happens in production.
Here is an example of a failing test and my attempts to fix it:
ActiveRecord::StatementInvalid:
PG::UndefinedFunction: ERROR: function round_half_down(numeric) does not exist
# Damn. We have to drop the database so we can reload structure.sql:
$ RAILS_ENV=test rake db:drop
$ RAILS_ENV=test rake db:create
# load structure.sql instead of schema.rb:
$ RAILS_ENV=test rake db:structure:load
# Not fixed:
ActiveRecord::StatementInvalid:
PG::UndefinedFunction: ERROR: function round_half_down(numeric) does not exist
$ RAILS_ENV=test rake db:migrate:redo VERSION=20160421184708
== 20171002190107 CreateRoundHalfDownFunction: reverting ======================
-- execute("DROP FUNCTION IF EXISTS round_half_down(numeric)")
-> 0.0004s
== 20171002190107 CreateRoundHalfDownFunction: reverted (0.0005s) =============
== 20171002190107 CreateRoundHalfDownFunction: migrating ======================
-- execute("CREATE OR REPLACE FUNCTION ROUND_HALF_DOWN(NUMERIC)\n RETURNS NUMERIC LANGUAGE SQL AS\n$FUNC$\n SELECT CASE WHEN ($1%1) < 0.6 THEN FLOOR($1) ELSE CEIL($1) END;\n$FUNC$\n")
-> 0.0014s
== 20171002190107 CreateRoundHalfDownFunction: migrated (0.0014s) =============
Now it is fixed!
Yes, I verified that the function is present in structure.sql:
--
-- Name: round_half_down(numeric); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION round_half_down(numeric) RETURNS numeric
LANGUAGE sql
AS $_$
SELECT CASE WHEN ($1%1) < 0.6 THEN FLOOR($1) ELSE CEIL($1) END;
$_$;
For the record, this stopped happening to me with newer versions of the pg gem and PostgreSQL itself. I also added config.active_record.schema_format = :sql to application.rb, because my application makes heavy use of Postgres-specific features, and a number of stored functions.

heroku run rake db:migrate reverts migrations

I am running my app on heroku, using the heroku pg stack, and when I run db:migrate it reverts my previous migrations instead of moving forward.
rake db:version shows the current migration
Current version: 20160516172744
rake db:migrate:status shows all migrations as up
......(more above all up)
up 20160512175053 Create spree roles permissions.spree admin roles and access
up 20160512175054 Add editable is default and index on editable is default and name to spree roles.spree admin roles and access
up 20160513135317 Add indexes for speed
up 20160513140704 Add filter search params to spree product
up 20160516172744 Add tsvector colums to spree products
But when I hit heroku run rake db:migrate this is the output:
Migrating to AddTsvectorColumsToSpreeProducts (20160516172744)
== 20160516172744 AddTsvectorColumsToSpreeProducts: reverting =================
-- execute(" DROP FUNCTION IF EXISTS spree_products_tsv_trigger() CASCADE;\n")
-> 0.0031s
-- remove_index(:spree_products, :tsv)
-> 0.0058s
-- remove_column(:spree_products, :tsv)
-> 0.0025s
== 20160516172744 AddTsvectorColumsToSpreeProducts: reverted (0.0122s) ========
Migrating to AddFilterSearchParamsToSpreeProduct (20160513140704)
== 20160513140704 AddFilterSearchParamsToSpreeProduct: reverting ==============
-- remove_column(:spree_products, :designer_id)
-> 0.0062s
-- remove_column(:spree_products, :main_taxon_id)
-> 0.0024s
-- remove_column(:spree_products, :colour_id)
-> 0.0025s
-- remove_column(:spree_products, :size_id)
-> 0.0045s
-- remove_column(:spree_products, :condition_id)
-> 0.0023s
-- remove_column(:spree_products, :on_site)
-> 0.0023s
-- remove_column(:spree_products, :sgd_price)
-> 0.0025s
-- remove_column(:spree_products, :search_designer)
-> 0.0022s
-- remove_column(:spree_products, :search_category)
-> 0.0021s
-- remove_column(:spree_products, :search_sku)
-> 0.0022s
== 20160513140704 AddFilterSearchParamsToSpreeProduct: reverted (0.0341s) =====
.....(cont.)
Any ideas?
-Dan
rake db:migrate will, without any arguments, only migrate up to the latest version. If you ever see it migrate down, you most likely have the VERSION environment variable set to some value (see the docs for explanation of how that variable works), and Rails is trying to migrate to that version.
In your case, per the comments, it looks like you'd set VERSION=v3 in your environment. Rails is likely casting v3 to 0 and trying to migrate all migrations down.

Heroku, Ruby, pg_dump, pg_restore with error: must be owner of extension plpgsql

When I make a database in Ruby, based on the Heroku instruction, I have to command like this:
heroku run rake db:migrate
So when I did this, my entries that are stored in my local pc weren't transferred to Heroku. So, after searching for the way how I can do this, I saw this method.
Dumping the database file.
Setting heroku
heroku pg:push mylocaldb HEROKU_POSTGRESQL_MAGENTA --app sushi
sushi = my app name in Heroku.
When I tried to do this, the below error occured: TOC entry error, owerner of extension plpqsql error.
...
pg_restore: [archiver (db)] Error while PROCESSING TOC:
pg_restore: [archiver (db)] Error from TOC entry 2257; 0 0 COMMENT EXTENSION plpgsql
pg_restore: [archiver (db)] could not execute query: ERROR: must be owner of extension plpgsql
Command was: COMMENT ON EXTENSION plpgsql IS 'PL/pgSQL procedural language';
..
WARNING: errors ignored on restore: 1
I make the database file by
pg_dump databasenamehere > databasedumpfile
And I found the "databasenamehere" in /config/database.yml,,, development name.
This keeps happening and I don't know what to do.. :(
How can I solve this issue?
Is there any good method to put the entries to the HEROKU?
Also, why rake:db:migrate didn't push the entries to heroku? ( thinking this is a rookie question )

psql ERROR: relation already exists

I'm writing a rails project using postgres and there is some data in the server. And I want dump the data from the remote end to the local, so I write script to do it, but some errors come out.
This's the dump script:
run "PGPASSWORD='#{remote_settings['password']}'
pg_dump -U #{remote_settings["username"]} #{"-h '#{remote_settings["host"]}'"
if remote_settings["host"]}
'#{remote_settings["database"]}' > #{remote_sql_file_path}"
There's some codes to transport..
Transport codes
And this's the restore script:
run_locally "PGPASSWORD='#{local_settings['password']}' psql -U
#{local_settings["username"]} -d #{local_settings["database"]}
-f #{local_sql_file_path}"
I get the data file successfully, but when there're some ERRORs* when **restore script is run:
psql:tmp/production-ib5k_production-2013-02-21_18:42:09.sql:46: ERROR: relation "refinery_images" already exists
psql:tmp/production-ib5k_production-2013-02-21_18:42:09.sql:49: ERROR: role "ib5k" does not exist
psql:tmp/production-ib5k_production-2013-02-21_18:42:09.sql:60: ERROR: relation "refinery_images_id_seq" already exists
psql:tmp/production-ib5k_production-2013-02-21_18:42:09.sql:63: ERROR: role "ib5k" does not exist
psql:tmp/production-ib5k_production-2013-02-21_18:42:09.sql:83: ERROR: relation "refinery_page_part_translations" already exists
psql:tmp/production-ib5k_production-2013-02-21_18:42:09.sql:86: ERROR: role "ib5k" does not exist
...
sql:tmp/production-ib5k_production-2013-02-21_18:42:09.sql:525: ERROR: duplicate key value violates unique constraint "refinery_images_pkey"
DETAIL: Key (id)=(1) already exists.
CONTEXT: COPY refinery_images, line 2: ""
psql:tmp/production-ib5k_production-2013-02-21_18:42:09.sql:547: ERROR: duplicate key value violates unique constraint "refinery_page_part_translations_pkey"
DETAIL: Key (id)=(1) already exists.
CONTEXT: COPY refinery_page_part_translations, line 8: ""
psql:tmp/production-ib5k_production-2013-02-21_18:42:09.sql:569: ERROR: duplicate key value violates unique constraint "refinery_page_parts_pkey"
DETAIL: Key (id)=(1) already exists.
CONTEXT: COPY refinery_page_parts, line 8: ""
...
And the database in local will not be updated.
I want to know how to solve it? Adding some arguments? Thank you in advance.
You can use the -c or --clean argument to pg_dump. That argument will drop existing database objects before running the commands to create them.
An alternative is to drop those objects yourself before restoring. (Probably using drop schema or drop database.)
Use with caution.

Trying to get Postres and Postgis running in Rails app

I am completely new to Postgresql, Postgis, and SQL and have a little bit of Rails knowledge. I am trying to get an existing app going on my local and am currently receiving the following messages when running a rake task to set up my database.
Blane-Cordess-MacBook:ajungo blanecordes$ rake postgres:create_postgis_template --trace
(in /Users/blanecordes/ajungo)
** Invoke postgres:create_postgis_template (first_time)
** Execute postgres:create_postgis_template
Enter your postgres username: blane
Password for user blane:
psql (9.2.1, server 9.1.4)
WARNING: psql version 9.2, server version 9.1.
Some psql features might not work.
You are now connected to database "template1" as user "blane".
psql:assets/sql/postgis_template_osx.sql:2: ERROR: permission denied to create database
psql:assets/sql/postgis_template_osx.sql:6: ERROR: permission denied for relation pg_database
psql:assets/sql/postgis_template_osx.sql:7: \connect: FATAL: database "template_postgis" does not exist
My postgis_template_osx.sql file is the following:
\c template1
CREATE DATABASE template_postgis WITH template = template1;
-- set the 'datistemplate' record in the 'pg_database' table for
-- 'template_postgis' to TRUE indicating its a template
UPDATE pg_database SET datistemplate = TRUE WHERE datname = 'template_postgis';
\c template_postgis
CREATE LANGUAGE plpgsql ;
\i /usr/local/Cellar/postgis/2.0.1/share/postgis/postgis.sql;
\i /usr/local/Cellar/postgis/2.0.1/share/postgis/spatial_ref_sys.sql;
-- 1.5.2
-- in a production environment you may want to
-- give role based permissions, but granting all for now
GRANT ALL ON geometry_columns TO PUBLIC;
GRANT ALL ON spatial_ref_sys TO PUBLIC;
-- vacuum freeze: it will guarantee that all rows in the database are
-- "frozen" and will not be subject to transaction ID wraparound
-- problems.
VACUUM FREEZE;
The PostgreSQL ROLE blane does not have permission to create databases. Either ALTER blane and add the CREATEDB option or use an existing administrator role that has that capability.
postgres=# ALTER ROLE blane WITH CREATEDB;

Resources