From 626427fa946aec9f490e56df4e05b6f2838a84bf Mon Sep 17 00:00:00 2001 From: Toon Willems Date: Thu, 22 Aug 2024 17:53:00 +0200 Subject: [PATCH] Add strong_migrations gem and annotate existing migrations --- Gemfile | 1 + Gemfile.lock | 3 + db/migrate/20220525122759_init_schema.rb | 477 +++++++++--------- .../20220601150058_add_add_on_to_fees.rb | 4 +- ...910_add_invoice_fields_to_organizations.rb | 26 +- .../20220620150551_add_slug_to_customers.rb | 8 +- .../20220621090834_add_number_to_invoices.rb | 12 +- .../20220705155228_add_unaccent_extension.rb | 2 +- ...ange_active_storage_attachments_id_type.rb | 8 +- ...722123417_add_subscription_id_to_events.rb | 8 +- ...25152220_add_unique_id_to_subscriptions.rb | 4 +- ...0727132848_create_invoice_subscriptions.rb | 4 +- ...20220727161448_add_customer_to_invoices.rb | 4 +- ...add_anniversary_fields_to_subscriptions.rb | 4 +- .../20220729062203_remove_invoice_columns.rb | 10 +- ...7_add_invoice_id_to_wallet_transactions.rb | 4 +- ..._remove_invoice_from_wallet_transaction.rb | 4 +- ...095619_add_invoiceable_and_type_to_fees.rb | 4 +- ..._precision_and_scale_for_decimal_fields.rb | 22 +- ...ique_id_to_external_id_on_subscriptions.rb | 6 +- ...customer_id_to_external_id_on_customers.rb | 4 +- ...51923_add_wallet_transaction_to_invoice.rb | 4 +- ...persisted_metrics_into_persisted_events.rb | 6 +- ..._billable_metric_id_to_persisted_events.rb | 12 +- ...59_add_membership_status_and_revoked_at.rb | 8 +- .../20220916131538_add_parent_id_on_plans.rb | 4 +- ...pon_type_and_percentage_rate_to_coupons.rb | 24 +- ...uency_and_frequency_duration_to_coupons.rb | 16 +- ...23092906_add_expiration_date_to_coupons.rb | 4 +- ...930143002_add_credit_note_id_to_credits.rb | 6 +- ...0221010083509_rename_credit_credit_note.rb | 4 +- .../20221010142031_update_credit_notes.rb | 20 +- ...20221011083520_rename_credit_note_items.rb | 6 +- ...21018144521_add_legacy_flag_to_invoices.rb | 16 +- ...020093745_add_credit_amount_to_invoices.rb | 26 +- ...90308_add_refund_fields_to_credit_notes.rb | 20 +- .../20221028091920_add_group_id_to_fees.rb | 4 +- ...1107151038_add_vat_rates_to_credit_note.rb | 16 +- ...51027_changes_credit_note_items_columns.rb | 12 +- ...20221115100834_add_invoice_grace_period.rb | 6 +- ...d_unique_index_on_customers_external_id.rb | 6 +- ...0221122163328_rename_status_on_invoices.rb | 16 +- ...111605_add_issuing_date_to_credit_notes.rb | 14 +- .../20221128132620_change_fees_boundaries.rb | 6 +- ...1129133433_change_expiration_dates_type.rb | 15 +- ...02130126_rename_wallets_expiration_date.rb | 12 +- ...206094412_change_subscription_date_type.rb | 9 +- ...add_properties_to_invoice_subscriptions.rb | 10 +- ...810_add_source_to_invoice_subscriptions.rb | 14 +- ...3_add_payment_retry_columns_to_invoices.rb | 8 +- ...1209_change_invoice_subscription_source.rb | 16 +- .../20221222164226_finalize_invoices.rb | 12 +- ...20_add_nullable_to_invoice_grace_period.rb | 16 +- ...02150636_change_invoices_default_status.rb | 7 +- ...6152449_turn_all_amount_cents_to_bigint.rb | 6 +- ...0324_add_deleted_at_to_billable_metrics.rb | 18 +- .../20230126103454_add_deleted_at_to_plans.rb | 9 +- ...0230131144740_add_deleted_at_to_add_ons.rb | 8 +- ...0230131152047_add_deleted_at_to_coupons.rb | 8 +- ...add_index_to_payment_provider_customers.rb | 5 +- ...30202150407_add_deleted_at_to_customers.rb | 8 +- ...2163249_add_organization_id_to_invoices.rb | 16 +- ...03132157_add_deleted_at_to_coupon_plans.rb | 4 +- ...change_webhooks_organization_id_to_uuid.rb | 6 +- .../20230214145444_add_default_to_vat_rate.rb | 10 +- ...ecise_amount_cents_to_credit_note_items.rb | 14 +- ...230323112252_add_payment_status_to_fees.rb | 30 +- ...0403093407_add_balance_cents_to_wallets.rb | 58 ++- ..._billable_metric_limitations_to_coupons.rb | 7 +- ..._wallet_consumed_amount_currency_naming.rb | 4 +- .../20230414074225_add_version_to_invoices.rb | 9 +- ...20230414130437_add_currency_to_invoices.rb | 16 +- ...94339_add_fees_amount_cents_to_invoices.rb | 11 +- ...20_add_coupons_amount_cents_to_invoices.rb | 10 +- ...d_credit_notes_amount_cents_to_invoices.rb | 10 +- ...0230418151450_add_subtotals_to_invoices.rb | 22 +- ...230419123538_add_true_up_fee_id_to_fees.rb | 4 +- ...0420114754_remove_invoice_credit_amount.rb | 4 +- .../20230420120806_remove_invoice_amount.rb | 4 +- ...30424092207_add_one_off_columns_to_fees.rb | 10 +- ..._drop_internal_credit_notes_vat_amounts.rb | 12 +- ...516_add_precise_amounts_to_credit_notes.rb | 24 +- ...424210224_rename_true_up_fee_id_on_fees.rb | 4 +- .../20230505093030_change_credits_id_type.rb | 14 +- ...ustomers_tax_rates_to_applied_tax_rates.rb | 4 +- ...093556_rename_instant_to_pay_in_advance.rb | 15 +- ...2091400_add_unique_index_on_memberships.rb | 4 +- ...0230522093423_rename_tax_rates_to_taxes.rb | 12 +- ...230522113810_add_invoiceable_to_charges.rb | 10 +- ...230523094557_rename_invoices_vat_fields.rb | 10 +- ...23140656_rename_credit_notes_vat_fields.rb | 8 +- .../20230524130637_rename_fees_vat_fields.rb | 8 +- ...rename_applied_taxes_to_customers_taxes.rb | 4 +- ...add_boundaries_to_invoice_subscriptions.rb | 28 +- ...e_properties_from_invoice_subscriptions.rb | 4 +- ...3_rename_persistent_to_quentified_event.rb | 6 +- ...d_unique_index_to_invoice_subscriptions.rb | 22 +- ...20230614191603_create_webhook_endpoints.rb | 32 +- ...add_event_refrence_to_quantified_events.rb | 4 +- ...211201_add_unique_index_to_webhook_urls.rb | 4 +- ...0230626123648_unify_invoices_taxes_rate.rb | 10 +- .../20230704112230_fix_organizations_taxes.rb | 10 +- ...er_payment_methods_for_stripe_customers.rb | 16 +- ...20230713122526_rename_credit_before_vat.rb | 4 +- ...ring_count_and_unique_count_aggregation.rb | 68 +-- .../20230721073114_fix_credit_before_taxes.rb | 6 +- ...yment_term_on_organization_and_customer.rb | 18 +- ...1135721_add_net_payment_term_to_invoice.rb | 19 +- ...808144739_fill_invoice_payment_due_date.rb | 6 +- ...ulate_fees_precise_coupons_amount_cents.rb | 118 ++--- ...late_fees_amount_cents_in_invoice_taxes.rb | 6 +- ...92555_set_default_properties_to_charges.rb | 6 +- ...d_unique_constraint_to_group_properties.rb | 4 +- ...0230830120517_remove_status_from_groups.rb | 14 +- ...dd_weighted_interval_to_billable_metric.rb | 6 +- ...53404_add_group_id_to_quantified_events.rb | 6 +- ...3923_add_total_aggregated_units_to_fees.rb | 10 +- ...85900_add_unique_index_to_applied_taxes.rb | 28 +- ...230915073205_update_code_index_on_plans.rb | 10 +- ...nvoice_display_name_to_group_properties.rb | 6 +- ...30922064617_remove_parent_id_from_plans.rb | 8 +- ...230926132500_add_external_ids_to_events.rb | 12 +- .../20230926144126_fill_event_external_ids.rb | 6 +- ...85938_deduplicate_events_transaction_id.rb | 6 +- ...090849_events_transaction_id_uniqueness.rb | 15 +- ...31_convert_unit_amount_cents_to_decimal.rb | 4 +- ...7144605_add_precise_unit_amount_to_fees.rb | 6 +- ...sing_indexes_to_subscriptions_and_plans.rb | 8 +- ...index_on_event_external_subscription_id.rb | 14 +- ...change_precision_to_precise_unit_amount.rb | 6 +- ...24_delete_versions_for_group_properties.rb | 6 +- ...09_undiscard_incorrectly_deleted_events.rb | 6 +- ...31109154934_add_events_validation_index.rb | 4 +- ...dd_organization_id_to_quantified_events.rb | 18 +- ...d_unique_index_on_invoice_subscriptions.rb | 4 +- ...0_add_fields_for_new_document_numbering.rb | 28 +- ..._add_code_and_name_to_payment_providers.rb | 20 +- ..._add_payment_provider_code_to_customers.rb | 7 +- ...emove_events_quantified_events_relation.rb | 4 +- ...x_on_invoice_organization_sequential_id.rb | 12 +- ...update_organization_sequential_id_index.rb | 18 +- ...103125624_add_group_id_to_adjusted_fees.rb | 4 +- ...0112091706_add_charge_filter_id_to_fees.rb | 4 +- ...edits_auto_refreshed_from_organizations.rb | 14 +- ...40123104811_change_fees_grouped_by_type.rb | 16 +- ...charge_filter_id_to_cached_aggregations.rb | 8 +- ..._add_missing_charge_filter_id_relations.rb | 8 +- ...240308150801_update_last_hour_events_mv.rb | 4 +- ...091817_add_index_on_last_hour_events_mv.rb | 4 +- .../20240312141641_add_new_index_to_groups.rb | 4 +- ...multiple_values_to_charge_filter_values.rb | 8 +- ...071539_add_payment_disputed_to_invoices.rb | 6 +- ...1_remove_unique_count_quantified_events.rb | 5 +- ...d_invoice_type_to_invoice_subscriptions.rb | 22 +- ...ansaction_status_to_wallet_transactions.rb | 6 +- ..._organization_timestamp_index_on_events.rb | 4 +- ...110420_rename_integration_items_columns.rb | 6 +- ...9_add_integration_item_uniqueness_index.rb | 4 +- ...3150_add_invoiced_to_transaction_status.rb | 6 +- ...40502075803_add_filters_missing_indexes.rb | 20 +- ...0502095122_add_indexes_to_subscriptions.rb | 4 +- ...d_method_to_recurring_transaction_rules.rb | 4 +- ...integration_id_to_integration_resources.rb | 12 +- ...arted_at_to_recurring_transaction_rules.rb | 4 +- ...t_transaction_id_to_cached_aggregations.rb | 4 +- ...1074215_add_payment_overdue_to_invoices.rb | 6 +- ...dvance_charge_periodic_invoicing_reason.rb | 6 +- ...94521_add_shipping_address_to_customers.rb | 16 +- ..._id_and_organization_id_to_data_exports.rb | 6 +- ...083830_remove_user_id_from_data_exports.rb | 4 +- ...integration_reference_from_error_detail.rb | 10 +- ...payable_type_and_payable_id_to_payments.rb | 22 +- .../20240801134832_create_payment_requests.rb | 9 +- ...0801142242_alter_events_vacuum_settings.rb | 4 +- ...52_rename_progressive_billing_tresholds.rb | 4 +- .../20240807113700_rename_usage_tresholds.rb | 6 +- ...ve_amount_currency_from_usage_tresholds.rb | 6 +- ...13095718_add_usage_threshold_id_to_fees.rb | 4 +- ...progressive_billing_to_invoicing_reason.rb | 6 +- ..._to_payment_requests_and_payable_groups.rb | 38 +- ...2354_add_deleted_at_to_usage_thresholds.rb | 6 +- ...progressive_billing_invoices_to_credits.rb | 6 +- ...093145_create_invoices_payment_requests.rb | 43 +- 183 files changed, 1427 insertions(+), 1073 deletions(-) diff --git a/Gemfile b/Gemfile index 022439d26e59..a8fd978add91 100644 --- a/Gemfile +++ b/Gemfile @@ -33,6 +33,7 @@ gem 'pg' gem 'ransack', '~> 4.1.0' gem 'scenic' gem 'with_advisory_lock' +gem 'strong_migrations' # Currencies, Countries, Timezones... gem 'bigdecimal' diff --git a/Gemfile.lock b/Gemfile.lock index 17634a1547b6..799f606eed18 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -827,6 +827,8 @@ GEM lint_roller (~> 1.1) rubocop-performance (~> 1.20.2) stripe (6.5.0) + strong_migrations (2.0.0) + activerecord (>= 6.1) strscan (3.1.0) temple (0.8.2) terminal-table (3.0.2) @@ -947,6 +949,7 @@ DEPENDENCIES slim-rails standard stripe + strong_migrations timecop tzinfo-data uglifier diff --git a/db/migrate/20220525122759_init_schema.rb b/db/migrate/20220525122759_init_schema.rb index 3b76791c4393..9d195b77ef0e 100644 --- a/db/migrate/20220525122759_init_schema.rb +++ b/db/migrate/20220525122759_init_schema.rb @@ -2,244 +2,245 @@ class InitSchema < ActiveRecord::Migration[7.0] def change - enable_extension 'pgcrypto' - - create_table 'add_ons', id: :uuid, default: -> { 'gen_random_uuid()' }, force: :cascade do |t| - t.uuid 'organization_id', null: false - t.string 'name', null: false - t.string 'code', null: false - t.string 'description' - t.bigint 'amount_cents', null: false - t.string 'amount_currency', null: false - t.datetime 'created_at', null: false - t.datetime 'updated_at', null: false - t.index %w[organization_id code], name: 'index_add_ons_on_organization_id_and_code', unique: true - t.index ['organization_id'], name: 'index_add_ons_on_organization_id' + safety_assured do + enable_extension 'pgcrypto' + + create_table 'add_ons', id: :uuid, default: -> { 'gen_random_uuid()' }, force: :cascade do |t| + t.uuid 'organization_id', null: false + t.string 'name', null: false + t.string 'code', null: false + t.string 'description' + t.bigint 'amount_cents', null: false + t.string 'amount_currency', null: false + t.datetime 'created_at', null: false + t.datetime 'updated_at', null: false + t.index %w[organization_id code], name: 'index_add_ons_on_organization_id_and_code', unique: true + t.index ['organization_id'], name: 'index_add_ons_on_organization_id' + end + + create_table 'applied_coupons', id: :uuid, default: -> { 'gen_random_uuid()' }, force: :cascade do |t| + t.uuid 'coupon_id', null: false + t.uuid 'customer_id', null: false + t.integer 'status', default: 0, null: false + t.integer 'amount_cents', null: false + t.string 'amount_currency', null: false + t.datetime 'created_at', null: false + t.datetime 'updated_at', null: false + t.datetime 'terminated_at', precision: nil + t.index %w[coupon_id customer_id], + name: 'index_applied_coupons_on_coupon_id_and_customer_id', + unique: true, + where: '(status = 0)' + t.index ['coupon_id'], name: 'index_applied_coupons_on_coupon_id' + t.index ['customer_id'], name: 'index_applied_coupons_on_customer_id' + end + + create_table 'billable_metrics', id: :uuid, default: -> { 'gen_random_uuid()' }, force: :cascade do |t| + t.uuid 'organization_id', null: false + t.string 'name', null: false + t.string 'code', null: false + t.string 'description' + t.jsonb 'properties', default: {} + t.integer 'aggregation_type', null: false + t.datetime 'created_at', null: false + t.datetime 'updated_at', null: false + t.string 'field_name' + t.index %w[organization_id code], name: 'index_billable_metrics_on_organization_id_and_code', unique: true + t.index ['organization_id'], name: 'index_billable_metrics_on_organization_id' + end + + create_table 'charges', id: :uuid, default: -> { 'gen_random_uuid()' }, force: :cascade do |t| + t.uuid 'billable_metric_id' + t.datetime 'created_at', null: false + t.datetime 'updated_at', null: false + t.uuid 'plan_id' + t.string 'amount_currency', null: false + t.integer 'charge_model', default: 0, null: false + t.jsonb 'properties', default: '{}', null: false + t.index ['billable_metric_id'], name: 'index_charges_on_billable_metric_id' + t.index ['plan_id'], name: 'index_charges_on_plan_id' + end + + create_table 'coupons', id: :uuid, default: -> { 'gen_random_uuid()' }, force: :cascade do |t| + t.uuid 'organization_id', null: false + t.string 'name', null: false + t.string 'code' + t.integer 'status', default: 0, null: false + t.datetime 'terminated_at' + t.bigint 'amount_cents', null: false + t.string 'amount_currency', null: false + t.integer 'expiration', null: false + t.integer 'expiration_duration' + t.datetime 'created_at', null: false + t.datetime 'updated_at', null: false + t.index %w[organization_id code], + name: 'index_coupons_on_organization_id_and_code', + unique: true, + where: '(code IS NOT NULL)' + t.index ['organization_id'], name: 'index_coupons_on_organization_id' + end + + create_table 'credits', force: :cascade do |t| + t.uuid 'invoice_id' + t.uuid 'applied_coupon_id' + t.bigint 'amount_cents', null: false + t.string 'amount_currency', null: false + t.datetime 'created_at', null: false + t.datetime 'updated_at', null: false + t.index ['applied_coupon_id'], name: 'index_credits_on_applied_coupon_id' + t.index ['invoice_id'], name: 'index_credits_on_invoice_id' + end + + create_table 'customers', id: :uuid, default: -> { 'gen_random_uuid()' }, force: :cascade do |t| + t.string 'customer_id', null: false + t.string 'name' + t.uuid 'organization_id', null: false + t.datetime 'created_at', null: false + t.datetime 'updated_at', null: false + t.string 'country' + t.string 'address_line1' + t.string 'address_line2' + t.string 'state' + t.string 'zipcode' + t.string 'email' + t.string 'city' + t.string 'url' + t.string 'phone' + t.string 'logo_url' + t.string 'legal_name' + t.string 'legal_number' + t.float 'vat_rate' + t.index ['customer_id'], name: 'index_customers_on_customer_id' + t.index ['organization_id'], name: 'index_customers_on_organization_id' + end + + create_table 'events', id: :uuid, default: -> { 'gen_random_uuid()' }, force: :cascade do |t| + t.uuid 'organization_id', null: false + t.uuid 'customer_id', null: false + t.string 'transaction_id', null: false + t.string 'code', null: false + t.jsonb 'properties', default: {}, null: false + t.datetime 'timestamp', precision: nil + t.datetime 'created_at', null: false + t.datetime 'updated_at', null: false + t.index ['customer_id'], name: 'index_events_on_customer_id' + t.index %w[organization_id code], name: 'index_events_on_organization_id_and_code' + t.index %w[organization_id transaction_id], + name: 'index_events_on_organization_id_and_transaction_id', + unique: true + t.index ['organization_id'], name: 'index_events_on_organization_id' + end + + create_table 'fees', id: :uuid, default: -> { 'gen_random_uuid()' }, force: :cascade do |t| + t.uuid 'invoice_id' + t.uuid 'charge_id' + t.uuid 'subscription_id' + t.bigint 'amount_cents', null: false + t.string 'amount_currency', null: false + t.bigint 'vat_amount_cents', null: false + t.string 'vat_amount_currency', null: false + t.float 'vat_rate' + t.datetime 'created_at', null: false + t.datetime 'updated_at', null: false + t.decimal 'units', default: '0.0', null: false + t.index ['charge_id'], name: 'index_fees_on_charge_id' + t.index ['invoice_id'], name: 'index_fees_on_invoice_id' + t.index ['subscription_id'], name: 'index_fees_on_subscription_id' + end + + create_table 'invoices', id: :uuid, default: -> { 'gen_random_uuid()' }, force: :cascade do |t| + t.uuid 'subscription_id', null: false + t.date 'from_date', null: false + t.date 'to_date', null: false + t.datetime 'created_at', null: false + t.datetime 'updated_at', null: false + t.date 'issuing_date' + t.bigint 'amount_cents', default: 0, null: false + t.string 'amount_currency' + t.bigint 'vat_amount_cents', default: 0, null: false + t.string 'vat_amount_currency' + t.bigint 'total_amount_cents', default: 0, null: false + t.string 'total_amount_currency' + t.integer 'sequential_id' + t.index ['sequential_id'], name: 'index_invoices_on_sequential_id' + t.index ['subscription_id'], name: 'index_invoices_on_subscription_id' + end + + create_table 'memberships', id: :uuid, default: -> { 'gen_random_uuid()' }, force: :cascade do |t| + t.uuid 'organization_id', null: false + t.uuid 'user_id', null: false + t.datetime 'created_at', null: false + t.datetime 'updated_at', null: false + t.integer 'role', default: 0, null: false + t.index ['organization_id'], name: 'index_memberships_on_organization_id' + t.index ['user_id'], name: 'index_memberships_on_user_id' + end + + create_table 'organizations', id: :uuid, default: -> { 'gen_random_uuid()' }, force: :cascade do |t| + t.string 'name', null: false + t.datetime 'created_at', null: false + t.datetime 'updated_at', null: false + t.string 'api_key' + t.string 'webhook_url' + t.float 'vat_rate', default: 0.0, null: false + t.index ['api_key'], name: 'index_organizations_on_api_key', unique: true + end + + create_table 'plans', id: :uuid, default: -> { 'gen_random_uuid()' }, force: :cascade do |t| + t.uuid 'organization_id', null: false + t.string 'name', null: false + t.datetime 'created_at', null: false + t.datetime 'updated_at', null: false + t.string 'code', null: false + t.integer 'interval', null: false + t.string 'description' + t.bigint 'amount_cents', null: false + t.string 'amount_currency', null: false + t.float 'trial_period' + t.boolean 'pay_in_advance', default: false, null: false + t.index %w[code organization_id], name: 'index_plans_on_code_and_organization_id', unique: true + t.index ['organization_id'], name: 'index_plans_on_organization_id' + end + + create_table 'subscriptions', id: :uuid, default: -> { 'gen_random_uuid()' }, force: :cascade do |t| + t.uuid 'customer_id', null: false + t.uuid 'plan_id', null: false + t.integer 'status', null: false + t.datetime 'canceled_at', precision: nil + t.datetime 'terminated_at', precision: nil + t.datetime 'started_at', precision: nil + t.datetime 'created_at', null: false + t.datetime 'updated_at', null: false + t.uuid 'previous_subscription_id' + t.date 'anniversary_date' + t.index ['customer_id'], name: 'index_subscriptions_on_customer_id' + t.index ['plan_id'], name: 'index_subscriptions_on_plan_id' + end + + create_table 'users', id: :uuid, default: -> { 'gen_random_uuid()' }, force: :cascade do |t| + t.string 'email' + t.string 'password_digest' + t.datetime 'created_at', null: false + t.datetime 'updated_at', null: false + end + add_foreign_key 'add_ons', 'organizations' + add_foreign_key 'billable_metrics', 'organizations' + add_foreign_key 'charges', 'billable_metrics' + add_foreign_key 'charges', 'plans' + add_foreign_key 'credits', 'applied_coupons' + add_foreign_key 'credits', 'invoices' + add_foreign_key 'customers', 'organizations' + add_foreign_key 'events', 'customers' + add_foreign_key 'events', 'organizations' + add_foreign_key 'fees', 'charges' + add_foreign_key 'fees', 'invoices' + add_foreign_key 'fees', 'subscriptions' + add_foreign_key 'invoices', 'subscriptions' + add_foreign_key 'memberships', 'organizations' + add_foreign_key 'memberships', 'users' + add_foreign_key 'plans', 'organizations' + add_foreign_key 'subscriptions', 'customers' + add_foreign_key 'subscriptions', 'plans' end - - create_table 'applied_coupons', id: :uuid, default: -> { 'gen_random_uuid()' }, force: :cascade do |t| - t.uuid 'coupon_id', null: false - t.uuid 'customer_id', null: false - t.integer 'status', default: 0, null: false - t.integer 'amount_cents', null: false - t.string 'amount_currency', null: false - t.datetime 'created_at', null: false - t.datetime 'updated_at', null: false - t.datetime 'terminated_at', precision: nil - t.index %w[coupon_id customer_id], - name: 'index_applied_coupons_on_coupon_id_and_customer_id', - unique: true, - where: '(status = 0)' - t.index ['coupon_id'], name: 'index_applied_coupons_on_coupon_id' - t.index ['customer_id'], name: 'index_applied_coupons_on_customer_id' - end - - create_table 'billable_metrics', id: :uuid, default: -> { 'gen_random_uuid()' }, force: :cascade do |t| - t.uuid 'organization_id', null: false - t.string 'name', null: false - t.string 'code', null: false - t.string 'description' - t.jsonb 'properties', default: {} - t.integer 'aggregation_type', null: false - t.datetime 'created_at', null: false - t.datetime 'updated_at', null: false - t.string 'field_name' - t.index %w[organization_id code], name: 'index_billable_metrics_on_organization_id_and_code', unique: true - t.index ['organization_id'], name: 'index_billable_metrics_on_organization_id' - end - - create_table 'charges', id: :uuid, default: -> { 'gen_random_uuid()' }, force: :cascade do |t| - t.uuid 'billable_metric_id' - t.datetime 'created_at', null: false - t.datetime 'updated_at', null: false - t.uuid 'plan_id' - t.string 'amount_currency', null: false - t.integer 'charge_model', default: 0, null: false - t.jsonb 'properties', default: '{}', null: false - t.index ['billable_metric_id'], name: 'index_charges_on_billable_metric_id' - t.index ['plan_id'], name: 'index_charges_on_plan_id' - end - - create_table 'coupons', id: :uuid, default: -> { 'gen_random_uuid()' }, force: :cascade do |t| - t.uuid 'organization_id', null: false - t.string 'name', null: false - t.string 'code' - t.integer 'status', default: 0, null: false - t.datetime 'terminated_at' - t.bigint 'amount_cents', null: false - t.string 'amount_currency', null: false - t.integer 'expiration', null: false - t.integer 'expiration_duration' - t.datetime 'created_at', null: false - t.datetime 'updated_at', null: false - t.index %w[organization_id code], - name: 'index_coupons_on_organization_id_and_code', - unique: true, - where: '(code IS NOT NULL)' - t.index ['organization_id'], name: 'index_coupons_on_organization_id' - end - - create_table 'credits', force: :cascade do |t| - t.uuid 'invoice_id' - t.uuid 'applied_coupon_id' - t.bigint 'amount_cents', null: false - t.string 'amount_currency', null: false - t.datetime 'created_at', null: false - t.datetime 'updated_at', null: false - t.index ['applied_coupon_id'], name: 'index_credits_on_applied_coupon_id' - t.index ['invoice_id'], name: 'index_credits_on_invoice_id' - end - - create_table 'customers', id: :uuid, default: -> { 'gen_random_uuid()' }, force: :cascade do |t| - t.string 'customer_id', null: false - t.string 'name' - t.uuid 'organization_id', null: false - t.datetime 'created_at', null: false - t.datetime 'updated_at', null: false - t.string 'country' - t.string 'address_line1' - t.string 'address_line2' - t.string 'state' - t.string 'zipcode' - t.string 'email' - t.string 'city' - t.string 'url' - t.string 'phone' - t.string 'logo_url' - t.string 'legal_name' - t.string 'legal_number' - t.float 'vat_rate' - t.index ['customer_id'], name: 'index_customers_on_customer_id' - t.index ['organization_id'], name: 'index_customers_on_organization_id' - end - - create_table 'events', id: :uuid, default: -> { 'gen_random_uuid()' }, force: :cascade do |t| - t.uuid 'organization_id', null: false - t.uuid 'customer_id', null: false - t.string 'transaction_id', null: false - t.string 'code', null: false - t.jsonb 'properties', default: {}, null: false - t.datetime 'timestamp', precision: nil - t.datetime 'created_at', null: false - t.datetime 'updated_at', null: false - t.index ['customer_id'], name: 'index_events_on_customer_id' - t.index %w[organization_id code], name: 'index_events_on_organization_id_and_code' - t.index %w[organization_id transaction_id], - name: 'index_events_on_organization_id_and_transaction_id', - unique: true - t.index ['organization_id'], name: 'index_events_on_organization_id' - end - - create_table 'fees', id: :uuid, default: -> { 'gen_random_uuid()' }, force: :cascade do |t| - t.uuid 'invoice_id' - t.uuid 'charge_id' - t.uuid 'subscription_id' - t.bigint 'amount_cents', null: false - t.string 'amount_currency', null: false - t.bigint 'vat_amount_cents', null: false - t.string 'vat_amount_currency', null: false - t.float 'vat_rate' - t.datetime 'created_at', null: false - t.datetime 'updated_at', null: false - t.decimal 'units', default: '0.0', null: false - t.index ['charge_id'], name: 'index_fees_on_charge_id' - t.index ['invoice_id'], name: 'index_fees_on_invoice_id' - t.index ['subscription_id'], name: 'index_fees_on_subscription_id' - end - - create_table 'invoices', id: :uuid, default: -> { 'gen_random_uuid()' }, force: :cascade do |t| - t.uuid 'subscription_id', null: false - t.date 'from_date', null: false - t.date 'to_date', null: false - t.datetime 'created_at', null: false - t.datetime 'updated_at', null: false - t.date 'issuing_date' - t.bigint 'amount_cents', default: 0, null: false - t.string 'amount_currency' - t.bigint 'vat_amount_cents', default: 0, null: false - t.string 'vat_amount_currency' - t.bigint 'total_amount_cents', default: 0, null: false - t.string 'total_amount_currency' - t.integer 'sequential_id' - t.index ['sequential_id'], name: 'index_invoices_on_sequential_id' - t.index ['subscription_id'], name: 'index_invoices_on_subscription_id' - end - - create_table 'memberships', id: :uuid, default: -> { 'gen_random_uuid()' }, force: :cascade do |t| - t.uuid 'organization_id', null: false - t.uuid 'user_id', null: false - t.datetime 'created_at', null: false - t.datetime 'updated_at', null: false - t.integer 'role', default: 0, null: false - t.index ['organization_id'], name: 'index_memberships_on_organization_id' - t.index ['user_id'], name: 'index_memberships_on_user_id' - end - - create_table 'organizations', id: :uuid, default: -> { 'gen_random_uuid()' }, force: :cascade do |t| - t.string 'name', null: false - t.datetime 'created_at', null: false - t.datetime 'updated_at', null: false - t.string 'api_key' - t.string 'webhook_url' - t.float 'vat_rate', default: 0.0, null: false - t.index ['api_key'], name: 'index_organizations_on_api_key', unique: true - end - - create_table 'plans', id: :uuid, default: -> { 'gen_random_uuid()' }, force: :cascade do |t| - t.uuid 'organization_id', null: false - t.string 'name', null: false - t.datetime 'created_at', null: false - t.datetime 'updated_at', null: false - t.string 'code', null: false - t.integer 'interval', null: false - t.string 'description' - t.bigint 'amount_cents', null: false - t.string 'amount_currency', null: false - t.float 'trial_period' - t.boolean 'pay_in_advance', default: false, null: false - t.index %w[code organization_id], name: 'index_plans_on_code_and_organization_id', unique: true - t.index ['organization_id'], name: 'index_plans_on_organization_id' - end - - create_table 'subscriptions', id: :uuid, default: -> { 'gen_random_uuid()' }, force: :cascade do |t| - t.uuid 'customer_id', null: false - t.uuid 'plan_id', null: false - t.integer 'status', null: false - t.datetime 'canceled_at', precision: nil - t.datetime 'terminated_at', precision: nil - t.datetime 'started_at', precision: nil - t.datetime 'created_at', null: false - t.datetime 'updated_at', null: false - t.uuid 'previous_subscription_id' - t.date 'anniversary_date' - t.index ['customer_id'], name: 'index_subscriptions_on_customer_id' - t.index ['plan_id'], name: 'index_subscriptions_on_plan_id' - end - - create_table 'users', id: :uuid, default: -> { 'gen_random_uuid()' }, force: :cascade do |t| - t.string 'email' - t.string 'password_digest' - t.datetime 'created_at', null: false - t.datetime 'updated_at', null: false - end - - add_foreign_key 'add_ons', 'organizations' - add_foreign_key 'billable_metrics', 'organizations' - add_foreign_key 'charges', 'billable_metrics' - add_foreign_key 'charges', 'plans' - add_foreign_key 'credits', 'applied_coupons' - add_foreign_key 'credits', 'invoices' - add_foreign_key 'customers', 'organizations' - add_foreign_key 'events', 'customers' - add_foreign_key 'events', 'organizations' - add_foreign_key 'fees', 'charges' - add_foreign_key 'fees', 'invoices' - add_foreign_key 'fees', 'subscriptions' - add_foreign_key 'invoices', 'subscriptions' - add_foreign_key 'memberships', 'organizations' - add_foreign_key 'memberships', 'users' - add_foreign_key 'plans', 'organizations' - add_foreign_key 'subscriptions', 'customers' - add_foreign_key 'subscriptions', 'plans' end end diff --git a/db/migrate/20220601150058_add_add_on_to_fees.rb b/db/migrate/20220601150058_add_add_on_to_fees.rb index b7edbbb2a4e1..13996deaf9e2 100644 --- a/db/migrate/20220601150058_add_add_on_to_fees.rb +++ b/db/migrate/20220601150058_add_add_on_to_fees.rb @@ -2,6 +2,8 @@ class AddAddOnToFees < ActiveRecord::Migration[7.0] def change - add_reference :fees, :applied_add_on, type: :uuid, foreign_key: true, index: true + safety_assured do + add_reference :fees, :applied_add_on, type: :uuid, foreign_key: true, index: true + end end end diff --git a/db/migrate/20220620141910_add_invoice_fields_to_organizations.rb b/db/migrate/20220620141910_add_invoice_fields_to_organizations.rb index f14243803717..963ca52c0a6f 100644 --- a/db/migrate/20220620141910_add_invoice_fields_to_organizations.rb +++ b/db/migrate/20220620141910_add_invoice_fields_to_organizations.rb @@ -2,18 +2,20 @@ class AddInvoiceFieldsToOrganizations < ActiveRecord::Migration[7.0] def change - change_table :organizations, bulk: true do |t| - t.string :country - t.string :address_line1 - t.string :address_line2 - t.string :state - t.string :zipcode - t.string :email - t.string :city - t.string :logo - t.string :legal_name - t.string :legal_number - t.text :invoice_footer + safety_assured do + change_table :organizations, bulk: true do |t| + t.string :country + t.string :address_line1 + t.string :address_line2 + t.string :state + t.string :zipcode + t.string :email + t.string :city + t.string :logo + t.string :legal_name + t.string :legal_number + t.text :invoice_footer + end end end end diff --git a/db/migrate/20220620150551_add_slug_to_customers.rb b/db/migrate/20220620150551_add_slug_to_customers.rb index d26f03558082..4e77399010a4 100644 --- a/db/migrate/20220620150551_add_slug_to_customers.rb +++ b/db/migrate/20220620150551_add_slug_to_customers.rb @@ -2,9 +2,11 @@ class AddSlugToCustomers < ActiveRecord::Migration[7.0] def change - change_table :customers, bulk: true do |t| - t.string :slug - t.bigint :sequential_id + safety_assured do + change_table :customers, bulk: true do |t| + t.string :slug + t.bigint :sequential_id + end end LagoApi::Application.load_tasks diff --git a/db/migrate/20220621090834_add_number_to_invoices.rb b/db/migrate/20220621090834_add_number_to_invoices.rb index 6466c72d14bb..270312fd7f75 100644 --- a/db/migrate/20220621090834_add_number_to_invoices.rb +++ b/db/migrate/20220621090834_add_number_to_invoices.rb @@ -3,12 +3,14 @@ class AddNumberToInvoices < ActiveRecord::Migration[7.0] def change # NOTE: sequential_id scope change we have to reset the column - change_table :invoices, bulk: true do |t| - t.remove_index :sequential_id - t.remove :sequential_id, type: :integer + safety_assured do + change_table :invoices, bulk: true do |t| + t.remove_index :sequential_id + t.remove :sequential_id, type: :integer - t.string :number, null: false, index: true, default: '' - t.integer :sequential_id, index: true + t.string :number, null: false, index: true, default: '' + t.integer :sequential_id, index: true + end end end end diff --git a/db/migrate/20220705155228_add_unaccent_extension.rb b/db/migrate/20220705155228_add_unaccent_extension.rb index 658bbcc6258c..12391f6a2637 100644 --- a/db/migrate/20220705155228_add_unaccent_extension.rb +++ b/db/migrate/20220705155228_add_unaccent_extension.rb @@ -2,6 +2,6 @@ class AddUnaccentExtension < ActiveRecord::Migration[7.0] def up - execute 'CREATE EXTENSION unaccent' + safety_assured { execute 'CREATE EXTENSION unaccent' } end end diff --git a/db/migrate/20220713171816_change_active_storage_attachments_id_type.rb b/db/migrate/20220713171816_change_active_storage_attachments_id_type.rb index 4dc4674f9ff0..bb135126f152 100644 --- a/db/migrate/20220713171816_change_active_storage_attachments_id_type.rb +++ b/db/migrate/20220713171816_change_active_storage_attachments_id_type.rb @@ -2,9 +2,11 @@ class ChangeActiveStorageAttachmentsIdType < ActiveRecord::Migration[7.0] def change - change_table :active_storage_attachments, bulk: true do |t| - t.remove :record_id # rubocop:disable Rails/ReversibleMigration - t.uuid :record_id + safety_assured do + change_table :active_storage_attachments, bulk: true do |t| + t.remove :record_id # rubocop:disable Rails/ReversibleMigration + t.uuid :record_id + end end end end diff --git a/db/migrate/20220722123417_add_subscription_id_to_events.rb b/db/migrate/20220722123417_add_subscription_id_to_events.rb index 184b53748c3d..f94420e761e7 100644 --- a/db/migrate/20220722123417_add_subscription_id_to_events.rb +++ b/db/migrate/20220722123417_add_subscription_id_to_events.rb @@ -4,8 +4,10 @@ class AddSubscriptionIdToEvents < ActiveRecord::Migration[7.0] def change remove_index :events, %i[organization_id transaction_id] - add_reference :events, :subscription, type: :uuid, foreign_key: true - add_index :events, %i[subscription_id code] - add_index :events, %i[subscription_id transaction_id], unique: true + safety_assured do + add_reference :events, :subscription, type: :uuid, foreign_key: true + add_index :events, %i[subscription_id code] + add_index :events, %i[subscription_id transaction_id], unique: true + end end end diff --git a/db/migrate/20220725152220_add_unique_id_to_subscriptions.rb b/db/migrate/20220725152220_add_unique_id_to_subscriptions.rb index aeea2f0c0b86..d0edcbc8dadc 100644 --- a/db/migrate/20220725152220_add_unique_id_to_subscriptions.rb +++ b/db/migrate/20220725152220_add_unique_id_to_subscriptions.rb @@ -3,7 +3,9 @@ class AddUniqueIdToSubscriptions < ActiveRecord::Migration[7.0] def up add_column :subscriptions, :unique_id, :string - change_column_null :subscriptions, :unique_id, false + safety_assured do + change_column_null :subscriptions, :unique_id, false + end end def down diff --git a/db/migrate/20220727132848_create_invoice_subscriptions.rb b/db/migrate/20220727132848_create_invoice_subscriptions.rb index 46a0d47d540b..efcf27e77bf2 100644 --- a/db/migrate/20220727132848_create_invoice_subscriptions.rb +++ b/db/migrate/20220727132848_create_invoice_subscriptions.rb @@ -8,6 +8,8 @@ def change t.timestamps end - remove_reference :invoices, :subscription, index: true, foreign_key: true + safety_assured do + remove_reference :invoices, :subscription, index: true, foreign_key: true + end end end diff --git a/db/migrate/20220727161448_add_customer_to_invoices.rb b/db/migrate/20220727161448_add_customer_to_invoices.rb index ab72a71ae149..48512be44f9b 100644 --- a/db/migrate/20220727161448_add_customer_to_invoices.rb +++ b/db/migrate/20220727161448_add_customer_to_invoices.rb @@ -2,6 +2,8 @@ class AddCustomerToInvoices < ActiveRecord::Migration[7.0] def change - add_reference :invoices, :customer, type: :uuid, foreign_key: true, index: true + safety_assured do + add_reference :invoices, :customer, type: :uuid, foreign_key: true, index: true + end end end diff --git a/db/migrate/20220728144707_add_anniversary_fields_to_subscriptions.rb b/db/migrate/20220728144707_add_anniversary_fields_to_subscriptions.rb index dba045dc01f8..f68deb68a459 100644 --- a/db/migrate/20220728144707_add_anniversary_fields_to_subscriptions.rb +++ b/db/migrate/20220728144707_add_anniversary_fields_to_subscriptions.rb @@ -3,6 +3,8 @@ class AddAnniversaryFieldsToSubscriptions < ActiveRecord::Migration[7.0] def change add_column :subscriptions, :billing_time, :integer, null: false, default: 0 - rename_column :subscriptions, :anniversary_date, :subscription_date + safety_assured do + rename_column :subscriptions, :anniversary_date, :subscription_date + end end end diff --git a/db/migrate/20220729062203_remove_invoice_columns.rb b/db/migrate/20220729062203_remove_invoice_columns.rb index be2e86f95ae7..a7755fea19eb 100644 --- a/db/migrate/20220729062203_remove_invoice_columns.rb +++ b/db/migrate/20220729062203_remove_invoice_columns.rb @@ -2,10 +2,12 @@ class RemoveInvoiceColumns < ActiveRecord::Migration[7.0] def up - change_table :invoices, bulk: true do |t| - t.remove :from_date - t.remove :to_date - t.remove :charges_from_date + safety_assured do + change_table :invoices, bulk: true do |t| + t.remove :from_date + t.remove :to_date + t.remove :charges_from_date + end end end diff --git a/db/migrate/20220807210117_add_invoice_id_to_wallet_transactions.rb b/db/migrate/20220807210117_add_invoice_id_to_wallet_transactions.rb index 2f198b4d9948..b213a350320d 100644 --- a/db/migrate/20220807210117_add_invoice_id_to_wallet_transactions.rb +++ b/db/migrate/20220807210117_add_invoice_id_to_wallet_transactions.rb @@ -2,6 +2,8 @@ class AddInvoiceIdToWalletTransactions < ActiveRecord::Migration[7.0] def change - add_reference :wallet_transactions, :invoice, type: :uuid, null: true, index: true, foreign_key: true + safety_assured do + add_reference :wallet_transactions, :invoice, type: :uuid, null: true, index: true, foreign_key: true + end end end diff --git a/db/migrate/20220817092945_remove_invoice_from_wallet_transaction.rb b/db/migrate/20220817092945_remove_invoice_from_wallet_transaction.rb index 26960ac4f161..195e3c24ed63 100644 --- a/db/migrate/20220817092945_remove_invoice_from_wallet_transaction.rb +++ b/db/migrate/20220817092945_remove_invoice_from_wallet_transaction.rb @@ -2,6 +2,8 @@ class RemoveInvoiceFromWalletTransaction < ActiveRecord::Migration[7.0] def change - remove_reference :wallet_transactions, :invoice, index: true, foreign_key: true + safety_assured do + remove_reference :wallet_transactions, :invoice, index: true, foreign_key: true + end end end diff --git a/db/migrate/20220817095619_add_invoiceable_and_type_to_fees.rb b/db/migrate/20220817095619_add_invoiceable_and_type_to_fees.rb index bc7704c007c6..6da6b380ec74 100644 --- a/db/migrate/20220817095619_add_invoiceable_and_type_to_fees.rb +++ b/db/migrate/20220817095619_add_invoiceable_and_type_to_fees.rb @@ -3,6 +3,8 @@ class AddInvoiceableAndTypeToFees < ActiveRecord::Migration[7.0] def change add_column :fees, :fee_type, :integer - add_reference :fees, :invoiceable, type: :uuid, polymorphic: true + safety_assured do + add_reference :fees, :invoiceable, type: :uuid, polymorphic: true + end end end diff --git a/db/migrate/20220823135203_change_precision_and_scale_for_decimal_fields.rb b/db/migrate/20220823135203_change_precision_and_scale_for_decimal_fields.rb index 23ada803fcd7..7645d1146c6e 100644 --- a/db/migrate/20220823135203_change_precision_and_scale_for_decimal_fields.rb +++ b/db/migrate/20220823135203_change_precision_and_scale_for_decimal_fields.rb @@ -2,17 +2,19 @@ class ChangePrecisionAndScaleForDecimalFields < ActiveRecord::Migration[7.0] def up - change_table :wallets, bulk: true do |t| - t.change :rate_amount, :decimal, precision: 30, scale: 5 - t.change :credits_balance, :decimal, precision: 30, scale: 5 - t.change :balance, :decimal, precision: 30, scale: 5 - t.change :consumed_credits, :decimal, precision: 30, scale: 5 - t.change :consumed_amount, :decimal, precision: 30, scale: 5 - end + safety_assured do + change_table :wallets, bulk: true do |t| + t.change :rate_amount, :decimal, precision: 30, scale: 5 + t.change :credits_balance, :decimal, precision: 30, scale: 5 + t.change :balance, :decimal, precision: 30, scale: 5 + t.change :consumed_credits, :decimal, precision: 30, scale: 5 + t.change :consumed_amount, :decimal, precision: 30, scale: 5 + end - change_table :wallet_transactions, bulk: true do |t| - t.change :amount, :decimal, precision: 30, scale: 5 - t.change :credit_amount, :decimal, precision: 30, scale: 5 + change_table :wallet_transactions, bulk: true do |t| + t.change :amount, :decimal, precision: 30, scale: 5 + t.change :credit_amount, :decimal, precision: 30, scale: 5 + end end end diff --git a/db/migrate/20220823145421_rename_unique_id_to_external_id_on_subscriptions.rb b/db/migrate/20220823145421_rename_unique_id_to_external_id_on_subscriptions.rb index 6b6ac8591414..ec82159836bf 100644 --- a/db/migrate/20220823145421_rename_unique_id_to_external_id_on_subscriptions.rb +++ b/db/migrate/20220823145421_rename_unique_id_to_external_id_on_subscriptions.rb @@ -2,7 +2,9 @@ class RenameUniqueIdToExternalIdOnSubscriptions < ActiveRecord::Migration[7.0] def change - rename_column :subscriptions, :unique_id, :external_id - add_index :subscriptions, :external_id + safety_assured do + rename_column :subscriptions, :unique_id, :external_id + add_index :subscriptions, :external_id + end end end diff --git a/db/migrate/20220824113131_rename_customer_id_to_external_id_on_customers.rb b/db/migrate/20220824113131_rename_customer_id_to_external_id_on_customers.rb index 1659c72f6a83..ad67592f3bfb 100644 --- a/db/migrate/20220824113131_rename_customer_id_to_external_id_on_customers.rb +++ b/db/migrate/20220824113131_rename_customer_id_to_external_id_on_customers.rb @@ -2,6 +2,8 @@ class RenameCustomerIdToExternalIdOnCustomers < ActiveRecord::Migration[7.0] def change - rename_column :customers, :customer_id, :external_id + safety_assured do + rename_column :customers, :customer_id, :external_id + end end end diff --git a/db/migrate/20220825051923_add_wallet_transaction_to_invoice.rb b/db/migrate/20220825051923_add_wallet_transaction_to_invoice.rb index 9822df2a13fc..69c1c2a849ce 100644 --- a/db/migrate/20220825051923_add_wallet_transaction_to_invoice.rb +++ b/db/migrate/20220825051923_add_wallet_transaction_to_invoice.rb @@ -2,6 +2,8 @@ class AddWalletTransactionToInvoice < ActiveRecord::Migration[7.0] def change - add_reference :wallet_transactions, :invoice, type: :uuid, null: true, index: true, foreign_key: true + safety_assured do + add_reference :wallet_transactions, :invoice, type: :uuid, null: true, index: true, foreign_key: true + end end end diff --git a/db/migrate/20220905095529_rename_persisted_metrics_into_persisted_events.rb b/db/migrate/20220905095529_rename_persisted_metrics_into_persisted_events.rb index 500d80c28b5d..576d20a16d5b 100644 --- a/db/migrate/20220905095529_rename_persisted_metrics_into_persisted_events.rb +++ b/db/migrate/20220905095529_rename_persisted_metrics_into_persisted_events.rb @@ -2,7 +2,9 @@ class RenamePersistedMetricsIntoPersistedEvents < ActiveRecord::Migration[7.0] def change - rename_table :persisted_metrics, :persisted_events - rename_index :persisted_events, :index_search_persisted_metrics, :index_search_persisted_events + safety_assured do + rename_table :persisted_metrics, :persisted_events + rename_index :persisted_events, :index_search_persisted_metrics, :index_search_persisted_events + end end end diff --git a/db/migrate/20220905142834_add_billable_metric_id_to_persisted_events.rb b/db/migrate/20220905142834_add_billable_metric_id_to_persisted_events.rb index 7d2a23f38e94..dbc44fcc43f0 100644 --- a/db/migrate/20220905142834_add_billable_metric_id_to_persisted_events.rb +++ b/db/migrate/20220905142834_add_billable_metric_id_to_persisted_events.rb @@ -2,11 +2,13 @@ class AddBillableMetricIdToPersistedEvents < ActiveRecord::Migration[7.0] def change - add_reference :persisted_events, :billable_metric, type: :uuid + safety_assured do + add_reference :persisted_events, :billable_metric, type: :uuid - remove_index :persisted_events, name: :index_search_persisted_events # rubocop:disable Rails/ReversibleMigration - add_index :persisted_events, - [:customer_id, :external_subscription_id, :billable_metric_id], - name: :index_search_persisted_events + remove_index :persisted_events, name: :index_search_persisted_events # rubocop:disable Rails/ReversibleMigration + add_index :persisted_events, + [:customer_id, :external_subscription_id, :billable_metric_id], + name: :index_search_persisted_events + end end end diff --git a/db/migrate/20220906065059_add_membership_status_and_revoked_at.rb b/db/migrate/20220906065059_add_membership_status_and_revoked_at.rb index 63d0bd3f37b8..760e72aa2791 100644 --- a/db/migrate/20220906065059_add_membership_status_and_revoked_at.rb +++ b/db/migrate/20220906065059_add_membership_status_and_revoked_at.rb @@ -2,9 +2,11 @@ class AddMembershipStatusAndRevokedAt < ActiveRecord::Migration[7.0] def change - change_table :memberships, bulk: true do |t| - t.integer :status, null: false, default: 0 - t.datetime :revoked_at, null: true + safety_assured do + change_table :memberships, bulk: true do |t| + t.integer :status, null: false, default: 0 + t.datetime :revoked_at, null: true + end end end end diff --git a/db/migrate/20220916131538_add_parent_id_on_plans.rb b/db/migrate/20220916131538_add_parent_id_on_plans.rb index b23e3ce3b3aa..c9475cda3132 100644 --- a/db/migrate/20220916131538_add_parent_id_on_plans.rb +++ b/db/migrate/20220916131538_add_parent_id_on_plans.rb @@ -2,6 +2,8 @@ class AddParentIdOnPlans < ActiveRecord::Migration[7.0] def change - add_reference :plans, :parent, type: :uuid, null: true, index: true, foreign_key: {to_table: :plans} + safety_assured do + add_reference :plans, :parent, type: :uuid, null: true, index: true, foreign_key: {to_table: :plans} + end end end diff --git a/db/migrate/20220921095507_add_coupon_type_and_percentage_rate_to_coupons.rb b/db/migrate/20220921095507_add_coupon_type_and_percentage_rate_to_coupons.rb index f8d61db997a4..6fdbf7723f4a 100644 --- a/db/migrate/20220921095507_add_coupon_type_and_percentage_rate_to_coupons.rb +++ b/db/migrate/20220921095507_add_coupon_type_and_percentage_rate_to_coupons.rb @@ -2,19 +2,21 @@ class AddCouponTypeAndPercentageRateToCoupons < ActiveRecord::Migration[7.0] def change - change_table :coupons, bulk: true do |t| - t.integer :coupon_type, null: false, default: 0 - t.decimal :percentage_rate, precision: 10, scale: 5 - end + safety_assured do + change_table :coupons, bulk: true do |t| + t.integer :coupon_type, null: false, default: 0 + t.decimal :percentage_rate, precision: 10, scale: 5 + end - change_column_null :coupons, :amount_cents, true - change_column_null :coupons, :amount_currency, true + change_column_null :coupons, :amount_cents, true + change_column_null :coupons, :amount_currency, true - change_table :applied_coupons, bulk: true do |t| - t.decimal :percentage_rate, precision: 10, scale: 5 - end + change_table :applied_coupons, bulk: true do |t| + t.decimal :percentage_rate, precision: 10, scale: 5 + end - change_column_null :applied_coupons, :amount_cents, true - change_column_null :applied_coupons, :amount_currency, true + change_column_null :applied_coupons, :amount_cents, true + change_column_null :applied_coupons, :amount_currency, true + end end end diff --git a/db/migrate/20220922105251_add_frequency_and_frequency_duration_to_coupons.rb b/db/migrate/20220922105251_add_frequency_and_frequency_duration_to_coupons.rb index 28f22a89b05b..a0b93d59eb31 100644 --- a/db/migrate/20220922105251_add_frequency_and_frequency_duration_to_coupons.rb +++ b/db/migrate/20220922105251_add_frequency_and_frequency_duration_to_coupons.rb @@ -2,14 +2,16 @@ class AddFrequencyAndFrequencyDurationToCoupons < ActiveRecord::Migration[7.0] def change - change_table :coupons, bulk: true do |t| - t.integer :frequency, null: false, default: 0 - t.integer :frequency_duration - end + safety_assured do + change_table :coupons, bulk: true do |t| + t.integer :frequency, null: false, default: 0 + t.integer :frequency_duration + end - change_table :applied_coupons, bulk: true do |t| - t.integer :frequency, null: false, default: 0 - t.integer :frequency_duration + change_table :applied_coupons, bulk: true do |t| + t.integer :frequency, null: false, default: 0 + t.integer :frequency_duration + end end end end diff --git a/db/migrate/20220923092906_add_expiration_date_to_coupons.rb b/db/migrate/20220923092906_add_expiration_date_to_coupons.rb index 3f64170316d2..dc128a4880a6 100644 --- a/db/migrate/20220923092906_add_expiration_date_to_coupons.rb +++ b/db/migrate/20220923092906_add_expiration_date_to_coupons.rb @@ -7,6 +7,8 @@ def change LagoApi::Application.load_tasks Rake::Task['coupons:fill_expiration_date'].invoke - remove_column :coupons, :expiration_duration, :integer + safety_assured do + remove_column :coupons, :expiration_duration, :integer + end end end diff --git a/db/migrate/20220930143002_add_credit_note_id_to_credits.rb b/db/migrate/20220930143002_add_credit_note_id_to_credits.rb index 70a9f7a1babe..f304f2873121 100644 --- a/db/migrate/20220930143002_add_credit_note_id_to_credits.rb +++ b/db/migrate/20220930143002_add_credit_note_id_to_credits.rb @@ -2,7 +2,9 @@ class AddCreditNoteIdToCredits < ActiveRecord::Migration[7.0] def change - add_reference :credits, :credit_notes, type: :uuid, null: true, foreign_key: true, index: true - change_column_null :credits, :applied_coupon_id, null: true + safety_assured do + add_reference :credits, :credit_notes, type: :uuid, null: true, foreign_key: true, index: true + change_column_null :credits, :applied_coupon_id, null: true + end end end diff --git a/db/migrate/20221010083509_rename_credit_credit_note.rb b/db/migrate/20221010083509_rename_credit_credit_note.rb index 8282e6e6d89e..c7984f0db218 100644 --- a/db/migrate/20221010083509_rename_credit_credit_note.rb +++ b/db/migrate/20221010083509_rename_credit_credit_note.rb @@ -2,6 +2,8 @@ class RenameCreditCreditNote < ActiveRecord::Migration[7.0] def change - rename_column :credits, :credit_notes_id, :credit_note_id + safety_assured do + rename_column :credits, :credit_notes_id, :credit_note_id + end end end diff --git a/db/migrate/20221010142031_update_credit_notes.rb b/db/migrate/20221010142031_update_credit_notes.rb index 573b6aea4a65..b33aa8468bbb 100644 --- a/db/migrate/20221010142031_update_credit_notes.rb +++ b/db/migrate/20221010142031_update_credit_notes.rb @@ -2,17 +2,19 @@ class UpdateCreditNotes < ActiveRecord::Migration[7.0] def change - rename_column :credit_notes, :status, :credit_status - rename_column :credit_notes, :amount_cents, :credit_amount_cents - rename_column :credit_notes, :amount_currency, :credit_amount_currency - rename_column :credit_notes, :remaining_amount_cents, :balance_amount_cents - rename_column :credit_notes, :remaining_amount_currency, :balance_amount_currency + safety_assured do + rename_column :credit_notes, :status, :credit_status + rename_column :credit_notes, :amount_cents, :credit_amount_cents + rename_column :credit_notes, :amount_currency, :credit_amount_currency + rename_column :credit_notes, :remaining_amount_cents, :balance_amount_cents + rename_column :credit_notes, :remaining_amount_currency, :balance_amount_currency - change_table :credit_notes, bulk: true do |t| - t.bigint :total_amount_cents, null: false, default: 0 + change_table :credit_notes, bulk: true do |t| + t.bigint :total_amount_cents, null: false, default: 0 - # NOTE: Disable rubocop comment as table is not used in production yet - t.string :total_amount_currency, null: false + # NOTE: Disable rubocop comment as table is not used in production yet + t.string :total_amount_currency, null: false + end end end end diff --git a/db/migrate/20221011083520_rename_credit_note_items.rb b/db/migrate/20221011083520_rename_credit_note_items.rb index 2d8dd7a8d91a..19fb660e4816 100644 --- a/db/migrate/20221011083520_rename_credit_note_items.rb +++ b/db/migrate/20221011083520_rename_credit_note_items.rb @@ -2,7 +2,9 @@ class RenameCreditNoteItems < ActiveRecord::Migration[7.0] def change - rename_column :credit_note_items, :amount_cents, :credit_amount_cents - rename_column :credit_note_items, :amount_currency, :credit_amount_currency + safety_assured do + rename_column :credit_note_items, :amount_cents, :credit_amount_cents + rename_column :credit_note_items, :amount_currency, :credit_amount_currency + end end end diff --git a/db/migrate/20221018144521_add_legacy_flag_to_invoices.rb b/db/migrate/20221018144521_add_legacy_flag_to_invoices.rb index cd1c73e1db11..10dfd0c65a96 100644 --- a/db/migrate/20221018144521_add_legacy_flag_to_invoices.rb +++ b/db/migrate/20221018144521_add_legacy_flag_to_invoices.rb @@ -2,18 +2,20 @@ class AddLegacyFlagToInvoices < ActiveRecord::Migration[7.0] def up - change_table :invoices, bulk: true do |t| - t.boolean :legacy, null: false, default: false - t.float :vat_rate - end + safety_assured do + change_table :invoices, bulk: true do |t| + t.boolean :legacy, null: false, default: false + t.float :vat_rate + end - execute "UPDATE invoices SET legacy = 'true';" + execute "UPDATE invoices SET legacy = 'true';" - execute <<-SQL + execute <<-SQL UPDATE invoices SET vat_rate = ROUND((vat_amount_cents::decimal / amount_cents) * 100, 2) WHERE vat_rate IS NULL; - SQL + SQL + end end def down diff --git a/db/migrate/20221020093745_add_credit_amount_to_invoices.rb b/db/migrate/20221020093745_add_credit_amount_to_invoices.rb index 3da01c4b1add..737e2f4d0bc9 100644 --- a/db/migrate/20221020093745_add_credit_amount_to_invoices.rb +++ b/db/migrate/20221020093745_add_credit_amount_to_invoices.rb @@ -9,19 +9,20 @@ class Wallet < ApplicationRecord end def up - change_table :invoices, bulk: true do |t| - t.bigint :credit_amount_cents, null: false, default: 0 - t.string :credit_amount_currency - end + safety_assured do + change_table :invoices, bulk: true do |t| + t.bigint :credit_amount_cents, null: false, default: 0 + t.string :credit_amount_currency + end - currency_list = WalletTransaction.joins(:wallet).pluck('DISTINCT(wallets.currency)') - currency_list << 'EUR' if currency_list.blank? - currency_sql = currency_list.each_with_object([]) do |code, currencies| - currency = Money::Currency.new(code) - currencies << "('#{code}', #{currency.exponent}, #{currency.subunit_to_unit})" - end + currency_list = WalletTransaction.joins(:wallet).pluck('DISTINCT(wallets.currency)') + currency_list << 'EUR' if currency_list.blank? + currency_sql = currency_list.each_with_object([]) do |code, currencies| + currency = Money::Currency.new(code) + currencies << "('#{code}', #{currency.exponent}, #{currency.subunit_to_unit})" + end - execute <<-SQL + execute <<-SQL WITH invoice_credit_amounts AS ( SELECT invoices.id AS invoice_id, @@ -55,7 +56,8 @@ def up credit_amount_cents = invoice_credit_amounts.credit_amount_cents + invoice_credit_amounts.prepaid_credit_amount_cents FROM invoice_credit_amounts WHERE invoice_credit_amounts.invoice_id = invoices.id - SQL + SQL + end end def down diff --git a/db/migrate/20221024090308_add_refund_fields_to_credit_notes.rb b/db/migrate/20221024090308_add_refund_fields_to_credit_notes.rb index e4f49a8b0d4e..e94915be8769 100644 --- a/db/migrate/20221024090308_add_refund_fields_to_credit_notes.rb +++ b/db/migrate/20221024090308_add_refund_fields_to_credit_notes.rb @@ -2,16 +2,18 @@ class AddRefundFieldsToCreditNotes < ActiveRecord::Migration[7.0] def up - change_table :credit_notes, bulk: true do |t| - t.bigint :refund_amount_cents, null: false, default: 0 - t.string :refund_amount_currency - t.integer :refund_status - end - change_column :credit_notes, :credit_status, :integer, null: true, default: :null + safety_assured do + change_table :credit_notes, bulk: true do |t| + t.bigint :refund_amount_cents, null: false, default: 0 + t.string :refund_amount_currency + t.integer :refund_status + end + change_column :credit_notes, :credit_status, :integer, null: true, default: :null - change_table :credit_note_items, bulk: true do |t| - t.bigint :refund_amount_cents, null: false, default: 0 - t.string :refund_amount_currency + change_table :credit_note_items, bulk: true do |t| + t.bigint :refund_amount_cents, null: false, default: 0 + t.string :refund_amount_currency + end end end diff --git a/db/migrate/20221028091920_add_group_id_to_fees.rb b/db/migrate/20221028091920_add_group_id_to_fees.rb index 586924b52104..fcade8eeeeec 100644 --- a/db/migrate/20221028091920_add_group_id_to_fees.rb +++ b/db/migrate/20221028091920_add_group_id_to_fees.rb @@ -2,6 +2,8 @@ class AddGroupIdToFees < ActiveRecord::Migration[7.0] def change - add_reference :fees, :group, type: :uuid, null: true, foreign_key: true, index: true + safety_assured do + add_reference :fees, :group, type: :uuid, null: true, foreign_key: true, index: true + end end end diff --git a/db/migrate/20221107151038_add_vat_rates_to_credit_note.rb b/db/migrate/20221107151038_add_vat_rates_to_credit_note.rb index 1ee21878e957..5ccc0d580c3a 100644 --- a/db/migrate/20221107151038_add_vat_rates_to_credit_note.rb +++ b/db/migrate/20221107151038_add_vat_rates_to_credit_note.rb @@ -2,15 +2,17 @@ class AddVatRatesToCreditNote < ActiveRecord::Migration[7.0] def change - change_table :credit_notes, bulk: true do |t| - t.bigint :credit_vat_amount_cents, default: 0, null: false - t.string :credit_vat_amount_currency + safety_assured do + change_table :credit_notes, bulk: true do |t| + t.bigint :credit_vat_amount_cents, default: 0, null: false + t.string :credit_vat_amount_currency - t.bigint :refund_vat_amount_cents, default: 0, null: false - t.string :refund_vat_amount_currency + t.bigint :refund_vat_amount_cents, default: 0, null: false + t.string :refund_vat_amount_currency - t.bigint :vat_amount_cents, default: 0, null: false - t.string :vat_amount_currency + t.bigint :vat_amount_cents, default: 0, null: false + t.string :vat_amount_currency + end end end end diff --git a/db/migrate/20221110151027_changes_credit_note_items_columns.rb b/db/migrate/20221110151027_changes_credit_note_items_columns.rb index 62be4dd2e57c..47e5606d209f 100644 --- a/db/migrate/20221110151027_changes_credit_note_items_columns.rb +++ b/db/migrate/20221110151027_changes_credit_note_items_columns.rb @@ -2,12 +2,14 @@ class ChangesCreditNoteItemsColumns < ActiveRecord::Migration[7.0] def up - change_table :credit_note_items, bulk: true do |t| - t.remove :refund_amount_cents - t.remove :refund_amount_currency + safety_assured do + change_table :credit_note_items, bulk: true do |t| + t.remove :refund_amount_cents + t.remove :refund_amount_currency - t.rename :credit_amount_cents, :amount_cents - t.rename :credit_amount_currency, :amount_currency + t.rename :credit_amount_cents, :amount_cents + t.rename :credit_amount_currency, :amount_currency + end end end diff --git a/db/migrate/20221115100834_add_invoice_grace_period.rb b/db/migrate/20221115100834_add_invoice_grace_period.rb index 73d83433ad10..e5b1ba398f7a 100644 --- a/db/migrate/20221115100834_add_invoice_grace_period.rb +++ b/db/migrate/20221115100834_add_invoice_grace_period.rb @@ -7,14 +7,16 @@ def change reversible do |dir| dir.up do - execute <<-SQL + safety_assured do + execute <<-SQL ALTER TABLE organizations ADD CONSTRAINT check_organizations_on_invoice_grace_period CHECK (invoice_grace_period >= 0); ALTER TABLE customers ADD CONSTRAINT check_customers_on_invoice_grace_period CHECK (invoice_grace_period >= 0); - SQL + SQL + end end dir.down do diff --git a/db/migrate/20221115110223_add_unique_index_on_customers_external_id.rb b/db/migrate/20221115110223_add_unique_index_on_customers_external_id.rb index 05dd0f646b84..834ff271a5a8 100644 --- a/db/migrate/20221115110223_add_unique_index_on_customers_external_id.rb +++ b/db/migrate/20221115110223_add_unique_index_on_customers_external_id.rb @@ -2,7 +2,9 @@ class AddUniqueIndexOnCustomersExternalId < ActiveRecord::Migration[7.0] def change - remove_index :customers, :external_id - add_index :customers, [:external_id, :organization_id], unique: true + safety_assured do + remove_index :customers, :external_id + add_index :customers, [:external_id, :organization_id], unique: true + end end end diff --git a/db/migrate/20221122163328_rename_status_on_invoices.rb b/db/migrate/20221122163328_rename_status_on_invoices.rb index 3367b3267731..df9a2688899d 100644 --- a/db/migrate/20221122163328_rename_status_on_invoices.rb +++ b/db/migrate/20221122163328_rename_status_on_invoices.rb @@ -2,15 +2,17 @@ class RenameStatusOnInvoices < ActiveRecord::Migration[7.0] def change - rename_column :invoices, :status, :payment_status - add_column :invoices, :status, :integer, null: false, default: 0 + safety_assured do + rename_column :invoices, :status, :payment_status + add_column :invoices, :status, :integer, null: false, default: 0 - reversible do |dir| - dir.up do - # Mark all existing invoices as finalized. - execute <<-SQL + reversible do |dir| + dir.up do + # Mark all existing invoices as finalized. + execute <<-SQL UPDATE invoices SET status = 1; - SQL + SQL + end end end end diff --git a/db/migrate/20221125111605_add_issuing_date_to_credit_notes.rb b/db/migrate/20221125111605_add_issuing_date_to_credit_notes.rb index 690018c36d94..ee8ceb47f557 100644 --- a/db/migrate/20221125111605_add_issuing_date_to_credit_notes.rb +++ b/db/migrate/20221125111605_add_issuing_date_to_credit_notes.rb @@ -4,14 +4,16 @@ class AddIssuingDateToCreditNotes < ActiveRecord::Migration[7.0] def change add_column :credit_notes, :issuing_date, :date - reversible do |dir| - dir.up do - execute <<-SQL + safety_assured do + reversible do |dir| + dir.up do + execute <<-SQL UPDATE credit_notes SET issuing_date = DATE(created_at); - SQL + SQL + end end - end - change_column_null :credit_notes, :issuing_date, false + change_column_null :credit_notes, :issuing_date, false + end end end diff --git a/db/migrate/20221128132620_change_fees_boundaries.rb b/db/migrate/20221128132620_change_fees_boundaries.rb index bf7ce76e5fb7..5d3d5f17b21e 100644 --- a/db/migrate/20221128132620_change_fees_boundaries.rb +++ b/db/migrate/20221128132620_change_fees_boundaries.rb @@ -4,7 +4,8 @@ class ChangeFeesBoundaries < ActiveRecord::Migration[7.0] def change reversible do |dir| dir.up do - execute <<-SQL + safety_assured do + execute <<-SQL UPDATE fees SET properties = CONCAT ( '{', @@ -19,7 +20,8 @@ def change '}' )::jsonb WHERE (properties ? 'from_date'); - SQL + SQL + end end end end diff --git a/db/migrate/20221129133433_change_expiration_dates_type.rb b/db/migrate/20221129133433_change_expiration_dates_type.rb index 1fca452a5a85..924c356a895d 100644 --- a/db/migrate/20221129133433_change_expiration_dates_type.rb +++ b/db/migrate/20221129133433_change_expiration_dates_type.rb @@ -3,17 +3,18 @@ class ChangeExpirationDatesType < ActiveRecord::Migration[7.0] def up add_column :coupons, :expiration_at, :datetime - - reversible do |dir| - dir.up do - execute <<-SQL + safety_assured do + reversible do |dir| + dir.up do + execute <<-SQL UPDATE coupons SET expiration_at = (date_trunc('day', expiration_date) + interval '1 day' - interval '1 second')::timestamp WHERE expiration_date IS NOT NULL; - SQL + SQL + end end - end - remove_column :coupons, :expiration_date + remove_column :coupons, :expiration_date + end end def down diff --git a/db/migrate/20221202130126_rename_wallets_expiration_date.rb b/db/migrate/20221202130126_rename_wallets_expiration_date.rb index fa6724dbe83a..3dff414e7db3 100644 --- a/db/migrate/20221202130126_rename_wallets_expiration_date.rb +++ b/db/migrate/20221202130126_rename_wallets_expiration_date.rb @@ -2,13 +2,15 @@ class RenameWalletsExpirationDate < ActiveRecord::Migration[7.0] def change - rename_column :wallets, :expiration_date, :expiration_at + safety_assured do + rename_column :wallets, :expiration_date, :expiration_at - reversible do |dir| - dir.up do - execute <<-SQL + reversible do |dir| + dir.up do + execute <<-SQL UPDATE wallets SET expiration_at = (date_trunc('day', expiration_at) + interval '1 day' - interval '1 second')::timestamp; - SQL + SQL + end end end end diff --git a/db/migrate/20221206094412_change_subscription_date_type.rb b/db/migrate/20221206094412_change_subscription_date_type.rb index 262f3ceb35b6..4646b50e9740 100644 --- a/db/migrate/20221206094412_change_subscription_date_type.rb +++ b/db/migrate/20221206094412_change_subscription_date_type.rb @@ -3,14 +3,15 @@ class ChangeSubscriptionDateType < ActiveRecord::Migration[7.0] def up add_column :subscriptions, :subscription_at, :datetime - - execute <<-SQL + safety_assured do + execute <<-SQL UPDATE subscriptions SET subscription_at = subscription_date::timestamp WHERE subscription_date IS NOT NULL; - SQL + SQL - remove_column :subscriptions, :subscription_date + remove_column :subscriptions, :subscription_date + end end def down diff --git a/db/migrate/20221208142739_add_properties_to_invoice_subscriptions.rb b/db/migrate/20221208142739_add_properties_to_invoice_subscriptions.rb index 2421b0285bfd..026460fd05eb 100644 --- a/db/migrate/20221208142739_add_properties_to_invoice_subscriptions.rb +++ b/db/migrate/20221208142739_add_properties_to_invoice_subscriptions.rb @@ -4,9 +4,10 @@ class AddPropertiesToInvoiceSubscriptions < ActiveRecord::Migration[7.0] def change add_column :invoice_subscriptions, :properties, :jsonb, null: false, default: '{}' - reversible do |dir| - dir.up do - execute <<-SQL + safety_assured do + reversible do |dir| + dir.up do + execute <<-SQL UPDATE invoice_subscriptions SET properties = COALESCE(( SELECT fees.properties @@ -16,7 +17,8 @@ def change ORDER BY fees.created_at ASC LIMIT 1 ), '{}'); - SQL + SQL + end end end end diff --git a/db/migrate/20221212153810_add_source_to_invoice_subscriptions.rb b/db/migrate/20221212153810_add_source_to_invoice_subscriptions.rb index 3e1f5c4b7118..850eb297e410 100644 --- a/db/migrate/20221212153810_add_source_to_invoice_subscriptions.rb +++ b/db/migrate/20221212153810_add_source_to_invoice_subscriptions.rb @@ -4,15 +4,17 @@ class AddSourceToInvoiceSubscriptions < ActiveRecord::Migration[7.0] def change add_column :invoice_subscriptions, :source, :integer - reversible do |dir| - dir.up do - execute <<-SQL + safety_assured do + reversible do |dir| + dir.up do + execute <<-SQL UPDATE invoice_subscriptions SET source = 0; - SQL + SQL + end end - end - change_column_null :invoice_subscriptions, :source, false + change_column_null :invoice_subscriptions, :source, false + end end end diff --git a/db/migrate/20221216154033_add_payment_retry_columns_to_invoices.rb b/db/migrate/20221216154033_add_payment_retry_columns_to_invoices.rb index b6f39aae43f3..a9e7bdb07fe7 100644 --- a/db/migrate/20221216154033_add_payment_retry_columns_to_invoices.rb +++ b/db/migrate/20221216154033_add_payment_retry_columns_to_invoices.rb @@ -2,9 +2,11 @@ class AddPaymentRetryColumnsToInvoices < ActiveRecord::Migration[7.0] def change - change_table :invoices, bulk: true do |t| - t.integer :payment_attempts, default: 0, null: false - t.boolean :ready_for_payment_processing, default: true, null: false + safety_assured do + change_table :invoices, bulk: true do |t| + t.integer :payment_attempts, default: 0, null: false + t.boolean :ready_for_payment_processing, default: true, null: false + end end end end diff --git a/db/migrate/20221219111209_change_invoice_subscription_source.rb b/db/migrate/20221219111209_change_invoice_subscription_source.rb index cec7df8a8d82..b3ceabc68cd9 100644 --- a/db/migrate/20221219111209_change_invoice_subscription_source.rb +++ b/db/migrate/20221219111209_change_invoice_subscription_source.rb @@ -2,18 +2,20 @@ class ChangeInvoiceSubscriptionSource < ActiveRecord::Migration[7.0] def up - change_table :invoice_subscriptions, bulk: true do |t| - t.remove :source + safety_assured do + change_table :invoice_subscriptions, bulk: true do |t| + t.remove :source - t.boolean :recurring, null: true - end + t.boolean :recurring, null: true + end - execute <<-SQL + execute <<-SQL UPDATE invoice_subscriptions SET recurring = true; - SQL + SQL - change_column_null :invoice_subscriptions, :recurring, null: false + change_column_null :invoice_subscriptions, :recurring, null: false + end end def down diff --git a/db/migrate/20221222164226_finalize_invoices.rb b/db/migrate/20221222164226_finalize_invoices.rb index 9f4a5a8ad959..1dba6df2801c 100644 --- a/db/migrate/20221222164226_finalize_invoices.rb +++ b/db/migrate/20221222164226_finalize_invoices.rb @@ -2,12 +2,14 @@ class FinalizeInvoices < ActiveRecord::Migration[7.0] def change - reversible do |dir| - dir.up do - # Mark all existing invoices as finalized. - execute <<-SQL + safety_assured do + reversible do |dir| + dir.up do + # Mark all existing invoices as finalized. + execute <<-SQL UPDATE invoices SET status = 1; - SQL + SQL + end end end end diff --git a/db/migrate/20221226091020_add_nullable_to_invoice_grace_period.rb b/db/migrate/20221226091020_add_nullable_to_invoice_grace_period.rb index 64ee9878b6bd..1d574702c1d7 100644 --- a/db/migrate/20221226091020_add_nullable_to_invoice_grace_period.rb +++ b/db/migrate/20221226091020_add_nullable_to_invoice_grace_period.rb @@ -2,15 +2,17 @@ class AddNullableToInvoiceGracePeriod < ActiveRecord::Migration[7.0] def change - change_column_null :customers, :invoice_grace_period, true - change_column_default :customers, :invoice_grace_period, from: 0, to: nil + safety_assured do + change_column_null :customers, :invoice_grace_period, true + change_column_default :customers, :invoice_grace_period, from: 0, to: nil - reversible do |dir| - dir.up do - # Update all existing customers to a nil invoice_grace_period. - execute <<-SQL + reversible do |dir| + dir.up do + # Update all existing customers to a nil invoice_grace_period. + execute <<-SQL UPDATE customers SET invoice_grace_period = NULL; - SQL + SQL + end end end end diff --git a/db/migrate/20230102150636_change_invoices_default_status.rb b/db/migrate/20230102150636_change_invoices_default_status.rb index 51a78c7a02ec..cd6d48cfc27e 100644 --- a/db/migrate/20230102150636_change_invoices_default_status.rb +++ b/db/migrate/20230102150636_change_invoices_default_status.rb @@ -3,10 +3,11 @@ class ChangeInvoicesDefaultStatus < ActiveRecord::Migration[7.0] def up change_column_default :invoices, :status, 1 - - execute <<-SQL + safety_assured do + execute <<-SQL UPDATE invoices SET status = 1; - SQL + SQL + end end def down diff --git a/db/migrate/20230106152449_turn_all_amount_cents_to_bigint.rb b/db/migrate/20230106152449_turn_all_amount_cents_to_bigint.rb index acbbf7760860..57e4bbef5d76 100644 --- a/db/migrate/20230106152449_turn_all_amount_cents_to_bigint.rb +++ b/db/migrate/20230106152449_turn_all_amount_cents_to_bigint.rb @@ -2,8 +2,10 @@ class TurnAllAmountCentsToBigint < ActiveRecord::Migration[7.0] def up - change_column :applied_add_ons, :amount_cents, :bigint, null: false - change_column :applied_coupons, :amount_cents, :bigint, null: true + safety_assured do + change_column :applied_add_ons, :amount_cents, :bigint, null: false + change_column :applied_coupons, :amount_cents, :bigint, null: true + end end def down diff --git a/db/migrate/20230118100324_add_deleted_at_to_billable_metrics.rb b/db/migrate/20230118100324_add_deleted_at_to_billable_metrics.rb index e6b0e29f900e..69182f54fc65 100644 --- a/db/migrate/20230118100324_add_deleted_at_to_billable_metrics.rb +++ b/db/migrate/20230118100324_add_deleted_at_to_billable_metrics.rb @@ -9,14 +9,16 @@ def change add_column :events, :deleted_at, :datetime add_column :persisted_events, :deleted_at, :datetime - add_index :billable_metrics, :deleted_at - add_index :charges, :deleted_at - add_index :groups, :deleted_at - add_index :group_properties, :deleted_at - add_index :events, :deleted_at - add_index :persisted_events, :deleted_at + safety_assured do + add_index :billable_metrics, :deleted_at + add_index :charges, :deleted_at + add_index :groups, :deleted_at + add_index :group_properties, :deleted_at + add_index :events, :deleted_at + add_index :persisted_events, :deleted_at - remove_index :billable_metrics, %i[organization_id code] - add_index :billable_metrics, %i[organization_id code], unique: true, where: 'deleted_at IS NULL' + remove_index :billable_metrics, %i[organization_id code] + add_index :billable_metrics, %i[organization_id code], unique: true, where: 'deleted_at IS NULL' + end end end diff --git a/db/migrate/20230126103454_add_deleted_at_to_plans.rb b/db/migrate/20230126103454_add_deleted_at_to_plans.rb index f46e13ce93c3..8a85c573a3f1 100644 --- a/db/migrate/20230126103454_add_deleted_at_to_plans.rb +++ b/db/migrate/20230126103454_add_deleted_at_to_plans.rb @@ -3,9 +3,12 @@ class AddDeletedAtToPlans < ActiveRecord::Migration[7.0] def change add_column :plans, :deleted_at, :datetime - add_index :plans, :deleted_at - remove_index :plans, %i[code organization_id] - add_index :plans, %i[organization_id code], unique: true, where: 'deleted_at IS NULL' + safety_assured do + add_index :plans, :deleted_at + + remove_index :plans, %i[code organization_id] + add_index :plans, %i[organization_id code], unique: true, where: 'deleted_at IS NULL' + end end end diff --git a/db/migrate/20230131144740_add_deleted_at_to_add_ons.rb b/db/migrate/20230131144740_add_deleted_at_to_add_ons.rb index 27de093af234..2127f48ed281 100644 --- a/db/migrate/20230131144740_add_deleted_at_to_add_ons.rb +++ b/db/migrate/20230131144740_add_deleted_at_to_add_ons.rb @@ -3,9 +3,11 @@ class AddDeletedAtToAddOns < ActiveRecord::Migration[7.0] def change add_column :add_ons, :deleted_at, :datetime - add_index :add_ons, :deleted_at + safety_assured do + add_index :add_ons, :deleted_at - remove_index :add_ons, %i[organization_id code] - add_index :add_ons, %i[organization_id code], unique: true, where: 'deleted_at IS NULL' + remove_index :add_ons, %i[organization_id code] + add_index :add_ons, %i[organization_id code], unique: true, where: 'deleted_at IS NULL' + end end end diff --git a/db/migrate/20230131152047_add_deleted_at_to_coupons.rb b/db/migrate/20230131152047_add_deleted_at_to_coupons.rb index 08eb6a1102f2..232b25d7b8bb 100644 --- a/db/migrate/20230131152047_add_deleted_at_to_coupons.rb +++ b/db/migrate/20230131152047_add_deleted_at_to_coupons.rb @@ -3,9 +3,11 @@ class AddDeletedAtToCoupons < ActiveRecord::Migration[7.0] def change add_column :coupons, :deleted_at, :datetime - add_index :coupons, :deleted_at + safety_assured do + add_index :coupons, :deleted_at - remove_index :coupons, %i[organization_id code] - add_index :coupons, %i[organization_id code], unique: true, where: 'deleted_at IS NULL' + remove_index :coupons, %i[organization_id code] + add_index :coupons, %i[organization_id code], unique: true, where: 'deleted_at IS NULL' + end end end diff --git a/db/migrate/20230202110407_add_index_to_payment_provider_customers.rb b/db/migrate/20230202110407_add_index_to_payment_provider_customers.rb index bb1fdf259555..fb6912120aee 100644 --- a/db/migrate/20230202110407_add_index_to_payment_provider_customers.rb +++ b/db/migrate/20230202110407_add_index_to_payment_provider_customers.rb @@ -19,8 +19,9 @@ def change end end end - - add_index :payment_provider_customers, %i[customer_id type], unique: true + safety_assured do + add_index :payment_provider_customers, %i[customer_id type], unique: true + end remove_index :payment_provider_customers, :customer_id end end diff --git a/db/migrate/20230202150407_add_deleted_at_to_customers.rb b/db/migrate/20230202150407_add_deleted_at_to_customers.rb index dff6648dad2b..39877506cb85 100644 --- a/db/migrate/20230202150407_add_deleted_at_to_customers.rb +++ b/db/migrate/20230202150407_add_deleted_at_to_customers.rb @@ -3,9 +3,11 @@ class AddDeletedAtToCustomers < ActiveRecord::Migration[7.0] def change add_column :customers, :deleted_at, :datetime - add_index :customers, :deleted_at + safety_assured do + add_index :customers, :deleted_at - remove_index :customers, %i[external_id organization_id] - add_index :customers, %i[external_id organization_id], unique: true, where: 'deleted_at IS NULL' + remove_index :customers, %i[external_id organization_id] + add_index :customers, %i[external_id organization_id], unique: true, where: 'deleted_at IS NULL' + end end end diff --git a/db/migrate/20230202163249_add_organization_id_to_invoices.rb b/db/migrate/20230202163249_add_organization_id_to_invoices.rb index e597bcd40f3c..ec5ba680297c 100644 --- a/db/migrate/20230202163249_add_organization_id_to_invoices.rb +++ b/db/migrate/20230202163249_add_organization_id_to_invoices.rb @@ -2,19 +2,21 @@ class AddOrganizationIdToInvoices < ActiveRecord::Migration[7.0] def change - add_reference :invoices, :organization, type: :uuid, foreign_key: true, index: true, null: true + safety_assured do + add_reference :invoices, :organization, type: :uuid, foreign_key: true, index: true, null: true - reversible do |dir| - dir.up do - execute <<-SQL + reversible do |dir| + dir.up do + execute <<-SQL UPDATE invoices SET organization_id = customers.organization_id FROM customers WHERE customers.id = invoices.customer_id - SQL + SQL + end end - end - change_column_null :invoices, :organization_id, false + change_column_null :invoices, :organization_id, false + end end end diff --git a/db/migrate/20230203132157_add_deleted_at_to_coupon_plans.rb b/db/migrate/20230203132157_add_deleted_at_to_coupon_plans.rb index 2cf0cc7eedde..5dd4cda159dc 100644 --- a/db/migrate/20230203132157_add_deleted_at_to_coupon_plans.rb +++ b/db/migrate/20230203132157_add_deleted_at_to_coupon_plans.rb @@ -3,6 +3,8 @@ class AddDeletedAtToCouponPlans < ActiveRecord::Migration[7.0] def change add_column :coupon_plans, :deleted_at, :datetime - add_index :coupon_plans, :deleted_at + safety_assured do + add_index :coupon_plans, :deleted_at + end end end diff --git a/db/migrate/20230214100638_change_webhooks_organization_id_to_uuid.rb b/db/migrate/20230214100638_change_webhooks_organization_id_to_uuid.rb index 682df1be2c32..20b8db5858dc 100644 --- a/db/migrate/20230214100638_change_webhooks_organization_id_to_uuid.rb +++ b/db/migrate/20230214100638_change_webhooks_organization_id_to_uuid.rb @@ -2,8 +2,10 @@ class ChangeWebhooksOrganizationIdToUuid < ActiveRecord::Migration[7.0] def change - remove_column :webhooks, :organization_id, :bigint + safety_assured do + remove_column :webhooks, :organization_id, :bigint - add_reference :webhooks, :organization, type: :uuid, index: true, null: false # rubocop:disable Rails/NotNullColumn + add_reference :webhooks, :organization, type: :uuid, index: true, null: false # rubocop:disable Rails/NotNullColumn + end end end diff --git a/db/migrate/20230214145444_add_default_to_vat_rate.rb b/db/migrate/20230214145444_add_default_to_vat_rate.rb index 66a86faf5341..d74f5d4778b5 100644 --- a/db/migrate/20230214145444_add_default_to_vat_rate.rb +++ b/db/migrate/20230214145444_add_default_to_vat_rate.rb @@ -2,10 +2,12 @@ class AddDefaultToVatRate < ActiveRecord::Migration[7.0] def change - change_column_default :fees, :vat_rate, from: nil, to: 0.0 - change_column_default :invoices, :vat_rate, from: nil, to: 0.0 + safety_assured do + change_column_default :fees, :vat_rate, from: nil, to: 0.0 + change_column_default :invoices, :vat_rate, from: nil, to: 0.0 - change_column_null :fees, :vat_rate, false, 0.0 - change_column_null :invoices, :vat_rate, false, 0.0 + change_column_null :fees, :vat_rate, false, 0.0 + change_column_null :invoices, :vat_rate, false, 0.0 + end end end diff --git a/db/migrate/20230221102035_add_precise_amount_cents_to_credit_note_items.rb b/db/migrate/20230221102035_add_precise_amount_cents_to_credit_note_items.rb index e3b3f7ec5c7f..b3aa0177b4ed 100644 --- a/db/migrate/20230221102035_add_precise_amount_cents_to_credit_note_items.rb +++ b/db/migrate/20230221102035_add_precise_amount_cents_to_credit_note_items.rb @@ -4,14 +4,16 @@ class AddPreciseAmountCentsToCreditNoteItems < ActiveRecord::Migration[7.0] def change add_column :credit_note_items, :precise_amount_cents, :decimal, precision: 30, scale: 5 - reversible do |dir| - dir.up do - execute <<-SQL + safety_assured do + reversible do |dir| + dir.up do + execute <<-SQL UPDATE credit_note_items SET precise_amount_cents = amount_cents; - SQL + SQL + end end - end - change_column_null :credit_note_items, :precise_amount_cents, false + change_column_null :credit_note_items, :precise_amount_cents, false + end end end diff --git a/db/migrate/20230323112252_add_payment_status_to_fees.rb b/db/migrate/20230323112252_add_payment_status_to_fees.rb index 6a4eef01e975..49f0ea11a28b 100644 --- a/db/migrate/20230323112252_add_payment_status_to_fees.rb +++ b/db/migrate/20230323112252_add_payment_status_to_fees.rb @@ -2,27 +2,29 @@ class AddPaymentStatusToFees < ActiveRecord::Migration[7.0] def change - change_table :fees, bulk: true do |t| - # NOTE: will be changed after migration - t.integer :payment_status, null: true + safety_assured do + change_table :fees, bulk: true do |t| + # NOTE: will be changed after migration + t.integer :payment_status, null: true - t.datetime :succeeded_at - t.datetime :failed_at - t.datetime :refunded_at - end + t.datetime :succeeded_at + t.datetime :failed_at + t.datetime :refunded_at + end - reversible do |dir| - dir.up do - execute <<-SQL + reversible do |dir| + dir.up do + execute <<-SQL UPDATE fees SET payment_status = invoices.payment_status FROM invoices WHERE invoices.id = fees.invoice_id - SQL - end + SQL + end - change_column_null :fees, :payment_status, false - change_column_default :fees, :payment_status, 0 + change_column_null :fees, :payment_status, false + change_column_default :fees, :payment_status, 0 + end end end end diff --git a/db/migrate/20230403093407_add_balance_cents_to_wallets.rb b/db/migrate/20230403093407_add_balance_cents_to_wallets.rb index 70ee82c114e5..c81a2ca2c9f1 100644 --- a/db/migrate/20230403093407_add_balance_cents_to_wallets.rb +++ b/db/migrate/20230403093407_add_balance_cents_to_wallets.rb @@ -2,41 +2,43 @@ class AddBalanceCentsToWallets < ActiveRecord::Migration[7.0] def change - change_table :wallets, bulk: true do |t| - t.bigint :balance_cents - t.string :balance_currency + safety_assured do + change_table :wallets, bulk: true do |t| + t.bigint :balance_cents + t.string :balance_currency - t.bigint :consumed_amount_cents - t.string :consumed_amount_currenty - end + t.bigint :consumed_amount_cents + t.string :consumed_amount_currenty + end - Wallet.find_each do |wallet| - currency = Money::Currency.new(wallet.attributes['currency']) + Wallet.find_each do |wallet| + currency = Money::Currency.new(wallet.attributes['currency']) - # NOTE: prevent validation issues with deleted customers - wallet.customer = Customer.with_discarded.find(wallet.customer_id) + # NOTE: prevent validation issues with deleted customers + wallet.customer = Customer.with_discarded.find(wallet.customer_id) - wallet.update!( - balance_cents: (wallet.attributes['balance'] * currency.subunit_to_unit).to_i, - balance_currency: currency.iso_code, - consumed_amount_cents: (wallet.attributes['consumed_amount'] * currency.subunit_to_unit).to_i, - consumed_amount_currenty: currency.iso_code - ) - end + wallet.update!( + balance_cents: (wallet.attributes['balance'] * currency.subunit_to_unit).to_i, + balance_currency: currency.iso_code, + consumed_amount_cents: (wallet.attributes['consumed_amount'] * currency.subunit_to_unit).to_i, + consumed_amount_currenty: currency.iso_code + ) + end - change_column_default :wallets, :balance_cents, from: nil, to: 0 - change_column_null :wallets, :balance_cents, false - change_column_null :wallets, :balance_currency, false + change_column_default :wallets, :balance_cents, from: nil, to: 0 + change_column_null :wallets, :balance_cents, false + change_column_null :wallets, :balance_currency, false - change_column_default :wallets, :consumed_amount_cents, from: nil, to: 0 - change_column_null :wallets, :consumed_amount_cents, false - change_column_null :wallets, :consumed_amount_currenty, false + change_column_default :wallets, :consumed_amount_cents, from: nil, to: 0 + change_column_null :wallets, :consumed_amount_cents, false + change_column_null :wallets, :consumed_amount_currenty, false - reversible do |dir| - dir.up do - remove_column :wallets, :balance - remove_column :wallets, :consumed_amount - remove_column :wallets, :currency + reversible do |dir| + dir.up do + remove_column :wallets, :balance + remove_column :wallets, :consumed_amount + remove_column :wallets, :currency + end end end end diff --git a/db/migrate/20230403094044_add_billable_metric_limitations_to_coupons.rb b/db/migrate/20230403094044_add_billable_metric_limitations_to_coupons.rb index 56bf419410cf..afccd8f4a45f 100644 --- a/db/migrate/20230403094044_add_billable_metric_limitations_to_coupons.rb +++ b/db/migrate/20230403094044_add_billable_metric_limitations_to_coupons.rb @@ -5,9 +5,10 @@ def change add_column :coupons, :limited_billable_metrics, :boolean, default: false, null: false change_column_null :coupon_plans, :plan_id, true + safety_assured do + add_reference :coupon_plans, :billable_metric, type: :uuid, null: true, index: true, foreign_key: true - add_reference :coupon_plans, :billable_metric, type: :uuid, null: true, index: true, foreign_key: true - - rename_table('coupon_plans', 'coupon_targets') + rename_table('coupon_plans', 'coupon_targets') + end end end diff --git a/db/migrate/20230411085545_fix_wallet_consumed_amount_currency_naming.rb b/db/migrate/20230411085545_fix_wallet_consumed_amount_currency_naming.rb index eb60fee69d02..1cac0f27ed6f 100644 --- a/db/migrate/20230411085545_fix_wallet_consumed_amount_currency_naming.rb +++ b/db/migrate/20230411085545_fix_wallet_consumed_amount_currency_naming.rb @@ -2,6 +2,8 @@ class FixWalletConsumedAmountCurrencyNaming < ActiveRecord::Migration[7.0] def change - rename_column(:wallets, :consumed_amount_currenty, :consumed_amount_currency) + safety_assured do + rename_column(:wallets, :consumed_amount_currenty, :consumed_amount_currency) + end end end diff --git a/db/migrate/20230414074225_add_version_to_invoices.rb b/db/migrate/20230414074225_add_version_to_invoices.rb index 001b280e387e..b8a6d63a8b64 100644 --- a/db/migrate/20230414074225_add_version_to_invoices.rb +++ b/db/migrate/20230414074225_add_version_to_invoices.rb @@ -3,14 +3,15 @@ class AddVersionToInvoices < ActiveRecord::Migration[7.0] def up add_column :invoices, :version_number, :integer, null: false, default: 2 - - execute <<-SQL + safety_assured do + execute <<-SQL UPDATE invoices SET version_number = 1 WHERE legacy = 't' - SQL + SQL - remove_column :invoices, :legacy + remove_column :invoices, :legacy + end end def down diff --git a/db/migrate/20230414130437_add_currency_to_invoices.rb b/db/migrate/20230414130437_add_currency_to_invoices.rb index 14f321d58989..4e0590b772fe 100644 --- a/db/migrate/20230414130437_add_currency_to_invoices.rb +++ b/db/migrate/20230414130437_add_currency_to_invoices.rb @@ -4,16 +4,18 @@ class AddCurrencyToInvoices < ActiveRecord::Migration[7.0] def up add_column :invoices, :currency, :string - execute <<-SQL + safety_assured do + execute <<-SQL UPDATE invoices SET currency = amount_currency - SQL + SQL - change_table :invoices, bulk: true do |t| - t.remove :amount_currency - t.remove :vat_amount_currency - t.remove :total_amount_currency - t.remove :credit_amount_currency + change_table :invoices, bulk: true do |t| + t.remove :amount_currency + t.remove :vat_amount_currency + t.remove :total_amount_currency + t.remove :credit_amount_currency + end end end diff --git a/db/migrate/20230417094339_add_fees_amount_cents_to_invoices.rb b/db/migrate/20230417094339_add_fees_amount_cents_to_invoices.rb index efa0e3176a39..e736bda57ab7 100644 --- a/db/migrate/20230417094339_add_fees_amount_cents_to_invoices.rb +++ b/db/migrate/20230417094339_add_fees_amount_cents_to_invoices.rb @@ -3,10 +3,10 @@ class AddFeesAmountCentsToInvoices < ActiveRecord::Migration[7.0] def change add_column :invoices, :fees_amount_cents, :bigint, null: false, default: 0 - - reversible do |dir| - dir.up do - execute <<-SQL + safety_assured do + reversible do |dir| + dir.up do + execute <<-SQL WITH fees_total AS ( SELECT fees.invoice_id, sum(fees.amount_cents) AS fees_amount_cents FROM fees @@ -16,7 +16,8 @@ def change SET fees_amount_cents = fees_total.fees_amount_cents FROM fees_total WHERE invoices.id = fees_total.invoice_id - SQL + SQL + end end end end diff --git a/db/migrate/20230417122020_add_coupons_amount_cents_to_invoices.rb b/db/migrate/20230417122020_add_coupons_amount_cents_to_invoices.rb index 85cb47451184..79ad93c8f640 100644 --- a/db/migrate/20230417122020_add_coupons_amount_cents_to_invoices.rb +++ b/db/migrate/20230417122020_add_coupons_amount_cents_to_invoices.rb @@ -4,9 +4,10 @@ class AddCouponsAmountCentsToInvoices < ActiveRecord::Migration[7.0] def change add_column :invoices, :coupons_amount_cents, :bigint, null: false, default: 0 - reversible do |dir| - dir.up do - execute <<-SQL + safety_assured do + reversible do |dir| + dir.up do + execute <<-SQL WITH coupons_total AS ( SELECT credits.invoice_id, sum(credits.amount_cents) AS coupons_amount_cents FROM credits @@ -17,7 +18,8 @@ def change SET coupons_amount_cents = coupons_total.coupons_amount_cents FROM coupons_total WHERE invoices.id = coupons_total.invoice_id - SQL + SQL + end end end end diff --git a/db/migrate/20230417131515_add_credit_notes_amount_cents_to_invoices.rb b/db/migrate/20230417131515_add_credit_notes_amount_cents_to_invoices.rb index 2b81bd0264c5..ed294c096485 100644 --- a/db/migrate/20230417131515_add_credit_notes_amount_cents_to_invoices.rb +++ b/db/migrate/20230417131515_add_credit_notes_amount_cents_to_invoices.rb @@ -4,9 +4,10 @@ class AddCreditNotesAmountCentsToInvoices < ActiveRecord::Migration[7.0] def change add_column :invoices, :credit_notes_amount_cents, :bigint, null: false, default: 0 - reversible do |dir| - dir.up do - execute <<-SQL + safety_assured do + reversible do |dir| + dir.up do + execute <<-SQL WITH credit_notes_total AS ( SELECT credits.invoice_id, sum(credits.amount_cents) AS credit_notes_amount_cents FROM credits @@ -17,7 +18,8 @@ def change SET credit_notes_amount_cents = credit_notes_total.credit_notes_amount_cents FROM credit_notes_total WHERE invoices.id = credit_notes_total.invoice_id - SQL + SQL + end end end end diff --git a/db/migrate/20230418151450_add_subtotals_to_invoices.rb b/db/migrate/20230418151450_add_subtotals_to_invoices.rb index a95ccec4c5c3..fc7ade69090f 100644 --- a/db/migrate/20230418151450_add_subtotals_to_invoices.rb +++ b/db/migrate/20230418151450_add_subtotals_to_invoices.rb @@ -2,26 +2,28 @@ class AddSubtotalsToInvoices < ActiveRecord::Migration[7.0] def change - change_table :invoices, bulk: true do |t| - t.bigint :sub_total_vat_excluded_amount_cents, null: false, default: 0 - t.bigint :sub_total_vat_included_amount_cents, null: false, default: 0 - end + safety_assured do + change_table :invoices, bulk: true do |t| + t.bigint :sub_total_vat_excluded_amount_cents, null: false, default: 0 + t.bigint :sub_total_vat_included_amount_cents, null: false, default: 0 + end - reversible do |dir| - dir.up do - execute <<-SQL + reversible do |dir| + dir.up do + execute <<-SQL UPDATE invoices SET sub_total_vat_excluded_amount_cents = amount_cents + prepaid_credit_amount_cents, sub_total_vat_included_amount_cents = total_amount_cents WHERE version_number = 1 - SQL + SQL - execute <<-SQL + execute <<-SQL UPDATE invoices SET sub_total_vat_excluded_amount_cents = fees_amount_cents, sub_total_vat_included_amount_cents = fees_amount_cents + vat_amount_cents WHERE version_number = 2 - SQL + SQL + end end end end diff --git a/db/migrate/20230419123538_add_true_up_fee_id_to_fees.rb b/db/migrate/20230419123538_add_true_up_fee_id_to_fees.rb index e328212a5ff7..deefe641e089 100644 --- a/db/migrate/20230419123538_add_true_up_fee_id_to_fees.rb +++ b/db/migrate/20230419123538_add_true_up_fee_id_to_fees.rb @@ -2,6 +2,8 @@ class AddTrueUpFeeIdToFees < ActiveRecord::Migration[7.0] def change - add_reference :fees, :true_up_fee, type: :uuid, null: true, index: true, foreign_key: {to_table: :fees} + safety_assured do + add_reference :fees, :true_up_fee, type: :uuid, null: true, index: true, foreign_key: {to_table: :fees} + end end end diff --git a/db/migrate/20230420114754_remove_invoice_credit_amount.rb b/db/migrate/20230420114754_remove_invoice_credit_amount.rb index 56121274be3f..7b7463320149 100644 --- a/db/migrate/20230420114754_remove_invoice_credit_amount.rb +++ b/db/migrate/20230420114754_remove_invoice_credit_amount.rb @@ -2,7 +2,9 @@ class RemoveInvoiceCreditAmount < ActiveRecord::Migration[7.0] def up - remove_column :invoices, :credit_amount_cents + safety_assured do + remove_column :invoices, :credit_amount_cents + end end def down diff --git a/db/migrate/20230420120806_remove_invoice_amount.rb b/db/migrate/20230420120806_remove_invoice_amount.rb index 2fbc79eaa355..a9093cffc3c8 100644 --- a/db/migrate/20230420120806_remove_invoice_amount.rb +++ b/db/migrate/20230420120806_remove_invoice_amount.rb @@ -2,7 +2,9 @@ class RemoveInvoiceAmount < ActiveRecord::Migration[7.0] def up - remove_column :invoices, :amount_cents + safety_assured do + remove_column :invoices, :amount_cents + end end def down diff --git a/db/migrate/20230424092207_add_one_off_columns_to_fees.rb b/db/migrate/20230424092207_add_one_off_columns_to_fees.rb index f1c267387100..2b62858b4aa0 100644 --- a/db/migrate/20230424092207_add_one_off_columns_to_fees.rb +++ b/db/migrate/20230424092207_add_one_off_columns_to_fees.rb @@ -2,10 +2,12 @@ class AddOneOffColumnsToFees < ActiveRecord::Migration[7.0] def change - change_table :fees, bulk: true do |t| - t.references :add_on, type: :uuid, null: true, index: true, foreign_key: true - t.string :description - t.bigint :unit_amount_cents, null: false, default: 0 + safety_assured do + change_table :fees, bulk: true do |t| + t.references :add_on, type: :uuid, null: true, index: true, foreign_key: true + t.string :description + t.bigint :unit_amount_cents, null: false, default: 0 + end end end end diff --git a/db/migrate/20230424150952_drop_internal_credit_notes_vat_amounts.rb b/db/migrate/20230424150952_drop_internal_credit_notes_vat_amounts.rb index 33954e46f16b..1a16d2e8cc8c 100644 --- a/db/migrate/20230424150952_drop_internal_credit_notes_vat_amounts.rb +++ b/db/migrate/20230424150952_drop_internal_credit_notes_vat_amounts.rb @@ -2,11 +2,13 @@ class DropInternalCreditNotesVatAmounts < ActiveRecord::Migration[7.0] def up - change_table :credit_notes, bulk: true do |t| - t.remove :credit_vat_amount_cents - t.remove :credit_vat_amount_currency - t.remove :refund_vat_amount_cents - t.remove :refund_vat_amount_currency + safety_assured do + change_table :credit_notes, bulk: true do |t| + t.remove :credit_vat_amount_cents + t.remove :credit_vat_amount_currency + t.remove :refund_vat_amount_cents + t.remove :refund_vat_amount_currency + end end end diff --git a/db/migrate/20230424154516_add_precise_amounts_to_credit_notes.rb b/db/migrate/20230424154516_add_precise_amounts_to_credit_notes.rb index 78ff885e3937..315a8ae68000 100644 --- a/db/migrate/20230424154516_add_precise_amounts_to_credit_notes.rb +++ b/db/migrate/20230424154516_add_precise_amounts_to_credit_notes.rb @@ -2,18 +2,20 @@ class AddPreciseAmountsToCreditNotes < ActiveRecord::Migration[7.0] def change - change_table :credit_notes, bulk: true do |t| - t.decimal :precise_coupons_adjustment_amount_cents, - precision: 30, - scale: 5, - null: false, - default: 0 + safety_assured do + change_table :credit_notes, bulk: true do |t| + t.decimal :precise_coupons_adjustment_amount_cents, + precision: 30, + scale: 5, + null: false, + default: 0 - t.decimal :precise_vat_amount_cents, - precision: 30, - scale: 5, - null: false, - default: 0 + t.decimal :precise_vat_amount_cents, + precision: 30, + scale: 5, + null: false, + default: 0 + end end end end diff --git a/db/migrate/20230424210224_rename_true_up_fee_id_on_fees.rb b/db/migrate/20230424210224_rename_true_up_fee_id_on_fees.rb index dda961d4e99d..f98c6681d0cd 100644 --- a/db/migrate/20230424210224_rename_true_up_fee_id_on_fees.rb +++ b/db/migrate/20230424210224_rename_true_up_fee_id_on_fees.rb @@ -2,6 +2,8 @@ class RenameTrueUpFeeIdOnFees < ActiveRecord::Migration[7.0] def change - rename_column :fees, :true_up_fee_id, :true_up_parent_fee_id + safety_assured do + rename_column :fees, :true_up_fee_id, :true_up_parent_fee_id + end end end diff --git a/db/migrate/20230505093030_change_credits_id_type.rb b/db/migrate/20230505093030_change_credits_id_type.rb index 1e56b5c89a31..4a9a2f125191 100644 --- a/db/migrate/20230505093030_change_credits_id_type.rb +++ b/db/migrate/20230505093030_change_credits_id_type.rb @@ -2,14 +2,16 @@ class ChangeCreditsIdType < ActiveRecord::Migration[7.0] def up - add_column :credits, :uuid, :uuid, null: false, default: -> { 'gen_random_uuid()' } + safety_assured do + add_column :credits, :uuid, :uuid, null: false, default: -> { 'gen_random_uuid()' } - change_table :credits do |t| - t.remove :id - t.rename :uuid, :id - end + change_table :credits do |t| + t.remove :id + t.rename :uuid, :id + end - execute 'ALTER TABLE credits ADD PRIMARY KEY (id);' + execute 'ALTER TABLE credits ADD PRIMARY KEY (id);' + end end def down diff --git a/db/migrate/20230511124419_rename_customers_tax_rates_to_applied_tax_rates.rb b/db/migrate/20230511124419_rename_customers_tax_rates_to_applied_tax_rates.rb index 86fac7bf850c..06e735d23794 100644 --- a/db/migrate/20230511124419_rename_customers_tax_rates_to_applied_tax_rates.rb +++ b/db/migrate/20230511124419_rename_customers_tax_rates_to_applied_tax_rates.rb @@ -2,6 +2,8 @@ class RenameCustomersTaxRatesToAppliedTaxRates < ActiveRecord::Migration[7.0] def change - rename_table :customers_tax_rates, :applied_tax_rates + safety_assured do + rename_table :customers_tax_rates, :applied_tax_rates + end end end diff --git a/db/migrate/20230517093556_rename_instant_to_pay_in_advance.rb b/db/migrate/20230517093556_rename_instant_to_pay_in_advance.rb index 056412060382..9a34a5c0853b 100644 --- a/db/migrate/20230517093556_rename_instant_to_pay_in_advance.rb +++ b/db/migrate/20230517093556_rename_instant_to_pay_in_advance.rb @@ -3,18 +3,21 @@ class RenameInstantToPayInAdvance < ActiveRecord::Migration[7.0] def change add_column :fees, :pay_in_advance, :boolean, null: false, default: false - rename_column :charges, :instant, :pay_in_advance - rename_column :fees, :instant_event_id, :pay_in_advance_event_id - reversible do |dir| - dir.up do - execute <<-SQL + safety_assured do + rename_column :charges, :instant, :pay_in_advance + rename_column :fees, :instant_event_id, :pay_in_advance_event_id + + reversible do |dir| + dir.up do + execute <<-SQL UPDATE fees SET pay_in_advance = charges.pay_in_advance, fee_type = CASE WHEN charges.pay_in_advance = true THEN 0 ELSE fee_type END FROM charges WHERE fees.charge_id = charges.id; - SQL + SQL + end end end end diff --git a/db/migrate/20230522091400_add_unique_index_on_memberships.rb b/db/migrate/20230522091400_add_unique_index_on_memberships.rb index 8ac949ec5cf4..8c6c1548f7d9 100644 --- a/db/migrate/20230522091400_add_unique_index_on_memberships.rb +++ b/db/migrate/20230522091400_add_unique_index_on_memberships.rb @@ -5,6 +5,8 @@ def change LagoApi::Application.load_tasks Rake::Task['memberships:revoke_duplicates'].invoke - add_index :memberships, [:user_id, :organization_id], unique: true, where: 'revoked_at IS NULL' + safety_assured do + add_index :memberships, [:user_id, :organization_id], unique: true, where: 'revoked_at IS NULL' + end end end diff --git a/db/migrate/20230522093423_rename_tax_rates_to_taxes.rb b/db/migrate/20230522093423_rename_tax_rates_to_taxes.rb index b6c10b5582a0..180f362014ca 100644 --- a/db/migrate/20230522093423_rename_tax_rates_to_taxes.rb +++ b/db/migrate/20230522093423_rename_tax_rates_to_taxes.rb @@ -2,11 +2,13 @@ class RenameTaxRatesToTaxes < ActiveRecord::Migration[7.0] def change - rename_table :tax_rates, :taxes - rename_table :applied_tax_rates, :applied_taxes + safety_assured do + rename_table :tax_rates, :taxes + rename_table :applied_tax_rates, :applied_taxes - rename_column :taxes, :applied_by_default, :applied_to_organization - rename_column :taxes, :value, :rate - rename_column :applied_taxes, :tax_rate_id, :tax_id + rename_column :taxes, :applied_by_default, :applied_to_organization + rename_column :taxes, :value, :rate + rename_column :applied_taxes, :tax_rate_id, :tax_id + end end end diff --git a/db/migrate/20230522113810_add_invoiceable_to_charges.rb b/db/migrate/20230522113810_add_invoiceable_to_charges.rb index 207e3a3d935a..52c4cb537ff5 100644 --- a/db/migrate/20230522113810_add_invoiceable_to_charges.rb +++ b/db/migrate/20230522113810_add_invoiceable_to_charges.rb @@ -4,13 +4,15 @@ class AddInvoiceableToCharges < ActiveRecord::Migration[7.0] def change add_column :charges, :invoiceable, :boolean, null: false, default: true - reversible do |dir| - dir.up do - execute <<-SQL + safety_assured do + reversible do |dir| + dir.up do + execute <<-SQL UPDATE charges SET invoiceable = false WHERE pay_in_advance = true; - SQL + SQL + end end end end diff --git a/db/migrate/20230523094557_rename_invoices_vat_fields.rb b/db/migrate/20230523094557_rename_invoices_vat_fields.rb index f0bae80373b0..fc65f75091d4 100644 --- a/db/migrate/20230523094557_rename_invoices_vat_fields.rb +++ b/db/migrate/20230523094557_rename_invoices_vat_fields.rb @@ -2,9 +2,11 @@ class RenameInvoicesVatFields < ActiveRecord::Migration[7.0] def change - rename_column :invoices, :vat_rate, :taxes_rate - rename_column :invoices, :vat_amount_cents, :taxes_amount_cents - rename_column :invoices, :sub_total_vat_excluded_amount_cents, :sub_total_excluding_taxes_amount_cents - rename_column :invoices, :sub_total_vat_included_amount_cents, :sub_total_including_taxes_amount_cents + safety_assured do + rename_column :invoices, :vat_rate, :taxes_rate + rename_column :invoices, :vat_amount_cents, :taxes_amount_cents + rename_column :invoices, :sub_total_vat_excluded_amount_cents, :sub_total_excluding_taxes_amount_cents + rename_column :invoices, :sub_total_vat_included_amount_cents, :sub_total_including_taxes_amount_cents + end end end diff --git a/db/migrate/20230523140656_rename_credit_notes_vat_fields.rb b/db/migrate/20230523140656_rename_credit_notes_vat_fields.rb index 9e0db5711157..64870e748c86 100644 --- a/db/migrate/20230523140656_rename_credit_notes_vat_fields.rb +++ b/db/migrate/20230523140656_rename_credit_notes_vat_fields.rb @@ -3,9 +3,11 @@ class RenameCreditNotesVatFields < ActiveRecord::Migration[7.0] def up add_column :credit_notes, :taxes_rate, :float, null: false, default: 0.0 - rename_column :credit_notes, :vat_amount_cents, :taxes_amount_cents - rename_column :credit_notes, :precise_vat_amount_cents, :precise_taxes_amount_cents - remove_column :credit_notes, :vat_amount_currency + safety_assured do + rename_column :credit_notes, :vat_amount_cents, :taxes_amount_cents + rename_column :credit_notes, :precise_vat_amount_cents, :precise_taxes_amount_cents + remove_column :credit_notes, :vat_amount_currency + end end def down diff --git a/db/migrate/20230524130637_rename_fees_vat_fields.rb b/db/migrate/20230524130637_rename_fees_vat_fields.rb index 61e9c2b1b29e..bff0bfc3aa7e 100644 --- a/db/migrate/20230524130637_rename_fees_vat_fields.rb +++ b/db/migrate/20230524130637_rename_fees_vat_fields.rb @@ -2,8 +2,10 @@ class RenameFeesVatFields < ActiveRecord::Migration[7.0] def change - rename_column :fees, :vat_rate, :taxes_rate - rename_column :fees, :vat_amount_cents, :taxes_amount_cents - remove_column :fees, :vat_amount_currency, :string + safety_assured do + rename_column :fees, :vat_rate, :taxes_rate + rename_column :fees, :vat_amount_cents, :taxes_amount_cents + remove_column :fees, :vat_amount_currency, :string + end end end diff --git a/db/migrate/20230529093955_rename_applied_taxes_to_customers_taxes.rb b/db/migrate/20230529093955_rename_applied_taxes_to_customers_taxes.rb index 94688b4180b2..b4f013d5696e 100644 --- a/db/migrate/20230529093955_rename_applied_taxes_to_customers_taxes.rb +++ b/db/migrate/20230529093955_rename_applied_taxes_to_customers_taxes.rb @@ -2,6 +2,8 @@ class RenameAppliedTaxesToCustomersTaxes < ActiveRecord::Migration[7.0] def change - rename_table :applied_taxes, :customers_taxes + safety_assured do + rename_table :applied_taxes, :customers_taxes + end end end diff --git a/db/migrate/20230602090325_add_boundaries_to_invoice_subscriptions.rb b/db/migrate/20230602090325_add_boundaries_to_invoice_subscriptions.rb index e633ee5ba1c6..5962f0beefad 100644 --- a/db/migrate/20230602090325_add_boundaries_to_invoice_subscriptions.rb +++ b/db/migrate/20230602090325_add_boundaries_to_invoice_subscriptions.rb @@ -2,18 +2,19 @@ class AddBoundariesToInvoiceSubscriptions < ActiveRecord::Migration[7.0] def change - change_table(:invoice_subscriptions, bulk: true) do |t| - t.column :timestamp, :datetime - t.column :from_datetime, :datetime - t.column :to_datetime, :datetime - t.column :charges_from_datetime, :datetime - t.column :charges_to_datetime, :datetime - end + safety_assured do + change_table(:invoice_subscriptions, bulk: true) do |t| + t.column :timestamp, :datetime + t.column :from_datetime, :datetime + t.column :to_datetime, :datetime + t.column :charges_from_datetime, :datetime + t.column :charges_to_datetime, :datetime + end - reversible do |dir| - dir.up do - # rubocop:disable Style/RedundantStringEscape - execute <<-SQL + reversible do |dir| + dir.up do + # rubocop:disable Style/RedundantStringEscape + execute <<-SQL /* Unify fees->timestamp to be a required timestamp */ UPDATE fees SET properties = jsonb_set(properties, '{timestamp}', to_jsonb( @@ -120,8 +121,9 @@ def change ORDER BY fees.created_at ASC LIMIT 1)::timestamp END - SQL - # rubocop:enable Style/RedundantStringEscape + SQL + # rubocop:enable Style/RedundantStringEscape + end end end end diff --git a/db/migrate/20230608085013_remove_properties_from_invoice_subscriptions.rb b/db/migrate/20230608085013_remove_properties_from_invoice_subscriptions.rb index b7858b0169d6..00401b3b8ef8 100644 --- a/db/migrate/20230608085013_remove_properties_from_invoice_subscriptions.rb +++ b/db/migrate/20230608085013_remove_properties_from_invoice_subscriptions.rb @@ -2,6 +2,8 @@ class RemovePropertiesFromInvoiceSubscriptions < ActiveRecord::Migration[7.0] def change - remove_column :invoice_subscriptions, :properties, :jsonb + safety_assured do + remove_column :invoice_subscriptions, :properties, :jsonb + end end end diff --git a/db/migrate/20230608133543_rename_persistent_to_quentified_event.rb b/db/migrate/20230608133543_rename_persistent_to_quentified_event.rb index 25273fc0476d..6660795c743a 100644 --- a/db/migrate/20230608133543_rename_persistent_to_quentified_event.rb +++ b/db/migrate/20230608133543_rename_persistent_to_quentified_event.rb @@ -2,7 +2,9 @@ class RenamePersistentToQuentifiedEvent < ActiveRecord::Migration[7.0] def change - rename_table :persisted_events, :quantified_events - rename_index :quantified_events, :index_search_persisted_events, :index_search_quantified_events + safety_assured do + rename_table :persisted_events, :quantified_events + rename_index :quantified_events, :index_search_persisted_events, :index_search_quantified_events + end end end diff --git a/db/migrate/20230608154821_add_unique_index_to_invoice_subscriptions.rb b/db/migrate/20230608154821_add_unique_index_to_invoice_subscriptions.rb index 29736e150eb8..dba5d82ad48f 100644 --- a/db/migrate/20230608154821_add_unique_index_to_invoice_subscriptions.rb +++ b/db/migrate/20230608154821_add_unique_index_to_invoice_subscriptions.rb @@ -2,16 +2,18 @@ class AddUniqueIndexToInvoiceSubscriptions < ActiveRecord::Migration[7.0] def change - add_index :invoice_subscriptions, - %i[subscription_id from_datetime to_datetime], - unique: true, - where: "created_at >= '2023-06-09' and recurring is true", - name: 'index_invoice_subscriptions_on_from_and_to_datetime' + safety_assured do + add_index :invoice_subscriptions, + %i[subscription_id from_datetime to_datetime], + unique: true, + where: "created_at >= '2023-06-09' and recurring is true", + name: 'index_invoice_subscriptions_on_from_and_to_datetime' - add_index :invoice_subscriptions, - %i[subscription_id charges_from_datetime charges_to_datetime], - unique: true, - where: "created_at >= '2023-06-09' and recurring is true", - name: 'index_invoice_subscriptions_on_charges_from_and_to_datetime' + add_index :invoice_subscriptions, + %i[subscription_id charges_from_datetime charges_to_datetime], + unique: true, + where: "created_at >= '2023-06-09' and recurring is true", + name: 'index_invoice_subscriptions_on_charges_from_and_to_datetime' + end end end diff --git a/db/migrate/20230614191603_create_webhook_endpoints.rb b/db/migrate/20230614191603_create_webhook_endpoints.rb index d87cf26793bc..ad0302e0e830 100644 --- a/db/migrate/20230614191603_create_webhook_endpoints.rb +++ b/db/migrate/20230614191603_create_webhook_endpoints.rb @@ -9,21 +9,23 @@ def up t.timestamps end - add_reference :webhooks, :webhook_endpoint, type: :uuid, foreign_key: true, index: true - - execute(<<~SQL.squish) - insert into webhook_endpoints(organization_id, webhook_url, created_at, updated_at) - select id, webhook_url, NOW(), NOW() from organizations where (webhook_url is not null or webhook_url <> ''); - SQL - - execute(<<~SQL.squish) - update webhooks - set webhook_endpoint_id = whe.id - from (select id, organization_id from webhook_endpoints) as whe - where webhooks.organization_id = whe.organization_id; - SQL - - remove_reference :webhooks, :organization, index: true + safety_assured do + add_reference :webhooks, :webhook_endpoint, type: :uuid, foreign_key: true, index: true + + execute(<<~SQL.squish) + insert into webhook_endpoints(organization_id, webhook_url, created_at, updated_at) + select id, webhook_url, NOW(), NOW() from organizations where (webhook_url is not null or webhook_url <> ''); + SQL + + execute(<<~SQL.squish) + update webhooks + set webhook_endpoint_id = whe.id + from (select id, organization_id from webhook_endpoints) as whe + where webhooks.organization_id = whe.organization_id; + SQL + + remove_reference :webhooks, :organization, index: true + end end def down diff --git a/db/migrate/20230615183805_add_event_refrence_to_quantified_events.rb b/db/migrate/20230615183805_add_event_refrence_to_quantified_events.rb index e38c26e18f81..3fd3c1746f69 100644 --- a/db/migrate/20230615183805_add_event_refrence_to_quantified_events.rb +++ b/db/migrate/20230615183805_add_event_refrence_to_quantified_events.rb @@ -2,6 +2,8 @@ class AddEventRefrenceToQuantifiedEvents < ActiveRecord::Migration[7.0] def change - add_reference :events, :quantified_event, type: :uuid, index: true + safety_assured do + add_reference :events, :quantified_event, type: :uuid, index: true + end end end diff --git a/db/migrate/20230620211201_add_unique_index_to_webhook_urls.rb b/db/migrate/20230620211201_add_unique_index_to_webhook_urls.rb index d84d192a872b..bcd42b7eea88 100644 --- a/db/migrate/20230620211201_add_unique_index_to_webhook_urls.rb +++ b/db/migrate/20230620211201_add_unique_index_to_webhook_urls.rb @@ -2,6 +2,8 @@ class AddUniqueIndexToWebhookUrls < ActiveRecord::Migration[7.0] def change - add_index :webhook_endpoints, [:webhook_url, :organization_id], unique: true + safety_assured do + add_index :webhook_endpoints, [:webhook_url, :organization_id], unique: true + end end end diff --git a/db/migrate/20230626123648_unify_invoices_taxes_rate.rb b/db/migrate/20230626123648_unify_invoices_taxes_rate.rb index 942cc34f7028..a46ab6c1092c 100644 --- a/db/migrate/20230626123648_unify_invoices_taxes_rate.rb +++ b/db/migrate/20230626123648_unify_invoices_taxes_rate.rb @@ -2,14 +2,16 @@ class UnifyInvoicesTaxesRate < ActiveRecord::Migration[7.0] def change - reversible do |dir| - dir.up do - execute <<-SQL + safety_assured do + reversible do |dir| + dir.up do + execute <<-SQL /* Unify invoices->taxes_rate to be a number */ UPDATE invoices SET taxes_rate = 0.0 WHERE taxes_rate = 'NaN'::NUMERIC; - SQL + SQL + end end end end diff --git a/db/migrate/20230704112230_fix_organizations_taxes.rb b/db/migrate/20230704112230_fix_organizations_taxes.rb index 281942701b74..8e7990718fab 100644 --- a/db/migrate/20230704112230_fix_organizations_taxes.rb +++ b/db/migrate/20230704112230_fix_organizations_taxes.rb @@ -2,16 +2,18 @@ class FixOrganizationsTaxes < ActiveRecord::Migration[7.0] def change - reversible do |dir| - dir.up do - execute <<-SQL + safety_assured do + reversible do |dir| + dir.up do + execute <<-SQL UPDATE taxes SET applied_to_organization = true FROM organizations WHERE organizations.id = taxes.organization_id AND organizations.vat_rate = taxes.rate AND applied_to_organization = false; - SQL + SQL + end end end end diff --git a/db/migrate/20230705213846_update_provider_payment_methods_for_stripe_customers.rb b/db/migrate/20230705213846_update_provider_payment_methods_for_stripe_customers.rb index 7d5cfba4b3f5..d4e1e505e803 100644 --- a/db/migrate/20230705213846_update_provider_payment_methods_for_stripe_customers.rb +++ b/db/migrate/20230705213846_update_provider_payment_methods_for_stripe_customers.rb @@ -2,13 +2,15 @@ class UpdateProviderPaymentMethodsForStripeCustomers < ActiveRecord::Migration[7.0] def change - reversible do |dir| - dir.up do - execute(<<~SQL.squish) - UPDATE payment_provider_customers - SET settings = jsonb_set(settings, '{provider_payment_methods}', '["card"]') - WHERE type = 'PaymentProviderCustomers::StripeCustomer'; - SQL + safety_assured do + reversible do |dir| + dir.up do + execute(<<~SQL.squish) + UPDATE payment_provider_customers + SET settings = jsonb_set(settings, '{provider_payment_methods}', '["card"]') + WHERE type = 'PaymentProviderCustomers::StripeCustomer'; + SQL + end end end end diff --git a/db/migrate/20230713122526_rename_credit_before_vat.rb b/db/migrate/20230713122526_rename_credit_before_vat.rb index 57bd1482d4da..0020691c819f 100644 --- a/db/migrate/20230713122526_rename_credit_before_vat.rb +++ b/db/migrate/20230713122526_rename_credit_before_vat.rb @@ -2,6 +2,8 @@ class RenameCreditBeforeVat < ActiveRecord::Migration[7.0] def change - rename_column :credits, :before_vat, :before_taxes + safety_assured do + rename_column :credits, :before_vat, :before_taxes + end end end diff --git a/db/migrate/20230719100256_migrate_recurring_count_and_unique_count_aggregation.rb b/db/migrate/20230719100256_migrate_recurring_count_and_unique_count_aggregation.rb index dfc2c26c46f5..772e37a6158d 100644 --- a/db/migrate/20230719100256_migrate_recurring_count_and_unique_count_aggregation.rb +++ b/db/migrate/20230719100256_migrate_recurring_count_and_unique_count_aggregation.rb @@ -20,7 +20,8 @@ def change reversible do |dir| dir.up do # Create quantified event object for all unique count events - sql = <<-SQL + safety_assured do + sql = <<-SQL SELECT events.id AS event_id, events.code AS code, @@ -39,37 +40,37 @@ def change AND billable_metrics.aggregation_type = 3 AND billable_metrics.organization_id = events.organization_id ORDER BY event_timestamp ASC; - SQL + SQL - ApplicationRecord.connection.select_all(sql).each_with_object({}) do |row, _result| - existing_quantified_event = - QuantifiedEvent.where( + ApplicationRecord.connection.select_all(sql).each_with_object({}) do |row, _result| + existing_quantified_event = + QuantifiedEvent.where( + customer_id: row['customer_id'], + billable_metric_id: row['billable_metric_id'], + external_subscription_id: row['subscription_external_id'], + external_id: JSON.parse(row['properties'])[row['field_name'].to_s] + ).where(removed_at: nil).any? + + # There can only be one quantified event for certain external_id which guarantees uniqueness + next if existing_quantified_event + + quantified_event = QuantifiedEvent.create!( customer_id: row['customer_id'], billable_metric_id: row['billable_metric_id'], external_subscription_id: row['subscription_external_id'], - external_id: JSON.parse(row['properties'])[row['field_name'].to_s] - ).where(removed_at: nil).any? - - # There can only be one quantified event for certain external_id which guarantees uniqueness - next if existing_quantified_event - - quantified_event = QuantifiedEvent.create!( - customer_id: row['customer_id'], - billable_metric_id: row['billable_metric_id'], - external_subscription_id: row['subscription_external_id'], - external_id: JSON.parse(row['properties'])[row['field_name'].to_s], - properties: JSON.parse(row['properties']), - added_at: row['event_timestamp'] - ) - - event = Event.find_by(id: row['event_id']) - event.quantified_event_id = quantified_event.id - event.save! - end - - # If charge is pay_in_advance and aggregation type is SUM we need to set event metadata since this metadata - # will be used in calculation of previous_event for further events in same period - execute <<-SQL + external_id: JSON.parse(row['properties'])[row['field_name'].to_s], + properties: JSON.parse(row['properties']), + added_at: row['event_timestamp'] + ) + + event = Event.find_by(id: row['event_id']) + event.quantified_event_id = quantified_event.id + event.save! + end + + # If charge is pay_in_advance and aggregation type is SUM we need to set event metadata since this metadata + # will be used in calculation of previous_event for further events in same period + execute <<-SQL WITH sum_in_advance_events AS ( SELECT events.id AS event_id, @@ -91,11 +92,11 @@ def change jsonb_set(metadata, '{current_aggregation}', to_jsonb(event_value), true) FROM sum_in_advance_events WHERE sum_in_advance_events.event_id = events.id - SQL + SQL - # If charge is pay_in_advance and aggregation type is UNIQUE COUNT we need to set event metadata since this - # metadata will be used in calculation of previous event for further events in the same period - execute <<-SQL + # If charge is pay_in_advance and aggregation type is UNIQUE COUNT we need to set event metadata since this + # metadata will be used in calculation of previous event for further events in the same period + execute <<-SQL WITH unique_count_in_advance_events AS ( SELECT events.id AS event_id FROM events @@ -111,7 +112,8 @@ def change jsonb_set(metadata, '{current_aggregation}', '1'::jsonb, true) FROM unique_count_in_advance_events WHERE unique_count_in_advance_events.event_id = events.id - SQL + SQL + end end end end diff --git a/db/migrate/20230721073114_fix_credit_before_taxes.rb b/db/migrate/20230721073114_fix_credit_before_taxes.rb index 7c66ca9c4b5d..f85aa1666b60 100644 --- a/db/migrate/20230721073114_fix_credit_before_taxes.rb +++ b/db/migrate/20230721073114_fix_credit_before_taxes.rb @@ -4,7 +4,8 @@ class FixCreditBeforeTaxes < ActiveRecord::Migration[7.0] def change reversible do |dir| dir.up do - execute <<-SQL + safety_assured do + execute <<-SQL WITH wrong_before_vat_credits AS ( SELECT credits.id AS credit_id FROM credits @@ -18,7 +19,8 @@ def change SET before_taxes = true FROM wrong_before_vat_credits WHERE wrong_before_vat_credits.credit_id = credits.id; - SQL + SQL + end end end end diff --git a/db/migrate/20230726165711_add_net_payment_term_on_organization_and_customer.rb b/db/migrate/20230726165711_add_net_payment_term_on_organization_and_customer.rb index 5faa51653a26..b3a69c2ec50f 100644 --- a/db/migrate/20230726165711_add_net_payment_term_on_organization_and_customer.rb +++ b/db/migrate/20230726165711_add_net_payment_term_on_organization_and_customer.rb @@ -5,23 +5,25 @@ def change add_column :organizations, :net_payment_term, :integer, default: 0, null: false add_column :customers, :net_payment_term, :integer, default: nil, null: true - reversible do |dir| - dir.up do - execute <<-SQL + safety_assured do + reversible do |dir| + dir.up do + execute <<-SQL ALTER TABLE organizations ADD CONSTRAINT check_organizations_on_net_payment_term CHECK (net_payment_term >= 0); ALTER TABLE customers ADD CONSTRAINT check_customers_on_net_payment_term CHECK (net_payment_term >= 0); - SQL - end + SQL + end - dir.down do - execute <<-SQL + dir.down do + execute <<-SQL ALTER TABLE organizations DROP CONSTRAINT check_organizations_on_net_payment_term; ALTER TABLE customers DROP CONSTRAINT check_customers_on_net_payment_term; - SQL + SQL + end end end end diff --git a/db/migrate/20230731135721_add_net_payment_term_to_invoice.rb b/db/migrate/20230731135721_add_net_payment_term_to_invoice.rb index abbf809032aa..d014abe8865b 100644 --- a/db/migrate/20230731135721_add_net_payment_term_to_invoice.rb +++ b/db/migrate/20230731135721_add_net_payment_term_to_invoice.rb @@ -3,20 +3,21 @@ class AddNetPaymentTermToInvoice < ActiveRecord::Migration[7.0] def change add_column :invoices, :net_payment_term, :integer, default: 0, null: false - - reversible do |dir| - dir.up do - execute <<-SQL + safety_assured do + reversible do |dir| + dir.up do + execute <<-SQL ALTER TABLE invoices ADD CONSTRAINT check_organizations_on_net_payment_term CHECK (net_payment_term >= 0); - SQL - end + SQL + end - dir.down do - execute <<-SQL + dir.down do + execute <<-SQL ALTER TABLE invoices DROP CONSTRAINT check_organizations_on_net_payment_term; - SQL + SQL + end end end end diff --git a/db/migrate/20230808144739_fill_invoice_payment_due_date.rb b/db/migrate/20230808144739_fill_invoice_payment_due_date.rb index 3428f3e33df7..e480951da1d7 100644 --- a/db/migrate/20230808144739_fill_invoice_payment_due_date.rb +++ b/db/migrate/20230808144739_fill_invoice_payment_due_date.rb @@ -4,11 +4,13 @@ class FillInvoicePaymentDueDate < ActiveRecord::Migration[7.0] def change reversible do |dir| dir.up do - execute <<-SQL + safety_assured do + execute <<-SQL UPDATE invoices SET payment_due_date = issuing_date WHERE payment_due_date IS NULL; - SQL + SQL + end end end end diff --git a/db/migrate/20230811081854_populate_fees_precise_coupons_amount_cents.rb b/db/migrate/20230811081854_populate_fees_precise_coupons_amount_cents.rb index 151e845b4b87..5c824ed2e096 100644 --- a/db/migrate/20230811081854_populate_fees_precise_coupons_amount_cents.rb +++ b/db/migrate/20230811081854_populate_fees_precise_coupons_amount_cents.rb @@ -31,64 +31,66 @@ class Credit < ApplicationRecord end def change - reversible do |dir| - dir.up do - credits = Credit - .joins(:invoice) - .joins(applied_coupon: :coupon) - .where('credits.amount_cents > 0') - .where(invoices: {version_number: 3}) - .order('credits.created_at ASC') - - # NOTE: prevent migration of fees already using the field - fees_id = Fee.where.not(precise_coupons_amount_cents: 0).pluck(:id) - - credits.each do |credit| - if credit.applied_coupon.coupon.limited_plans - coupon = credit.applied_coupon.coupon - fees = credit.invoice.fees - .where.not(id: fees_id) - .joins(:subscription) - .where(subscriptions: {plan_id: coupon.coupon_targets.where.not(plan_id: nil).select(:plan_id)}) - - fees.find_each do |fee| - base_amount_cents = fees.sum(:amount_cents) - next if base_amount_cents.zero? - - fee.precise_coupons_amount_cents += (credit.amount_cents * fee.amount_cents).fdiv(base_amount_cents) - fee.save! - end - - elsif credit.applied_coupon.coupon.limited_billable_metrics - coupon = credit.applied_coupon.coupon - fees = credit.invoice.fees - .where.not(id: fees_id) - .joins(:charge) - .where(charge: { - billable_metric_id: coupon.coupon_targets - .where.not(billable_metric_id: nil) - .select(:billable_metric_id) - }) - - fees.find_each do |fee| - base_amount_cents = fees.sum(:amount_cents) - next if base_amount_cents.zero? - - fee.precise_coupons_amount_cents += (credit.amount_cents * fee.amount_cents).fdiv(base_amount_cents) - fee.save! - end - else - fees = credit.invoice.fees.where.not(id: fees_id) - - fees.find_each do |fee| - # NOTE: When applying coupons without limitations, - # the base of computation is the remaining amount of all fees. - base_amount_cents = Fee.where(invoice_id: fee.invoice_id) - .sum('fees.amount_cents - fees.precise_coupons_amount_cents') - next if base_amount_cents.zero? - - fee.precise_coupons_amount_cents += (credit.amount_cents * fee.amount_cents).fdiv(base_amount_cents) - fee.save! + safety_assured do + reversible do |dir| + dir.up do + credits = Credit + .joins(:invoice) + .joins(applied_coupon: :coupon) + .where('credits.amount_cents > 0') + .where(invoices: {version_number: 3}) + .order('credits.created_at ASC') + + # NOTE: prevent migration of fees already using the field + fees_id = Fee.where.not(precise_coupons_amount_cents: 0).pluck(:id) + + credits.each do |credit| + if credit.applied_coupon.coupon.limited_plans + coupon = credit.applied_coupon.coupon + fees = credit.invoice.fees + .where.not(id: fees_id) + .joins(:subscription) + .where(subscriptions: {plan_id: coupon.coupon_targets.where.not(plan_id: nil).select(:plan_id)}) + + fees.find_each do |fee| + base_amount_cents = fees.sum(:amount_cents) + next if base_amount_cents.zero? + + fee.precise_coupons_amount_cents += (credit.amount_cents * fee.amount_cents).fdiv(base_amount_cents) + fee.save! + end + + elsif credit.applied_coupon.coupon.limited_billable_metrics + coupon = credit.applied_coupon.coupon + fees = credit.invoice.fees + .where.not(id: fees_id) + .joins(:charge) + .where(charge: { + billable_metric_id: coupon.coupon_targets + .where.not(billable_metric_id: nil) + .select(:billable_metric_id) + }) + + fees.find_each do |fee| + base_amount_cents = fees.sum(:amount_cents) + next if base_amount_cents.zero? + + fee.precise_coupons_amount_cents += (credit.amount_cents * fee.amount_cents).fdiv(base_amount_cents) + fee.save! + end + else + fees = credit.invoice.fees.where.not(id: fees_id) + + fees.find_each do |fee| + # NOTE: When applying coupons without limitations, + # the base of computation is the remaining amount of all fees. + base_amount_cents = Fee.where(invoice_id: fee.invoice_id) + .sum('fees.amount_cents - fees.precise_coupons_amount_cents') + next if base_amount_cents.zero? + + fee.precise_coupons_amount_cents += (credit.amount_cents * fee.amount_cents).fdiv(base_amount_cents) + fee.save! + end end end end diff --git a/db/migrate/20230811120622_populate_fees_amount_cents_in_invoice_taxes.rb b/db/migrate/20230811120622_populate_fees_amount_cents_in_invoice_taxes.rb index 122123024bee..04dccc232b1f 100644 --- a/db/migrate/20230811120622_populate_fees_amount_cents_in_invoice_taxes.rb +++ b/db/migrate/20230811120622_populate_fees_amount_cents_in_invoice_taxes.rb @@ -4,7 +4,8 @@ class PopulateFeesAmountCentsInInvoiceTaxes < ActiveRecord::Migration[7.0] def change reversible do |dir| dir.up do - execute <<-SQL + safety_assured do + execute <<-SQL WITH invoice_fees_amounts AS ( SELECT invoices.id AS invoice_id, @@ -21,7 +22,8 @@ def change FROM invoice_fees_amounts WHERE invoice_fees_amounts.invoice_id = invoices_taxes.invoice_id AND invoice_fees_amounts.tax_id = invoices_taxes.tax_id - SQL + SQL + end end end end diff --git a/db/migrate/20230817092555_set_default_properties_to_charges.rb b/db/migrate/20230817092555_set_default_properties_to_charges.rb index a16480ea4a6d..a18d7452ba61 100644 --- a/db/migrate/20230817092555_set_default_properties_to_charges.rb +++ b/db/migrate/20230817092555_set_default_properties_to_charges.rb @@ -4,7 +4,8 @@ class SetDefaultPropertiesToCharges < ActiveRecord::Migration[7.0] def change reversible do |dir| dir.up do - execute <<-SQL + safety_assured do + execute <<-SQL -- Standard charges UPDATE charges SET properties = '{ @@ -78,7 +79,8 @@ def change }' WHERE charge_model = 5 AND length(properties::text) = 2; - SQL + SQL + end end end end diff --git a/db/migrate/20230821135235_add_unique_constraint_to_group_properties.rb b/db/migrate/20230821135235_add_unique_constraint_to_group_properties.rb index 3bf6f14685a6..0cb5f337e1d6 100644 --- a/db/migrate/20230821135235_add_unique_constraint_to_group_properties.rb +++ b/db/migrate/20230821135235_add_unique_constraint_to_group_properties.rb @@ -2,6 +2,8 @@ class AddUniqueConstraintToGroupProperties < ActiveRecord::Migration[7.0] def change - add_index :group_properties, %i[charge_id group_id], unique: true + safety_assured do + add_index :group_properties, %i[charge_id group_id], unique: true + end end end diff --git a/db/migrate/20230830120517_remove_status_from_groups.rb b/db/migrate/20230830120517_remove_status_from_groups.rb index e7864cc3c9a8..7e641f2b2d9c 100644 --- a/db/migrate/20230830120517_remove_status_from_groups.rb +++ b/db/migrate/20230830120517_remove_status_from_groups.rb @@ -2,17 +2,19 @@ class RemoveStatusFromGroups < ActiveRecord::Migration[7.0] def change - reversible do |dir| - dir.up do - execute <<-SQL + safety_assured do + reversible do |dir| + dir.up do + execute <<-SQL UPDATE groups SET deleted_at = updated_at WHERE deleted_at IS NULL AND groups.status = 1 - SQL + SQL + end end - end - remove_column :groups, :status, :integer, default: 0 + remove_column :groups, :status, :integer, default: 0 + end end end diff --git a/db/migrate/20230905081225_add_weighted_interval_to_billable_metric.rb b/db/migrate/20230905081225_add_weighted_interval_to_billable_metric.rb index 5726c0b8a462..167a0e830195 100644 --- a/db/migrate/20230905081225_add_weighted_interval_to_billable_metric.rb +++ b/db/migrate/20230905081225_add_weighted_interval_to_billable_metric.rb @@ -4,8 +4,10 @@ class AddWeightedIntervalToBillableMetric < ActiveRecord::Migration[7.0] def change create_enum :billable_metric_weighted_interval, %w[seconds] - change_table :billable_metrics do |t| - t.enum :weighted_interval, enum_type: 'billable_metric_weighted_interval', null: true + safety_assured do + change_table :billable_metrics do |t| + t.enum :weighted_interval, enum_type: 'billable_metric_weighted_interval', null: true + end end end end diff --git a/db/migrate/20230907153404_add_group_id_to_quantified_events.rb b/db/migrate/20230907153404_add_group_id_to_quantified_events.rb index b23d8d7a9bc5..d4af9f45459f 100644 --- a/db/migrate/20230907153404_add_group_id_to_quantified_events.rb +++ b/db/migrate/20230907153404_add_group_id_to_quantified_events.rb @@ -2,7 +2,9 @@ class AddGroupIdToQuantifiedEvents < ActiveRecord::Migration[7.0] def change - add_reference :quantified_events, :group, type: :uuid, foreign_key: true, index: true - change_column_null :quantified_events, :external_id, true + safety_assured do + add_reference :quantified_events, :group, type: :uuid, foreign_key: true, index: true + change_column_null :quantified_events, :external_id, true + end end end diff --git a/db/migrate/20230911083923_add_total_aggregated_units_to_fees.rb b/db/migrate/20230911083923_add_total_aggregated_units_to_fees.rb index 13f5df530baa..3a2538276145 100644 --- a/db/migrate/20230911083923_add_total_aggregated_units_to_fees.rb +++ b/db/migrate/20230911083923_add_total_aggregated_units_to_fees.rb @@ -4,13 +4,15 @@ class AddTotalAggregatedUnitsToFees < ActiveRecord::Migration[7.0] def change add_column :fees, :total_aggregated_units, :decimal - reversible do |dir| - dir.up do - execute <<-SQL + safety_assured do + reversible do |dir| + dir.up do + execute <<-SQL UPDATE fees SET total_aggregated_units = units WHERE fee_type = 0; - SQL + SQL + end end end end diff --git a/db/migrate/20230911185900_add_unique_index_to_applied_taxes.rb b/db/migrate/20230911185900_add_unique_index_to_applied_taxes.rb index 4b592ce1beff..bf33149f9420 100644 --- a/db/migrate/20230911185900_add_unique_index_to_applied_taxes.rb +++ b/db/migrate/20230911185900_add_unique_index_to_applied_taxes.rb @@ -2,20 +2,22 @@ class AddUniqueIndexToAppliedTaxes < ActiveRecord::Migration[7.0] def change - add_index :add_ons_taxes, %i[add_on_id tax_id], unique: true - add_index :charges_taxes, %i[charge_id tax_id], unique: true - add_index :credit_notes_taxes, %i[credit_note_id tax_id], unique: true - add_index :customers_taxes, %i[customer_id tax_id], unique: true - add_index :plans_taxes, %i[plan_id tax_id], unique: true + safety_assured do + add_index :add_ons_taxes, %i[add_on_id tax_id], unique: true + add_index :charges_taxes, %i[charge_id tax_id], unique: true + add_index :credit_notes_taxes, %i[credit_note_id tax_id], unique: true + add_index :customers_taxes, %i[customer_id tax_id], unique: true + add_index :plans_taxes, %i[plan_id tax_id], unique: true - add_index :fees_taxes, - %i[fee_id tax_id], - unique: true, - where: "created_at >= '2023-09-12'" + add_index :fees_taxes, + %i[fee_id tax_id], + unique: true, + where: "created_at >= '2023-09-12'" - add_index :invoices_taxes, - %i[invoice_id tax_id], - unique: true, - where: "created_at >= '2023-09-12'" + add_index :invoices_taxes, + %i[invoice_id tax_id], + unique: true, + where: "created_at >= '2023-09-12'" + end end end diff --git a/db/migrate/20230915073205_update_code_index_on_plans.rb b/db/migrate/20230915073205_update_code_index_on_plans.rb index 06c9a15b0c66..87488bab6021 100644 --- a/db/migrate/20230915073205_update_code_index_on_plans.rb +++ b/db/migrate/20230915073205_update_code_index_on_plans.rb @@ -4,9 +4,11 @@ class UpdateCodeIndexOnPlans < ActiveRecord::Migration[7.0] def change remove_index :plans, %i[organization_id code] - add_index :plans, - %i[organization_id code], - unique: true, - where: 'deleted_at IS NULL AND parent_id IS NULL' + safety_assured do + add_index :plans, + %i[organization_id code], + unique: true, + where: 'deleted_at IS NULL AND parent_id IS NULL' + end end end diff --git a/db/migrate/20230915135256_add_invoice_display_name_to_group_properties.rb b/db/migrate/20230915135256_add_invoice_display_name_to_group_properties.rb index f7b59ee898de..85f2dd78950f 100644 --- a/db/migrate/20230915135256_add_invoice_display_name_to_group_properties.rb +++ b/db/migrate/20230915135256_add_invoice_display_name_to_group_properties.rb @@ -2,8 +2,10 @@ class AddInvoiceDisplayNameToGroupProperties < ActiveRecord::Migration[7.0] def up - remove_column :groups, :invoice_value - add_column :group_properties, :invoice_display_name, :string + safety_assured do + remove_column :groups, :invoice_value + add_column :group_properties, :invoice_display_name, :string + end end def down diff --git a/db/migrate/20230922064617_remove_parent_id_from_plans.rb b/db/migrate/20230922064617_remove_parent_id_from_plans.rb index 0f363cd6f753..5121b0b6530d 100644 --- a/db/migrate/20230922064617_remove_parent_id_from_plans.rb +++ b/db/migrate/20230922064617_remove_parent_id_from_plans.rb @@ -3,11 +3,13 @@ class RemoveParentIdFromPlans < ActiveRecord::Migration[7.0] def change reversible do |dir| - dir.up do - execute <<-SQL + safety_assured do + dir.up do + execute <<-SQL UPDATE plans SET parent_id = NULL - SQL + SQL + end end end end diff --git a/db/migrate/20230926132500_add_external_ids_to_events.rb b/db/migrate/20230926132500_add_external_ids_to_events.rb index 5c5a4b143d09..c88511ec5fcd 100644 --- a/db/migrate/20230926132500_add_external_ids_to_events.rb +++ b/db/migrate/20230926132500_add_external_ids_to_events.rb @@ -2,11 +2,13 @@ class AddExternalIdsToEvents < ActiveRecord::Migration[7.0] def change - change_table :events, bulk: true do |t| - t.string :external_customer_id - t.string :external_subscription_id - end + safety_assured do + change_table :events, bulk: true do |t| + t.string :external_customer_id + t.string :external_subscription_id + end - change_column_null :events, :customer_id, true + change_column_null :events, :customer_id, true + end end end diff --git a/db/migrate/20230926144126_fill_event_external_ids.rb b/db/migrate/20230926144126_fill_event_external_ids.rb index 6b9e6a83619c..dd9991e98dbd 100644 --- a/db/migrate/20230926144126_fill_event_external_ids.rb +++ b/db/migrate/20230926144126_fill_event_external_ids.rb @@ -6,7 +6,8 @@ class FillEventExternalIds < ActiveRecord::Migration[7.0] def change reversible do |dir| dir.up do - execute <<-SQL + safety_assured do + execute <<-SQL WITH events_external_ids AS ( SELECT events.id AS event_id, @@ -25,7 +26,8 @@ def change external_subscription_id = events_external_ids.external_subscription_id FROM events_external_ids WHERE events_external_ids.event_id = events.id - SQL + SQL + end end end end diff --git a/db/migrate/20231010085938_deduplicate_events_transaction_id.rb b/db/migrate/20231010085938_deduplicate_events_transaction_id.rb index da1528b5e7e7..fe78cb0942ff 100644 --- a/db/migrate/20231010085938_deduplicate_events_transaction_id.rb +++ b/db/migrate/20231010085938_deduplicate_events_transaction_id.rb @@ -4,7 +4,8 @@ class DeduplicateEventsTransactionId < ActiveRecord::Migration[7.0] def change reversible do |dir| dir.up do - execute <<-SQL + safety_assured do + execute <<-SQL WITH duplicated_transaction_id AS ( SELECT events.id AS event_id FROM events @@ -30,7 +31,8 @@ def change SET transaction_id = events.transaction_id || '_' || events.id FROM duplicated_transaction_id WHERE duplicated_transaction_id.event_id = events.id - SQL + SQL + end end end end diff --git a/db/migrate/20231010090849_events_transaction_id_uniqueness.rb b/db/migrate/20231010090849_events_transaction_id_uniqueness.rb index f72e0a0e800a..2b9946768c27 100644 --- a/db/migrate/20231010090849_events_transaction_id_uniqueness.rb +++ b/db/migrate/20231010090849_events_transaction_id_uniqueness.rb @@ -4,13 +4,14 @@ class EventsTransactionIdUniqueness < ActiveRecord::Migration[7.0] disable_ddl_transaction! def change - add_index( - :events, - %i[organization_id external_subscription_id transaction_id], - unique: true, - name: 'index_unique_transaction_id' - ) - + safety_assured do + add_index( + :events, + %i[organization_id external_subscription_id transaction_id], + unique: true, + name: 'index_unique_transaction_id' + ) + end remove_index :events, %i[subscription_id transaction_id] end end diff --git a/db/migrate/20231020091031_convert_unit_amount_cents_to_decimal.rb b/db/migrate/20231020091031_convert_unit_amount_cents_to_decimal.rb index f47a8501d654..84c66e587d67 100644 --- a/db/migrate/20231020091031_convert_unit_amount_cents_to_decimal.rb +++ b/db/migrate/20231020091031_convert_unit_amount_cents_to_decimal.rb @@ -2,7 +2,9 @@ class ConvertUnitAmountCentsToDecimal < ActiveRecord::Migration[7.0] def up - change_column :fees, :unit_amount_cents, :decimal, precision: 30, scale: 5, null: false + safety_assured do + change_column :fees, :unit_amount_cents, :decimal, precision: 30, scale: 5, null: false + end end def down diff --git a/db/migrate/20231027144605_add_precise_unit_amount_to_fees.rb b/db/migrate/20231027144605_add_precise_unit_amount_to_fees.rb index babc935b46c7..f9e727087ea2 100644 --- a/db/migrate/20231027144605_add_precise_unit_amount_to_fees.rb +++ b/db/migrate/20231027144605_add_precise_unit_amount_to_fees.rb @@ -6,8 +6,10 @@ class Fee < ApplicationRecord end def up - change_table :fees, bulk: true do |t| - t.change :unit_amount_cents, :bigint, null: false, default: 0 + safety_assured do + change_table :fees, bulk: true do |t| + t.change :unit_amount_cents, :bigint, null: false, default: 0 + end end add_column :fees, :precise_unit_amount, :decimal, precision: 30, scale: 5, null: false, default: '0.0' diff --git a/db/migrate/20231102085146_add_missing_indexes_to_subscriptions_and_plans.rb b/db/migrate/20231102085146_add_missing_indexes_to_subscriptions_and_plans.rb index 51bbe7c67e37..b206a25d4bae 100644 --- a/db/migrate/20231102085146_add_missing_indexes_to_subscriptions_and_plans.rb +++ b/db/migrate/20231102085146_add_missing_indexes_to_subscriptions_and_plans.rb @@ -2,9 +2,11 @@ class AddMissingIndexesToSubscriptionsAndPlans < ActiveRecord::Migration[7.0] def change - add_index :subscriptions, :started_at - add_index :subscriptions, :status + safety_assured do + add_index :subscriptions, :started_at + add_index :subscriptions, :status - add_index :plans, :created_at + add_index :plans, :created_at + end end end diff --git a/db/migrate/20231102141929_add_index_on_event_external_subscription_id.rb b/db/migrate/20231102141929_add_index_on_event_external_subscription_id.rb index 95719967c825..0051fee97883 100644 --- a/db/migrate/20231102141929_add_index_on_event_external_subscription_id.rb +++ b/db/migrate/20231102141929_add_index_on_event_external_subscription_id.rb @@ -4,11 +4,13 @@ class AddIndexOnEventExternalSubscriptionId < ActiveRecord::Migration[7.0] disable_ddl_transaction! def change - add_index( - :events, - %i[organization_id external_subscription_id code timestamp], - name: 'index_events_on_external_subscription_id_and_code_and_timestamp', - where: '(deleted_at IS NULL)' - ) + safety_assured do + add_index( + :events, + %i[organization_id external_subscription_id code timestamp], + name: 'index_events_on_external_subscription_id_and_code_and_timestamp', + where: '(deleted_at IS NULL)' + ) + end end end diff --git a/db/migrate/20231102154537_change_precision_to_precise_unit_amount.rb b/db/migrate/20231102154537_change_precision_to_precise_unit_amount.rb index 8c6ed2abdc4c..519374c54705 100644 --- a/db/migrate/20231102154537_change_precision_to_precise_unit_amount.rb +++ b/db/migrate/20231102154537_change_precision_to_precise_unit_amount.rb @@ -2,8 +2,10 @@ class ChangePrecisionToPreciseUnitAmount < ActiveRecord::Migration[7.0] def up - change_table :fees, bulk: true do |t| - t.change :precise_unit_amount, :decimal, precision: 30, scale: 15, null: false, default: '0.0' + safety_assured do + change_table :fees, bulk: true do |t| + t.change :precise_unit_amount, :decimal, precision: 30, scale: 15, null: false, default: '0.0' + end end end diff --git a/db/migrate/20231106145424_delete_versions_for_group_properties.rb b/db/migrate/20231106145424_delete_versions_for_group_properties.rb index c0db7bf0a06d..fb2094c06f9c 100644 --- a/db/migrate/20231106145424_delete_versions_for_group_properties.rb +++ b/db/migrate/20231106145424_delete_versions_for_group_properties.rb @@ -4,10 +4,12 @@ class DeleteVersionsForGroupProperties < ActiveRecord::Migration[7.0] def change reversible do |dir| dir.up do - execute <<-SQL + safety_assured do + execute <<-SQL DELETE FROM VERSIONS WHERE item_type = 'GroupProperty'; - SQL + SQL + end end end end diff --git a/db/migrate/20231107110809_undiscard_incorrectly_deleted_events.rb b/db/migrate/20231107110809_undiscard_incorrectly_deleted_events.rb index 78d335f4ccf1..8ea9b676a6bb 100644 --- a/db/migrate/20231107110809_undiscard_incorrectly_deleted_events.rb +++ b/db/migrate/20231107110809_undiscard_incorrectly_deleted_events.rb @@ -4,7 +4,8 @@ class UndiscardIncorrectlyDeletedEvents < ActiveRecord::Migration[7.0] def change reversible do |dir| dir.up do - execute <<-SQL + safety_assured do + execute <<-SQL WITH discarded_events AS ( SELECT events.id AS event_id FROM events @@ -21,7 +22,8 @@ def change SET deleted_at = NULL FROM discarded_events WHERE discarded_events.event_id = events.id - SQL + SQL + end end end end diff --git a/db/migrate/20231109154934_add_events_validation_index.rb b/db/migrate/20231109154934_add_events_validation_index.rb index 447b6862bc49..9f2d8dd7fa96 100644 --- a/db/migrate/20231109154934_add_events_validation_index.rb +++ b/db/migrate/20231109154934_add_events_validation_index.rb @@ -4,6 +4,8 @@ class AddEventsValidationIndex < ActiveRecord::Migration[7.0] disable_ddl_transaction! def change - add_index :events, %i[organization_id code created_at], where: 'deleted_at IS NULL' + safety_assured do + add_index :events, %i[organization_id code created_at], where: 'deleted_at IS NULL' + end end end diff --git a/db/migrate/20231117123744_add_organization_id_to_quantified_events.rb b/db/migrate/20231117123744_add_organization_id_to_quantified_events.rb index 660d22f09ab6..16834703a06a 100644 --- a/db/migrate/20231117123744_add_organization_id_to_quantified_events.rb +++ b/db/migrate/20231117123744_add_organization_id_to_quantified_events.rb @@ -2,20 +2,22 @@ class AddOrganizationIdToQuantifiedEvents < ActiveRecord::Migration[7.0] def up - add_reference :quantified_events, :organization, type: :uuid, null: true, index: true, foreign_key: true + safety_assured do + add_reference :quantified_events, :organization, type: :uuid, null: true, index: true, foreign_key: true - execute <<-SQL + execute <<-SQL UPDATE quantified_events SET organization_id = customers.organization_id FROM customers WHERE quantified_events.customer_id = customers.id - SQL + SQL - change_column_null :quantified_events, :organization_id, false - remove_column :quantified_events, :customer_id - add_index :quantified_events, - %i[organization_id external_subscription_id billable_metric_id], - name: 'index_search_quantified_events' + change_column_null :quantified_events, :organization_id, false + remove_column :quantified_events, :customer_id + add_index :quantified_events, + %i[organization_id external_subscription_id billable_metric_id], + name: 'index_search_quantified_events' + end end def down diff --git a/db/migrate/20231123095209_add_unique_index_on_invoice_subscriptions.rb b/db/migrate/20231123095209_add_unique_index_on_invoice_subscriptions.rb index 8ae9cdd7f3b8..8416c009ad12 100644 --- a/db/migrate/20231123095209_add_unique_index_on_invoice_subscriptions.rb +++ b/db/migrate/20231123095209_add_unique_index_on_invoice_subscriptions.rb @@ -2,6 +2,8 @@ class AddUniqueIndexOnInvoiceSubscriptions < ActiveRecord::Migration[7.0] def change - add_index :invoice_subscriptions, %i[invoice_id subscription_id], unique: true, where: "created_at >= '2023-11-23'" + safety_assured do + add_index :invoice_subscriptions, %i[invoice_id subscription_id], unique: true, where: "created_at >= '2023-11-23'" + end end end diff --git a/db/migrate/20231123105540_add_fields_for_new_document_numbering.rb b/db/migrate/20231123105540_add_fields_for_new_document_numbering.rb index 4684ce6cd9c8..d62351029803 100644 --- a/db/migrate/20231123105540_add_fields_for_new_document_numbering.rb +++ b/db/migrate/20231123105540_add_fields_for_new_document_numbering.rb @@ -2,21 +2,22 @@ class AddFieldsForNewDocumentNumbering < ActiveRecord::Migration[7.0] def change - change_table :organizations, bulk: true do |t| - t.integer :document_numbering, default: 0, null: false - t.string :document_number_prefix, null: true - end - add_column :invoices, :organization_sequential_id, :integer, null: false, default: 0 + safety_assured do + change_table :organizations, bulk: true do |t| + t.integer :document_numbering, default: 0, null: false + t.string :document_number_prefix, null: true + end + add_column :invoices, :organization_sequential_id, :integer, null: false, default: 0 - reversible do |dir| - dir.up do - execute <<-SQL + reversible do |dir| + dir.up do + execute <<-SQL UPDATE organizations SET document_number_prefix = UPPER(CAST(SUBSTRING(name, 1, 3) AS VARCHAR)) || '-' || UPPER(CAST(RIGHT(id::TEXT, 4) AS VARCHAR)) WHERE document_number_prefix IS NULL; - SQL + SQL - execute <<-SQL + execute <<-SQL WITH ordered_organization_invoices AS ( SELECT invoices.id AS invoice_id, @@ -29,10 +30,11 @@ def change FROM ordered_organization_invoices WHERE invoices.organization_sequential_id = 0 AND ordered_organization_invoices.invoice_id = invoices.id; - SQL + SQL + end end - end - change_column_null :organizations, :document_number_prefix, null: false + change_column_null :organizations, :document_number_prefix, null: false + end end end diff --git a/db/migrate/20231201091348_add_code_and_name_to_payment_providers.rb b/db/migrate/20231201091348_add_code_and_name_to_payment_providers.rb index 6d7bb019cc6e..2bfbd72dd540 100644 --- a/db/migrate/20231201091348_add_code_and_name_to_payment_providers.rb +++ b/db/migrate/20231201091348_add_code_and_name_to_payment_providers.rb @@ -2,12 +2,13 @@ class AddCodeAndNameToPaymentProviders < ActiveRecord::Migration[7.0] def up - change_table :payment_providers, bulk: true do |t| - t.column :code, :string - t.column :name, :string - end + safety_assured do + change_table :payment_providers, bulk: true do |t| + t.column :code, :string + t.column :name, :string + end - execute <<-SQL + execute <<-SQL UPDATE payment_providers SET name = ( @@ -22,11 +23,12 @@ def up WHEN type = 'PaymentProviders::StripeProvider' THEN 'stripe_account_1' END ) - SQL + SQL - change_column_null :payment_providers, :code, false - change_column_null :payment_providers, :name, false - add_index :payment_providers, %i[code organization_id], unique: true + change_column_null :payment_providers, :code, false + change_column_null :payment_providers, :name, false + add_index :payment_providers, %i[code organization_id], unique: true + end end def down diff --git a/db/migrate/20231204131333_add_payment_provider_code_to_customers.rb b/db/migrate/20231204131333_add_payment_provider_code_to_customers.rb index ae41de83bf31..bfc757406262 100644 --- a/db/migrate/20231204131333_add_payment_provider_code_to_customers.rb +++ b/db/migrate/20231204131333_add_payment_provider_code_to_customers.rb @@ -3,8 +3,8 @@ class AddPaymentProviderCodeToCustomers < ActiveRecord::Migration[7.0] def up add_column :customers, :payment_provider_code, :string - - execute <<-SQL + safety_assured do + execute <<-SQL UPDATE customers SET payment_provider_code = ( CASE WHEN payment_provider = 'adyen' THEN 'adyen_account_1' @@ -12,7 +12,8 @@ def up WHEN payment_provider = 'stripe' THEN 'stripe_account_1' END ) - SQL + SQL + end end def down diff --git a/db/migrate/20231205153156_remove_events_quantified_events_relation.rb b/db/migrate/20231205153156_remove_events_quantified_events_relation.rb index 43de7b668c5a..9a4cbf094e9f 100644 --- a/db/migrate/20231205153156_remove_events_quantified_events_relation.rb +++ b/db/migrate/20231205153156_remove_events_quantified_events_relation.rb @@ -2,7 +2,9 @@ class RemoveEventsQuantifiedEventsRelation < ActiveRecord::Migration[7.0] def up - remove_column :events, :quantified_event_id + safety_assured do + remove_column :events, :quantified_event_id + end end def down diff --git a/db/migrate/20231214133638_add_unique_index_on_invoice_organization_sequential_id.rb b/db/migrate/20231214133638_add_unique_index_on_invoice_organization_sequential_id.rb index 649b7686031d..34f6ee34c239 100644 --- a/db/migrate/20231214133638_add_unique_index_on_invoice_organization_sequential_id.rb +++ b/db/migrate/20231214133638_add_unique_index_on_invoice_organization_sequential_id.rb @@ -4,10 +4,12 @@ class AddUniqueIndexOnInvoiceOrganizationSequentialId < ActiveRecord::Migration[ disable_ddl_transaction! def change - add_index :invoices, - "organization_id, organization_sequential_id, date_trunc('month'::text, created_at)", - name: 'unique_organization_sequential_id', - unique: true, - where: 'organization_sequential_id != 0' + safety_assured do + add_index :invoices, + "organization_id, organization_sequential_id, date_trunc('month'::text, created_at)", + name: 'unique_organization_sequential_id', + unique: true, + where: 'organization_sequential_id != 0' + end end end diff --git a/db/migrate/20231220115621_update_organization_sequential_id_index.rb b/db/migrate/20231220115621_update_organization_sequential_id_index.rb index 76ef507c2817..28c1663111c0 100644 --- a/db/migrate/20231220115621_update_organization_sequential_id_index.rb +++ b/db/migrate/20231220115621_update_organization_sequential_id_index.rb @@ -4,13 +4,15 @@ class UpdateOrganizationSequentialIdIndex < ActiveRecord::Migration[7.0] disable_ddl_transaction! def change - remove_index :invoices, - "organization_id, organization_sequential_id, date_trunc('month'::text, created_at)", - name: :unique_organization_sequential_id - add_index :invoices, - "organization_id, organization_sequential_id, (date_trunc('month', created_at)::date)", - name: 'unique_organization_sequential_id', - unique: true, - where: 'organization_sequential_id != 0' + safety_assured do + remove_index :invoices, + "organization_id, organization_sequential_id, date_trunc('month'::text, created_at)", + name: :unique_organization_sequential_id + add_index :invoices, + "organization_id, organization_sequential_id, (date_trunc('month', created_at)::date)", + name: 'unique_organization_sequential_id', + unique: true, + where: 'organization_sequential_id != 0' + end end end diff --git a/db/migrate/20240103125624_add_group_id_to_adjusted_fees.rb b/db/migrate/20240103125624_add_group_id_to_adjusted_fees.rb index 895f19ab51f5..c281fd27ef8e 100644 --- a/db/migrate/20240103125624_add_group_id_to_adjusted_fees.rb +++ b/db/migrate/20240103125624_add_group_id_to_adjusted_fees.rb @@ -2,6 +2,8 @@ class AddGroupIdToAdjustedFees < ActiveRecord::Migration[7.0] def change - add_reference :adjusted_fees, :group, type: :uuid, null: true, index: true, foreign_key: true + safety_assured do + add_reference :adjusted_fees, :group, type: :uuid, null: true, index: true, foreign_key: true + end end end diff --git a/db/migrate/20240112091706_add_charge_filter_id_to_fees.rb b/db/migrate/20240112091706_add_charge_filter_id_to_fees.rb index f1389315e963..42777ccaf000 100644 --- a/db/migrate/20240112091706_add_charge_filter_id_to_fees.rb +++ b/db/migrate/20240112091706_add_charge_filter_id_to_fees.rb @@ -3,6 +3,8 @@ class AddChargeFilterIdToFees < ActiveRecord::Migration[7.0] def change add_column :fees, :charge_filter_id, :uuid, null: true - add_index :fees, :charge_filter_id + safety_assured do + add_index :fees, :charge_filter_id + end end end diff --git a/db/migrate/20240118135350_remove_credits_auto_refreshed_from_organizations.rb b/db/migrate/20240118135350_remove_credits_auto_refreshed_from_organizations.rb index 5c8cf8216535..83fe7cb44efd 100644 --- a/db/migrate/20240118135350_remove_credits_auto_refreshed_from_organizations.rb +++ b/db/migrate/20240118135350_remove_credits_auto_refreshed_from_organizations.rb @@ -2,14 +2,16 @@ class RemoveCreditsAutoRefreshedFromOrganizations < ActiveRecord::Migration[7.0] def change - remove_column :organizations, :credits_auto_refreshed, :boolean + safety_assured do + remove_column :organizations, :credits_auto_refreshed, :boolean - change_table :wallets, bulk: true do |t| - t.bigint :ongoing_balance_cents, default: 0, null: false - t.bigint :ongoing_usage_balance_cents, default: 0, null: false + change_table :wallets, bulk: true do |t| + t.bigint :ongoing_balance_cents, default: 0, null: false + t.bigint :ongoing_usage_balance_cents, default: 0, null: false - t.decimal :credits_ongoing_balance, precision: 30, scale: 5, default: '0.0', null: false - t.decimal :credits_ongoing_usage_balance, precision: 30, scale: 5, default: '0.0', null: false + t.decimal :credits_ongoing_balance, precision: 30, scale: 5, default: '0.0', null: false + t.decimal :credits_ongoing_usage_balance, precision: 30, scale: 5, default: '0.0', null: false + end end end end diff --git a/db/migrate/20240123104811_change_fees_grouped_by_type.rb b/db/migrate/20240123104811_change_fees_grouped_by_type.rb index 5ae40718d977..4f8c5018600a 100644 --- a/db/migrate/20240123104811_change_fees_grouped_by_type.rb +++ b/db/migrate/20240123104811_change_fees_grouped_by_type.rb @@ -2,14 +2,16 @@ class ChangeFeesGroupedByType < ActiveRecord::Migration[7.0] def up - change_table :fees, bulk: true do |t| - t.remove :grouped_by - t.jsonb :grouped_by, null: false, default: {} - end + safety_assured do + change_table :fees, bulk: true do |t| + t.remove :grouped_by + t.jsonb :grouped_by, null: false, default: {} + end - change_table :cached_aggregations, bulk: true do |t| - t.remove :grouped_by - t.jsonb :grouped_by, null: false, default: {} + change_table :cached_aggregations, bulk: true do |t| + t.remove :grouped_by + t.jsonb :grouped_by, null: false, default: {} + end end end diff --git a/db/migrate/20240301133006_add_charge_filter_id_to_cached_aggregations.rb b/db/migrate/20240301133006_add_charge_filter_id_to_cached_aggregations.rb index c87dd65d9ac3..94a47f72b0d2 100644 --- a/db/migrate/20240301133006_add_charge_filter_id_to_cached_aggregations.rb +++ b/db/migrate/20240301133006_add_charge_filter_id_to_cached_aggregations.rb @@ -3,8 +3,10 @@ class AddChargeFilterIdToCachedAggregations < ActiveRecord::Migration[7.0] def change add_column :cached_aggregations, :charge_filter_id, :uuid, null: true - add_index :cached_aggregations, - %i[organization_id timestamp charge_id charge_filter_id], - name: :index_timestamp_filter_lookup + safety_assured do + add_index :cached_aggregations, + %i[organization_id timestamp charge_id charge_filter_id], + name: :index_timestamp_filter_lookup + end end end diff --git a/db/migrate/20240305164449_add_missing_charge_filter_id_relations.rb b/db/migrate/20240305164449_add_missing_charge_filter_id_relations.rb index 70e8ea2c0259..a732dffbcb2a 100644 --- a/db/migrate/20240305164449_add_missing_charge_filter_id_relations.rb +++ b/db/migrate/20240305164449_add_missing_charge_filter_id_relations.rb @@ -3,9 +3,13 @@ class AddMissingChargeFilterIdRelations < ActiveRecord::Migration[7.0] def change add_column :quantified_events, :charge_filter_id, :uuid, null: true - add_index :quantified_events, :charge_filter_id + safety_assured do + add_index :quantified_events, :charge_filter_id + end add_column :adjusted_fees, :charge_filter_id, :uuid, null: true - add_index :adjusted_fees, :charge_filter_id + safety_assured do + add_index :adjusted_fees, :charge_filter_id + end end end diff --git a/db/migrate/20240308150801_update_last_hour_events_mv.rb b/db/migrate/20240308150801_update_last_hour_events_mv.rb index 47d6ccff5e8d..f19d6428b585 100644 --- a/db/migrate/20240308150801_update_last_hour_events_mv.rb +++ b/db/migrate/20240308150801_update_last_hour_events_mv.rb @@ -4,6 +4,8 @@ class UpdateLastHourEventsMv < ActiveRecord::Migration[7.0] def change drop_view :last_hour_events_mv, materialized: true create_view :last_hour_events_mv, materialized: {no_data: true}, version: 3 - add_index :last_hour_events_mv, :organization_id + safety_assured do + add_index :last_hour_events_mv, :organization_id + end end end diff --git a/db/migrate/20240311091817_add_index_on_last_hour_events_mv.rb b/db/migrate/20240311091817_add_index_on_last_hour_events_mv.rb index e417ecb96fc0..f7ea4a313b69 100644 --- a/db/migrate/20240311091817_add_index_on_last_hour_events_mv.rb +++ b/db/migrate/20240311091817_add_index_on_last_hour_events_mv.rb @@ -4,6 +4,8 @@ class AddIndexOnLastHourEventsMv < ActiveRecord::Migration[7.0] disable_ddl_transaction! def change - add_index :last_hour_events_mv, :organization_id, if_not_exists: true + safety_assured do + add_index :last_hour_events_mv, :organization_id, if_not_exists: true + end end end diff --git a/db/migrate/20240312141641_add_new_index_to_groups.rb b/db/migrate/20240312141641_add_new_index_to_groups.rb index b4cfba4ffccb..1632d372fc84 100644 --- a/db/migrate/20240312141641_add_new_index_to_groups.rb +++ b/db/migrate/20240312141641_add_new_index_to_groups.rb @@ -4,6 +4,8 @@ class AddNewIndexToGroups < ActiveRecord::Migration[7.0] disable_ddl_transaction! def change - add_index :groups, %w[billable_metric_id parent_group_id] + safety_assured do + add_index :groups, %w[billable_metric_id parent_group_id] + end end end diff --git a/db/migrate/20240314163426_add_multiple_values_to_charge_filter_values.rb b/db/migrate/20240314163426_add_multiple_values_to_charge_filter_values.rb index bcf7bebf2f94..db3459ff649c 100644 --- a/db/migrate/20240314163426_add_multiple_values_to_charge_filter_values.rb +++ b/db/migrate/20240314163426_add_multiple_values_to_charge_filter_values.rb @@ -2,9 +2,11 @@ class AddMultipleValuesToChargeFilterValues < ActiveRecord::Migration[7.0] def up - change_table :charge_filter_values, bulk: true do |t| - t.remove :value - t.string :values, array: true, default: [], null: false + safety_assured do + change_table :charge_filter_values, bulk: true do |t| + t.remove :value + t.string :values, array: true, default: [], null: false + end end end diff --git a/db/migrate/20240327071539_add_payment_disputed_to_invoices.rb b/db/migrate/20240327071539_add_payment_disputed_to_invoices.rb index 82acca68c78b..1f375dc2c77b 100644 --- a/db/migrate/20240327071539_add_payment_disputed_to_invoices.rb +++ b/db/migrate/20240327071539_add_payment_disputed_to_invoices.rb @@ -2,8 +2,10 @@ class AddPaymentDisputedToInvoices < ActiveRecord::Migration[7.0] def change - change_table :invoices, bulk: true do |t| - t.datetime :payment_dispute_lost_at, default: nil + safety_assured do + change_table :invoices, bulk: true do |t| + t.datetime :payment_dispute_lost_at, default: nil + end end end end diff --git a/db/migrate/20240328153701_remove_unique_count_quantified_events.rb b/db/migrate/20240328153701_remove_unique_count_quantified_events.rb index 7f78ec383062..07ca9b1cb5a9 100644 --- a/db/migrate/20240328153701_remove_unique_count_quantified_events.rb +++ b/db/migrate/20240328153701_remove_unique_count_quantified_events.rb @@ -10,8 +10,9 @@ def up WHERE aggregation_type = 3 ) SQL - - execute(sql) + safety_assured do + execute(sql) + end end def down diff --git a/db/migrate/20240412133335_add_invoice_type_to_invoice_subscriptions.rb b/db/migrate/20240412133335_add_invoice_type_to_invoice_subscriptions.rb index fa1f9380d66b..3bf25b172f33 100644 --- a/db/migrate/20240412133335_add_invoice_type_to_invoice_subscriptions.rb +++ b/db/migrate/20240412133335_add_invoice_type_to_invoice_subscriptions.rb @@ -10,17 +10,19 @@ def change in_advance_charge ] - change_table :invoice_subscriptions do |t| - t.enum :invoicing_reason, enum_type: 'subscription_invoicing_reason', null: true + safety_assured do + change_table :invoice_subscriptions do |t| + t.enum :invoicing_reason, enum_type: 'subscription_invoicing_reason', null: true - t.index %w[subscription_id invoicing_reason], - unique: true, - where: "invoicing_reason = 'subscription_starting'", - name: 'index_unique_starting_subscription_invoice' - t.index %w[subscription_id invoicing_reason], - unique: true, - where: "invoicing_reason = 'subscription_terminating'", - name: 'index_unique_terminating_subscription_invoice' + t.index %w[subscription_id invoicing_reason], + unique: true, + where: "invoicing_reason = 'subscription_starting'", + name: 'index_unique_starting_subscription_invoice' + t.index %w[subscription_id invoicing_reason], + unique: true, + where: "invoicing_reason = 'subscription_terminating'", + name: 'index_unique_terminating_subscription_invoice' + end end end end diff --git a/db/migrate/20240419071607_add_transaction_status_to_wallet_transactions.rb b/db/migrate/20240419071607_add_transaction_status_to_wallet_transactions.rb index 02a91a90259c..3634ebb909ee 100644 --- a/db/migrate/20240419071607_add_transaction_status_to_wallet_transactions.rb +++ b/db/migrate/20240419071607_add_transaction_status_to_wallet_transactions.rb @@ -7,12 +7,14 @@ def change reversible do |dir| dir.up do # Set existing wallet transactions as granted if no invoices linked and status is settled. - execute <<-SQL + safety_assured do + execute <<-SQL UPDATE wallet_transactions SET transaction_status = 1 -- granted WHERE invoice_id IS NULL AND status = 1; -- settled - SQL + SQL + end end end end diff --git a/db/migrate/20240423155113_add_organization_timestamp_index_on_events.rb b/db/migrate/20240423155113_add_organization_timestamp_index_on_events.rb index 4325ddebfd57..d23bfdccfb4a 100644 --- a/db/migrate/20240423155113_add_organization_timestamp_index_on_events.rb +++ b/db/migrate/20240423155113_add_organization_timestamp_index_on_events.rb @@ -4,6 +4,8 @@ class AddOrganizationTimestampIndexOnEvents < ActiveRecord::Migration[7.0] disable_ddl_transaction! def change - add_index :events, %i[organization_id timestamp], where: 'deleted_at IS NULL' + safety_assured do + add_index :events, %i[organization_id timestamp], where: 'deleted_at IS NULL' + end end end diff --git a/db/migrate/20240424110420_rename_integration_items_columns.rb b/db/migrate/20240424110420_rename_integration_items_columns.rb index 86fafadede5c..b9cade44a8a3 100644 --- a/db/migrate/20240424110420_rename_integration_items_columns.rb +++ b/db/migrate/20240424110420_rename_integration_items_columns.rb @@ -2,7 +2,9 @@ class RenameIntegrationItemsColumns < ActiveRecord::Migration[7.0] def change - rename_column :integration_items, :name, :external_name - rename_column :integration_items, :account_code, :external_account_code + safety_assured do + rename_column :integration_items, :name, :external_name + rename_column :integration_items, :account_code, :external_account_code + end end end diff --git a/db/migrate/20240426143059_add_integration_item_uniqueness_index.rb b/db/migrate/20240426143059_add_integration_item_uniqueness_index.rb index 6472d0cc8493..031cf93fa646 100644 --- a/db/migrate/20240426143059_add_integration_item_uniqueness_index.rb +++ b/db/migrate/20240426143059_add_integration_item_uniqueness_index.rb @@ -2,6 +2,8 @@ class AddIntegrationItemUniquenessIndex < ActiveRecord::Migration[7.0] def change - add_index :integration_items, [:external_id, :integration_id], unique: true + safety_assured do + add_index :integration_items, [:external_id, :integration_id], unique: true + end end end diff --git a/db/migrate/20240430133150_add_invoiced_to_transaction_status.rb b/db/migrate/20240430133150_add_invoiced_to_transaction_status.rb index 72544a7c4933..d71bbd5ea861 100644 --- a/db/migrate/20240430133150_add_invoiced_to_transaction_status.rb +++ b/db/migrate/20240430133150_add_invoiced_to_transaction_status.rb @@ -5,11 +5,13 @@ def change reversible do |dir| dir.up do # Set existing wallet transactions as invoiced when transaction_type is outbound. - execute <<-SQL + safety_assured do + execute <<-SQL UPDATE wallet_transactions SET transaction_status = 3 -- invoiced WHERE transaction_type = 1; -- outbound - SQL + SQL + end end end end diff --git a/db/migrate/20240502075803_add_filters_missing_indexes.rb b/db/migrate/20240502075803_add_filters_missing_indexes.rb index 9fd37d179b5c..b7947315ea58 100644 --- a/db/migrate/20240502075803_add_filters_missing_indexes.rb +++ b/db/migrate/20240502075803_add_filters_missing_indexes.rb @@ -4,14 +4,16 @@ class AddFiltersMissingIndexes < ActiveRecord::Migration[7.0] disable_ddl_transaction! def change - add_index :charge_filters, :charge_id, where: 'deleted_at IS NULL', name: 'index_active_charge_filters' - add_index :charge_filter_values, - :charge_filter_id, - where: 'deleted_at IS NULL', - name: 'index_active_charge_filter_values' - add_index :billable_metric_filters, - :billable_metric_id, - where: 'deleted_at IS NULL', - name: 'index_active_metric_filters' + safety_assured do + add_index :charge_filters, :charge_id, where: 'deleted_at IS NULL', name: 'index_active_charge_filters' + add_index :charge_filter_values, + :charge_filter_id, + where: 'deleted_at IS NULL', + name: 'index_active_charge_filter_values' + add_index :billable_metric_filters, + :billable_metric_id, + where: 'deleted_at IS NULL', + name: 'index_active_metric_filters' + end end end diff --git a/db/migrate/20240502095122_add_indexes_to_subscriptions.rb b/db/migrate/20240502095122_add_indexes_to_subscriptions.rb index bfd19de1a8dc..ef0828f6d69a 100644 --- a/db/migrate/20240502095122_add_indexes_to_subscriptions.rb +++ b/db/migrate/20240502095122_add_indexes_to_subscriptions.rb @@ -4,7 +4,9 @@ class AddIndexesToSubscriptions < ActiveRecord::Migration[7.0] disable_ddl_transaction! def change - add_index :subscriptions, [:started_at, :ending_at] + safety_assured do + add_index :subscriptions, [:started_at, :ending_at] + end add_index :invoice_subscriptions, [:subscription_id, :from_datetime, :to_datetime], name: 'index_invoice_subscriptions_boundaries', diff --git a/db/migrate/20240514072741_add_method_to_recurring_transaction_rules.rb b/db/migrate/20240514072741_add_method_to_recurring_transaction_rules.rb index 7c45257076fc..f6d5b0b8bb7d 100644 --- a/db/migrate/20240514072741_add_method_to_recurring_transaction_rules.rb +++ b/db/migrate/20240514072741_add_method_to_recurring_transaction_rules.rb @@ -2,7 +2,9 @@ class AddMethodToRecurringTransactionRules < ActiveRecord::Migration[7.0] def change - rename_column :recurring_transaction_rules, :rule_type, :trigger + safety_assured do + rename_column :recurring_transaction_rules, :rule_type, :trigger + end add_column :recurring_transaction_rules, :method, :integer, null: false, default: 0 end end diff --git a/db/migrate/20240520115450_add_integration_id_to_integration_resources.rb b/db/migrate/20240520115450_add_integration_id_to_integration_resources.rb index c6a4d9e9acda..f71b95627b98 100644 --- a/db/migrate/20240520115450_add_integration_id_to_integration_resources.rb +++ b/db/migrate/20240520115450_add_integration_id_to_integration_resources.rb @@ -4,12 +4,14 @@ class AddIntegrationIdToIntegrationResources < ActiveRecord::Migration[7.0] def up remove_index :integration_items, [:external_id, :integration_id] - add_index :integration_items, - [:external_id, :integration_id, :item_type], - name: :index_int_items_on_external_id_and_int_id_and_type, - unique: true + safety_assured do + add_index :integration_items, + [:external_id, :integration_id, :item_type], + name: :index_int_items_on_external_id_and_int_id_and_type, + unique: true - add_reference :integration_resources, :integration, type: :uuid, foreign_key: true + add_reference :integration_resources, :integration, type: :uuid, foreign_key: true + end end def down diff --git a/db/migrate/20240530123427_add_started_at_to_recurring_transaction_rules.rb b/db/migrate/20240530123427_add_started_at_to_recurring_transaction_rules.rb index e0358910b83b..88223138d018 100644 --- a/db/migrate/20240530123427_add_started_at_to_recurring_transaction_rules.rb +++ b/db/migrate/20240530123427_add_started_at_to_recurring_transaction_rules.rb @@ -3,6 +3,8 @@ class AddStartedAtToRecurringTransactionRules < ActiveRecord::Migration[7.0] def change add_column :recurring_transaction_rules, :started_at, :datetime - add_index :recurring_transaction_rules, :started_at + safety_assured do + add_index :recurring_transaction_rules, :started_at + end end end diff --git a/db/migrate/20240607095155_add_event_transaction_id_to_cached_aggregations.rb b/db/migrate/20240607095155_add_event_transaction_id_to_cached_aggregations.rb index 6cdc64789743..b788ee38bf80 100644 --- a/db/migrate/20240607095155_add_event_transaction_id_to_cached_aggregations.rb +++ b/db/migrate/20240607095155_add_event_transaction_id_to_cached_aggregations.rb @@ -3,6 +3,8 @@ class AddEventTransactionIdToCachedAggregations < ActiveRecord::Migration[7.0] def change add_column :cached_aggregations, :event_transaction_id, :string - add_index :cached_aggregations, %i[organization_id event_transaction_id], name: 'index_cached_aggregations_on_event_transaction_id' + safety_assured do + add_index :cached_aggregations, %i[organization_id event_transaction_id], name: 'index_cached_aggregations_on_event_transaction_id' + end end end diff --git a/db/migrate/20240611074215_add_payment_overdue_to_invoices.rb b/db/migrate/20240611074215_add_payment_overdue_to_invoices.rb index 02781ef8d407..ef846c6cc8b4 100644 --- a/db/migrate/20240611074215_add_payment_overdue_to_invoices.rb +++ b/db/migrate/20240611074215_add_payment_overdue_to_invoices.rb @@ -7,14 +7,16 @@ def change reversible do |dir| dir.up do # Set existing invoices as payment_overdue - execute <<-SQL + safety_assured do + execute <<-SQL UPDATE invoices SET payment_overdue = true WHERE status = 1 -- finalized AND payment_status != 1 -- not succeeded AND payment_dispute_lost_at IS NULL -- not lost dispute AND payment_due_date < NOW(); -- due date is in the past - SQL + SQL + end end end end diff --git a/db/migrate/20240619082054_add_in_advance_charge_periodic_invoicing_reason.rb b/db/migrate/20240619082054_add_in_advance_charge_periodic_invoicing_reason.rb index 36b2643d0b1f..6c4776d426d2 100644 --- a/db/migrate/20240619082054_add_in_advance_charge_periodic_invoicing_reason.rb +++ b/db/migrate/20240619082054_add_in_advance_charge_periodic_invoicing_reason.rb @@ -3,9 +3,11 @@ class AddInAdvanceChargePeriodicInvoicingReason < ActiveRecord::Migration[7.0] disable_ddl_transaction! def up - execute <<-SQL + safety_assured do + execute <<-SQL ALTER TYPE subscription_invoicing_reason ADD VALUE IF NOT EXISTS 'in_advance_charge_periodic'; - SQL + SQL + end end def down diff --git a/db/migrate/20240626094521_add_shipping_address_to_customers.rb b/db/migrate/20240626094521_add_shipping_address_to_customers.rb index 02b6e78407b1..cd19c2f2827f 100644 --- a/db/migrate/20240626094521_add_shipping_address_to_customers.rb +++ b/db/migrate/20240626094521_add_shipping_address_to_customers.rb @@ -1,12 +1,14 @@ # frozen_string_literal: true class AddShippingAddressToCustomers < ActiveRecord::Migration[7.1] - change_table :customers, bulk: true do |t| - t.string :shipping_address_line1 - t.string :shipping_address_line2 - t.string :shipping_city - t.string :shipping_zipcode - t.string :shipping_state - t.string :shipping_country + safety_assured do + change_table :customers, bulk: true do |t| + t.string :shipping_address_line1 + t.string :shipping_address_line2 + t.string :shipping_city + t.string :shipping_zipcode + t.string :shipping_state + t.string :shipping_country + end end end diff --git a/db/migrate/20240628083654_add_membership_id_and_organization_id_to_data_exports.rb b/db/migrate/20240628083654_add_membership_id_and_organization_id_to_data_exports.rb index 66a37430b59d..ec7ad0bd8a18 100644 --- a/db/migrate/20240628083654_add_membership_id_and_organization_id_to_data_exports.rb +++ b/db/migrate/20240628083654_add_membership_id_and_organization_id_to_data_exports.rb @@ -2,7 +2,9 @@ class AddMembershipIdAndOrganizationIdToDataExports < ActiveRecord::Migration[7.1] def change - add_reference :data_exports, :membership, foreign_key: true, type: :uuid - add_reference :data_exports, :organization, foreign_key: true, type: :uuid + safety_assured do + add_reference :data_exports, :membership, foreign_key: true, type: :uuid + add_reference :data_exports, :organization, foreign_key: true, type: :uuid + end end end diff --git a/db/migrate/20240628083830_remove_user_id_from_data_exports.rb b/db/migrate/20240628083830_remove_user_id_from_data_exports.rb index 000e5340f930..9d44211986ae 100644 --- a/db/migrate/20240628083830_remove_user_id_from_data_exports.rb +++ b/db/migrate/20240628083830_remove_user_id_from_data_exports.rb @@ -2,6 +2,8 @@ class RemoveUserIdFromDataExports < ActiveRecord::Migration[7.1] def change - remove_reference :data_exports, :user, null: false, foreign_key: true, type: :uuid + safety_assured do + remove_reference :data_exports, :user, null: false, foreign_key: true, type: :uuid + end end end diff --git a/db/migrate/20240723150221_remove_integration_reference_from_error_detail.rb b/db/migrate/20240723150221_remove_integration_reference_from_error_detail.rb index 984728e1a1f5..9906d7b89f34 100644 --- a/db/migrate/20240723150221_remove_integration_reference_from_error_detail.rb +++ b/db/migrate/20240723150221_remove_integration_reference_from_error_detail.rb @@ -2,10 +2,12 @@ class RemoveIntegrationReferenceFromErrorDetail < ActiveRecord::Migration[7.1] def up - change_table :error_details, bulk: true do |t| - t.remove :error_code - t.remove_references :integration, polymorphic: true - t.integer :error_code, null: false, default: 0, index: true + safety_assured do + change_table :error_details, bulk: true do |t| + t.remove :error_code + t.remove_references :integration, polymorphic: true + t.integer :error_code, null: false, default: 0, index: true + end end end diff --git a/db/migrate/20240729151049_add_payable_type_and_payable_id_to_payments.rb b/db/migrate/20240729151049_add_payable_type_and_payable_id_to_payments.rb index 66ddb8d69af9..fdbf433f4172 100644 --- a/db/migrate/20240729151049_add_payable_type_and_payable_id_to_payments.rb +++ b/db/migrate/20240729151049_add_payable_type_and_payable_id_to_payments.rb @@ -2,20 +2,22 @@ class AddPayableTypeAndPayableIdToPayments < ActiveRecord::Migration[7.1] def change - change_table :payments, bulk: true do |t| - t.string :payable_type, null: false, default: "Invoice" - t.uuid :payable_id - end + safety_assured do + change_table :payments, bulk: true do |t| + t.string :payable_type, null: false, default: "Invoice" + t.uuid :payable_id + end - # invoice_id is now deprecated and will be removed in the future - change_column_null :payments, :invoice_id, true + # invoice_id is now deprecated and will be removed in the future + change_column_null :payments, :invoice_id, true - reversible do |dir| - dir.up do - execute <<-SQL + reversible do |dir| + dir.up do + execute <<-SQL UPDATE payments SET payable_id = invoice_id - SQL + SQL + end end end end diff --git a/db/migrate/20240801134832_create_payment_requests.rb b/db/migrate/20240801134832_create_payment_requests.rb index 1488a16c7f0e..52d13244a561 100644 --- a/db/migrate/20240801134832_create_payment_requests.rb +++ b/db/migrate/20240801134832_create_payment_requests.rb @@ -11,10 +11,11 @@ def change t.string :email, null: false t.timestamps end - - change_table :payments, bulk: true do |t| - t.uuid :payment_request_id + safety_assured do + change_table :payments, bulk: true do |t| + t.uuid :payment_request_id + end + add_foreign_key :payments, :payment_requests end - add_foreign_key :payments, :payment_requests end end diff --git a/db/migrate/20240801142242_alter_events_vacuum_settings.rb b/db/migrate/20240801142242_alter_events_vacuum_settings.rb index 0093ac1e3a73..06a7231b4ce1 100644 --- a/db/migrate/20240801142242_alter_events_vacuum_settings.rb +++ b/db/migrate/20240801142242_alter_events_vacuum_settings.rb @@ -2,7 +2,9 @@ class AlterEventsVacuumSettings < ActiveRecord::Migration[7.1] def up - execute "ALTER TABLE events set (autovacuum_vacuum_scale_factor=0.005)" + safety_assured do + execute "ALTER TABLE events set (autovacuum_vacuum_scale_factor=0.005)" + end end def down diff --git a/db/migrate/20240807072052_rename_progressive_billing_tresholds.rb b/db/migrate/20240807072052_rename_progressive_billing_tresholds.rb index 8920b57ee17c..354af9375406 100644 --- a/db/migrate/20240807072052_rename_progressive_billing_tresholds.rb +++ b/db/migrate/20240807072052_rename_progressive_billing_tresholds.rb @@ -2,6 +2,8 @@ class RenameProgressiveBillingTresholds < ActiveRecord::Migration[7.1] def change - rename_table :progressive_billing_tresholds, :usage_tresholds + safety_assured do + rename_table :progressive_billing_tresholds, :usage_tresholds + end end end diff --git a/db/migrate/20240807113700_rename_usage_tresholds.rb b/db/migrate/20240807113700_rename_usage_tresholds.rb index 4b9c521649cc..1dd57c366243 100644 --- a/db/migrate/20240807113700_rename_usage_tresholds.rb +++ b/db/migrate/20240807113700_rename_usage_tresholds.rb @@ -2,7 +2,9 @@ class RenameUsageTresholds < ActiveRecord::Migration[7.1] def change - rename_table :usage_tresholds, :usage_thresholds - rename_column :usage_thresholds, :treshold_display_name, :threshold_display_name + safety_assured do + rename_table :usage_tresholds, :usage_thresholds + rename_column :usage_thresholds, :treshold_display_name, :threshold_display_name + end end end diff --git a/db/migrate/20240808080611_remove_amount_currency_from_usage_tresholds.rb b/db/migrate/20240808080611_remove_amount_currency_from_usage_tresholds.rb index 8a0cdbed702a..38eb4a95467b 100644 --- a/db/migrate/20240808080611_remove_amount_currency_from_usage_tresholds.rb +++ b/db/migrate/20240808080611_remove_amount_currency_from_usage_tresholds.rb @@ -2,8 +2,10 @@ class RemoveAmountCurrencyFromUsageTresholds < ActiveRecord::Migration[7.1] def up - change_table :usage_thresholds, bulk: true do |t| - t.remove :amount_currency + safety_assured do + change_table :usage_thresholds, bulk: true do |t| + t.remove :amount_currency + end end end diff --git a/db/migrate/20240813095718_add_usage_threshold_id_to_fees.rb b/db/migrate/20240813095718_add_usage_threshold_id_to_fees.rb index 6462c4a38faa..11ac3da83408 100644 --- a/db/migrate/20240813095718_add_usage_threshold_id_to_fees.rb +++ b/db/migrate/20240813095718_add_usage_threshold_id_to_fees.rb @@ -2,6 +2,8 @@ class AddUsageThresholdIdToFees < ActiveRecord::Migration[7.1] def change - add_reference :fees, :usage_threshold, type: :uuid, foreign_key: true, index: true + safety_assured do + add_reference :fees, :usage_threshold, type: :uuid, foreign_key: true, index: true + end end end diff --git a/db/migrate/20240813121307_add_progressive_billing_to_invoicing_reason.rb b/db/migrate/20240813121307_add_progressive_billing_to_invoicing_reason.rb index aa50417a0124..86db9d11d2f4 100644 --- a/db/migrate/20240813121307_add_progressive_billing_to_invoicing_reason.rb +++ b/db/migrate/20240813121307_add_progressive_billing_to_invoicing_reason.rb @@ -4,9 +4,11 @@ class AddProgressiveBillingToInvoicingReason < ActiveRecord::Migration[7.1] disable_ddl_transaction! def up - execute <<-SQL + safety_assured do + execute <<-SQL ALTER TYPE subscription_invoicing_reason ADD VALUE IF NOT EXISTS 'progressive_billing'; - SQL + SQL + end end def down diff --git a/db/migrate/20240814144137_add_organization_id_to_payment_requests_and_payable_groups.rb b/db/migrate/20240814144137_add_organization_id_to_payment_requests_and_payable_groups.rb index aee7bc4a813c..ba31d3f7c796 100644 --- a/db/migrate/20240814144137_add_organization_id_to_payment_requests_and_payable_groups.rb +++ b/db/migrate/20240814144137_add_organization_id_to_payment_requests_and_payable_groups.rb @@ -2,18 +2,19 @@ class AddOrganizationIdToPaymentRequestsAndPayableGroups < ActiveRecord::Migration[7.1] def change - change_table :payment_requests, bulk: true do |t| - t.uuid :organization_id - end + safety_assured do + change_table :payment_requests, bulk: true do |t| + t.uuid :organization_id + end - change_table :payable_groups, bulk: true do |t| - t.uuid :organization_id - end + change_table :payable_groups, bulk: true do |t| + t.uuid :organization_id + end - # NOTE: Set organization_id for existing records based on customer_id - reversible do |dir| - dir.up do - execute <<-SQL + # NOTE: Set organization_id for existing records based on customer_id + reversible do |dir| + dir.up do + execute <<-SQL UPDATE payment_requests SET organization_id = ( SELECT organization_id @@ -27,15 +28,16 @@ def change FROM customers WHERE customers.id = payable_groups.customer_id ) - SQL + SQL + end end - end - change_column_null :payment_requests, :organization_id, false - change_column_null :payable_groups, :organization_id, false - add_index :payment_requests, :organization_id - add_index :payable_groups, :organization_id - add_foreign_key :payment_requests, :organizations - add_foreign_key :payable_groups, :organizations + change_column_null :payment_requests, :organization_id, false + change_column_null :payable_groups, :organization_id, false + add_index :payment_requests, :organization_id + add_index :payable_groups, :organization_id + add_foreign_key :payment_requests, :organizations + add_foreign_key :payable_groups, :organizations + end end end diff --git a/db/migrate/20240819092354_add_deleted_at_to_usage_thresholds.rb b/db/migrate/20240819092354_add_deleted_at_to_usage_thresholds.rb index 30ce72cf5c26..6ad50ee75fd7 100644 --- a/db/migrate/20240819092354_add_deleted_at_to_usage_thresholds.rb +++ b/db/migrate/20240819092354_add_deleted_at_to_usage_thresholds.rb @@ -7,7 +7,9 @@ def change remove_index :usage_thresholds, %i[amount_cents plan_id recurring], unique: true remove_index :usage_thresholds, %i[plan_id recurring], unique: true, where: "recurring is true" - add_index :usage_thresholds, %i[amount_cents plan_id recurring], unique: true, where: 'deleted_at IS NULL' - add_index :usage_thresholds, %i[plan_id recurring], unique: true, where: "recurring is true and deleted_at IS NULL" + safety_assured do + add_index :usage_thresholds, %i[amount_cents plan_id recurring], unique: true, where: 'deleted_at IS NULL' + add_index :usage_thresholds, %i[plan_id recurring], unique: true, where: "recurring is true and deleted_at IS NULL" + end end end diff --git a/db/migrate/20240820090312_attach_progressive_billing_invoices_to_credits.rb b/db/migrate/20240820090312_attach_progressive_billing_invoices_to_credits.rb index e2b57390e7d1..253effc6b12d 100644 --- a/db/migrate/20240820090312_attach_progressive_billing_invoices_to_credits.rb +++ b/db/migrate/20240820090312_attach_progressive_billing_invoices_to_credits.rb @@ -3,7 +3,9 @@ class AttachProgressiveBillingInvoicesToCredits < ActiveRecord::Migration[7.1] def change add_column :credits, :progressive_billing_invoice_id, :uuid - add_foreign_key :credits, :invoices, column: :progressive_billing_invoice_id - add_index :credits, :progressive_billing_invoice_id + safety_assured do + add_foreign_key :credits, :invoices, column: :progressive_billing_invoice_id + add_index :credits, :progressive_billing_invoice_id + end end end diff --git a/db/migrate/20240821093145_create_invoices_payment_requests.rb b/db/migrate/20240821093145_create_invoices_payment_requests.rb index e533a578a41b..fee3ff605bf1 100644 --- a/db/migrate/20240821093145_create_invoices_payment_requests.rb +++ b/db/migrate/20240821093145_create_invoices_payment_requests.rb @@ -9,37 +9,38 @@ def change end add_index :invoices_payment_requests, %i[invoice_id payment_request_id], unique: true + safety_assured do + remove_column :payments, :payment_request_id, :uuid - remove_column :payments, :payment_request_id, :uuid - - # NOTE: Migrate existing data from invoices to invoices_payment_requests - reversible do |dir| - dir.up do - execute <<-SQL + # NOTE: Migrate existing data from invoices to invoices_payment_requests + reversible do |dir| + dir.up do + execute <<-SQL INSERT INTO invoices_payment_requests (invoice_id, payment_request_id, created_at, updated_at) SELECT invoices.id, payment_requests.id, invoices.created_at, invoices.updated_at FROM invoices inner join payment_requests on payment_requests.payment_requestable_id = invoices.payable_group_id - and payment_requests.payment_requestable_type = 'PayableGroup' + and payment_requests.payment_requestable_type = 'PayableGroup' WHERE payable_group_id IS NOT NULL - SQL + SQL + end end - end - drop_table :payable_groups, id: :uuid do |t| - t.references :customer, type: :uuid, null: false, foreign_key: true - t.references :organization, type: :uuid, null: false, foreign_key: true - t.integer :payment_status, null: false, default: 0 - t.timestamps - end + drop_table :payable_groups, id: :uuid do |t| + t.references :customer, type: :uuid, null: false, foreign_key: true + t.references :organization, type: :uuid, null: false, foreign_key: true + t.integer :payment_status, null: false, default: 0 + t.timestamps + end - change_table :payment_requests, bulk: true do |t| - t.remove :payment_requestable_id, type: :uuid - t.remove :payment_requestable_type, type: :string + change_table :payment_requests, bulk: true do |t| + t.remove :payment_requestable_id, type: :uuid + t.remove :payment_requestable_type, type: :string - t.integer :payment_status, null: false, default: 0 - end + t.integer :payment_status, null: false, default: 0 + end - remove_column :invoices, :payable_group_id, :uuid + remove_column :invoices, :payable_group_id, :uuid + end end end