Add Index to 'type' column in 'Post' table (#8278)

* update `Post` table schema and migration plan to index `type` column

* use `unsafeExecuteSql()` for adding index during migration

* Update SQL query to create index

Co-authored-by: Elias Nahum <nahumhbl@gmail.com>

* Update docs

* Remove `UNIQUE` from SQL query

Co-authored-by: Rahim Rahman <rahim.rahman@mattermost.com>

---------

Co-authored-by: Elias Nahum <nahumhbl@gmail.com>
Co-authored-by: Rahim Rahman <rahim.rahman@mattermost.com>
This commit is contained in:
TheoForger 2024-10-26 08:14:56 -04:00 committed by GitHub
parent c5171418b6
commit e932cf30a0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 691 additions and 9 deletions

View file

@ -4,13 +4,19 @@
// NOTE : To implement migration, please follow this document
// https://nozbe.github.io/WatermelonDB/Advanced/Migrations.html
import {addColumns, createTable, schemaMigrations} from '@nozbe/watermelondb/Schema/migrations';
import {addColumns, createTable, schemaMigrations, unsafeExecuteSql} from '@nozbe/watermelondb/Schema/migrations';
import {MM_TABLES} from '@constants/database';
const {CHANNEL_BOOKMARK, CHANNEL_INFO, DRAFT, POST} = MM_TABLES.SERVER;
export default schemaMigrations({migrations: [
{
toVersion: 6,
steps: [
unsafeExecuteSql('CREATE INDEX IF NOT EXISTS Post_type ON Post (type)'),
],
},
{
toVersion: 5,
steps: [

View file

@ -40,7 +40,7 @@ import {
} from './table_schemas';
export const serverSchema: AppSchema = appSchema({
version: 5,
version: 6,
tables: [
CategorySchema,
CategoryChannelSchema,

View file

@ -23,7 +23,7 @@ export default tableSchema({
{name: 'previous_post_id', type: 'string'},
{name: 'props', type: 'string'},
{name: 'root_id', type: 'string'},
{name: 'type', type: 'string'},
{name: 'type', type: 'string', isIndexed: true},
{name: 'update_at', type: 'number'},
{name: 'user_id', type: 'string', isIndexed: true},
],

View file

@ -46,7 +46,7 @@ const {
describe('*** Test schema for SERVER database ***', () => {
it('=> The SERVER SCHEMA should strictly match', () => {
expect(serverSchema).toEqual({
version: 5,
version: 6,
unsafeSql: undefined,
tables: {
[CATEGORY]: {
@ -413,7 +413,7 @@ describe('*** Test schema for SERVER database ***', () => {
previous_post_id: {name: 'previous_post_id', type: 'string'},
props: {name: 'props', type: 'string'},
root_id: {name: 'root_id', type: 'string'},
type: {name: 'type', type: 'string'},
type: {name: 'type', type: 'string', isIndexed: true},
update_at: {name: 'update_at', type: 'number'},
user_id: {name: 'user_id', type: 'string', isIndexed: true},
},
@ -431,7 +431,7 @@ describe('*** Test schema for SERVER database ***', () => {
{name: 'previous_post_id', type: 'string'},
{name: 'props', type: 'string'},
{name: 'root_id', type: 'string'},
{name: 'type', type: 'string'},
{name: 'type', type: 'string', isIndexed: true},
{name: 'update_at', type: 'number'},
{name: 'user_id', type: 'string', isIndexed: true},
],

Binary file not shown.

File diff suppressed because one or more lines are too long

View file

@ -660,6 +660,9 @@ ON [Post] ([channel_id])
CREATE INDEX [idx_Post_pending_post_id]
ON [Post] ([pending_post_id])
CREATE INDEX [idx_Post_type]
ON [Post] ([type])
CREATE INDEX [idx_Post_user_id]
ON [Post] ([user_id])
@ -696,4 +699,4 @@ ON [ThreadParticipant] ([thread_id])
CREATE INDEX [idx_ThreadParticipant_user_id]
ON [ThreadParticipant] ([user_id])
COMMIT TRANSACTION QUICKDBD
COMMIT TRANSACTION QUICKDBD

View file

@ -166,7 +166,7 @@ pending_post_id string INDEX
previous_post_id string
props string
root_id string
type string
type string INDEX
update_at number
user_id string INDEX FK >- User.id