Skip to content

Instantly share code, notes, and snippets.

@BenMorganIO
Last active April 5, 2023 02:13
Show Gist options
  • Save BenMorganIO/4874b7a1d37c465999535a08ef2c2927 to your computer and use it in GitHub Desktop.
Save BenMorganIO/4874b7a1d37c465999535a08ef2c2927 to your computer and use it in GitHub Desktop.
Make Solidus Tables NOT NULL for Timestamps
class NotNullTimestampsSpreeTables < ActiveRecord::Migration[7.0]
TABLES = %i[
friendly_id_slugs
spree_addresses spree_adjustment_reasons spree_adjustments spree_assets
spree_calculators spree_cartons spree_countries spree_credit_cards
spree_customer_returns spree_inventory_units spree_line_item_actions
spree_line_items spree_log_entries spree_option_type_prototypes
spree_option_types spree_option_values spree_option_values_variants
spree_order_mutexes spree_orders spree_orders_promotions
spree_payment_capture_events spree_payment_methods spree_payments
spree_preferences spree_prices spree_product_option_types
spree_product_promotion_rules spree_product_properties spree_products
spree_products_taxons spree_promotion_action_line_items
spree_promotion_actions spree_promotion_categories spree_promotion_codes
spree_promotion_rule_taxons spree_promotion_rules
spree_promotion_rules_users spree_promotions spree_properties
spree_property_prototypes spree_prototype_taxons spree_prototypes
spree_refund_reasons spree_refunds spree_reimbursement_credits
spree_reimbursement_types spree_reimbursements spree_return_authorizations
spree_return_items spree_return_reasons spree_roles spree_roles_users
spree_shipments spree_shipping_categories spree_shipping_method_categories
spree_shipping_method_stock_locations spree_shipping_method_zones
spree_shipping_methods spree_shipping_rates spree_state_changes spree_states
spree_stock_items spree_stock_locations spree_store_credit_categories
spree_store_credit_events spree_store_credit_types spree_store_credits
spree_stores spree_tax_categories spree_tax_rates spree_taxonomies
spree_taxons spree_unit_cancels spree_user_stock_locations
spree_variant_property_rule_values spree_variants spree_zone_members
spree_zones
].freeze
TABLES_WITHOUT_UPDATED_AT = %i[spree_order_mutexes].freeze
def up
TABLES.each do |table|
execute <<~SQL.squish
UPDATE #{table}
SET created_at = NOW()
WHERE created_at IS NULL
SQL
execute <<~SQL.squish unless TABLES_WITHOUT_UPDATED_AT.include?(table)
UPDATE #{table}
SET updated_at = NOW()
WHERE updated_at IS NULL
SQL
change_table table, bulk: true do |t|
t.change_null :created_at, false
t.change_null :updated_at, false unless TABLES_WITHOUT_UPDATED_AT.include?(table)
end
end
end
def down
change_table table, bulk: true do |t|
t.change_null :created_at, true
t.change_null :updated_at, true unless TABLES_WITHOUT_UPDATED_AT.include?(table)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment