-
Notifications
You must be signed in to change notification settings - Fork 113
feat: Add Twitter/X post support (daily-api) #3437
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
Draft
idoshamun
wants to merge
19
commits into
main
Choose a base branch
from
twitter-support2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
fe26d6a
feat: add schema isolation infrastructure for parallel Jest workers
idoshamun 15be260
feat: enable parallel Jest workers with schema isolation
idoshamun c5be61b
fix: complete schema isolation with FK constraints and trigger fixes
idoshamun c5d1b0c
feat: exclude seed data tables from deletion in tests
idoshamun 692be83
fix: create proper sequences in worker schemas for schema isolation
idoshamun b9bb1ef
fix: explicitly qualify table references in materialized view definit…
idoshamun 638bd55
fix: lint issue
idoshamun 5e38708
feat: migrate schema isolation to use migrations instead of schema copy
idoshamun 29b9140
fix: lint formatting
idoshamun 9a5af19
fix: add 30s timeout to beforeEach hook for CI
idoshamun 8adc0ce
fix: reduce connection pool sizes to prevent OOM on CI
idoshamun 59e8c46
refactor: move schema creation to globalSetup for better memory effic…
idoshamun 5360fb6
fix: add global testTimeout of 30s for CI stability
idoshamun 69af62b
feat(posts): add PostType.Tweet enum value
idoshamun 2a41c94
feat(posts): create TweetPost entity
idoshamun 6e61633
feat(migration): add TweetPost columns to post table
idoshamun 91ddd18
feat(graphql): add tweet fields to Post type
idoshamun 86ee5ff
feat(posts): detect Twitter URLs in external link submission
idoshamun ba5f39b
feat(worker): handle Twitter content from yggdrasil publisher
idoshamun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,188 @@ | ||
| import { DataSource, QueryRunner } from 'typeorm'; | ||
|
|
||
| /** | ||
| * Replace hardcoded 'public.' schema references with the target schema. | ||
| */ | ||
| const replaceSchemaReferences = (sql: string, targetSchema: string): string => { | ||
| if (targetSchema === 'public') return sql; | ||
|
|
||
| let result = sql; | ||
|
|
||
| // Handle DROP INDEX separately - remove schema qualification and add IF EXISTS | ||
| result = result.replace( | ||
| /DROP INDEX\s+(?:IF EXISTS\s+)?(?:"public"\.|public\.)?("[^"]+"|[\w]+)/gi, | ||
| (_, indexName) => `DROP INDEX IF EXISTS ${indexName}`, | ||
| ); | ||
|
|
||
| // Replace various patterns of public schema references | ||
| result = result | ||
| .replace(/\bpublic\."(\w+)"/gi, `"${targetSchema}"."$1"`) | ||
| .replace(/\bpublic\.(\w+)(?=[\s,;())]|$)/gi, `"${targetSchema}"."$1"`) | ||
| .replace(/"public"\."(\w+)"/gi, `"${targetSchema}"."$1"`) | ||
| .replace(/\bON\s+public\./gi, `ON "${targetSchema}".`); | ||
|
|
||
| return result; | ||
| }; | ||
|
|
||
| /** | ||
| * Wrap a QueryRunner to intercept and transform SQL queries. | ||
| */ | ||
| const wrapQueryRunner = ( | ||
| queryRunner: QueryRunner, | ||
| targetSchema: string, | ||
| ): QueryRunner => { | ||
| const originalQuery = queryRunner.query.bind(queryRunner); | ||
|
|
||
| queryRunner.query = async ( | ||
| query: string, | ||
| parameters?: unknown[], | ||
| ): Promise<unknown> => { | ||
| const transformedQuery = replaceSchemaReferences(query, targetSchema); | ||
| return originalQuery(transformedQuery, parameters); | ||
| }; | ||
|
|
||
| return queryRunner; | ||
| }; | ||
|
|
||
| /** | ||
| * Create and run migrations for a single worker schema. | ||
| */ | ||
| const createWorkerSchema = async (schema: string): Promise<void> => { | ||
| const workerDataSource = new DataSource({ | ||
| type: 'postgres', | ||
| host: process.env.TYPEORM_HOST || 'localhost', | ||
| port: 5432, | ||
| username: process.env.TYPEORM_USERNAME || 'postgres', | ||
| password: process.env.TYPEORM_PASSWORD || '12345', | ||
| database: | ||
| process.env.TYPEORM_DATABASE || | ||
| (process.env.NODE_ENV === 'test' ? 'api_test' : 'api'), | ||
| schema, | ||
| extra: { | ||
| max: 2, | ||
| options: `-c search_path=${schema},public`, | ||
| }, | ||
| entities: ['src/entity/**/*.{js,ts}'], | ||
| migrations: ['src/migration/**/*.{js,ts}'], | ||
| migrationsTableName: 'migrations', | ||
| logging: false, | ||
| }); | ||
|
|
||
| await workerDataSource.initialize(); | ||
|
|
||
| const queryRunner = workerDataSource.createQueryRunner(); | ||
| await queryRunner.connect(); | ||
| wrapQueryRunner(queryRunner, schema); | ||
|
|
||
| try { | ||
| // Create migrations table | ||
| await queryRunner.query(` | ||
| CREATE TABLE IF NOT EXISTS "${schema}"."migrations" ( | ||
| "id" SERIAL PRIMARY KEY, | ||
| "timestamp" bigint NOT NULL, | ||
| "name" varchar NOT NULL | ||
| ) | ||
| `); | ||
|
|
||
| // Create typeorm_metadata table | ||
| await queryRunner.query(` | ||
| CREATE TABLE IF NOT EXISTS "${schema}"."typeorm_metadata" ( | ||
| "type" varchar NOT NULL, | ||
| "database" varchar, | ||
| "schema" varchar, | ||
| "table" varchar, | ||
| "name" varchar, | ||
| "value" text | ||
| ) | ||
| `); | ||
|
|
||
| // Sort migrations by timestamp | ||
| const allMigrations = [...workerDataSource.migrations].sort((a, b) => { | ||
| const getTimestamp = (migration: { | ||
| name?: string; | ||
| constructor: { name: string }; | ||
| }): number => { | ||
| const name = migration.name || migration.constructor.name; | ||
| const match = name.match(/(\d{13})$/); | ||
| return match ? parseInt(match[1], 10) : 0; | ||
| }; | ||
| return getTimestamp(a) - getTimestamp(b); | ||
| }); | ||
|
|
||
| for (const migration of allMigrations) { | ||
| const migrationName = migration.name || migration.constructor.name; | ||
|
|
||
| const alreadyRun = await queryRunner.query( | ||
| `SELECT * FROM "${schema}"."migrations" WHERE "name" = $1`, | ||
| [migrationName], | ||
| ); | ||
|
|
||
| if (alreadyRun.length === 0) { | ||
| await migration.up(queryRunner); | ||
|
|
||
| const timestampMatch = migrationName.match(/(\d{13})$/); | ||
| const timestamp = timestampMatch | ||
| ? parseInt(timestampMatch[1], 10) | ||
| : Date.now(); | ||
|
|
||
| await queryRunner.query( | ||
| `INSERT INTO "${schema}"."migrations" ("timestamp", "name") VALUES ($1, $2)`, | ||
| [timestamp, migrationName], | ||
| ); | ||
| } | ||
| } | ||
| } finally { | ||
| await queryRunner.release(); | ||
| } | ||
|
|
||
| await workerDataSource.destroy(); | ||
| }; | ||
|
|
||
| /** | ||
| * Jest global setup - runs once before all workers start. | ||
| * Creates worker schemas for parallel test isolation. | ||
| */ | ||
| export default async function globalSetup(): Promise<void> { | ||
| // Only run when schema isolation is enabled | ||
| if (process.env.ENABLE_SCHEMA_ISOLATION !== 'true') { | ||
| return; | ||
| } | ||
|
|
||
| const maxWorkers = parseInt(process.env.JEST_MAX_WORKERS || '4', 10); | ||
| console.log( | ||
| `\nCreating ${maxWorkers} worker schemas for parallel testing...`, | ||
| ); | ||
|
|
||
| // First, create all schemas | ||
| const dataSource = new DataSource({ | ||
| type: 'postgres', | ||
| host: process.env.TYPEORM_HOST || 'localhost', | ||
| port: 5432, | ||
| username: process.env.TYPEORM_USERNAME || 'postgres', | ||
| password: process.env.TYPEORM_PASSWORD || '12345', | ||
| database: | ||
| process.env.TYPEORM_DATABASE || | ||
| (process.env.NODE_ENV === 'test' ? 'api_test' : 'api'), | ||
| schema: 'public', | ||
| extra: { max: 1 }, | ||
| }); | ||
|
|
||
| await dataSource.initialize(); | ||
|
|
||
| for (let i = 1; i <= maxWorkers; i++) { | ||
| const schema = `test_worker_${i}`; | ||
| await dataSource.query(`DROP SCHEMA IF EXISTS "${schema}" CASCADE`); | ||
| await dataSource.query(`CREATE SCHEMA "${schema}"`); | ||
| } | ||
|
|
||
| await dataSource.destroy(); | ||
|
|
||
| // Run migrations for each schema sequentially to avoid memory spikes | ||
| for (let i = 1; i <= maxWorkers; i++) { | ||
| const schema = `test_worker_${i}`; | ||
| console.log(`Running migrations for ${schema}...`); | ||
| await createWorkerSchema(schema); | ||
| } | ||
|
|
||
| console.log('All worker schemas ready!\n'); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For future, you can add your local variables to
.envinside.infra, and pulumi+tilt will automagically sync it to adhoc environment.