Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(pg): create hypertable for time series data #1068

Merged
merged 1 commit into from
Feb 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions apps/telegram-monitor/src/migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,42 @@ AddMigration({
`,
});

AddMigration({
id: '9f5e4d03-2e50-4977-9168-2d1b616f8b04',
name: 'create-hypertable-for-telegram_messages',
dependencies: ['eb62f3a4-11fd-4386-a86d-1841c3beee13'],
statement: `
CREATE EXTENSION IF NOT EXISTS timescaledb;
ALTER TABLE telegram_messages DROP CONSTRAINT IF EXISTS telegram_messages_pkey;
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1
FROM pg_constraint
WHERE conrelid = 'telegram_messages'::regclass
AND conname = 'telegram_messages_key'
) THEN
ALTER TABLE telegram_messages
ADD CONSTRAINT telegram_messages_key
UNIQUE (id, created_at);
END IF;
END $$;
ALTER TABLE telegram_messages DROP CONSTRAINT IF EXISTS telegram_messages_message_id_key;
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1
FROM pg_constraint
WHERE conrelid = 'telegram_messages'::regclass
AND conname = 'telegram_messages_message_id_created_at_key'
) THEN
ALTER TABLE telegram_messages
ADD CONSTRAINT telegram_messages_message_id_created_at_key
UNIQUE (message_id, created_at);
END IF;
END $$;
SELECT create_hypertable('telegram_messages', by_range('created_at'), migrate_data => TRUE, if_not_exists => TRUE);
`,
});

ExecuteMigrations(terminal);
24 changes: 24 additions & 0 deletions apps/twitter-monitor/src/migrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,28 @@ AddMigration({
`,
});

AddMigration({
id: '0d6d3c0c-0c6d-4f9f-9c7b-8e0e9b0b8c5d',
name: 'create-hypertable-for-twitter_messages',
dependencies: ['73ce7f3e-f359-4968-b57a-8ecf8deb71c7'],
statement: `
CREATE EXTENSION IF NOT EXISTS timescaledb;
ALTER TABLE twitter_messages DROP CONSTRAINT IF EXISTS twitter_messages_pkey;
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1
FROM pg_constraint
WHERE conrelid = 'twitter_messages'::regclass
AND conname = 'twitter_messages_key'
) THEN
ALTER TABLE twitter_messages
ADD CONSTRAINT twitter_messages_key
UNIQUE (id, created_at);
END IF;
END $$;
SELECT create_hypertable('twitter_messages', by_range('created_at'), migrate_data => TRUE, if_not_exists => TRUE);
`,
});

ExecuteMigrations(terminal);
10 changes: 10 additions & 0 deletions common/changes/@yuants/app-telegram-monitor/2025-02-17-18-42.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@yuants/app-telegram-monitor",
"comment": "create hypertable for time series data",
"type": "patch"
}
],
"packageName": "@yuants/app-telegram-monitor"
}
10 changes: 10 additions & 0 deletions common/changes/@yuants/app-twitter-monitor/2025-02-17-18-42.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@yuants/app-twitter-monitor",
"comment": "create hypertable for time series data",
"type": "patch"
}
],
"packageName": "@yuants/app-twitter-monitor"
}