-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1109 from dm3-org/usePrismaAccountsInBackend
Use prisma accounts in backend
- Loading branch information
Showing
53 changed files
with
1,118 additions
and
796 deletions.
There are no files selected for viewing
This file contains 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 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
27 changes: 27 additions & 0 deletions
27
packages/backend/manual_data_migration/insertWithinDocker.sh
This file contains 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,27 @@ | ||
#!/bin/bash | ||
|
||
# Input file | ||
input_file="dump.txt" | ||
|
||
DB_NAME="dm3" | ||
DB_USER="prisma" | ||
|
||
|
||
# Read the input file line by line | ||
while IFS= read -r line | ||
do | ||
# Extract the ID (first part of the line) and timestamp (after "createdAt") | ||
id=$(echo "$line" | cut -d ':' -f 2) | ||
timestamp=$(echo "$line" | grep -oP '(?<="createdAt":)[0-9]+') | ||
|
||
# Convert the timestamp from milliseconds to seconds | ||
timestamp_seconds=$(echo $timestamp | sed 's/...$//') | ||
|
||
# Insert the extracted values into the PostgreSQL table | ||
psql -U $DB_USER -d $DB_NAME -c "INSERT INTO \"Account\" (id, \"createdAt\") \ | ||
VALUES ('$id', to_timestamp($timestamp_seconds))\ | ||
ON CONFLICT (id) \ | ||
DO UPDATE SET \"createdAt\" = excluded.\"createdAt\";" | ||
|
||
done < "$input_file" | ||
|
This file contains 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,37 @@ | ||
Process: | ||
|
||
check data | ||
`docker exec -it dm3-db-1 redis-cli --scan --pattern 'session*addr.dm3.eth'` | ||
|
||
`docker exec -it dm3-storage psql -U prisma -d dm3 -c 'SELECT * FROM "Account";'` | ||
|
||
go into the redis container | ||
docker exec -it dm3-db-1 bash | ||
|
||
dump all relevant sessions | ||
for key in `redis-cli --scan --pattern 'session*addr.dm3.eth'`; do echo $key: `redis-cli GET $key` >> dump.txt; echo $key; done | ||
|
||
copy the dump to the host | ||
docker cp dm3-db-1:/data/dump.txt . | ||
|
||
copy the dump to the postgres container | ||
docker cp dump.txt dm3-storage:/ | ||
|
||
paste script onto server | ||
vi insertWithinDocker.sh | ||
-> paste, close | ||
|
||
copy the script to the postgres container | ||
docker cp insertWithinDocker.sh dm3-storage:/ | ||
|
||
go into the postgres container | ||
docker exec -it dm3-storage bash | ||
|
||
make script executable | ||
chmod a+x insertWithinDocker.sh | ||
|
||
run the script | ||
./insertWithinDocker.sh | ||
|
||
check the data from outside the container | ||
docker exec -it dm3-storage psql -U prisma -d dm3 -c 'SELECT \* FROM "Account";' |
2 changes: 2 additions & 0 deletions
2
packages/backend/migrations/20240719092732_add_account_created_at/migration.sql
This file contains 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,2 @@ | ||
-- AlterTable | ||
ALTER TABLE "Account" ADD COLUMN "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP; |
This file contains 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 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 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
File renamed without changes.
File renamed without changes.
33 changes: 33 additions & 0 deletions
33
packages/backend/src/persistence/account/setAccount.test.ts
This file contains 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,33 @@ | ||
import { UserProfile } from '@dm3-org/dm3-lib-profile'; | ||
import { PrismaClient } from '@prisma/client'; | ||
import { IBackendDatabase, getDatabase, getPrismaClient } from '../getDatabase'; | ||
|
||
const USER_NAME = '0x25A643B6e52864d0eD816F1E43c0CF49C83B8292.dm3.eth'; | ||
|
||
describe('Set Account', () => { | ||
let prismaClient: PrismaClient; | ||
let db: IBackendDatabase; | ||
|
||
beforeEach(async () => { | ||
prismaClient = await getPrismaClient(); | ||
db = await getDatabase(prismaClient); | ||
}); | ||
|
||
it('Creates a new Account ', async () => { | ||
const profile: UserProfile = { | ||
publicEncryptionKey: '', | ||
publicSigningKey: '', | ||
deliveryServices: [], | ||
}; | ||
|
||
const priorSetAccount = await db.getAccount(USER_NAME); | ||
|
||
//User has no account yet | ||
expect(priorSetAccount).toBe(null); | ||
await db.setAccount(USER_NAME); | ||
|
||
const afterSetAccount = await db.getAccount(USER_NAME); | ||
//User has no account yet | ||
expect(afterSetAccount?.id).toEqual(USER_NAME); | ||
}); | ||
}); |
File renamed without changes.
This file contains 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
60 changes: 0 additions & 60 deletions
60
packages/backend/src/persistence/session/setAccount.test.ts
This file was deleted.
Oops, something went wrong.
This file contains 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
18 changes: 18 additions & 0 deletions
18
packages/backend/src/persistence/storage/postgres/getAccount.ts
This file contains 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,18 @@ | ||
import { PrismaClient } from '@prisma/client'; | ||
|
||
export const getAccount = (db: PrismaClient) => async (ensName: string) => { | ||
// Find the account | ||
const account = await db.account.findFirst({ | ||
where: { | ||
id: ensName, | ||
}, | ||
}); | ||
|
||
// Return the account if it exists | ||
if (account) { | ||
return account; | ||
} | ||
|
||
// If the account does not exist, return null | ||
return null; | ||
}; |
15 changes: 15 additions & 0 deletions
15
packages/backend/src/persistence/storage/postgres/hasAccount.ts
This file contains 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,15 @@ | ||
import { PrismaClient } from '@prisma/client'; | ||
|
||
/// Check if an account exists for a given ENS name | ||
/// @param db | ||
/// @returns true if account exists, false otherwise | ||
export const hasAccount = (db: PrismaClient) => async (ensName: string) => { | ||
//Check if account exists | ||
const account = await db.account.findFirst({ | ||
where: { | ||
id: ensName, | ||
}, | ||
}); | ||
|
||
return !!account; | ||
}; |
6 changes: 6 additions & 0 deletions
6
packages/backend/src/persistence/storage/postgres/setAccount.ts
This file contains 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,6 @@ | ||
import { PrismaClient } from '@prisma/client'; | ||
import { getOrCreateAccount } from './utils/getOrCreateAccount'; | ||
|
||
export const setAccount = (db: PrismaClient) => async (ensName: string) => { | ||
return getOrCreateAccount(db, ensName); | ||
}; |
Oops, something went wrong.