(
code?: string,
extraServerConfig: Partial<PluginsServerConfig> = {},
extraRows: ExtraDatabaseRows = {},
{ withExtendedTestData = true }: { withExtendedTestData?: boolean } = {}
)
| 38 | ` |
| 39 | |
| 40 | export async function resetTestDatabase( |
| 41 | code?: string, |
| 42 | extraServerConfig: Partial<PluginsServerConfig> = {}, |
| 43 | extraRows: ExtraDatabaseRows = {}, |
| 44 | { withExtendedTestData = true }: { withExtendedTestData?: boolean } = {} |
| 45 | ): Promise<void> { |
| 46 | const config = { ...defaultConfig, ...extraServerConfig } |
| 47 | const db = new Pool({ connectionString: config.DATABASE_URL!, max: 1 }) |
| 48 | |
| 49 | await db.query(POSTGRES_DELETE_TABLES_QUERY) |
| 50 | const mocks = makePluginObjects(code) |
| 51 | const teamIds = mocks.pluginConfigRows.map((c) => c.team_id) |
| 52 | const teamIdToCreate = teamIds[0] |
| 53 | await createUserTeamAndOrganization(db, teamIdToCreate) |
| 54 | if (withExtendedTestData) { |
| 55 | await insertRow(db, 'posthog_action', { |
| 56 | id: teamIdToCreate + 67, |
| 57 | team_id: teamIdToCreate, |
| 58 | name: 'Test Action', |
| 59 | description: '', |
| 60 | created_at: new Date().toISOString(), |
| 61 | created_by_id: commonUserId, |
| 62 | deleted: false, |
| 63 | post_to_slack: true, |
| 64 | slack_message_format: '', |
| 65 | is_calculating: false, |
| 66 | updated_at: new Date().toISOString(), |
| 67 | last_calculated_at: new Date().toISOString(), |
| 68 | } as RawAction) |
| 69 | await insertRow(db, 'posthog_actionstep', { |
| 70 | id: teamIdToCreate + 911, |
| 71 | action_id: teamIdToCreate + 67, |
| 72 | tag_name: null, |
| 73 | text: null, |
| 74 | href: null, |
| 75 | selector: null, |
| 76 | url: null, |
| 77 | url_matching: null, |
| 78 | name: null, |
| 79 | event: null, |
| 80 | properties: [{ type: 'event', operator: PropertyOperator.Exact, key: 'foo', value: ['bar'] }], |
| 81 | }) |
| 82 | for (const plugin of mocks.pluginRows.concat(extraRows.plugins ?? [])) { |
| 83 | await insertRow(db, 'posthog_plugin', plugin) |
| 84 | } |
| 85 | for (const pluginConfig of mocks.pluginConfigRows.concat(extraRows.pluginConfigs ?? [])) { |
| 86 | await insertRow(db, 'posthog_pluginconfig', pluginConfig) |
| 87 | } |
| 88 | for (const pluginAttachment of mocks.pluginAttachmentRows.concat(extraRows.pluginAttachments ?? [])) { |
| 89 | await insertRow(db, 'posthog_pluginattachment', pluginAttachment) |
| 90 | } |
| 91 | } |
| 92 | await db.end() |
| 93 | } |
| 94 | |
| 95 | export async function insertRow(db: Pool, table: string, objectProvided: Record<string, any>) { |
| 96 | // Handling of related fields |
searching dependent graphs…