Skip to content

Commit b1b7ebc

Browse files
authored
refactor: add m2m and user token usage cols to daily token usage table (#8029)
* refactor: add m2m and user token usage cols to daily token usage table * refactor: update code
1 parent 58ba472 commit b1b7ebc

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { sql } from '@silverhand/slonik';
2+
3+
import type { AlterationScript } from '../lib/types/alteration.js';
4+
5+
const alteration: AlterationScript = {
6+
up: async (pool) => {
7+
// Add new columns for user and m2m token usage tracking
8+
await pool.query(sql`
9+
alter table daily_token_usage
10+
add column user_token_usage bigint not null default 0,
11+
add column m2m_token_usage bigint not null default 0;
12+
`);
13+
14+
// Remove foreign key constraint to support enterprise tenant deletion requirements
15+
await pool.query(sql`
16+
alter table daily_token_usage
17+
drop constraint if exists daily_token_usage_tenant_id_fkey;
18+
`);
19+
},
20+
down: async (pool) => {
21+
// Remove the new columns
22+
await pool.query(sql`
23+
alter table daily_token_usage
24+
drop column if exists user_token_usage,
25+
drop column if exists m2m_token_usage;
26+
`);
27+
28+
// Re-add the foreign key constraint
29+
await pool.query(sql`
30+
alter table daily_token_usage
31+
add constraint daily_token_usage_tenant_id_fkey
32+
foreign key (tenant_id) references tenants (id) on update cascade on delete cascade;
33+
`);
34+
},
35+
};
36+
37+
export default alteration;

packages/schemas/tables/daily_token_usage.sql

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
create table daily_token_usage (
22
id varchar(21) not null,
3-
tenant_id varchar(21) not null
4-
references tenants (id) on update cascade on delete cascade,
3+
tenant_id varchar(21) not null,
54
usage bigint not null default(0),
5+
user_token_usage bigint not null default(0),
6+
m2m_token_usage bigint not null default(0),
67
date timestamptz not null,
78
primary key (id)
89
);

0 commit comments

Comments
 (0)