From a0f301602474394308d6e375be26fdcbd2c2348f Mon Sep 17 00:00:00 2001 From: Big Boss Date: Tue, 15 Oct 2024 19:02:16 -0500 Subject: [PATCH] update directions for snapshot --- packages/snaplet/.gitignore | 1 + packages/snaplet/README.md | 30 ++++++++++++++++-------------- packages/snaplet/snaplet.config.ts | 19 +++++++++++++++++++ 3 files changed, 36 insertions(+), 14 deletions(-) create mode 100644 packages/snaplet/.gitignore diff --git a/packages/snaplet/.gitignore b/packages/snaplet/.gitignore new file mode 100644 index 000000000..6f685ede8 --- /dev/null +++ b/packages/snaplet/.gitignore @@ -0,0 +1 @@ +.snaplet/snapshots/* diff --git a/packages/snaplet/README.md b/packages/snaplet/README.md index fa8ef7735..fd6af5387 100644 --- a/packages/snaplet/README.md +++ b/packages/snaplet/README.md @@ -8,9 +8,21 @@ It seeds our database using two main methods: [`seed.ts`](./seed.ts): This resets the database and seeds it with a set of default data. It is used to seed the database for local development esepcially for data that is not available in the production database. +## Capturing snapshots + +`bunx @snaplet/snapshot snapshot capture`: This captures a snapshot of the database and saves it locally. It can then be shared with other developers by uploading the snapshot to snaplet's cloud storage. + +**⚠️ Capturing snapshots** requires access to the production database. + +```shell +# set to production database url +export SNAPLET_SOURCE_DATABASE_URL=postgresql://postgres:postgres@127.0.0.1:54322/postgres +bunx @snaplet/snapshot snapshot capture +``` + ## Restoring from snapshot -`bunx snaplet snapshot restore --no-reset`: This restores the database from a snapshot hosted in snaplet's cloud storage. It is used to restore the database for local development and restores production-like data. This is useful for testing and debugging. +`bunx @snaplet/snapshot snapshot restore --no-reset`: This restores the database from a snapshot hosted in snaplet's cloud storage. It is used to restore the database for local development and restores production-like data. This is useful for testing and debugging. **⚠️ When restoring from snapshot** migrations are not run and can make your local database inconsistent with the production database or even fail to restore some data. To mitigate this, remove any migrations that are not in production yet. See below for how to remove migrations to overcome this. @@ -26,22 +38,12 @@ git diff --name-only --diff-filter=A origin/main..HEAD -- supabase/migrations | ### Restore ```shell +# set target database url to local development database +export SNAPLET_TARGET_DATABASE_URL=$SUPABASE_DB_URL # now run the snapshot restore command bunx supabase db reset && \ -bunx snaplet snapshot restore --no-reset --latest && \ +bunx @snaplet/snapshot snapshot restore --no-reset --latest && \ git checkout ./supabase/migrations && \ bunx supabase db push --local --include-all psql $SUPABASE_DB_URL -c "insert into send_accounts (user_id, address, chain_id, init_code) select u.id as user_id, c.address, '845337' as chain_id, CONCAT( '\\x00', upper( CONCAT( md5(random() :: text), md5(random() :: text), md5(random() :: text), md5(random() :: text) ) ) ) :: bytea as init_code from auth.users u join chain_addresses c on c.user_id = u.id where user_id not in ( select user_id from send_accounts );" ``` - -## Capturing snapshots - -`bunx snaplet snapshot capture`: This captures a snapshot of the database and saves it locally. It can then be shared with other developers by uploading the snapshot to snaplet's cloud storage. - -**⚠️ Capturing snapshots** requires access to the production database. - -```shell -export SNAPLET_SOURCE_DATABASE_URL=postgresql://postgres:postgres@127.0.0.1:54322/postgres -bunx snaplet snapshot capture -bunx snaplet snapshot share -``` diff --git a/packages/snaplet/snaplet.config.ts b/packages/snaplet/snaplet.config.ts index 01c95f8eb..6f272ffe4 100644 --- a/packages/snaplet/snaplet.config.ts +++ b/packages/snaplet/snaplet.config.ts @@ -51,8 +51,27 @@ export default defineConfig({ shovel: false, // @ts-ignore pgtap: false, + public: { + // activity: false, + }, }, + // TODO: figure out how much data we need to snapshot + // subset: { + // targets: [ + // // { + // // table: "public.activity", + // // orderBy: `"activity"."created_at" desc`, + // // percent: 10 + // // }, + // // { + // // table: "public.send_account_transfers", + // // orderBy: `"send_account_transfers"."block_num" desc`, + // // percent: 5 + // // } + // ], + // }, transform: { + $mode: 'auto', auth: { users: ({ row }) => { let phone: string