Removed tags column. Added tags and snippets_tags tables

This commit is contained in:
unknown
2021-09-27 14:38:18 +02:00
parent bd7eaf3d17
commit 5b0fc6976a
9 changed files with 209 additions and 55 deletions

View File

@@ -1,19 +1,90 @@
import { DataTypes, QueryInterface, QueryTypes } from 'sequelize'; import { Logger } from '../../utils';
import { sequelize } from '../'; import { DataTypes, QueryInterface } from 'sequelize';
const { STRING } = DataTypes; import {
SnippetModel,
Snippet_TagModel,
TagInstance,
TagModel
} from '../../models';
const { STRING, INTEGER } = DataTypes;
const logger = new Logger('migration[02]');
export const up = async (queryInterface: QueryInterface): Promise<void> => { export const up = async (queryInterface: QueryInterface): Promise<void> => {
await queryInterface.addColumn('snippets', 'tags', { await queryInterface.createTable('tags', {
type: STRING, id: {
allowNull: true, type: INTEGER,
defaultValue: '' allowNull: false,
primaryKey: true,
autoIncrement: true
},
name: {
type: STRING,
allowNull: false
}
}); });
await sequelize.query(`UPDATE snippets SET tags = language`, { await queryInterface.createTable('snippets_tags', {
type: QueryTypes.UPDATE id: {
type: INTEGER,
allowNull: false,
primaryKey: true,
autoIncrement: true
},
snippet_id: {
type: INTEGER,
allowNull: false
},
tag_id: {
type: INTEGER,
allowNull: false
}
});
// Create new tags from language column
const snippets = await SnippetModel.findAll();
const languages = snippets.map(snippet => snippet.language);
const uniqueLanguages = [...new Set(languages)];
const tags: TagInstance[] = [];
await new Promise<void>(resolve => {
uniqueLanguages.forEach(async language => {
try {
const tag = await TagModel.create({ name: language });
tags.push(tag);
} catch (err) {
logger.log('Error while creating new tags');
} finally {
if (uniqueLanguages.length == tags.length) {
resolve();
}
}
});
});
// Assign tag to snippet
await new Promise<void>(resolve => {
snippets.forEach(async snippet => {
try {
const tag = tags.find(tag => tag.name == snippet.language);
if (tag) {
await Snippet_TagModel.create({
snippet_id: snippet.id,
tag_id: tag.id
});
}
} catch (err) {
logger.log('Error while assigning tags to snippets');
console.log(err);
} finally {
resolve();
}
});
}); });
}; };
export const down = async (queryInterface: QueryInterface): Promise<void> => { export const down = async (queryInterface: QueryInterface): Promise<void> => {
await queryInterface.removeColumn('snippets', 'tags'); await queryInterface.dropTable('tags');
await queryInterface.dropTable('snippets_tags');
}; };

View File

@@ -4,52 +4,53 @@ import { Snippet, SnippetCreationAttributes } from '../typescript/interfaces';
const { INTEGER, STRING, DATE, TEXT } = DataTypes; const { INTEGER, STRING, DATE, TEXT } = DataTypes;
interface SnippetInstance export interface SnippetInstance
extends Model<Snippet, SnippetCreationAttributes>, extends Model<Snippet, SnippetCreationAttributes>,
Snippet {} Snippet {}
export const SnippetModel = sequelize.define<SnippetInstance>('Snippet', { export const SnippetModel = sequelize.define<SnippetInstance>(
id: { 'Snippet',
type: INTEGER, {
primaryKey: true, id: {
autoIncrement: true type: INTEGER,
primaryKey: true,
autoIncrement: true
},
title: {
type: STRING,
allowNull: false
},
description: {
type: TEXT,
allowNull: true,
defaultValue: ''
},
language: {
type: STRING,
allowNull: false
},
code: {
type: TEXT,
allowNull: false
},
docs: {
type: TEXT,
allowNull: true,
defaultValue: ''
},
isPinned: {
type: INTEGER,
allowNull: true,
defaultValue: 0
},
createdAt: {
type: DATE
},
updatedAt: {
type: DATE
}
}, },
title: { {
type: STRING, tableName: 'snippets'
allowNull: false
},
description: {
type: TEXT,
allowNull: true,
defaultValue: ''
},
language: {
type: STRING,
allowNull: false
},
code: {
type: TEXT,
allowNull: false
},
docs: {
type: TEXT,
allowNull: true,
defaultValue: ''
},
isPinned: {
type: INTEGER,
allowNull: true,
defaultValue: 0
},
tags: {
type: STRING,
allowNull: true,
defaultValue: ''
},
createdAt: {
type: DATE
},
updatedAt: {
type: DATE
} }
}); );

35
src/models/Snippet_Tag.ts Normal file
View File

@@ -0,0 +1,35 @@
import { Model, DataTypes } from 'sequelize';
import { sequelize } from '../db';
import {
Snippet_Tag,
Snippet_TagCreationAttributes
} from '../typescript/interfaces';
const { INTEGER } = DataTypes;
export interface Snippet_TagInstance
extends Model<Snippet_Tag, Snippet_TagCreationAttributes>,
Snippet_Tag {}
export const Snippet_TagModel = sequelize.define<Snippet_TagInstance>(
'Snippet_Tag',
{
id: {
type: INTEGER,
primaryKey: true,
autoIncrement: true
},
snippet_id: {
type: INTEGER,
allowNull: false
},
tag_id: {
type: INTEGER,
allowNull: false
}
},
{
timestamps: false,
tableName: 'snippets_tags'
}
);

26
src/models/Tag.ts Normal file
View File

@@ -0,0 +1,26 @@
import { Model, DataTypes } from 'sequelize';
import { sequelize } from '../db';
import { Tag, TagCreationAttributes } from '../typescript/interfaces';
const { INTEGER, STRING } = DataTypes;
export interface TagInstance extends Model<Tag, TagCreationAttributes>, Tag {}
export const TagModel = sequelize.define<TagInstance>(
'Tag',
{
id: {
type: INTEGER,
primaryKey: true,
autoIncrement: true
},
name: {
type: STRING,
allowNull: false
}
},
{
timestamps: false,
tableName: 'tags'
}
);

View File

@@ -1 +1,3 @@
export * from './Snippet'; export * from './Snippet';
export * from './Tag';
export * from './Snippet_Tag';

View File

@@ -8,7 +8,6 @@ export interface Snippet extends Model {
code: string; code: string;
docs: string; docs: string;
isPinned: number; isPinned: number;
tags: string;
} }
export interface SnippetCreationAttributes export interface SnippetCreationAttributes

View File

@@ -0,0 +1,10 @@
import { Optional } from 'sequelize';
export interface Snippet_Tag {
id: number;
snippet_id: number;
tag_id: number;
}
export interface Snippet_TagCreationAttributes
extends Optional<Snippet_Tag, 'id'> {}

View File

@@ -0,0 +1,8 @@
import { Optional } from 'sequelize';
export interface Tag {
id: number;
name: string;
}
export interface TagCreationAttributes extends Optional<Tag, 'id'> {}

View File

@@ -1,2 +1,4 @@
export * from './Model'; export * from './Model';
export * from './Snippet'; export * from './Snippet';
export * from './Tag';
export * from './Snippet_Tag';