mirror of
https://github.com/Portabase/portabase.git
synced 2026-07-14 11:16:13 +02:00
chore: migration for the storage-backend feature
This commit is contained in:
@@ -0,0 +1,2 @@
|
|||||||
|
ALTER TABLE "settings" ADD COLUMN "default_storage_channel_id" uuid;--> statement-breakpoint
|
||||||
|
ALTER TABLE "settings" ADD CONSTRAINT "settings_default_storage_channel_id_storage_channel_id_fk" FOREIGN KEY ("default_storage_channel_id") REFERENCES "public"."storage_channel"("id") ON DELETE set null ON UPDATE no action;
|
||||||
@@ -0,0 +1,93 @@
|
|||||||
|
-- Custom SQL migration file, put your code below! --
|
||||||
|
CREATE EXTENSION IF NOT EXISTS "pgcrypto";
|
||||||
|
|
||||||
|
DO $$
|
||||||
|
DECLARE
|
||||||
|
s RECORD;
|
||||||
|
channel_id UUID;
|
||||||
|
existing_local RECORD;
|
||||||
|
b RECORD;
|
||||||
|
BEGIN
|
||||||
|
-- Get the current settings (assume single row)
|
||||||
|
SELECT * INTO s FROM settings LIMIT 1;
|
||||||
|
|
||||||
|
IF s.storage = 's3' THEN
|
||||||
|
-- Create a new S3 storage_channel
|
||||||
|
INSERT INTO storage_channel (
|
||||||
|
id, organization_id, provider, name, config, enabled, created_at, updated_at
|
||||||
|
)
|
||||||
|
VALUES (
|
||||||
|
gen_random_uuid(),
|
||||||
|
NULL,
|
||||||
|
's3',
|
||||||
|
'Default S3 Channel',
|
||||||
|
jsonb_build_object(
|
||||||
|
'endPointUrl', s.s3_endpoint_url,
|
||||||
|
'accessKey', s.s3_access_key_id,
|
||||||
|
'secretKey', s.s3_secret_access_key,
|
||||||
|
'bucketName', s.s3_bucket_name
|
||||||
|
),
|
||||||
|
true,
|
||||||
|
NOW(),
|
||||||
|
NOW()
|
||||||
|
)
|
||||||
|
RETURNING id INTO channel_id;
|
||||||
|
|
||||||
|
ELSE
|
||||||
|
-- local storage: check if a local channel exists
|
||||||
|
SELECT * INTO existing_local
|
||||||
|
FROM storage_channel
|
||||||
|
WHERE provider = 'local'
|
||||||
|
LIMIT 1;
|
||||||
|
|
||||||
|
IF existing_local IS NULL THEN
|
||||||
|
-- Create local channel
|
||||||
|
INSERT INTO storage_channel (
|
||||||
|
id, organization_id, provider, name, config, enabled, created_at, updated_at
|
||||||
|
)
|
||||||
|
VALUES (
|
||||||
|
gen_random_uuid(),
|
||||||
|
NULL,
|
||||||
|
'local',
|
||||||
|
'System',
|
||||||
|
'{}'::jsonb,
|
||||||
|
true,
|
||||||
|
NOW(),
|
||||||
|
NOW()
|
||||||
|
)
|
||||||
|
RETURNING id INTO channel_id;
|
||||||
|
ELSE
|
||||||
|
-- Update existing local channel
|
||||||
|
UPDATE storage_channel
|
||||||
|
SET name = 'System',
|
||||||
|
config = '{}'::jsonb,
|
||||||
|
enabled = true,
|
||||||
|
updated_at = NOW()
|
||||||
|
WHERE id = existing_local.id
|
||||||
|
RETURNING id INTO channel_id;
|
||||||
|
END IF;
|
||||||
|
END IF;
|
||||||
|
|
||||||
|
-- **Update settings.default_storage_channel_id for BOTH s3 and local**
|
||||||
|
UPDATE settings
|
||||||
|
SET default_storage_channel_id = channel_id
|
||||||
|
WHERE id = s.id;
|
||||||
|
|
||||||
|
-- For all backups, create backup_storage entries
|
||||||
|
FOR b IN SELECT * FROM backups LOOP
|
||||||
|
INSERT INTO backup_storage (
|
||||||
|
id, backup_id, storage_channel_id, status, path, size, checksum, created_at, updated_at
|
||||||
|
)
|
||||||
|
VALUES (
|
||||||
|
gen_random_uuid(),
|
||||||
|
b.id,
|
||||||
|
channel_id,
|
||||||
|
'success',
|
||||||
|
b.file,
|
||||||
|
b.file_size,
|
||||||
|
NULL,
|
||||||
|
NOW(),
|
||||||
|
NOW()
|
||||||
|
);
|
||||||
|
END LOOP;
|
||||||
|
END $$;
|
||||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -176,6 +176,13 @@
|
|||||||
"when": 1768412750113,
|
"when": 1768412750113,
|
||||||
"tag": "0024_lush_blindfold",
|
"tag": "0024_lush_blindfold",
|
||||||
"breakpoints": true
|
"breakpoints": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"idx": 25,
|
||||||
|
"version": "7",
|
||||||
|
"when": 1768424936753,
|
||||||
|
"tag": "0025_past_franklin_richards",
|
||||||
|
"breakpoints": true
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
+17
-14
@@ -1,22 +1,25 @@
|
|||||||
import { pgTable, timestamp, uuid, varchar } from "drizzle-orm/pg-core";
|
import {pgTable, timestamp, uuid, varchar} from "drizzle-orm/pg-core";
|
||||||
import { typeStorageEnum } from "./types";
|
import {typeStorageEnum} from "./types";
|
||||||
import { createSelectSchema } from "drizzle-zod";
|
import {createSelectSchema} from "drizzle-zod";
|
||||||
import { z } from "zod";
|
import {z} from "zod";
|
||||||
import {timestamps} from "@/db/schema/00_common";
|
import {timestamps} from "@/db/schema/00_common";
|
||||||
|
import {storageChannel} from "@/db/schema/12_storage-channel";
|
||||||
|
|
||||||
export const setting = pgTable("settings", {
|
export const setting = pgTable("settings", {
|
||||||
id: uuid("id").primaryKey().defaultRandom(),
|
id: uuid("id").primaryKey().defaultRandom(),
|
||||||
storage: typeStorageEnum("storage").default("local").notNull(),
|
storage: typeStorageEnum("storage").default("local").notNull(),
|
||||||
name: varchar("name", { length: 255 }).unique().notNull(),
|
name: varchar("name", {length: 255}).unique().notNull(),
|
||||||
s3EndPointUrl: varchar("s3_endpoint_url", { length: 255 }),
|
s3EndPointUrl: varchar("s3_endpoint_url", {length: 255}),
|
||||||
s3AccessKeyId: varchar("s3_access_key_id", { length: 255 }),
|
s3AccessKeyId: varchar("s3_access_key_id", {length: 255}),
|
||||||
s3SecretAccessKey: varchar("s3_secret_access_key", { length: 255 }),
|
s3SecretAccessKey: varchar("s3_secret_access_key", {length: 255}),
|
||||||
S3BucketName: varchar("s3_bucket_name", { length: 255 }),
|
S3BucketName: varchar("s3_bucket_name", {length: 255}),
|
||||||
smtpPassword: varchar("smtp_password", { length: 255 }),
|
smtpPassword: varchar("smtp_password", {length: 255}),
|
||||||
smtpFrom: varchar("smtp_from", { length: 255 }),
|
smtpFrom: varchar("smtp_from", {length: 255}),
|
||||||
smtpHost: varchar("smtp_host", { length: 255 }),
|
smtpHost: varchar("smtp_host", {length: 255}),
|
||||||
smtpPort: varchar("smtp_port", { length: 255 }),
|
smtpPort: varchar("smtp_port", {length: 255}),
|
||||||
smtpUser: varchar("smtp_user", { length: 255 }),
|
smtpUser: varchar("smtp_user", {length: 255}),
|
||||||
|
defaultStorageChannelId: uuid('default_storage_channel_id')
|
||||||
|
.references(() => storageChannel.id, {onDelete: "set null"}),
|
||||||
...timestamps
|
...timestamps
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
+16
-6
@@ -29,16 +29,16 @@ async function setupCronJobs() {
|
|||||||
async function createSettingsIfNotExist() {
|
async function createSettingsIfNotExist() {
|
||||||
const configSettings = {
|
const configSettings = {
|
||||||
name: "system",
|
name: "system",
|
||||||
storage: env.STORAGE_TYPE!,
|
// storage: env.STORAGE_TYPE!,
|
||||||
smtpPassword: env.SMTP_PASSWORD ?? null,
|
smtpPassword: env.SMTP_PASSWORD ?? null,
|
||||||
smtpFrom: env.SMTP_FROM ?? null,
|
smtpFrom: env.SMTP_FROM ?? null,
|
||||||
smtpHost: env.SMTP_HOST ?? null,
|
smtpHost: env.SMTP_HOST ?? null,
|
||||||
smtpPort: env.SMTP_PORT ?? null,
|
smtpPort: env.SMTP_PORT ?? null,
|
||||||
smtpUser: env.SMTP_USER ?? null,
|
smtpUser: env.SMTP_USER ?? null,
|
||||||
s3EndPointUrl: env.S3_ENDPOINT ?? null,
|
// s3EndPointUrl: env.S3_ENDPOINT ?? null,
|
||||||
s3AccessKeyId: env.S3_ACCESS_KEY ?? null,
|
// s3AccessKeyId: env.S3_ACCESS_KEY ?? null,
|
||||||
s3SecretAccessKey: env.S3_SECRET_KEY ?? null,
|
// s3SecretAccessKey: env.S3_SECRET_KEY ?? null,
|
||||||
S3BucketName: env.S3_BUCKET_NAME ?? null,
|
// S3BucketName: env.S3_BUCKET_NAME ?? null,
|
||||||
};
|
};
|
||||||
|
|
||||||
const [existing] = await db.select().from(drizzleDb.schemas.setting).where(eq(drizzleDb.schemas.setting.name, "system")).limit(1);
|
const [existing] = await db.select().from(drizzleDb.schemas.setting).where(eq(drizzleDb.schemas.setting.name, "system")).limit(1);
|
||||||
@@ -62,13 +62,23 @@ async function createSettingsIfNotExist() {
|
|||||||
|
|
||||||
if (!existingLocalChannelStorage) {
|
if (!existingLocalChannelStorage) {
|
||||||
console.log("====Local Storage : Create ====");
|
console.log("====Local Storage : Create ====");
|
||||||
await db.insert(drizzleDb.schemas.storageChannel).values(localChannelValues);
|
const [localChannelCreated] = await db.insert(drizzleDb.schemas.storageChannel).values(localChannelValues).returning();
|
||||||
|
|
||||||
|
if (localChannelCreated){
|
||||||
|
await db.update(drizzleDb.schemas.setting).set({
|
||||||
|
defaultStorageChannelId: localChannelCreated.id,
|
||||||
|
}).where(eq(drizzleDb.schemas.setting.name, "system"));
|
||||||
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
console.log("====Local Storage : Update ====");
|
console.log("====Local Storage : Update ====");
|
||||||
await db.update(drizzleDb.schemas.storageChannel).set(localChannelValues).where(eq(drizzleDb.schemas.storageChannel.provider, "local"));
|
await db.update(drizzleDb.schemas.storageChannel).set(localChannelValues).where(eq(drizzleDb.schemas.storageChannel.provider, "local"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function createDefaultOrganization() {
|
async function createDefaultOrganization() {
|
||||||
|
|||||||
Reference in New Issue
Block a user