Working state for attachments and buckets

This commit is contained in:
2024-06-12 18:08:08 +02:00
parent e84ce38604
commit c72ac5e67f
5 changed files with 156 additions and 56 deletions
+37 -10
View File
@@ -6,11 +6,42 @@ import { APIError} from "@/util/api/error"
import { UserAuth, parseBasicAuth, getAssociatedUser } from "@/util/api/user"
import { Auth, Post, Tag, User } from "@/model/Models";
import { Project } from "@/model/Project";
import { Attachment } from "@/model/Attachment";
import { Bucket } from "@/model/Bucket";
import { DBState } from "@/model/DBState";
import Sequelize, { DataTypes } from "@sequelize/core";
import { SqliteColumnsDescription, SqliteDialect } from "@sequelize/sqlite3";
import { SqliteColumnsDescription, SqliteDialect, SqliteQueryInterface } from "@sequelize/sqlite3";
import { hashPassword } from "@/util/Auth";
async function seedDatabase(queryInterface:SqliteQueryInterface<SqliteDialect>){
const password = await hashPassword('changeme');
const project = await Project.findOne({where: {
readableIdentifier: 'blog'
}}).then(e=> e ? e : Project.create({name:'General Blog',readableIdentifier:'blog'}));
const user = await User.findOne({where: {
username: 'admin'
}}).then(e=> e ? e : User.create({username: 'admin', password: password, perms: {isAdmin: true}}, {include: User.associations.perms}));
await Post.create({
title: 'Test Post',
content: `
# Hello <ExampleComponent />
this is some **test** markdown
`,
project_id: project.id,
user_id: user.id
})
await Post.create({
title: 'Test Post 2',
content: `
# Hello <ExampleComponent />
this is amother post with some **test** markdown
`,
project_id: project.id,
user_id: user.id
})
}
async function trySetup(request:Request){
const sequelize = await new Sequelize({
@@ -21,27 +52,23 @@ async function trySetup(request:Request){
// await User.sync();
await Auth.sync();
await User.sync();
await Attachment.sync();
await Bucket.sync();
await Project.sync()
await Tag.sync();
await Post.sync();
await DBState.sync();
const version = await (await DBState.findAll()).sort((a,b)=> ((a.version > b.version) ? 1 : -1)).map(a=>a.version)[0];
const version = (await DBState.findAll()).sort((a,b)=> ((a.version > b.version) ? 1 : -1)).map(a=>a.version)[0];
await Project.findOne({where: {
readableIdentifier: 'blog'
}}).then(e=> e ? e : Project.create({name:'General Blog',readableIdentifier:'blog'}));
await User.findOne({where: {
username: 'admin'
}}).then(e=> e ? e : User.create({username: 'admin', password: 'changeme', perms: {isAdmin: true}}));
switch(version){
case 1:
break;
default:
const postsRows:SqliteColumnsDescription = await queryInterface.describeTable('Posts').then(t=>t);
seedDatabase(queryInterface);
const postsRows:SqliteColumnsDescription = await queryInterface.describeTable(Post.table.tableName).then(t=>t);
if (!postsRows['project_id']) queryInterface.addColumn('Posts','project_id',{type: DataTypes.INTEGER, acceptsNull:()=>false,defaultValue:1})
break;
}