diff options
author | pluralcafe-docker <git@plural.cafe> | 2018-12-02 08:24:11 +0000 |
---|---|---|
committer | pluralcafe-docker <git@plural.cafe> | 2018-12-02 08:24:11 +0000 |
commit | ae737d94ac10284738928109c11f43aeecca8a0b (patch) | |
tree | ebe739f663d259e0f2aaf2e2f2984a8d0bc9cf1e /db/migrate | |
parent | 384a602fd4117a73338542c59985f54acf5fb3f8 (diff) | |
parent | 4b85bf12ab7005a5a6b4472b8aeaf022ce042ad7 (diff) |
Merge branch 'glitch'
Diffstat (limited to 'db/migrate')
-rw-r--r-- | db/migrate/20181116165755_create_account_stats.rb | 12 | ||||
-rw-r--r-- | db/migrate/20181116173541_copy_account_stats.rb | 54 | ||||
-rw-r--r-- | db/migrate/20181127130500_identity_id_to_bigint.rb | 27 | ||||
-rw-r--r-- | db/migrate/20181127165847_add_show_replies_to_lists.rb | 23 |
4 files changed, 116 insertions, 0 deletions
diff --git a/db/migrate/20181116165755_create_account_stats.rb b/db/migrate/20181116165755_create_account_stats.rb new file mode 100644 index 000000000..a798e8166 --- /dev/null +++ b/db/migrate/20181116165755_create_account_stats.rb @@ -0,0 +1,12 @@ +class CreateAccountStats < ActiveRecord::Migration[5.2] + def change + create_table :account_stats do |t| + t.belongs_to :account, null: false, foreign_key: { on_delete: :cascade }, index: { unique: true } + t.bigint :statuses_count, null: false, default: 0 + t.bigint :following_count, null: false, default: 0 + t.bigint :followers_count, null: false, default: 0 + + t.timestamps + end + end +end diff --git a/db/migrate/20181116173541_copy_account_stats.rb b/db/migrate/20181116173541_copy_account_stats.rb new file mode 100644 index 000000000..bb523fbbd --- /dev/null +++ b/db/migrate/20181116173541_copy_account_stats.rb @@ -0,0 +1,54 @@ +class CopyAccountStats < ActiveRecord::Migration[5.2] + disable_ddl_transaction! + + def up + safety_assured do + if supports_upsert? + up_fast + else + up_slow + end + end + end + + def down + # Nothing + end + + private + + def supports_upsert? + version = select_one("SELECT current_setting('server_version_num') AS v")['v'].to_i + version >= 90500 + end + + def up_fast + say 'Upsert is available, importing counters using the fast method' + + Account.unscoped.select('id').find_in_batches(batch_size: 5_000) do |accounts| + execute <<-SQL.squish + INSERT INTO account_stats (account_id, statuses_count, following_count, followers_count, created_at, updated_at) + SELECT id, statuses_count, following_count, followers_count, created_at, updated_at + FROM accounts + WHERE id IN (#{accounts.map(&:id).join(', ')}) + ON CONFLICT (account_id) DO UPDATE + SET statuses_count = EXCLUDED.statuses_count, following_count = EXCLUDED.following_count, followers_count = EXCLUDED.followers_count + SQL + end + end + + def up_slow + say 'Upsert is not available in PostgreSQL below 9.5, falling back to slow import of counters' + + # We cannot use bulk INSERT or overarching transactions here because of possible + # uniqueness violations that we need to skip over + Account.unscoped.select('id, statuses_count, following_count, followers_count, created_at, updated_at').find_each do |account| + begin + params = [[nil, account.id], [nil, account.statuses_count], [nil, account.following_count], [nil, account.followers_count], [nil, account.created_at], [nil, account.updated_at]] + exec_insert('INSERT INTO account_stats (account_id, statuses_count, following_count, followers_count, created_at, updated_at) VALUES ($1, $2, $3, $4, $5, $6)', nil, params) + rescue ActiveRecord::RecordNotUnique + next + end + end + end +end diff --git a/db/migrate/20181127130500_identity_id_to_bigint.rb b/db/migrate/20181127130500_identity_id_to_bigint.rb new file mode 100644 index 000000000..5f388aca7 --- /dev/null +++ b/db/migrate/20181127130500_identity_id_to_bigint.rb @@ -0,0 +1,27 @@ +require Rails.root.join('lib', 'mastodon', 'migration_helpers') + +class IdentityIdToBigint < ActiveRecord::Migration[5.2] + include Mastodon::MigrationHelpers + + disable_ddl_transaction! + + def up + safety_assured do + change_column_type_concurrently :identities, :id, :bigint + cleanup_concurrent_column_type_change :identities, :id + + change_column_type_concurrently :identities, :user_id, :bigint + cleanup_concurrent_column_type_change :identities, :user_id + end + end + + def down + safety_assured do + change_column_type_concurrently :identities, :id, :integer + cleanup_concurrent_column_type_change :identities, :id + + change_column_type_concurrently :identities, :user_id, :integer + cleanup_concurrent_column_type_change :identities, :user_id + end + end +end diff --git a/db/migrate/20181127165847_add_show_replies_to_lists.rb b/db/migrate/20181127165847_add_show_replies_to_lists.rb new file mode 100644 index 000000000..f68c98daf --- /dev/null +++ b/db/migrate/20181127165847_add_show_replies_to_lists.rb @@ -0,0 +1,23 @@ +require Rails.root.join('lib', 'mastodon', 'migration_helpers') + +class AddShowRepliesToLists < ActiveRecord::Migration[5.2] + include Mastodon::MigrationHelpers + + disable_ddl_transaction! + + def up + safety_assured do + add_column_with_default( + :lists, + :replies_policy, + :integer, + allow_null: false, + default: 0 + ) + end + end + + def down + remove_column :lists, :replies_policy + end +end |