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: Add missing indexes and constraints #25

Merged
merged 2 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 4 additions & 2 deletions src/adapters/redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ export default async function createRedisComponent(
): Promise<IRedisComponent> {
const { logs, config } = components
const logger = logs.getLogger('redis-component')
const REDIS_URL = (await config.getString('REDIS_CONNECTION_STRING')) || `redis://127.0.0.1:6379`
const REDIS_HOST = (await config.getString('REDIS_HOST')) || '127.0.0.1'

const url = `redis://${REDIS_HOST}:6379`

const client = createClient({
url: REDIS_URL
url
})

client.on('error', (err) => {
Expand Down
8 changes: 4 additions & 4 deletions src/adapters/rpcServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export default async function createRpcServerComponent(
async getPendingFriendshipRequests(_request, context) {
try {
const pendingRequests = await db.getReceivedFriendshipRequests(context.address)
const mappedRequestss = pendingRequests.map(({ address, timestamp, metadata }) => ({
const mappedRequests = pendingRequests.map(({ address, timestamp, metadata }) => ({
user: { address },
createdAt: new Date(timestamp).getTime(),
message: metadata?.message || ''
Expand All @@ -134,7 +134,7 @@ export default async function createRpcServerComponent(
response: {
$case: 'requests',
requests: {
requests: mappedRequestss
requests: mappedRequests
}
}
}
Expand All @@ -151,7 +151,7 @@ export default async function createRpcServerComponent(
async getSentFriendshipRequests(_request, context) {
try {
const pendingRequests = await db.getSentFriendshipRequests(context.address)
const mappedRequestss = pendingRequests.map(({ address, timestamp, metadata }) => ({
const mappedRequests = pendingRequests.map(({ address, timestamp, metadata }) => ({
user: { address },
createdAt: new Date(timestamp).getTime(),
message: metadata?.message || ''
Expand All @@ -161,7 +161,7 @@ export default async function createRpcServerComponent(
response: {
$case: 'requests',
requests: {
requests: mappedRequestss
requests: mappedRequests
}
}
}
Expand Down
41 changes: 41 additions & 0 deletions src/migrations/1736277138587_add-indexes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* eslint-disable @typescript-eslint/naming-convention */
kevinszuchet marked this conversation as resolved.
Show resolved Hide resolved
import { MigrationBuilder, ColumnDefinitions } from 'node-pg-migrate'

export const shorthands: ColumnDefinitions | undefined = undefined

export async function up(pgm: MigrationBuilder): Promise<void> {
// Address indexes + constraints
pgm.createIndex('friendships', 'address_requester', { name: 'friendships_address_requester', method: 'hash' })
pgm.createIndex('friendships', 'address_requested', { name: 'friendships_address_requested', method: 'hash' })

pgm.createConstraint('friendships', 'unique_addresses', {
unique: ['address_requester', 'address_requested']
})

pgm.createConstraint('friendships', 'address_requester_smaller_than_address_requested', {
check: 'address_requester < address_requested'
})

// Lowercase indexes
pgm.createIndex('friendships', 'LOWER(address_requester) text_pattern_ops', {
name: 'friendships_address_requester_lower',
method: 'btree'
})
pgm.createIndex('friendships', 'LOWER(address_requested) text_pattern_ops', {
name: 'friendships_address_requested_lower',
method: 'btree'
})

// Friendship history index
pgm.createIndex('friendship_actions', 'friendship_id', { name: 'friendship_actions_friendship_id' })
}

export async function down(pgm: MigrationBuilder): Promise<void> {
pgm.dropIndex('friendships', 'friendships_address_requester')
pgm.dropIndex('friendships', 'friendships_address_requested')
pgm.dropConstraint('friendships', 'unique_addresses')
pgm.dropConstraint('friendships', 'address_requester_smaller_than_address_requested')
pgm.dropIndex('friendships', 'friendships_address_requester_lower')
pgm.dropIndex('friendships', 'friendships_address_requested_lower')
pgm.dropIndex('friendship_actions', 'friendship_actions_friendship_id')
}
Loading