Skip to content
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

MM-60705 DB migration: added update_at column to draft table for mobile #8237

Merged
merged 4 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions app/actions/local/draft.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const draft: Draft = {
channel_id: channel.id,
message: 'test',
root_id: '',
update_at: Date.now(),
} as Draft;

beforeEach(async () => {
Expand Down Expand Up @@ -106,7 +107,7 @@ describe('removeDraftFile', () => {
});

it('remove draft file, no message', async () => {
await operator.handleDraft({drafts: [{channel_id: channel.id, files: [fileInfo], root_id: ''}], prepareRecordsOnly: false});
await operator.handleDraft({drafts: [{channel_id: channel.id, files: [fileInfo], root_id: '', update_at: Date.now()}], prepareRecordsOnly: false});

const {draft: draftModel, error} = await removeDraftFile(serverUrl, channelId, '', 'clientid', false);
expect(error).toBeUndefined();
Expand Down Expand Up @@ -152,7 +153,7 @@ describe('updateDraftMessage', () => {
});

it('update draft message, no file', async () => {
await operator.handleDraft({drafts: [{channel_id: channel.id, files: [], root_id: ''}], prepareRecordsOnly: false});
await operator.handleDraft({drafts: [{channel_id: channel.id, files: [], root_id: '', update_at: Date.now()}], prepareRecordsOnly: false});

const result = await updateDraftMessage(serverUrl, channelId, '', 'newmessage', false) as {draft: DraftModel; error: unknown};
expect(result.error).toBeUndefined();
Expand Down
8 changes: 8 additions & 0 deletions app/actions/local/draft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export async function updateDraftFile(serverUrl: string, channelId: string, root
newFiles[i] = file;
draft.prepareUpdate((d) => {
d.files = newFiles;
d.updateAt = Date.now();
});

if (!prepareRecordsOnly) {
Expand Down Expand Up @@ -54,6 +55,7 @@ export async function removeDraftFile(serverUrl: string, channelId: string, root
} else {
draft.prepareUpdate((d) => {
d.files = draft.files.filter((v, index) => index !== i);
d.updateAt = Date.now();
});
}

Expand Down Expand Up @@ -81,6 +83,7 @@ export async function updateDraftMessage(serverUrl: string, channelId: string, r
channel_id: channelId,
root_id: rootId,
message,
update_at: Date.now(),
};

return operator.handleDraft({drafts: [newDraft], prepareRecordsOnly});
Expand All @@ -95,6 +98,7 @@ export async function updateDraftMessage(serverUrl: string, channelId: string, r
} else {
draft.prepareUpdate((d) => {
d.message = message;
d.updateAt = Date.now();
});
}

Expand All @@ -119,13 +123,15 @@ export async function addFilesToDraft(serverUrl: string, channelId: string, root
root_id: rootId,
files,
message: '',
update_at: Date.now(),
};

return operator.handleDraft({drafts: [newDraft], prepareRecordsOnly});
}

draft.prepareUpdate((d) => {
d.files = [...draft.files, ...files];
d.updateAt = Date.now();
});

if (!prepareRecordsOnly) {
Expand Down Expand Up @@ -167,6 +173,7 @@ export async function updateDraftPriority(serverUrl: string, channelId: string,
metadata: {
priority: postPriority,
},
update_at: Date.now(),
};

return operator.handleDraft({drafts: [newDraft], prepareRecordsOnly});
Expand All @@ -177,6 +184,7 @@ export async function updateDraftPriority(serverUrl: string, channelId: string,
...d.metadata,
priority: postPriority,
};
d.updateAt = Date.now();
});

if (!prepareRecordsOnly) {
Expand Down
11 changes: 11 additions & 0 deletions app/database/migration/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ import {MM_TABLES} from '@constants/database';
const {CHANNEL_BOOKMARK, CHANNEL_INFO, DRAFT, POST} = MM_TABLES.SERVER;

export default schemaMigrations({migrations: [
{
toVersion: 5,
steps: [
addColumns({
table: DRAFT,
columns: [
{name: 'update_at', type: 'number'},
],
}),
],
},
{
toVersion: 4,
steps: [
Expand Down
3 changes: 3 additions & 0 deletions app/database/models/server/draft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,7 @@ export default class DraftModel extends Model implements DraftModelInterface {
@json('files', safeParseJSON) files!: FileInfo[];

@json('metadata', identity) metadata?: PostMetadata;

/** update_at : The timestamp to when this post was last updated on the server */
@field('update_at') updateAt!: number;
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ describe('*** Operator: Post Handlers tests ***', () => {
],
message: 'test draft message for post',
root_id: '',
update_at: Date.now(),
},
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ describe('*** POST Prepare Records Test ***', () => {
message: 'draft message',
channel_id: 'channel_idp23232e',
files: [],
update_at: Date.now(),
},
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ export const transformDraftRecord = ({action, database, value}: TransformerArgs)
draft.channelId = raw?.channel_id ?? '';
draft.files = raw?.files ?? emptyFileInfo;
draft.metadata = raw?.metadata ?? emptyPostMetadata;
draft.updateAt = raw.update_at ?? Date.now();
};

return prepareBaseRecord({
Expand Down
2 changes: 1 addition & 1 deletion app/database/schema/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import {
} from './table_schemas';

export const serverSchema: AppSchema = appSchema({
version: 4,
version: 5,
tables: [
CategorySchema,
CategoryChannelSchema,
Expand Down
1 change: 1 addition & 0 deletions app/database/schema/server/table_schemas/draft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ export default tableSchema({
{name: 'message', type: 'string'},
{name: 'root_id', type: 'string', isIndexed: true},
{name: 'metadata', type: 'string', isOptional: true},
{name: 'update_at', type: 'number'},
],
});
4 changes: 3 additions & 1 deletion app/database/schema/server/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const {
describe('*** Test schema for SERVER database ***', () => {
it('=> The SERVER SCHEMA should strictly match', () => {
expect(serverSchema).toEqual({
version: 4,
version: 5,
unsafeSql: undefined,
tables: {
[CATEGORY]: {
Expand Down Expand Up @@ -265,13 +265,15 @@ describe('*** Test schema for SERVER database ***', () => {
message: {name: 'message', type: 'string'},
root_id: {name: 'root_id', type: 'string', isIndexed: true},
metadata: {name: 'metadata', type: 'string', isOptional: true},
update_at: {name: 'update_at', type: 'number'},
},
columnArray: [
{name: 'channel_id', type: 'string', isIndexed: true},
{name: 'files', type: 'string'},
{name: 'message', type: 'string'},
{name: 'root_id', type: 'string', isIndexed: true},
{name: 'metadata', type: 'string', isOptional: true},
{name: 'update_at', type: 'number'},
],
},
[FILE]: {
Expand Down
Binary file removed docs/database/server/server-v2.png
Binary file not shown.
Binary file not shown.
Binary file added docs/database/server/server-v3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-- Exported from QuickDBD: https://www.quickdatabasediagrams.com/
-- Link to schema: https://app.quickdatabasediagrams.com/#/d/6OtugI
-- Link to schema: https://app.quickdatabasediagrams.com/#/d/PITc8R
-- NOTE! If you have used non-SQL datatypes in your design, you will have to change these here.

-- Server Database - Schema Version 2
Expand Down Expand Up @@ -101,6 +101,7 @@ CREATE TABLE [Draft] (
[files] string NOT NULL ,
[message] string NOT NULL ,
[root_id] string NULL ,
[update_at] number NOT NULL ,
CONSTRAINT [PK_Draft] PRIMARY KEY CLUSTERED (
[id] ASC
)
Expand Down
2 changes: 1 addition & 1 deletion docs/database/server/server.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Server Database - Schema Version 2
# Server Database - Schema Version 3
# Please bump the version by 1, any time the schema changes.
# Also, include the migration plan under app/database/migration/server,
# update all models, relationships and types.
Expand Down
3 changes: 3 additions & 0 deletions types/database/models/servers/draft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ declare class DraftModel extends Model {
files: FileInfo[];

metadata?: PostMetadata;

/** update_at : The timestamp to when this post was last updated on the server */
updateAt: number;
}

export default DraftModel;
1 change: 1 addition & 0 deletions types/database/raw_values.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type Draft = {
message?: string;
root_id: string;
metadata?: PostMetadata;
update_at: number;
};

type MyTeam = {
Expand Down