@hogsend/db 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/LICENSE +93 -0
  2. package/README.md +14 -0
  3. package/drizzle/0000_nifty_songbird.sql +188 -0
  4. package/drizzle/0001_minor_shockwave.sql +13 -0
  5. package/drizzle/0002_early_owl.sql +1 -0
  6. package/drizzle/0003_bizarre_annihilus.sql +9 -0
  7. package/drizzle/0004_brave_betty_brant.sql +1 -0
  8. package/drizzle/0005_groovy_princess_powerful.sql +8 -0
  9. package/drizzle/0006_groovy_charles_xavier.sql +100 -0
  10. package/drizzle/0007_serious_captain_universe.sql +1 -0
  11. package/drizzle/0008_demonic_agent_brand.sql +5 -0
  12. package/drizzle/meta/0000_snapshot.json +1264 -0
  13. package/drizzle/meta/0001_snapshot.json +1353 -0
  14. package/drizzle/meta/0002_snapshot.json +1380 -0
  15. package/drizzle/meta/0003_snapshot.json +1443 -0
  16. package/drizzle/meta/0004_snapshot.json +1464 -0
  17. package/drizzle/meta/0005_snapshot.json +1588 -0
  18. package/drizzle/meta/0006_snapshot.json +2331 -0
  19. package/drizzle/meta/0007_snapshot.json +2346 -0
  20. package/drizzle/meta/0008_snapshot.json +2449 -0
  21. package/drizzle/meta/_journal.json +69 -0
  22. package/package.json +49 -0
  23. package/src/index.ts +35 -0
  24. package/src/migrate-client.ts +56 -0
  25. package/src/migrate.ts +173 -0
  26. package/src/schema/_shared.ts +10 -0
  27. package/src/schema/alert-history.ts +21 -0
  28. package/src/schema/alert-rules.ts +36 -0
  29. package/src/schema/api-keys.ts +30 -0
  30. package/src/schema/audit-logs.ts +22 -0
  31. package/src/schema/auth.ts +89 -0
  32. package/src/schema/contacts.ts +31 -0
  33. package/src/schema/dead-letter-queue.ts +31 -0
  34. package/src/schema/email-preferences.ts +35 -0
  35. package/src/schema/email-sends.ts +34 -0
  36. package/src/schema/enums.ts +47 -0
  37. package/src/schema/import-jobs.ts +26 -0
  38. package/src/schema/index.ts +18 -0
  39. package/src/schema/journey-configs.ts +15 -0
  40. package/src/schema/journey-logs.ts +21 -0
  41. package/src/schema/journey-states.ts +54 -0
  42. package/src/schema/link-clicks.ts +21 -0
  43. package/src/schema/relations.ts +160 -0
  44. package/src/schema/tracked-links.ts +17 -0
  45. package/src/schema/user-events.ts +35 -0
  46. package/src/seed.ts +91 -0
  47. package/src/version.ts +162 -0
package/LICENSE ADDED
@@ -0,0 +1,93 @@
1
+ Elastic License 2.0 (ELv2)
2
+
3
+ Copyright 2025 Doug Silkstone and Hogsend contributors
4
+
5
+ ## Acceptance
6
+
7
+ By using the software, you agree to all of the terms and conditions below.
8
+
9
+ ## Copyright License
10
+
11
+ The licensor grants you a non-exclusive, royalty-free, worldwide,
12
+ non-sublicensable, non-transferable license to use, copy, distribute, make
13
+ available, and prepare derivative works of the software, in each case subject to
14
+ the limitations and conditions below.
15
+
16
+ ## Limitations
17
+
18
+ You may not provide the software to third parties as a hosted or managed
19
+ service, where the service provides users with access to any substantial set of
20
+ the features or functionality of the software.
21
+
22
+ You may not move, change, disable, or circumvent the license key functionality
23
+ in the software, and you may not remove or obscure any functionality in the
24
+ software that is protected by the license key.
25
+
26
+ You may not alter, remove, or obscure any licensing, copyright, or other notices
27
+ of the licensor in the software. Any use of the licensor's trademarks is subject
28
+ to applicable law.
29
+
30
+ ## Patents
31
+
32
+ The licensor grants you a license, under any patent claims the licensor can
33
+ license, or becomes able to license, to make, have made, use, sell, offer for
34
+ sale, import and have imported the software, in each case subject to the
35
+ limitations and conditions in this license. This license does not cover any
36
+ patent claims that you cause to be infringed by modifications or additions to
37
+ the software. If you or your company make any written claim that the software
38
+ infringes or contributes to infringement of any patent, your patent license for
39
+ the software granted under these terms ends immediately. If your company makes
40
+ such a claim, your patent license ends immediately for work on behalf of your
41
+ company.
42
+
43
+ ## Notices
44
+
45
+ You must ensure that anyone who gets a copy of any part of the software from you
46
+ also gets a copy of these terms.
47
+
48
+ If you modify the software, you must include in any modified copies of the
49
+ software prominent notices stating that you have modified the software.
50
+
51
+ ## No Other Rights
52
+
53
+ These terms do not imply any licenses other than those expressly granted in
54
+ these terms.
55
+
56
+ ## Termination
57
+
58
+ If you use the software in violation of these terms, such use is not licensed,
59
+ and your licenses will automatically terminate. If the licensor provides you
60
+ with a notice of your violation, and you cease all violation of this license no
61
+ later than 30 days after you receive that notice, your licenses will be
62
+ reinstated retroactively. However, if you violate these terms after such
63
+ reinstatement, any additional violation of these terms will cause your licenses
64
+ to terminate automatically and permanently.
65
+
66
+ ## No Liability
67
+
68
+ *As far as the law allows, the software comes as is, without any warranty or
69
+ condition, and the licensor will not be liable to you for any damages arising out
70
+ of these terms or the use or nature of the software, under any kind of legal
71
+ claim.*
72
+
73
+ ## Definitions
74
+
75
+ The **licensor** is the entity offering these terms, and the **software** is the
76
+ software the licensor makes available under these terms, including any portion
77
+ of it.
78
+
79
+ **you** refers to the individual or entity agreeing to these terms.
80
+
81
+ **your company** is any legal entity, sole proprietorship, or other kind of
82
+ organization that you work for, plus all organizations that have control over,
83
+ are under the control of, or are under common control with that organization.
84
+ **control** means ownership of substantially all the assets of an entity, or the
85
+ power to direct its management and policies by vote, contract, or otherwise.
86
+ Control can be direct or indirect.
87
+
88
+ **your licenses** are all the licenses granted to you for the software under
89
+ these terms.
90
+
91
+ **use** means anything you do with the software requiring one of your licenses.
92
+
93
+ **trademark** means trademarks, service marks, and similar rights.
package/README.md ADDED
@@ -0,0 +1,14 @@
1
+ # @hogsend/db
2
+
3
+ Drizzle ORM schema, the bundled **engine migrations** (`drizzle/`), the hardened
4
+ migrator (advisory lock + timeouts), and the count-based schema-version probe for
5
+ [Hogsend](https://github.com/dougwithseismic/hogsend).
6
+
7
+ The published tarball includes the `drizzle/` folder (SQL + `meta/_journal.json`)
8
+ because the migrator loads it from disk at runtime — engine migrations ship
9
+ versioned with this package. See
10
+ [RELEASING.md](https://github.com/dougwithseismic/hogsend/blob/main/docs/RELEASING.md)
11
+ and [UPGRADING.md](https://github.com/dougwithseismic/hogsend/blob/main/docs/UPGRADING.md).
12
+
13
+ This package ships raw TypeScript source; consumers bundle it via their own build
14
+ (tsup `noExternal`).
@@ -0,0 +1,188 @@
1
+ CREATE TYPE "public"."email_send_status" AS ENUM('queued', 'rendered', 'sent', 'delivered', 'opened', 'clicked', 'bounced', 'complained', 'failed');--> statement-breakpoint
2
+ CREATE TYPE "public"."journey_status" AS ENUM('active', 'waiting', 'completed', 'failed', 'exited');--> statement-breakpoint
3
+ CREATE TABLE "account" (
4
+ "id" text PRIMARY KEY NOT NULL,
5
+ "account_id" text NOT NULL,
6
+ "provider_id" text NOT NULL,
7
+ "user_id" text NOT NULL,
8
+ "access_token" text,
9
+ "refresh_token" text,
10
+ "id_token" text,
11
+ "access_token_expires_at" timestamp with time zone,
12
+ "refresh_token_expires_at" timestamp with time zone,
13
+ "scope" text,
14
+ "password" text,
15
+ "created_at" timestamp with time zone DEFAULT now() NOT NULL,
16
+ "updated_at" timestamp with time zone DEFAULT now() NOT NULL
17
+ );
18
+ --> statement-breakpoint
19
+ CREATE TABLE "invitation" (
20
+ "id" text PRIMARY KEY NOT NULL,
21
+ "organization_id" text NOT NULL,
22
+ "email" text NOT NULL,
23
+ "role" text,
24
+ "status" text DEFAULT 'pending' NOT NULL,
25
+ "expires_at" timestamp with time zone NOT NULL,
26
+ "inviter_id" text NOT NULL,
27
+ "created_at" timestamp with time zone DEFAULT now() NOT NULL,
28
+ "updated_at" timestamp with time zone DEFAULT now() NOT NULL
29
+ );
30
+ --> statement-breakpoint
31
+ CREATE TABLE "member" (
32
+ "id" text PRIMARY KEY NOT NULL,
33
+ "organization_id" text NOT NULL,
34
+ "user_id" text NOT NULL,
35
+ "role" text DEFAULT 'member' NOT NULL,
36
+ "created_at" timestamp with time zone DEFAULT now() NOT NULL,
37
+ "updated_at" timestamp with time zone DEFAULT now() NOT NULL
38
+ );
39
+ --> statement-breakpoint
40
+ CREATE TABLE "organization" (
41
+ "id" text PRIMARY KEY NOT NULL,
42
+ "name" text NOT NULL,
43
+ "slug" text,
44
+ "logo" text,
45
+ "metadata" text,
46
+ "created_at" timestamp with time zone DEFAULT now() NOT NULL,
47
+ "updated_at" timestamp with time zone DEFAULT now() NOT NULL,
48
+ CONSTRAINT "organization_slug_unique" UNIQUE("slug")
49
+ );
50
+ --> statement-breakpoint
51
+ CREATE TABLE "session" (
52
+ "id" text PRIMARY KEY NOT NULL,
53
+ "expires_at" timestamp with time zone NOT NULL,
54
+ "token" text NOT NULL,
55
+ "ip_address" text,
56
+ "user_agent" text,
57
+ "user_id" text NOT NULL,
58
+ "active_organization_id" text,
59
+ "created_at" timestamp with time zone DEFAULT now() NOT NULL,
60
+ "updated_at" timestamp with time zone DEFAULT now() NOT NULL,
61
+ CONSTRAINT "session_token_unique" UNIQUE("token")
62
+ );
63
+ --> statement-breakpoint
64
+ CREATE TABLE "user" (
65
+ "id" text PRIMARY KEY NOT NULL,
66
+ "name" text NOT NULL,
67
+ "email" text NOT NULL,
68
+ "email_verified" boolean DEFAULT false NOT NULL,
69
+ "image" text,
70
+ "created_at" timestamp with time zone DEFAULT now() NOT NULL,
71
+ "updated_at" timestamp with time zone DEFAULT now() NOT NULL,
72
+ CONSTRAINT "user_email_unique" UNIQUE("email")
73
+ );
74
+ --> statement-breakpoint
75
+ CREATE TABLE "verification" (
76
+ "id" text PRIMARY KEY NOT NULL,
77
+ "identifier" text NOT NULL,
78
+ "value" text NOT NULL,
79
+ "expires_at" timestamp with time zone NOT NULL,
80
+ "created_at" timestamp with time zone DEFAULT now() NOT NULL,
81
+ "updated_at" timestamp with time zone DEFAULT now() NOT NULL
82
+ );
83
+ --> statement-breakpoint
84
+ CREATE TABLE "email_preferences" (
85
+ "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
86
+ "user_id" text NOT NULL,
87
+ "email" text NOT NULL,
88
+ "unsubscribed_all" boolean DEFAULT false NOT NULL,
89
+ "suppressed" boolean DEFAULT false NOT NULL,
90
+ "bounce_count" integer DEFAULT 0 NOT NULL,
91
+ "categories" jsonb DEFAULT '{}'::jsonb,
92
+ "suppressed_at" timestamp with time zone,
93
+ "last_bounce_at" timestamp with time zone,
94
+ "created_at" timestamp with time zone DEFAULT now() NOT NULL,
95
+ "updated_at" timestamp with time zone DEFAULT now() NOT NULL
96
+ );
97
+ --> statement-breakpoint
98
+ CREATE TABLE "email_sends" (
99
+ "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
100
+ "journey_state_id" uuid,
101
+ "template_key" text,
102
+ "resend_id" text,
103
+ "from_email" text NOT NULL,
104
+ "to_email" text NOT NULL,
105
+ "subject" text NOT NULL,
106
+ "category" text,
107
+ "status" "email_send_status" DEFAULT 'queued' NOT NULL,
108
+ "sent_at" timestamp with time zone,
109
+ "delivered_at" timestamp with time zone,
110
+ "opened_at" timestamp with time zone,
111
+ "clicked_at" timestamp with time zone,
112
+ "bounced_at" timestamp with time zone,
113
+ "complained_at" timestamp with time zone,
114
+ "created_at" timestamp with time zone DEFAULT now() NOT NULL,
115
+ "updated_at" timestamp with time zone DEFAULT now() NOT NULL
116
+ );
117
+ --> statement-breakpoint
118
+ CREATE TABLE "journey_logs" (
119
+ "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
120
+ "journey_state_id" uuid NOT NULL,
121
+ "from_node_id" text,
122
+ "to_node_id" text,
123
+ "action" text NOT NULL,
124
+ "detail" jsonb,
125
+ "created_at" timestamp with time zone DEFAULT now() NOT NULL,
126
+ "updated_at" timestamp with time zone DEFAULT now() NOT NULL
127
+ );
128
+ --> statement-breakpoint
129
+ CREATE TABLE "journey_states" (
130
+ "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
131
+ "user_id" text NOT NULL,
132
+ "user_email" text NOT NULL,
133
+ "journey_id" text NOT NULL,
134
+ "current_node_id" text NOT NULL,
135
+ "status" "journey_status" DEFAULT 'active' NOT NULL,
136
+ "hatchet_run_id" text,
137
+ "context" jsonb DEFAULT '{}'::jsonb,
138
+ "error_message" text,
139
+ "entry_count" integer DEFAULT 1 NOT NULL,
140
+ "completed_at" timestamp with time zone,
141
+ "exited_at" timestamp with time zone,
142
+ "created_at" timestamp with time zone DEFAULT now() NOT NULL,
143
+ "updated_at" timestamp with time zone DEFAULT now() NOT NULL
144
+ );
145
+ --> statement-breakpoint
146
+ CREATE TABLE "link_clicks" (
147
+ "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
148
+ "tracked_link_id" uuid NOT NULL,
149
+ "ip_address" text,
150
+ "user_agent" text,
151
+ "clicked_at" timestamp with time zone DEFAULT now() NOT NULL
152
+ );
153
+ --> statement-breakpoint
154
+ CREATE TABLE "tracked_links" (
155
+ "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
156
+ "email_send_id" uuid NOT NULL,
157
+ "original_url" text NOT NULL,
158
+ "click_count" integer DEFAULT 0 NOT NULL,
159
+ "created_at" timestamp with time zone DEFAULT now() NOT NULL,
160
+ "updated_at" timestamp with time zone DEFAULT now() NOT NULL
161
+ );
162
+ --> statement-breakpoint
163
+ CREATE TABLE "user_events" (
164
+ "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
165
+ "user_id" text NOT NULL,
166
+ "event" text NOT NULL,
167
+ "properties" jsonb,
168
+ "occurred_at" timestamp with time zone DEFAULT now() NOT NULL
169
+ );
170
+ --> statement-breakpoint
171
+ ALTER TABLE "account" ADD CONSTRAINT "account_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
172
+ ALTER TABLE "invitation" ADD CONSTRAINT "invitation_organization_id_organization_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
173
+ ALTER TABLE "invitation" ADD CONSTRAINT "invitation_inviter_id_user_id_fk" FOREIGN KEY ("inviter_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
174
+ ALTER TABLE "member" ADD CONSTRAINT "member_organization_id_organization_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
175
+ ALTER TABLE "member" ADD CONSTRAINT "member_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
176
+ ALTER TABLE "session" ADD CONSTRAINT "session_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
177
+ ALTER TABLE "email_sends" ADD CONSTRAINT "email_sends_journey_state_id_journey_states_id_fk" FOREIGN KEY ("journey_state_id") REFERENCES "public"."journey_states"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
178
+ ALTER TABLE "journey_logs" ADD CONSTRAINT "journey_logs_journey_state_id_journey_states_id_fk" FOREIGN KEY ("journey_state_id") REFERENCES "public"."journey_states"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
179
+ ALTER TABLE "link_clicks" ADD CONSTRAINT "link_clicks_tracked_link_id_tracked_links_id_fk" FOREIGN KEY ("tracked_link_id") REFERENCES "public"."tracked_links"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
180
+ ALTER TABLE "tracked_links" ADD CONSTRAINT "tracked_links_email_send_id_email_sends_id_fk" FOREIGN KEY ("email_send_id") REFERENCES "public"."email_sends"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
181
+ CREATE UNIQUE INDEX "email_preferences_user_email_idx" ON "email_preferences" USING btree ("user_id","email");--> statement-breakpoint
182
+ CREATE UNIQUE INDEX "uq_user_journey_active" ON "journey_states" USING btree ("user_id","journey_id","status");--> statement-breakpoint
183
+ CREATE INDEX "journey_states_status_idx" ON "journey_states" USING btree ("status");--> statement-breakpoint
184
+ CREATE INDEX "journey_states_hatchet_run_idx" ON "journey_states" USING btree ("hatchet_run_id");--> statement-breakpoint
185
+ CREATE INDEX "journey_states_user_id_idx" ON "journey_states" USING btree ("user_id");--> statement-breakpoint
186
+ CREATE INDEX "user_events_user_id_idx" ON "user_events" USING btree ("user_id");--> statement-breakpoint
187
+ CREATE INDEX "user_events_event_idx" ON "user_events" USING btree ("event");--> statement-breakpoint
188
+ CREATE INDEX "user_events_occurred_at_idx" ON "user_events" USING btree ("occurred_at");
@@ -0,0 +1,13 @@
1
+ CREATE TABLE "contacts" (
2
+ "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
3
+ "external_id" text NOT NULL,
4
+ "email" text,
5
+ "properties" jsonb DEFAULT '{}'::jsonb,
6
+ "first_seen_at" timestamp with time zone DEFAULT now() NOT NULL,
7
+ "last_seen_at" timestamp with time zone DEFAULT now() NOT NULL,
8
+ "created_at" timestamp with time zone DEFAULT now() NOT NULL,
9
+ "updated_at" timestamp with time zone DEFAULT now() NOT NULL,
10
+ CONSTRAINT "contacts_external_id_unique" UNIQUE("external_id")
11
+ );
12
+ --> statement-breakpoint
13
+ CREATE INDEX "contacts_email_idx" ON "contacts" USING btree ("email");
@@ -0,0 +1 @@
1
+ CREATE INDEX "user_events_user_event_occurred_idx" ON "user_events" USING btree ("user_id","event","occurred_at");
@@ -0,0 +1,9 @@
1
+ CREATE TABLE "journey_configs" (
2
+ "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
3
+ "journey_id" text NOT NULL,
4
+ "enabled" boolean DEFAULT true NOT NULL,
5
+ "created_at" timestamp with time zone DEFAULT now() NOT NULL,
6
+ "updated_at" timestamp with time zone DEFAULT now() NOT NULL
7
+ );
8
+ --> statement-breakpoint
9
+ CREATE UNIQUE INDEX "journey_configs_journey_id_idx" ON "journey_configs" USING btree ("journey_id");
@@ -0,0 +1 @@
1
+ CREATE INDEX "journey_states_journey_id_status_idx" ON "journey_states" USING btree ("journey_id","status");
@@ -0,0 +1,8 @@
1
+ CREATE INDEX "email_sends_to_email_idx" ON "email_sends" USING btree ("to_email");--> statement-breakpoint
2
+ CREATE INDEX "email_sends_template_key_idx" ON "email_sends" USING btree ("template_key");--> statement-breakpoint
3
+ CREATE INDEX "email_sends_status_idx" ON "email_sends" USING btree ("status");--> statement-breakpoint
4
+ CREATE INDEX "email_sends_created_at_idx" ON "email_sends" USING btree ("created_at");--> statement-breakpoint
5
+ CREATE INDEX "email_sends_journey_state_id_idx" ON "email_sends" USING btree ("journey_state_id");--> statement-breakpoint
6
+ CREATE INDEX "journey_logs_journey_state_id_idx" ON "journey_logs" USING btree ("journey_state_id");--> statement-breakpoint
7
+ CREATE INDEX "link_clicks_tracked_link_id_idx" ON "link_clicks" USING btree ("tracked_link_id");--> statement-breakpoint
8
+ CREATE INDEX "tracked_links_email_send_id_idx" ON "tracked_links" USING btree ("email_send_id");
@@ -0,0 +1,100 @@
1
+ CREATE TYPE "public"."alert_channel" AS ENUM('webhook', 'slack', 'email');--> statement-breakpoint
2
+ CREATE TYPE "public"."alert_rule_type" AS ENUM('bounce_rate_exceeded', 'journey_failure_spike', 'delivery_issue', 'high_complaint_rate');--> statement-breakpoint
3
+ CREATE TYPE "public"."dlq_status" AS ENUM('pending', 'retried', 'discarded');--> statement-breakpoint
4
+ CREATE TYPE "public"."import_job_status" AS ENUM('pending', 'processing', 'completed', 'failed');--> statement-breakpoint
5
+ CREATE TABLE "alert_history" (
6
+ "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
7
+ "alert_rule_id" uuid NOT NULL,
8
+ "payload" jsonb,
9
+ "delivery_status" text NOT NULL,
10
+ "error" text,
11
+ "created_at" timestamp with time zone DEFAULT now() NOT NULL,
12
+ "updated_at" timestamp with time zone DEFAULT now() NOT NULL
13
+ );
14
+ --> statement-breakpoint
15
+ CREATE TABLE "alert_rules" (
16
+ "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
17
+ "name" text NOT NULL,
18
+ "type" "alert_rule_type" NOT NULL,
19
+ "threshold" jsonb NOT NULL,
20
+ "channel" "alert_channel" NOT NULL,
21
+ "channel_config" jsonb NOT NULL,
22
+ "enabled" boolean DEFAULT true NOT NULL,
23
+ "cooldown_minutes" integer DEFAULT 60 NOT NULL,
24
+ "last_fired_at" timestamp with time zone,
25
+ "created_at" timestamp with time zone DEFAULT now() NOT NULL,
26
+ "updated_at" timestamp with time zone DEFAULT now() NOT NULL
27
+ );
28
+ --> statement-breakpoint
29
+ CREATE TABLE "api_keys" (
30
+ "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
31
+ "name" text NOT NULL,
32
+ "key_prefix" text NOT NULL,
33
+ "key_hash" text NOT NULL,
34
+ "scopes" jsonb DEFAULT '["read"]'::jsonb NOT NULL,
35
+ "created_by" text,
36
+ "last_used_at" timestamp with time zone,
37
+ "revoked_at" timestamp with time zone,
38
+ "expires_at" timestamp with time zone,
39
+ "created_at" timestamp with time zone DEFAULT now() NOT NULL,
40
+ "updated_at" timestamp with time zone DEFAULT now() NOT NULL,
41
+ CONSTRAINT "api_keys_key_hash_unique" UNIQUE("key_hash")
42
+ );
43
+ --> statement-breakpoint
44
+ CREATE TABLE "audit_logs" (
45
+ "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
46
+ "actor" text NOT NULL,
47
+ "actor_key_id" uuid,
48
+ "action" text NOT NULL,
49
+ "resource" text NOT NULL,
50
+ "resource_id" text,
51
+ "detail" jsonb,
52
+ "ip_address" text,
53
+ "created_at" timestamp with time zone DEFAULT now() NOT NULL,
54
+ "updated_at" timestamp with time zone DEFAULT now() NOT NULL
55
+ );
56
+ --> statement-breakpoint
57
+ CREATE TABLE "dead_letter_queue" (
58
+ "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
59
+ "source" text NOT NULL,
60
+ "source_id" text,
61
+ "payload" jsonb NOT NULL,
62
+ "error" text NOT NULL,
63
+ "retry_count" integer DEFAULT 0 NOT NULL,
64
+ "status" "dlq_status" DEFAULT 'pending' NOT NULL,
65
+ "retried_at" timestamp with time zone,
66
+ "created_at" timestamp with time zone DEFAULT now() NOT NULL,
67
+ "updated_at" timestamp with time zone DEFAULT now() NOT NULL
68
+ );
69
+ --> statement-breakpoint
70
+ CREATE TABLE "import_jobs" (
71
+ "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
72
+ "file_name" text,
73
+ "format" text NOT NULL,
74
+ "status" "import_job_status" DEFAULT 'pending' NOT NULL,
75
+ "total_rows" integer,
76
+ "processed_rows" integer DEFAULT 0 NOT NULL,
77
+ "failed_rows" integer DEFAULT 0 NOT NULL,
78
+ "errors" jsonb,
79
+ "created_at" timestamp with time zone DEFAULT now() NOT NULL,
80
+ "updated_at" timestamp with time zone DEFAULT now() NOT NULL
81
+ );
82
+ --> statement-breakpoint
83
+ ALTER TABLE "contacts" ADD COLUMN "deleted_at" timestamp with time zone;--> statement-breakpoint
84
+ ALTER TABLE "journey_states" ADD COLUMN "deleted_at" timestamp with time zone;--> statement-breakpoint
85
+ ALTER TABLE "user_events" ADD COLUMN "idempotency_key" text;--> statement-breakpoint
86
+ ALTER TABLE "alert_history" ADD CONSTRAINT "alert_history_alert_rule_id_alert_rules_id_fk" FOREIGN KEY ("alert_rule_id") REFERENCES "public"."alert_rules"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
87
+ CREATE INDEX "alert_history_rule_id_idx" ON "alert_history" USING btree ("alert_rule_id");--> statement-breakpoint
88
+ CREATE INDEX "alert_history_created_at_idx" ON "alert_history" USING btree ("created_at");--> statement-breakpoint
89
+ CREATE INDEX "alert_rules_type_idx" ON "alert_rules" USING btree ("type");--> statement-breakpoint
90
+ CREATE INDEX "alert_rules_enabled_idx" ON "alert_rules" USING btree ("enabled");--> statement-breakpoint
91
+ CREATE INDEX "api_keys_key_hash_idx" ON "api_keys" USING btree ("key_hash");--> statement-breakpoint
92
+ CREATE INDEX "api_keys_revoked_at_idx" ON "api_keys" USING btree ("revoked_at");--> statement-breakpoint
93
+ CREATE INDEX "audit_logs_actor_idx" ON "audit_logs" USING btree ("actor");--> statement-breakpoint
94
+ CREATE INDEX "audit_logs_resource_idx" ON "audit_logs" USING btree ("resource","resource_id");--> statement-breakpoint
95
+ CREATE INDEX "audit_logs_created_at_idx" ON "audit_logs" USING btree ("created_at");--> statement-breakpoint
96
+ CREATE INDEX "dlq_source_idx" ON "dead_letter_queue" USING btree ("source");--> statement-breakpoint
97
+ CREATE INDEX "dlq_status_idx" ON "dead_letter_queue" USING btree ("status");--> statement-breakpoint
98
+ CREATE INDEX "dlq_created_at_idx" ON "dead_letter_queue" USING btree ("created_at");--> statement-breakpoint
99
+ CREATE INDEX "import_jobs_status_idx" ON "import_jobs" USING btree ("status");--> statement-breakpoint
100
+ CREATE UNIQUE INDEX "user_events_idempotency_key_idx" ON "user_events" USING btree ("idempotency_key");
@@ -0,0 +1 @@
1
+ CREATE INDEX "link_clicks_clicked_at_idx" ON "link_clicks" USING btree ("clicked_at");
@@ -0,0 +1,5 @@
1
+ ALTER TABLE "api_keys" ADD COLUMN "organization_id" text;--> statement-breakpoint
2
+ ALTER TABLE "contacts" ADD COLUMN "organization_id" text;--> statement-breakpoint
3
+ ALTER TABLE "email_sends" ADD COLUMN "organization_id" text;--> statement-breakpoint
4
+ ALTER TABLE "journey_states" ADD COLUMN "organization_id" text;--> statement-breakpoint
5
+ ALTER TABLE "user_events" ADD COLUMN "organization_id" text;