Forráskód Böngészése

refactor: Adds sequelize migrations

Owen Diffey 4 hónapja
szülő
commit
ca4f78ef1a

+ 2 - 6
backend/src/modules/DataModule.ts

@@ -91,9 +91,9 @@ export class DataModule extends BaseModule {
 	public override async startup() {
 		await super.startup();
 
-		await this._setupSequelize();
+		await this._runMigrations();
 
-		// await this._runMigrations();
+		await this._setupSequelize();
 
 		await super._started();
 	}
@@ -181,10 +181,6 @@ export class DataModule extends BaseModule {
 		);
 
 		await forEachIn(setupFunctions, setup => setup());
-
-		await this._sequelize.sync();
-
-		await this._runMigrations();
 	}
 
 	/**

+ 35 - 0
backend/src/modules/DataModule/migrations/1725485641-create-users-table.ts

@@ -0,0 +1,35 @@
+import { Sequelize, DataTypes } from "sequelize";
+import { MigrationParams } from "umzug";
+
+export const up = async ({
+	context: sequelize
+}: MigrationParams<Sequelize>) => {
+	await sequelize.getQueryInterface().createTable("users", {
+		_id: {
+			type: DataTypes.OBJECTID,
+			primaryKey: true,
+			allowNull: false
+		},
+		username: {
+			type: DataTypes.STRING,
+			allowNull: false
+		},
+		name: {
+			type: DataTypes.STRING,
+			allowNull: false
+		},
+		role: {
+			type: DataTypes.ENUM("admin", "moderator", "user"),
+			defaultValue: "user",
+			allowNull: false
+		},
+		createdAt: DataTypes.DATE,
+		updatedAt: DataTypes.DATE,
+	});
+};
+
+export const down = async ({
+	context: sequelize
+}: MigrationParams<Sequelize>) => {
+	await sequelize.getQueryInterface().dropTable("users");
+};

+ 0 - 3
backend/src/modules/DataModule/migrations/1725485642-create-minimal-users-view.ts → backend/src/modules/DataModule/migrations/1725485642-create-minified-users-view.ts

@@ -4,9 +4,6 @@ import { MigrationParams } from "umzug";
 export const up = async ({
 	context: sequelize
 }: MigrationParams<Sequelize>) => {
-	// TODO: Remove this when sync is no longer used
-	await sequelize.getQueryInterface().dropTable("minifiedUsers");
-
 	await sequelize.query(
 		'CREATE VIEW "minifiedUsers" AS SELECT _id, username, name, role FROM users',
 		{

+ 37 - 0
backend/src/modules/DataModule/migrations/1725660137-create-sessions-table.ts

@@ -0,0 +1,37 @@
+import { Sequelize, DataTypes } from "sequelize";
+import { MigrationParams } from "umzug";
+
+export const up = async ({
+	context: sequelize
+}: MigrationParams<Sequelize>) => {
+	await sequelize.getQueryInterface().createTable("sessions", {
+		sessionId: {
+			type: DataTypes.OBJECTID,
+			allowNull: false,
+			primaryKey: true
+		},
+		userId: {
+			type: DataTypes.OBJECTID,
+			allowNull: false
+		},
+		createdAt: DataTypes.DATE,
+		updatedAt: DataTypes.DATE,
+	});
+
+	await sequelize.getQueryInterface().addConstraint("sessions", {
+		type: "foreign key",
+		fields: ["userId"],
+		references: {
+			table: "users",
+			field: "_id"
+		},
+		onDelete: "RESTRICT",
+		onUpdate: "RESTRICT"
+	});
+};
+
+export const down = async ({
+	context: sequelize
+}: MigrationParams<Sequelize>) => {
+	await sequelize.getQueryInterface().dropTable("sessions");
+};

+ 55 - 0
backend/src/modules/DataModule/migrations/1725660211-create-news-table.ts

@@ -0,0 +1,55 @@
+import { Sequelize, DataTypes } from "sequelize";
+import { MigrationParams } from "umzug";
+
+export const up = async ({
+	context: sequelize
+}: MigrationParams<Sequelize>) => {
+	await sequelize.getQueryInterface().createTable("news", {
+		_id: {
+			type: DataTypes.OBJECTID,
+			allowNull: false,
+			primaryKey: true
+		},
+		title: {
+			type: DataTypes.STRING,
+			allowNull: false
+		},
+		markdown: {
+			type: DataTypes.TEXT,
+			allowNull: false
+		},
+		status: {
+			type: DataTypes.ENUM("draft", "published", "archived"),
+			defaultValue: "draft",
+			allowNull: false
+		},
+		showToNewUsers: {
+			type: DataTypes.BOOLEAN,
+			defaultValue: false,
+			allowNull: false
+		},
+		createdBy: {
+			type: DataTypes.OBJECTID,
+			allowNull: false
+		},
+		createdAt: DataTypes.DATE,
+		updatedAt: DataTypes.DATE,
+	});
+
+	await sequelize.getQueryInterface().addConstraint("news", {
+		type: "foreign key",
+		fields: ["createdBy"],
+		references: {
+			table: "users",
+			field: "_id"
+		},
+		onDelete: "RESTRICT",
+		onUpdate: "RESTRICT"
+	});
+};
+
+export const down = async ({
+	context: sequelize
+}: MigrationParams<Sequelize>) => {
+	await sequelize.getQueryInterface().dropTable("news");
+};

+ 0 - 4
backend/src/modules/DataModule/models/User.ts

@@ -119,10 +119,6 @@ export const schema = {
 		defaultValue: UserRole.USER,
 		allowNull: false
 	},
-	// createdBy: {
-	// 	type: DataTypes.OBJECTID,
-	// 	allowNull: false
-	// },
 	createdAt: DataTypes.DATE,
 	updatedAt: DataTypes.DATE,
 	_name: {