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

refactor(msg): payload type table migration and update #1562

Merged
merged 1 commit into from
Jan 17, 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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"db:log": "TS_NODE_PROJECT=tsconfig.commonjs.json ts-node ./node_modules/.bin/typeorm schema:log -f ormconfig.ts",
"db:migration:show": "TS_NODE_PROJECT=tsconfig.commonjs.json ts-node ./node_modules/.bin/typeorm migration:show -f ormconfig.ts",
"db:migration:generate": "TS_NODE_PROJECT=tsconfig.commonjs.json ts-node ./node_modules/.bin/typeorm migration:generate -f ormconfig.ts --pretty",
"db:migration:create": "TS_NODE_PROJECT=tsconfig.commonjs.json ts-node ./node_modules/.bin/typeorm migration:create -f ormconfig.ts",
"db:migration:run": "TS_NODE_PROJECT=tsconfig.commonjs.json ts-node ./node_modules/.bin/typeorm migration:run -f ormconfig.ts",
"db:migration:revert": "TS_NODE_PROJECT=tsconfig.commonjs.json ts-node ./node_modules/.bin/typeorm migration:revert -f ormconfig.ts",
"db:migration:sync": "TS_NODE_PROJECT=tsconfig.commonjs.json ts-node ./node_modules/.bin/typeorm schema:sync -f ormconfig.ts",
Expand Down
2 changes: 1 addition & 1 deletion src/components/MsgLeftItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export default class MsgLeftItem extends Vue {
return
}
try {
if (this.payload && this.msgType === 'JSON' && !this.msgError) {
if (this.payload && ['JSON', 'CBOR'].includes(this.msgType) && !this.msgError) {
this.hightlight = true
this.$nextTick(() => {
Prism.highlightAllUnder(this.$refs.msgLeftItem as HTMLElement)
Expand Down
8 changes: 4 additions & 4 deletions src/components/MsgPublish.vue
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ export default class MsgPublish extends Vue {
@Watch('payloadType')
private handleTypeChange(val: PayloadType, oldVal: PayloadType) {
const { payload } = this.msgRecord
if (val === 'JSON') {
if (['CBOR', 'JSON'].includes(val)) {
this.payloadLang = 'json'
} else {
this.payloadLang = 'plaintext'
Expand Down Expand Up @@ -346,7 +346,7 @@ export default class MsgPublish extends Vue {
private handleHistoryIndexChange(val: number, lastval: number) {
if (lastval !== val && val >= 0 && val < this.payloadsHistory.length) {
this.msgRecord = Object.assign(this.msgRecord, this.payloadsHistory[val])
this.payloadType = this.payloadsHistory[val].payloadType
this.payloadType = this.payloadsHistory[val].payloadType as PayloadType
}
}

Expand Down Expand Up @@ -477,7 +477,7 @@ export default class MsgPublish extends Vue {
const payloadsHistory = (await historyMessagePayloadService.getAll()) ?? []
const historyMsg = payloadsHistory[payloadsHistory.length - 1]
if (historyMsg && isLoadData) {
this.payloadType = historyMsg.payloadType
this.payloadType = historyMsg.payloadType as PayloadType
}
this.headersHistory = headersHistory
this.payloadsHistory = payloadsHistory
Expand All @@ -497,7 +497,7 @@ export default class MsgPublish extends Vue {
)
const headersHistoryIndex = this.payloadsHistory[this.historyIndex]
if (headersHistoryIndex) {
this.payloadType = headersHistoryIndex.payloadType
this.payloadType = headersHistoryIndex.payloadType as PayloadType
}
this.loadProperties()
}
Expand Down
2 changes: 2 additions & 0 deletions src/database/database.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import { aiSettings1701761407723 } from './migration/1701761407723-aiSettings'
import { aiTables1701936842016 } from './migration/1701936842016-aiTables'
import { enableCopilot1703659148195 } from './migration/1703659148195-enableCopilot'
import { logLevel1704941582350 } from './migration/1704941582350-logLevel'
import { updatePayloadTypeToVarchar1630403733965 } from './migration/1705478422620-updatePayloadTypeToVarchar'

const STORE_PATH = getAppDataPath('MQTTX')
try {
Expand Down Expand Up @@ -92,6 +93,7 @@ const ORMConfig = {
aiTables1701936842016,
enableCopilot1703659148195,
logLevel1704941582350,
updatePayloadTypeToVarchar1630403733965,
],
migrationsTableName: 'temp_migration_table',
entities: [
Expand Down
54 changes: 54 additions & 0 deletions src/database/migration/1705478422620-updatePayloadTypeToVarchar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { MigrationInterface, QueryRunner } from 'typeorm'

export class updatePayloadTypeToVarchar1630403733965 implements MigrationInterface {
name = 'updatePayloadTypeToVarchar1630403733965'

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
CREATE TABLE "temporary_historyMessagePayloadEntity" (
"id" uuid PRIMARY KEY NOT NULL,
"payload" varchar NOT NULL,
"payloadType" varchar NOT NULL DEFAULT 'JSON',
"createAt" datetime NOT NULL DEFAULT (CURRENT_TIMESTAMP)
)
`)
await queryRunner.query(`
INSERT INTO "temporary_historyMessagePayloadEntity" ("id", "payload", "payloadType", "createAt")
SELECT "id", "payload", "payloadType", "createAt"
FROM "historyMessagePayloadEntity"
`)
await queryRunner.query(`
DROP TABLE "historyMessagePayloadEntity"
`)
await queryRunner.query(`
ALTER TABLE "temporary_historyMessagePayloadEntity"
RENAME TO "historyMessagePayloadEntity"
`)
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
CREATE TABLE "temporary_historyMessagePayloadEntity" (
"id" uuid PRIMARY KEY NOT NULL,
"payload" varchar NOT NULL,
"payloadType" varchar CHECK(payloadType IN ('Plaintext', 'Base64', 'JSON', 'Hex')) NOT NULL DEFAULT 'JSON',
"createAt" datetime NOT NULL DEFAULT (CURRENT_TIMESTAMP)
)
`)

await queryRunner.query(`
INSERT INTO "temporary_historyMessagePayloadEntity" ("id", "payload", "payloadType", "createAt")
SELECT "id", "payload", "payloadType", "createAt"
FROM "historyMessagePayloadEntity"
`)

await queryRunner.query(`
DROP TABLE "historyMessagePayloadEntity"
`)

await queryRunner.query(`
ALTER TABLE "temporary_historyMessagePayloadEntity"
RENAME TO "historyMessagePayloadEntity"
`)
}
}
6 changes: 2 additions & 4 deletions src/database/models/HistoryMessagePayloadEntity.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm'

type PayloadType = 'Plaintext' | 'Base64' | 'JSON' | 'Hex' | 'CBOR'

@Entity('historyMessagePayloadEntity')
export default class HistoryMessagePayloadEntity {
@PrimaryGeneratedColumn('uuid')
Expand All @@ -10,8 +8,8 @@ export default class HistoryMessagePayloadEntity {
@Column({ type: 'varchar' })
payload!: string

@Column({ type: 'simple-enum', enum: ['Plaintext', 'Base64', 'JSON', 'Hex', 'CBOR'], default: 'JSON' })
payloadType!: PayloadType
@Column({ type: 'varchar', default: 'JSON' })
payloadType!: string

@Column({ type: 'datetime', default: () => 'CURRENT_TIMESTAMP' })
createAt?: string
Expand Down
2 changes: 1 addition & 1 deletion src/types/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ declare global {
connectionId?: string
id?: string
payload: string
payloadType: PayloadType
payloadType: string
createAt?: string
}

Expand Down
2 changes: 1 addition & 1 deletion src/views/connections/ConnectionsDetail.vue
Original file line number Diff line number Diff line change
Expand Up @@ -1662,7 +1662,7 @@ export default class ConnectionsDetail extends Vue {
}
}
if (receiveType === 'CBOR') {
return jsonStringify(cbor.decodeFirstSync(receiveValue))
return jsonStringify(cbor.decodeFirstSync(receiveValue), null, 2)
}
return receiveValue.toString()
}
Expand Down
Loading