-
Notifications
You must be signed in to change notification settings - Fork 30
configurable language for fulltext-search #2133
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
blued-gear
wants to merge
7
commits into
main
Choose a base branch
from
new/ts-lang_server_setting
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
09100e9
add env setting for ts_lang for search
blued-gear d20214c
add command for migration
blued-gear ac90d1f
add env-var to .env files
blued-gear e44e25a
add documentation
blued-gear 545710f
extend docs
blued-gear a98add0
fix tests
blued-gear 48f0fe1
Merge branch 'main' into new/ts-lang_server_setting
BentiGorlich File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Command; | ||
|
|
||
| use App\Service\SettingsManager; | ||
| use Doctrine\DBAL\Connection; | ||
| use Doctrine\ORM\EntityManagerInterface; | ||
| use Symfony\Component\Console\Attribute\AsCommand; | ||
| use Symfony\Component\Console\Command\Command; | ||
| use Symfony\Component\Console\Input\InputInterface; | ||
| use Symfony\Component\Console\Output\OutputInterface; | ||
| use Symfony\Component\Console\Style\SymfonyStyle; | ||
|
|
||
| #[AsCommand( | ||
| name: 'mbin:db:migrate-search-lang', | ||
| description: 'Migrates all ts_vector columns to the current language', | ||
| )] | ||
| class MigrateDbTsCommand extends Command | ||
| { | ||
| public function __construct( | ||
| private readonly EntityManagerInterface $entityManager, | ||
| private readonly SettingsManager $settingsManager, | ||
| ) { | ||
| parent::__construct(); | ||
| } | ||
|
|
||
| protected function execute(InputInterface $input, OutputInterface $output): int | ||
| { | ||
| $io = new SymfonyStyle($input, $output); | ||
| $lang = $this->settingsManager->getSearchLang(); | ||
|
|
||
| if (!$this->checkLanguage($lang)) { | ||
| $io->error("the language '$lang' is not supported by the database"); | ||
|
|
||
| return Command::FAILURE; | ||
| } | ||
| $io->info("migrating ts_vectors to '$lang'"); | ||
|
|
||
| $conn = $this->entityManager->getConnection(); | ||
| $this->recreateColumn($conn, 'entry', 'title_ts', 'title', 'entry_title_ts_idx', $lang, $io); | ||
| $this->recreateColumn($conn, 'entry', 'body_ts', 'body', 'entry_body_ts_idx', $lang, $io); | ||
| $this->recreateColumn($conn, 'post', 'body_ts', 'body', 'post_body_ts_idx', $lang, $io); | ||
| $this->recreateColumn($conn, 'post_comment', 'body_ts', 'body', 'post_comment_body_ts_idx', $lang, $io); | ||
| $this->recreateColumn($conn, 'entry_comment', 'body_ts', 'body', 'entry_comment_body_ts_idx', $lang, $io); | ||
| $this->recreateColumn($conn, 'magazine', 'name_ts', 'name', 'magazine_name_ts', $lang, $io); | ||
| $this->recreateColumn($conn, 'magazine', 'title_ts', 'title', 'magazine_title_ts', $lang, $io); | ||
| $this->recreateColumn($conn, 'magazine', 'description_ts', 'description', 'magazine_description_ts', $lang, $io); | ||
| $this->recreateColumn($conn, '"user"', 'username_ts', 'username', 'user_username_ts', $lang, $io); | ||
| $this->recreateColumn($conn, '"user"', 'title_ts', 'title', 'user_title_ts', $lang, $io); | ||
| $this->recreateColumn($conn, '"user"', 'about_ts', 'about', 'user_about_ts', $lang, $io); | ||
|
|
||
| $io->success('done'); | ||
|
|
||
| return Command::SUCCESS; | ||
| } | ||
|
|
||
| private function checkLanguage(string $lang): bool | ||
| { | ||
| $conn = $this->entityManager->getConnection(); | ||
| $supportedLanguages = $conn->executeQuery('SELECT cfgname FROM pg_ts_config;')->fetchFirstColumn(); | ||
|
|
||
| return \in_array($lang, $supportedLanguages, true); | ||
| } | ||
|
|
||
| private function recreateColumn(Connection $conn, string $table, string $column, string $srcColumn, string $idxName, string $lang, SymfonyStyle $io): void | ||
| { | ||
| $conn->executeStatement("DROP INDEX $idxName;"); | ||
| $conn->executeStatement("ALTER TABLE $table DROP COLUMN $column;"); | ||
| $conn->executeStatement("ALTER TABLE $table ADD COLUMN $column tsvector GENERATED ALWAYS AS (to_tsvector('$lang', $srcColumn)) STORED;"); | ||
| $conn->executeStatement("CREATE INDEX $idxName ON $table USING GIN ($column);"); | ||
|
|
||
| $io->writeln("$table.$column"); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These KBIN_* options are still in use. Keep it in the example. right? Eventually we might need to rename all the configs with
MBIN_so its all consistent. but that is unrelated to this PR.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They are in the file, I just deleted duplicated keys (see lines 52+).