🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.
As schema (#671)
Bill Yang81f926484401
diff --git a/server/drizzle.config.ts b/server/drizzle.config.tsindex 56dcdd319..612f39672 100644--- a/server/drizzle.config.ts+++ b/server/drizzle.config.ts@@ -16,5 +16,5 @@ export default defineConfig({ssl: false,},verbose: true,- tablesFilter: ['!pg_*', '!as_*'],+ schemaFilter: ["public"],});diff --git a/server/src/api/as/activate.ts b/server/src/api/as/activate.tsindex 6f40d655a..9b1332458 100644--- a/server/src/api/as/activate.ts+++ b/server/src/api/as/activate.ts@@ -148,7 +148,7 @@ export async function activateAppSumoLicense(// Step 3: Check if license already existsconst existingLicense = await db.execute(- sql`SELECT organization_id FROM as_licenses WHERE license_key = ${licenseData.licenseKey} LIMIT 1`+ sql`SELECT organization_id FROM appsumo.licenses WHERE license_key = ${licenseData.licenseKey} LIMIT 1`);if (Array.isArray(existingLicense) && existingLicense.length > 0) {@@ -163,7 +163,7 @@ export async function activateAppSumoLicense(// License exists as placeholder (from purchase webhook) - update itawait db.execute(sql`- UPDATE as_licenses+ UPDATE appsumo.licensesSETorganization_id = ${organizationId},tier = ${licenseData.tier},@@ -175,7 +175,7 @@ export async function activateAppSumoLicense(} else {// Step 4: License doesn't exist - create new recordawait db.execute(sql`- INSERT INTO as_licenses (+ INSERT INTO appsumo.licenses (organization_id,license_key,tier,diff --git a/server/src/api/as/webhook.ts b/server/src/api/as/webhook.tsindex 5c5d8144e..889e7dc73 100644--- a/server/src/api/as/webhook.ts+++ b/server/src/api/as/webhook.ts@@ -96,7 +96,7 @@ export async function handleAppSumoWebhook(// Log webhook event for audit trailawait db.execute(sql`- INSERT INTO as_webhook_events (+ INSERT INTO appsumo.webhook_events (license_key,event,payload,@@ -197,14 +197,14 @@ async function handlePurchaseEvent(// Check if license already existsconst existing = await db.execute(- sql`SELECT id FROM as_licenses WHERE license_key = ${licenseKey} LIMIT 1`+ sql`SELECT id FROM appsumo.licenses WHERE license_key = ${licenseKey} LIMIT 1`);if (Array.isArray(existing) && existing.length === 0) {// Create placeholder - will be linked to org when user activatesconsole.log(`[AppSumo] Creating new pending license ${licenseKey}`);await db.execute(sql`- INSERT INTO as_licenses (+ INSERT INTO appsumo.licenses (organization_id,license_key,tier,@@ -237,7 +237,7 @@ async function handleActivateEvent(licenseKey: string, tier: any) {console.log(`[AppSumo] handleActivateEvent - license: ${licenseKey}, tier: ${tierValue}`);await db.execute(sql`- UPDATE as_licenses+ UPDATE appsumo.licensesSETstatus = 'active',tier = ${tierValue},@@ -263,7 +263,7 @@ async function handleUpgradeEvent(licenseKey: string, tier: any, prevLicenseKey?// Get the old license to find the organizationconsole.log(`[AppSumo] Querying old license: ${prevLicenseKey}`);let oldLicenseResult = await db.execute(- sql`SELECT organization_id FROM as_licenses WHERE license_key = ${prevLicenseKey} LIMIT 1`+ sql`SELECT organization_id FROM appsumo.licenses WHERE license_key = ${prevLicenseKey} LIMIT 1`);console.log(`[AppSumo] Old license query result:`, JSON.stringify(oldLicenseResult, null, 2));@@ -272,7 +272,7 @@ async function handleUpgradeEvent(licenseKey: string, tier: any, prevLicenseKey?if (!Array.isArray(oldLicenseResult) || oldLicenseResult.length === 0) {console.warn(`[AppSumo] Old license not found: ${prevLicenseKey}, searching for any license with organization as fallback`);oldLicenseResult = await db.execute(- sql`SELECT organization_id FROM as_licenses WHERE organization_id IS NOT NULL ORDER BY updated_at DESC LIMIT 1`+ sql`SELECT organization_id FROM appsumo.licenses WHERE organization_id IS NOT NULL ORDER BY updated_at DESC LIMIT 1`);if (!Array.isArray(oldLicenseResult) || oldLicenseResult.length === 0) {@@ -292,7 +292,7 @@ async function handleUpgradeEvent(licenseKey: string, tier: any, prevLicenseKey?console.log(`[AppSumo] Organization exists - creating active license for ${licenseKey}`);try {await db.execute(sql`- INSERT INTO as_licenses (+ INSERT INTO appsumo.licenses (organization_id,license_key,tier,@@ -326,7 +326,7 @@ async function handleUpgradeEvent(licenseKey: string, tier: any, prevLicenseKey?console.log(`[AppSumo] No organization - creating pending license for ${licenseKey}`);try {await db.execute(sql`- INSERT INTO as_licenses (+ INSERT INTO appsumo.licenses (organization_id,license_key,tier,@@ -357,7 +357,7 @@ async function handleUpgradeEvent(licenseKey: string, tier: any, prevLicenseKey?console.log(`[AppSumo] Deactivating old license ${prevLicenseKey}`);try {await db.execute(sql`- UPDATE as_licenses+ UPDATE appsumo.licensesSETstatus = 'inactive',deactivated_at = NOW(),@@ -386,7 +386,7 @@ async function handleDowngradeEvent(licenseKey: string, tier: any, prevLicenseKe// Get the old license to find the organizationconsole.log(`[AppSumo] Querying old license: ${prevLicenseKey}`);let oldLicenseResult = await db.execute(- sql`SELECT organization_id FROM as_licenses WHERE license_key = ${prevLicenseKey} LIMIT 1`+ sql`SELECT organization_id FROM appsumo.licenses WHERE license_key = ${prevLicenseKey} LIMIT 1`);console.log(`[AppSumo] Old license query result:`, JSON.stringify(oldLicenseResult, null, 2));@@ -395,7 +395,7 @@ async function handleDowngradeEvent(licenseKey: string, tier: any, prevLicenseKeif (!Array.isArray(oldLicenseResult) || oldLicenseResult.length === 0) {console.warn(`[AppSumo] Old license not found: ${prevLicenseKey}, searching for any license with organization as fallback`);oldLicenseResult = await db.execute(- sql`SELECT organization_id FROM as_licenses WHERE organization_id IS NOT NULL ORDER BY updated_at DESC LIMIT 1`+ sql`SELECT organization_id FROM appsumo.licenses WHERE organization_id IS NOT NULL ORDER BY updated_at DESC LIMIT 1`);if (!Array.isArray(oldLicenseResult) || oldLicenseResult.length === 0) {@@ -415,7 +415,7 @@ async function handleDowngradeEvent(licenseKey: string, tier: any, prevLicenseKeconsole.log(`[AppSumo] Organization exists - creating active license for ${licenseKey}`);try {await db.execute(sql`- INSERT INTO as_licenses (+ INSERT INTO appsumo.licenses (organization_id,license_key,tier,@@ -449,7 +449,7 @@ async function handleDowngradeEvent(licenseKey: string, tier: any, prevLicenseKeconsole.log(`[AppSumo] No organization - creating pending license for ${licenseKey}`);try {await db.execute(sql`- INSERT INTO as_licenses (+ INSERT INTO appsumo.licenses (organization_id,license_key,tier,@@ -480,7 +480,7 @@ async function handleDowngradeEvent(licenseKey: string, tier: any, prevLicenseKeconsole.log(`[AppSumo] Deactivating old license ${prevLicenseKey}`);try {await db.execute(sql`- UPDATE as_licenses+ UPDATE appsumo.licensesSETstatus = 'inactive',deactivated_at = NOW(),@@ -500,7 +500,7 @@ async function handleDowngradeEvent(licenseKey: string, tier: any, prevLicenseKeasync function handleDeactivateEvent(licenseKey: string) {console.log(`[AppSumo] handleDeactivateEvent - license: ${licenseKey}`);await db.execute(sql`- UPDATE as_licenses+ UPDATE appsumo.licensesSETstatus = 'inactive',deactivated_at = NOW(),@@ -522,7 +522,7 @@ async function handleMigrateEvent(console.log(`[AppSumo] handleMigrateEvent - license: ${licenseKey}, tier: ${tierValue}, parent: ${parentLicenseKey}`);await db.execute(sql`- UPDATE as_licenses+ UPDATE appsumo.licensesSETtier = ${tierValue},parent_license_key = ${parentLicenseKey || null},diff --git a/server/src/db/postgres/initPostgres.ts b/server/src/db/postgres/initPostgres.tsindex 0c0103f25..8c3dcf8f9 100644--- a/server/src/db/postgres/initPostgres.ts+++ b/server/src/db/postgres/initPostgres.ts@@ -27,10 +27,14 @@ export const initPostgres = async () => {* This runs only in cloud environments (IS_CLOUD === true)*/async function initializeAppSumoTables() {+ console.info("Initializing AppSumo tables...");try {- // Create as_licenses table+ // Create 'appsumo' schema for AppSumo tables+ await db.execute(sql`CREATE SCHEMA IF NOT EXISTS appsumo`);++ // Create appsumo.licenses tableawait db.execute(sql`- CREATE TABLE IF NOT EXISTS as_licenses (+ CREATE TABLE IF NOT EXISTS appsumo.licenses (id SERIAL PRIMARY KEY NOT NULL,organization_id TEXT REFERENCES organization(id),license_key TEXT NOT NULL UNIQUE,@@ -44,9 +48,9 @@ async function initializeAppSumoTables() {)`);- // Create as_webhook_events table for audit trail+ // Create appsumo.webhook_events table for audit trailawait db.execute(sql`- CREATE TABLE IF NOT EXISTS as_webhook_events (+ CREATE TABLE IF NOT EXISTS appsumo.webhook_events (id SERIAL PRIMARY KEY NOT NULL,license_key TEXT NOT NULL,event TEXT NOT NULL,@@ -56,7 +60,7 @@ async function initializeAppSumoTables() {)`);- console.info("AppSumo tables initialized successfully");+ console.info("AppSumo schema and tables initialized successfully");} catch (error) {console.error("Error initializing AppSumo tables:", error);}diff --git a/server/src/db/postgres/schema-appsumo.ts b/server/src/db/postgres/schema-appsumo.tsindex b18b36fca..81f2c936d 100644--- a/server/src/db/postgres/schema-appsumo.ts+++ b/server/src/db/postgres/schema-appsumo.ts@@ -1,8 +1,11 @@-import { pgTable, serial, text, timestamp } from "drizzle-orm/pg-core";+import { pgSchema, serial, text, timestamp } from "drizzle-orm/pg-core";import { organization } from "./schema.js";+// Create the 'appsumo' schema for AppSumo tables+export const asSchema = pgSchema("appsumo");+// AppSumo licenses table (only created in cloud environments)-export const asLicenses = pgTable("as_licenses", {+export const asLicenses = asSchema.table("licenses", {id: serial("id").primaryKey().notNull(),organizationId: text("organization_id").references(() => organization.id),licenseKey: text("license_key").notNull().unique(),@@ -16,7 +19,7 @@ export const asLicenses = pgTable("as_licenses", {});// Webhook events log for audit trail and idempotency-export const asWebhookEvents = pgTable("as_webhook_events", {+export const asWebhookEvents = asSchema.table("webhook_events", {id: serial("id").primaryKey().notNull(),licenseKey: text("license_key").notNull(),event: text("event").notNull(), // 'purchase', 'activate', 'upgrade', 'downgrade', 'deactivate', 'migrate'diff --git a/server/src/lib/subscriptionUtils.ts b/server/src/lib/subscriptionUtils.tsindex f908eea60..469b8377c 100644--- a/server/src/lib/subscriptionUtils.ts+++ b/server/src/lib/subscriptionUtils.ts@@ -56,7 +56,7 @@ function getStartOfMonth(): string {export async function getAppSumoSubscription(organizationId: string): Promise<AppSumoSubscriptionInfo | null> {try {const appsumoLicense = await db.execute(- sql`SELECT tier, status FROM as_licenses WHERE organization_id = ${organizationId} AND status = 'active' LIMIT 1`+ sql`SELECT tier, status FROM appsumo.licenses WHERE organization_id = ${organizationId} AND status = 'active' LIMIT 1`);if (Array.isArray(appsumoLicense) && appsumoLicense.length > 0) {