Skip to content

Commit

Permalink
SQL Datasources: Fix annotation migration (#59438)
Browse files Browse the repository at this point in the history
  • Loading branch information
zoltanbedi committed Nov 29, 2022
1 parent 110fdf4 commit 71e4a82
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
28 changes: 28 additions & 0 deletions public/app/features/plugins/sql/utils/migration.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { QueryFormat } from '../types';

import migrateAnnotation from './migration';

describe('Annotation migration', () => {
const annotation = {
datasource: {
uid: 'P4FDCC188E688367F',
type: 'mysql',
},
enable: false,
hide: false,
iconColor: 'rgba(0, 211, 255, 1)',
limit: 100,
name: 'Single',
rawQuery:
"SELECT\n createdAt as time,\n 'single' as text,\n hostname as tags\nFROM\n grafana_metric\nWHERE\n $__timeFilter(createdAt)\nORDER BY time\nLIMIT 1\n",
showIn: 0,
tags: [],
type: 'tags',
};

it('should migrate from old format to new', () => {
const newAnnotationFormat = migrateAnnotation(annotation);
expect(newAnnotationFormat.target?.format).toBe(QueryFormat.Table);
expect(newAnnotationFormat.target?.rawSql).toBe(annotation.rawQuery);
});
});
9 changes: 2 additions & 7 deletions public/app/features/plugins/sql/utils/migration.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AnnotationQuery } from '@grafana/data';
import { EditorMode } from '@grafana/experimental';

import { applyQueryDefaults } from '../defaults';
import { SQLQuery } from '../types';

export default function migrateAnnotation(annotation: AnnotationQuery<SQLQuery>) {
Expand All @@ -10,12 +10,7 @@ export default function migrateAnnotation(annotation: AnnotationQuery<SQLQuery>)
return annotation;
}

const newQuery: SQLQuery = {
...(annotation.target ?? {}),
refId: annotation.target?.refId ?? 'Anno',
editorMode: EditorMode.Code,
rawSql: oldQuery,
};
const newQuery = applyQueryDefaults({ refId: 'Annotation', ...(annotation.target ?? {}), rawSql: oldQuery });

return {
...annotation,
Expand Down

0 comments on commit 71e4a82

Please sign in to comment.