|
| 1 | +#!/usr/bin/env /src/bin/php-exec-one |
| 2 | +<?php |
| 3 | +use Manticoresearch\Client; |
| 4 | + |
| 5 | +ini_set('memory_limit', '512M'); |
| 6 | +define('MANTICORE_CONFIG', array_merge(config('manticore'), [ |
| 7 | + 'retries' => 3, |
| 8 | + 'timeout' => 86400, |
| 9 | + 'connection_timeout' => 3, |
| 10 | +])); |
| 11 | + |
| 12 | +$limit = 1000; |
| 13 | +$client = new Client(MANTICORE_CONFIG); |
| 14 | +$result = $client->sql('SELECT id, org_id FROM repo LIMIT 1000'); |
| 15 | +$list = $result['hits']['hits'] ?? []; |
| 16 | +$org_map = []; |
| 17 | +foreach ($list as $item) { |
| 18 | + $id = $item['_id']; |
| 19 | + $org_id = $item['_source']['org_id']; |
| 20 | + $org_map[$org_id] ??= []; |
| 21 | + $org_map[$org_id][] = $id; |
| 22 | +} |
| 23 | + |
| 24 | +foreach ($org_map as $org_id => $repo_ids) { |
| 25 | + Cli::print("Org id: $org_id"); |
| 26 | + $repos = implode(',', $repo_ids); |
| 27 | + Cli::print("Repos: $repos"); |
| 28 | + |
| 29 | + Cli::print('Migrating issues'); |
| 30 | + /* $client->sql('DROP TABLE IF EXISTS issue_' . $org_id, true); */ |
| 31 | + $client->sql('CREATE TABLE IF NOT EXISTS issue_' . $org_id . ' LIKE issue WITH DATA', true); |
| 32 | + $client->sql('DELETE FROM issue_' . $org_id . ' WHERE repo_id NOT IN (' . $repos . ')', true); |
| 33 | + $client->sql('ALTER TABLE issue_' . $org_id . ' ADD COLUMN org_id bigint', true); |
| 34 | + $client->sql('UPDATE issue_' . $org_id . ' SET org_id = ' . $org_id . ' WHERE repo_id IN (' . $repos . ')', true); |
| 35 | + $client->sql('OPTIMIZE TABLE issue_' . $org_id . ' OPTION cutoff=1, sync=1', true); |
| 36 | + [$count] = $client->sql('SELECT COUNT(*) FROM issue_' . $org_id, true); |
| 37 | + Cli::print(" Count: $count"); |
| 38 | + |
| 39 | + Cli::print('Migrating comments'); |
| 40 | + /* $client->sql('DROP TABLE IF EXISTS comment_' . $org_id, true); */ |
| 41 | + $client->sql('CREATE TABLE IF NOT EXISTS comment_' . $org_id . ' LIKE `comment` WITH DATA', true); |
| 42 | + $client->sql('DELETE FROM comment_' . $org_id . ' WHERE repo_id NOT IN (' . $repos . ')', true); |
| 43 | + $client->sql('ALTER TABLE comment_' . $org_id . ' ADD COLUMN org_id bigint', true); |
| 44 | + $client->sql('UPDATE comment_' . $org_id . ' SET org_id = ' . $org_id . ' WHERE repo_id IN (' . $repos . ')', true); |
| 45 | + $client->sql('OPTIMIZE TABLE comment_' . $org_id . ' OPTION cutoff=1, sync=1', true); |
| 46 | + [$count] = $client->sql('SELECT COUNT(*) FROM comment_' . $org_id, true); |
| 47 | + Cli::print(" Count: $count"); |
| 48 | + |
| 49 | +} |
| 50 | + |
| 51 | +Cli::print('Done'); |
0 commit comments