improve: prepare for coolify and general self-hosting improvements (#175)
* fix(self-hosting): improve docker compose, add healthchecks, rename env SELF_HOSTED * improve(db): improve initial migration when no data exists * fix(db): misstakes were made * improve(dashboard): better curl preview depending on project type * fix(db): fix migrations * fix(onboarding): ensure we publish event correctly * wip * fix: curl preview * add coolify template * fix(dashboard): page -> route * fix * fix env
This commit is contained in:
committed by
GitHub
parent
4a2dbc5c4d
commit
92d62c3e5c
@@ -11,7 +11,7 @@ import {
|
||||
renameTable,
|
||||
runClickhouseMigrationCommands,
|
||||
} from '../src/clickhouse/migration';
|
||||
import { printBoxMessage } from './helpers';
|
||||
import { getIsSelfHosting, printBoxMessage } from './helpers';
|
||||
|
||||
export async function up() {
|
||||
const replicatedVersion = '1';
|
||||
@@ -25,7 +25,7 @@ export async function up() {
|
||||
'profile_aliases_distributed',
|
||||
);
|
||||
|
||||
const isSelfHosting = !!process.env.SELF_HOSTED;
|
||||
const isSelfHosting = getIsSelfHosting();
|
||||
const isClustered = !isSelfHosting;
|
||||
|
||||
const isSelfHostingPostCluster =
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { formatClickhouseDate } from '../src/clickhouse/client';
|
||||
import { TABLE_NAMES, formatClickhouseDate } from '../src/clickhouse/client';
|
||||
import {
|
||||
chMigrationClient,
|
||||
createTable,
|
||||
runClickhouseMigrationCommands,
|
||||
} from '../src/clickhouse/migration';
|
||||
@@ -66,7 +67,7 @@ export async function up() {
|
||||
}),
|
||||
];
|
||||
|
||||
sqls.push(...createOldSessions());
|
||||
sqls.push(...(await createOldSessions()));
|
||||
|
||||
fs.writeFileSync(
|
||||
path.join(__filename.replace('.ts', '.sql')),
|
||||
@@ -86,8 +87,31 @@ export async function up() {
|
||||
}
|
||||
}
|
||||
|
||||
function createOldSessions() {
|
||||
let startDate = new Date('2024-03-01');
|
||||
async function createOldSessions() {
|
||||
async function getFirstEventAt() {
|
||||
const defaultDate = new Date('2024-03-01');
|
||||
try {
|
||||
const res = await chMigrationClient.query({
|
||||
query: `SELECT min(created_at) as created_at, count() as count FROM ${TABLE_NAMES.events}`,
|
||||
format: 'JSONEachRow',
|
||||
});
|
||||
const json = await res.json<{ created_at: string; count: string }>();
|
||||
const row = json[0];
|
||||
if (!row || row.count === '0') {
|
||||
return null;
|
||||
}
|
||||
return new Date(row.created_at);
|
||||
} catch (e) {
|
||||
return defaultDate;
|
||||
}
|
||||
}
|
||||
|
||||
let startDate = await getFirstEventAt();
|
||||
|
||||
if (!startDate) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const endDate = new Date();
|
||||
const sqls: string[] = [];
|
||||
while (startDate <= endDate) {
|
||||
|
||||
@@ -24,7 +24,9 @@ export function getIsCluster() {
|
||||
}
|
||||
|
||||
export function getIsSelfHosting() {
|
||||
return !!process.env.SELF_HOSTED;
|
||||
return (
|
||||
process.env.NEXT_PUBLIC_SELF_HOSTED === 'true' || !!process.env.SELF_HOSTED
|
||||
);
|
||||
}
|
||||
|
||||
export function getIsDry() {
|
||||
|
||||
@@ -261,7 +261,7 @@ return "OK"
|
||||
if (!_multi) {
|
||||
await multi.exec();
|
||||
}
|
||||
await publishEvent('events', 'received', transformEvent(event), multi);
|
||||
await publishEvent('events', 'received', transformEvent(event));
|
||||
} catch (error) {
|
||||
this.logger.error('Failed to add event to Redis buffer', { error });
|
||||
}
|
||||
|
||||
@@ -59,12 +59,20 @@ const getPrismaClient = () => {
|
||||
subscriptionStatus: {
|
||||
needs: { subscriptionStatus: true, subscriptionCanceledAt: true },
|
||||
compute(org) {
|
||||
if (process.env.NEXT_PUBLIC_SELF_HOSTED === 'true') {
|
||||
return 'active';
|
||||
}
|
||||
|
||||
return org.subscriptionStatus || 'trialing';
|
||||
},
|
||||
},
|
||||
hasSubscription: {
|
||||
needs: { subscriptionStatus: true, subscriptionEndsAt: true },
|
||||
compute(org) {
|
||||
if (process.env.NEXT_PUBLIC_SELF_HOSTED === 'true') {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (
|
||||
[null, 'canceled', 'trialing'].includes(org.subscriptionStatus)
|
||||
) {
|
||||
@@ -86,6 +94,10 @@ const getPrismaClient = () => {
|
||||
subscriptionPeriodEventsCountExceededAt: true,
|
||||
},
|
||||
compute(org) {
|
||||
if (process.env.NEXT_PUBLIC_SELF_HOSTED === 'true') {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (
|
||||
org.subscriptionEndsAt &&
|
||||
org.subscriptionPeriodEventsCountExceededAt
|
||||
@@ -119,6 +131,10 @@ const getPrismaClient = () => {
|
||||
isCanceled: {
|
||||
needs: { subscriptionStatus: true, subscriptionCanceledAt: true },
|
||||
compute(org) {
|
||||
if (process.env.NEXT_PUBLIC_SELF_HOSTED === 'true') {
|
||||
return false;
|
||||
}
|
||||
|
||||
return isCanceled(org);
|
||||
},
|
||||
},
|
||||
@@ -129,6 +145,10 @@ const getPrismaClient = () => {
|
||||
subscriptionEndsAt: true,
|
||||
},
|
||||
compute(org) {
|
||||
if (process.env.NEXT_PUBLIC_SELF_HOSTED === 'true') {
|
||||
return false;
|
||||
}
|
||||
|
||||
return isWillBeCanceled(org);
|
||||
},
|
||||
},
|
||||
@@ -139,6 +159,10 @@ const getPrismaClient = () => {
|
||||
subscriptionCanceledAt: true,
|
||||
},
|
||||
compute(org) {
|
||||
if (process.env.NEXT_PUBLIC_SELF_HOSTED === 'true') {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (isCanceled(org)) {
|
||||
return false;
|
||||
}
|
||||
@@ -158,6 +182,10 @@ const getPrismaClient = () => {
|
||||
subscriptionPeriodEventsLimit: true,
|
||||
},
|
||||
compute(org) {
|
||||
if (process.env.NEXT_PUBLIC_SELF_HOSTED === 'true') {
|
||||
return false;
|
||||
}
|
||||
|
||||
return (
|
||||
org.subscriptionPeriodEventsCount >
|
||||
org.subscriptionPeriodEventsLimit
|
||||
@@ -167,7 +195,13 @@ const getPrismaClient = () => {
|
||||
subscriptionCurrentPeriodStart: {
|
||||
needs: { subscriptionStartsAt: true, subscriptionInterval: true },
|
||||
compute(org) {
|
||||
if (!org.subscriptionStartsAt) return org.subscriptionStartsAt;
|
||||
if (process.env.NEXT_PUBLIC_SELF_HOSTED === 'true') {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!org.subscriptionStartsAt) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (org.subscriptionInterval === 'year') {
|
||||
const startDay = org.subscriptionStartsAt.getUTCDate();
|
||||
@@ -195,7 +229,13 @@ const getPrismaClient = () => {
|
||||
subscriptionInterval: true,
|
||||
},
|
||||
compute(org) {
|
||||
if (!org.subscriptionStartsAt) return org.subscriptionEndsAt;
|
||||
if (process.env.NEXT_PUBLIC_SELF_HOSTED === 'true') {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!org.subscriptionStartsAt) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (org.subscriptionInterval === 'year') {
|
||||
const startDay = org.subscriptionStartsAt.getUTCDate();
|
||||
|
||||
Reference in New Issue
Block a user