From 1fa656a012831d712db2ab7a2804cbefe4a62a15 Mon Sep 17 00:00:00 2001 From: cte Date: Tue, 13 May 2025 23:24:02 -0700 Subject: [PATCH] Add events table --- .docker/scripts/postgres/create-databases.sh | 2 +- migrations/0001_glamorous_thunderbird.sql | 8 + migrations/meta/0001_snapshot.json | 149 +++++++++++++++++++ migrations/meta/_journal.json | 7 + src/db/schema.ts | 19 ++- 5 files changed, 179 insertions(+), 6 deletions(-) create mode 100644 migrations/0001_glamorous_thunderbird.sql create mode 100644 migrations/meta/0001_snapshot.json diff --git a/.docker/scripts/postgres/create-databases.sh b/.docker/scripts/postgres/create-databases.sh index 481352f2c3..347f026f8a 100755 --- a/.docker/scripts/postgres/create-databases.sh +++ b/.docker/scripts/postgres/create-databases.sh @@ -6,6 +6,6 @@ set -u if [ -n "$POSTGRES_DATABASES" ]; then for db in $(echo $POSTGRES_DATABASES | tr ',' ' '); do echo "Creating $db..." - psql -v ON_ERROR_STOP=1 -c "CREATE DATABASE $db;" + psql -U postgres -v ON_ERROR_STOP=1 -c "CREATE DATABASE $db;" done fi diff --git a/migrations/0001_glamorous_thunderbird.sql b/migrations/0001_glamorous_thunderbird.sql new file mode 100644 index 0000000000..e7a22c4ef1 --- /dev/null +++ b/migrations/0001_glamorous_thunderbird.sql @@ -0,0 +1,8 @@ +CREATE TABLE "event" ( + "id" text PRIMARY KEY NOT NULL, + "type" text NOT NULL, + "timestamp" bigint NOT NULL, + "properties" json NOT NULL, + "created_at" timestamp DEFAULT now() NOT NULL, + "updated_at" timestamp DEFAULT now() NOT NULL +); diff --git a/migrations/meta/0001_snapshot.json b/migrations/meta/0001_snapshot.json new file mode 100644 index 0000000000..6383fb94f8 --- /dev/null +++ b/migrations/meta/0001_snapshot.json @@ -0,0 +1,149 @@ +{ + "id": "37608d29-a721-43ec-91e6-c4a356d23b3e", + "prevId": "013accf9-070a-47cd-9b49-1318a1c8a237", + "version": "7", + "dialect": "postgresql", + "tables": { + "public.event": { + "name": "event", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "type": { + "name": "type", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "timestamp": { + "name": "timestamp", + "type": "bigint", + "primaryKey": false, + "notNull": true + }, + "properties": { + "name": "properties", + "type": "json", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.organization": { + "name": "organization", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "stripe_customer_id": { + "name": "stripe_customer_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "stripe_subscription_id": { + "name": "stripe_subscription_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "stripe_subscription_price_id": { + "name": "stripe_subscription_price_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "stripe_subscription_status": { + "name": "stripe_subscription_status", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "stripe_subscription_current_period_end": { + "name": "stripe_subscription_current_period_end", + "type": "bigint", + "primaryKey": false, + "notNull": false + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "stripe_customer_id_idx": { + "name": "stripe_customer_id_idx", + "columns": [ + { + "expression": "stripe_customer_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + } + }, + "enums": {}, + "schemas": {}, + "sequences": {}, + "roles": {}, + "policies": {}, + "views": {}, + "_meta": { + "columns": {}, + "schemas": {}, + "tables": {} + } +} diff --git a/migrations/meta/_journal.json b/migrations/meta/_journal.json index c6f820ea20..35730d6981 100644 --- a/migrations/meta/_journal.json +++ b/migrations/meta/_journal.json @@ -8,6 +8,13 @@ "when": 1747031111132, "tag": "0000_misty_leo", "breakpoints": true + }, + { + "idx": 1, + "version": "7", + "when": 1747203099634, + "tag": "0001_glamorous_thunderbird", + "breakpoints": true } ] } diff --git a/src/db/schema.ts b/src/db/schema.ts index 94ca4c5e2a..06f42c482c 100644 --- a/src/db/schema.ts +++ b/src/db/schema.ts @@ -4,6 +4,7 @@ import { text, timestamp, uniqueIndex, + json, } from 'drizzle-orm/pg-core'; export const organizationSchema = pgTable( @@ -24,9 +25,17 @@ export const organizationSchema = pgTable( .notNull(), createdAt: timestamp('created_at', { mode: 'date' }).defaultNow().notNull(), }, - (table) => ({ - stripeCustomerIdIdx: uniqueIndex('stripe_customer_id_idx').on( - table.stripeCustomerId, - ), - }), + (table) => [uniqueIndex('stripe_customer_id_idx').on(table.stripeCustomerId)], ); + +export const eventSchema = pgTable('event', { + id: text('id').primaryKey(), + type: text('type').notNull(), + timestamp: bigint('timestamp', { mode: 'number' }).notNull(), + properties: json('properties').notNull(), + createdAt: timestamp('created_at', { mode: 'date' }).defaultNow().notNull(), + updatedAt: timestamp('updated_at', { mode: 'date' }) + .defaultNow() + .$onUpdate(() => new Date()) + .notNull(), +});