Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Fixes problem when trying to migrate with PostgreSQL #713

Closed
wants to merge 2 commits into from

5 participants

@hoverlover

You can read the comments in core/config/initializers/old_tables.rb to get the details of the issue this commit fixes. Basically, when running under PostgreSQL, the migrations would fail when the table names were temporarily set using .table_name = 'blah'. This is the cleanest way I could find to fix it. Let me know if you have any questions.

@schof
Owner

Anyone using Postgres want to comment on this. Seems weird that nobody else is complaining about this problem. Perhaps its limited to a specific version of Postgres?

@hoverlover

Just so everyone has more context, here is my situation:

  • Using spree_core only, installed into existing project.
  • PostgreSQL 9.1.1

  1. rails g spree:site
  2. bundle exec rake db:migrate. Errors ensue.
@joneslee85
Collaborator

Confirmed that the fix does work for spree_core yet it breaks migration of auth and api. Can you please fix and get the full test suites passing first?

@radar radar commented on the diff
core/config/initializers/old_tables.rb
((40 lines not shown))
+ set_table_name 'shipments'
+ end
+
+ Spree::Order.class_eval do
+ set_table_name 'orders'
+ end
+
+ Spree::Adjustment.class_eval do
+ set_table_name 'adjustments'
+ end
+
+ Spree::Creditcard.class_eval do
+ set_table_name 'creditcards'
+ end
+else
+ puts "NOTE: Initializer #{__FILE__} is no longer needed and can be removed"
@radar Collaborator
radar added a note

This line would indicate to me that this message would be shown every single time the application is initialized. Not just during migrations. Also, people are unable to easily remove this file as it is inside the gem.

You are correct. Total oversight on my part.

@parndt
parndt added a note

Above, you should use self.table_name = 'orders' etc instead of set_table_name which is deprecated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@radar
Collaborator

Could someone elaborate how it breaks PostgreSQL please? All we've been told so far is "errors", which is unhelpful.

@hoverlover

@radar the details of the error are in the comments in the initializer. That's really all the information surrounding errors I have to give.

@hoverlover

I'll also add that this very well may not be the best way to fix this problem. This is just what worked for me. If anyone knows of a better way to fix it, go for it.

@schof
Owner

Closing for now due to the age of the ticket and the fact that it can no longer be merged w/out conflict. If someone wants to resurrect this issue and provide fresh pull request, etc. we will be happy to consider.

@schof schof closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
56 core/config/initializers/old_tables.rb
@@ -0,0 +1,56 @@
+# NOTE: It is safe to remove this initializer once the database has been migrated above the namespace_top_level_models migration.
+#
+# This is a hack needed when running under PostgreSQL. It's needed because certain ActiveRecord::Base
+# calls (table_exists?, etc) will trigger a query such as this:
+#
+# PGError: ERROR: relation "spree_products" does not exist
+# LINE 4: WHERE a.attrelid = '"spree_products"'::regclass
+# ^
+# : SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull
+# FROM pg_attribute a LEFT JOIN pg_attrdef d
+# ON a.attrelid = d.adrelid AND a.attnum = d.adnum
+# WHERE a.attrelid = '"spree_products"'::regclass
+# AND a.attnum > 0 AND NOT a.attisdropped
+# ORDER BY a.attnum
+#
+# Some of these calls happen when the model classes are loaded (Spree::Product). When in the migrations,
+# by the time you call Spree::Product.table_name = 'products', it's too late. Setting the table names explicitly
+# below was the only way I could get the migrations to run properly.
+#
+tables = ActiveRecord::Base.connection.tables
+
+if !tables.include?("schema_migrations") or !tables.include?("spree_products")
+ Spree::Variant.class_eval do
+ set_table_name 'variants'
+ end
+
+ Spree::Product.class_eval do
+ set_table_name 'products'
+ end
+
+ Spree::InventoryUnit.class_eval do
+ set_table_name 'inventory_units'
+ end
+
+ Spree::Taxon.class_eval do
+ set_table_name 'taxons'
+ end
+
+ Spree::Shipment.class_eval do
+ set_table_name 'shipments'
+ end
+
+ Spree::Order.class_eval do
+ set_table_name 'orders'
+ end
+
+ Spree::Adjustment.class_eval do
+ set_table_name 'adjustments'
+ end
+
+ Spree::Creditcard.class_eval do
+ set_table_name 'creditcards'
+ end
+else
+ puts "NOTE: Initializer #{__FILE__} is no longer needed and can be removed"
@radar Collaborator
radar added a note

This line would indicate to me that this message would be shown every single time the application is initialized. Not just during migrations. Also, people are unable to easily remove this file as it is inside the gem.

You are correct. Total oversight on my part.

@parndt
parndt added a note

Above, you should use self.table_name = 'orders' etc instead of set_table_name which is deprecated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+end
View
12 core/db/migrate/20090923100315_add_count_on_hand_to_variants_and_products.rb
@@ -3,13 +3,6 @@ def self.up
add_column :variants, :count_on_hand, :integer, :default => 0, :null => false
add_column :products, :count_on_hand, :integer, :default => 0, :null => false
- # Due to our namespacing changes, this migration (from earlier Spree versions) is broken
- # To fix it, temporarily set table name on each of the models involved
- # And then...
- Spree::Variant.table_name = 'variants'
- Spree::Product.table_name = 'products'
- Spree::InventoryUnit.table_name = 'inventory_units'
-
# In some cases needed to reflect changes in table structure
Spree::Variant.reset_column_information
Spree::Product.reset_column_information
@@ -29,11 +22,6 @@ def self.up
p.update_attribute(:count_on_hand, product_count_on_hand)
end
end
-
- # ... Switch things back at the end of the migration
- Spree::Variant.table_name = 'spree_variants'
- Spree::Product.table_name = 'spree_products'
- Spree::InventoryUnit.table_name = 'spree_inventory_units'
end
def self.down
View
6 core/db/migrate/20091007134354_change_taxons_to_nested_set.rb
@@ -5,9 +5,6 @@ def self.up
Spree::Taxon.reset_column_information # So the new root ids get saved
- # Temporarily set the table back to taxons
- Spree::Taxon.table_name = 'taxons'
-
Spree::Taxon.class_eval do
# adapted from awesome nested set to use 'position' information
indices = {}
@@ -34,9 +31,6 @@ def self.up
set_left_and_rights.call(root_node)
end
end
-
- # Set it back after the migration
- Spree::Taxon.table_name = 'spree_taxons'
end
def self.down
View
5 core/db/migrate/20091015153048_add_openid_field_to_users.rb
@@ -11,14 +11,9 @@ def self.up
def self.down
remove_column :users, :openid_identifier
- # Due to namespacing change, temporarily set the table back to users
- Spree::User.table_name = 'users'
-
[:login, :crypted_password, :salt].each do |field|
Spree::User.where(field => nil).each { |user| user.update_attribute(field, '') if user.send(field).nil? }
change_column :users, field, :string, :default => '', :null => false
end
-
- Spree::User.table_name = 'spree_users'
end
end
View
6 core/db/migrate/20091021133257_charge_refactoring.rb
@@ -7,9 +7,6 @@ class Order < ActiveRecord::Base
class ChargeRefactoring < ActiveRecord::Migration
def self.up
- # Temporarily set table name for legacy support
- Spree::Adjustment.table_name = "adjustments"
-
add_column :orders, :completed_at, :timestamp
Order.reset_column_information
Order.all.each {|o| o.update_attribute(:completed_at, o.checkout && o.checkout.read_attribute(:completed_at)) }
@@ -19,9 +16,6 @@ def self.up
Spree::Adjustment.update_all "type = secondary_type"
Spree::Adjustment.update_all "type = 'CouponCredit'", "type = 'Credit'"
remove_column :adjustments, :secondary_type
-
- # Reset table name
- Spree::Adjustment.table_name = "spree_adjustments"
end
def self.down
View
5 core/db/migrate/20100105132138_shipment_id_for_inventory_units.rb
@@ -3,9 +3,6 @@ def self.up
add_column :inventory_units, :shipment_id, :integer
add_index :inventory_units, :shipment_id
- # migrate legacy shipments
- Spree::Shipment.table_name = 'shipments'
-
Spree::Shipment.all.each do |shipment|
unless shipment.order
puts "Warning: shipment has invalid order - #{shipment.id}"
@@ -15,8 +12,6 @@ def self.up
unit.update_attribute('shipment_id', shipment.id)
end
end
-
- Spree::Shipment.table_name = 'spree_shipments'
end
def self.down
View
4 core/db/migrate/20100209144531_polymorphic_payments.rb
@@ -12,8 +12,6 @@ def self.up
end
execute "UPDATE payments SET payable_type = 'Order'"
- Spree::Creditcard.table_name = 'creditcards'
-
Spree::Creditcard.all.each do |creditcard|
if checkout = Checkout.find_by_id(creditcard.checkout_id) and checkout.order
if payment = checkout.order.payments.first
@@ -22,8 +20,6 @@ def self.up
end
end
- Spree::Creditcard.table_name = 'spree_creditcards'
-
remove_column :creditcards, :checkout_id
end
View
5 core/db/migrate/20100214212536_assign_creditcard_txns_to_payment.rb
@@ -2,17 +2,12 @@ class AssignCreditcardTxnsToPayment < ActiveRecord::Migration
def self.up
add_column :creditcard_txns, :payment_id, :integer
- # Temporarily set back to creditcards
- Spree::Creditcard.table_name = 'creditcards'
-
ActiveRecord::Base.connection.select_all('SELECT * FROM creditcard_txns').each do |txn_attrs|
if creditcard = Spree::Creditcard.find_by_id(txn_attrs['creditcard_id']) and creditcard.payments.first
execute "UPDATE creditcard_txns SET payment_id = #{creditcard.payments.first.id} WHERE id = #{txn_attrs['id']}"
end
end
- Spree::Creditcard.table_name = 'spree_creditcards'
-
remove_column :creditcard_txns, :creditcard_payment_id
end
View
4 core/db/migrate/20100506180619_add_icon_to_taxons.rb
@@ -1,15 +1,11 @@
class AddIconToTaxons < ActiveRecord::Migration
def self.up
- # legacy table support
- Spree::Taxon.table_name = 'taxons'
# skip this migration if the attribute already exists because of advanced taxon extension
return if Spree::Taxon.new.respond_to? :icon_file_name
add_column :taxons, :icon_file_name, :string
add_column :taxons, :icon_content_type, :string
add_column :taxons, :icon_file_size, :integer
add_column :taxons, :icon_updated_at, :datetime
-
- Spree::Taxon.table_name = 'spree_taxons'
end
def self.down
View
5 core/db/migrate/20101026184714_migrate_transactions_to_payment_state.rb
@@ -12,9 +12,6 @@ class MigrateTransactionsToPaymentState < ActiveRecord::Migration
PAYMENT_COMPLETE = 'completed'
PAYMENT_VOID = 'void'
PAYMENT_PENDING = 'pending'
-
- # Temporarily set the table back to payments
- Spree::Payment.table_name = 'payments'
def self.up
migrate_authorized_only_transactions
@@ -22,8 +19,6 @@ def self.up
migrate_completed_transactions
migrate_purchased_transactions
migrate_credited_transactions
-
- Spree::Payment.table_name = 'spree_payments'
end
def self.migrate_credited_transactions
View
6 core/db/migrate/20101026184959_generate_anonymous_users.rb
@@ -1,8 +1,5 @@
class GenerateAnonymousUsers < ActiveRecord::Migration
def self.up
- Spree::User.table_name = 'users'
- Spree::Order.table_name = 'orders'
-
Spree::User.reset_column_information
Spree::Order.where(:user_id => nil).each do |order|
user = Spree::User.anonymous!
@@ -10,9 +7,6 @@ def self.up
order.user = user
order.save!
end
-
- Spree::User.table_name = 'spree_users'
- Spree::Order.table_name = 'spree_orders'
end
def self.down
View
4 core/db/migrate/20101026185022_update_order_state.rb
@@ -1,10 +1,6 @@
class UpdateOrderState < ActiveRecord::Migration
def self.up
- Spree::Order.table_name = 'orders'
-
Spree::Order.all.map(&:update!)
-
- Spree::Order.table_name = 'spree_orders'
end
def self.down
Something went wrong with that request. Please try again.