fix: outdated config schema (#1769)

This commit is contained in:
Manuel
2023-12-30 21:54:48 +01:00
committed by GitHub
parent b9a5b5ed09
commit 108803ae2a
3 changed files with 50 additions and 11 deletions

View File

@@ -12,6 +12,9 @@ import { boardCustomizationSchema } from '~/validations/boards';
import { IRssWidget } from '~/widgets/rss/RssWidgetTile';
import { adminProcedure, createTRPCRouter, publicProcedure } from '../trpc';
import { db } from '~/server/db';
import { users } from '~/server/db/schema';
import { sql } from 'drizzle-orm';
export const configNameSchema = z.string().regex(/^[a-zA-Z0-9-_]+$/);
@@ -20,14 +23,14 @@ export const configRouter = createTRPCRouter({
.input(
z.object({
name: configNameSchema,
})
}),
)
.mutation(async ({ input }) => {
if (input.name.toLowerCase() === 'default') {
Consola.error("Rejected config deletion because default configuration can't be deleted");
Consola.error('Rejected config deletion because default configuration can\'t be deleted');
throw new TRPCError({
code: 'FORBIDDEN',
message: "Default config can't be deleted",
message: 'Default config can\'t be deleted',
});
}
@@ -44,7 +47,7 @@ export const configRouter = createTRPCRouter({
// If the target is not in the list of files, return an error
if (!matchedFile) {
Consola.error(
`Rejected config deletion request because config name '${input.name}' was not included in present configurations`
`Rejected config deletion request because config name '${input.name}' was not included in present configurations`,
);
throw new TRPCError({
code: 'NOT_FOUND',
@@ -64,7 +67,7 @@ export const configRouter = createTRPCRouter({
z.object({
name: configNameSchema,
config: z.custom<ConfigType>((x) => !!x && typeof x === 'object'),
})
}),
)
.mutation(async ({ input }) => {
Consola.info(`Saving updated configuration of '${input.name}' config.`);
@@ -96,16 +99,16 @@ export const configRouter = createTRPCRouter({
}
const previousApp = previousConfig.apps.find(
(previousApp) => previousApp.id === app.id
(previousApp) => previousApp.id === app.id,
);
const previousProperty = previousApp?.integration?.properties.find(
(previousProperty) => previousProperty.field === property.field
(previousProperty) => previousProperty.field === property.field,
);
if (property.value !== undefined && property.value !== null) {
Consola.info(
'Detected credential change of private secret. Value will be overwritten in configuration'
'Detected credential change of private secret. Value will be overwritten in configuration',
);
return {
field: property.field,
@@ -165,7 +168,7 @@ export const configRouter = createTRPCRouter({
.input(
z.object({
name: configNameSchema,
})
}),
)
.query(async ({ ctx, input }) => {
if (!configExists(input.name)) {
@@ -223,4 +226,21 @@ export const configRouter = createTRPCRouter({
const targetPath = path.join('data/configs', `${input.name}.json`);
fs.writeFileSync(targetPath, JSON.stringify(newConfig, null, 2), 'utf8');
}),
// publicProcedure is not optimal, but should be fince, since there is no input and output data nor can you break the config
updateConfigurationSchemaToLatest: publicProcedure.mutation(async () => {
const files = fs.readdirSync('./data/configs').filter((file) => file.endsWith('.json'));
console.log('updating the schema version of', files.length, 'configurations');
for (const file of files) {
const name = file.replace('.json', '');
const config = await getFrontendConfig(name);
config.schemaVersion = 2;
const targetPath = `data/configs/${name}.json`;
fs.writeFileSync(targetPath, JSON.stringify(config, null, 2), 'utf8');
console.log('updated', name, 'to schema version', config.schemaVersion);
}
}),
});