From 5e9d66193bb28c2c7ce008bdcad04dadcac79a07 Mon Sep 17 00:00:00 2001 From: raykast Date: Thu, 4 Aug 2022 15:52:30 -0700 Subject: [PATCH] Refactor test harness. --- crates/core/Cargo.toml | 3 +- crates/core/src/db/mod.rs | 449 +++--------------- crates/core/src/db/queries/collections.rs | 24 +- crates/core/src/db/queries/nft_count.rs | 10 +- .../2022-08-04-200854_seed_nfts/down.sql | 27 ++ .../2022-08-04-200854_seed_nfts/up.sql | 198 ++++++++ .../down.sql | 14 + .../up.sql | 96 ++++ diesel.sh | 5 +- test-migration.sh | 7 + 10 files changed, 428 insertions(+), 405 deletions(-) create mode 100644 crates/core/test_migrations/2022-08-04-200854_seed_nfts/down.sql create mode 100644 crates/core/test_migrations/2022-08-04-200854_seed_nfts/up.sql create mode 100644 crates/core/test_migrations/2022-08-04-205234_seed_auction_houses/down.sql create mode 100644 crates/core/test_migrations/2022-08-04-205234_seed_auction_houses/up.sql create mode 100755 test-migration.sh diff --git a/crates/core/Cargo.toml b/crates/core/Cargo.toml index b2d102e60..4e0b82226 100644 --- a/crates/core/Cargo.toml +++ b/crates/core/Cargo.toml @@ -72,6 +72,5 @@ cid = { version = "0.7.0", optional = true } url = "2.2.2" md5 = { version = "0.7.0", optional = true } - [dev-dependencies] -lazy_static = "1.4.0" \ No newline at end of file +lazy_static = "1.4.0" diff --git a/crates/core/src/db/mod.rs b/crates/core/src/db/mod.rs index 2a221e1b0..9544965da 100644 --- a/crates/core/src/db/mod.rs +++ b/crates/core/src/db/mod.rs @@ -118,6 +118,31 @@ impl From for ConnectionType { } } +fn migrate( + conn: &C, + desc: impl std::fmt::Display, + run: impl FnOnce(&C, &mut dyn std::io::Write) -> Result<(), diesel_migrations::RunMigrationsError>, +) -> Result<()> { + info!("Running {}...", desc); + + let mut out = vec![]; + + run(conn, &mut out).with_context(|| format!("Failed to run {}", desc))?; + + match std::str::from_utf8(&out) { + Ok(s) => { + let s = s.trim(); + + if !s.is_empty() { + info!("Output from {}:\n{}", desc, s); + } + }, + Err(e) => warn!("Failed to read output from {}: {}", desc, e), + } + + Ok(()) +} + /// Create a pooled connection to the Postgres database, using the given CLI /// arguments and a hint indicating if the database is writable. /// @@ -159,8 +184,6 @@ pub fn connect(args: ConnectArgs, mode: ConnectMode) -> Result { .build(man) .context("Failed to create database connection pool")?; - let mut out = vec![]; - if cfg!(not(debug_assertions)) && matches!(ty, ConnectionType::Default) { warn!("Cannot determine if database is writable; assuming yes"); } @@ -187,24 +210,13 @@ pub fn connect(args: ConnectArgs, mode: ConnectMode) -> Result { }; if migrated { - info!("Running database migrations..."); - - embedded_migrations::run_with_output( - &pool.get().context("Failed to connect to the database")?, - &mut out, - ) - .context("Failed to run database migrations")?; - } - - match std::str::from_utf8(&out) { - Ok(s) => { - let s = s.trim(); - - if !s.is_empty() { - info!("Output from migrations:\n{}", s); - } - }, - Err(e) => warn!("Failed to read migration output: {}", e), + migrate( + &*pool + .get() + .context("Failed to acquire database connection")?, + "database migrations", + embedded_migrations::run_with_output, + )?; } Ok(ConnectResult { pool, ty, migrated }) @@ -212,375 +224,50 @@ pub fn connect(args: ConnectArgs, mode: ConnectMode) -> Result { #[cfg(test)] pub mod test { - use diesel::{insert_into, prelude::*}; - use uuid::Uuid; - - use super::{ - connect, models, - schema::{ - auction_houses, listings, metadata_collection_keys, metadata_jsons, metadatas, - purchases, - }, - ConnectArgs, - }; - use crate::prelude::*; + embed_migrations!("test_migrations"); + + fn initialize() -> super::ConnectResult { + dotenv::from_filename(".env.dev").expect("Failed to load .env.dev"); + dotenv::from_filename(".env").expect("Failed to load .env"); - fn initialize() -> super::Pool { - let conn_args = ConnectArgs { + let conn_args = super::ConnectArgs { database_read_url: None, database_write_url: Some( - "postgres://postgres:holap1ex@localhost:5337/holaplex-indexer".into(), + std::env::var("DATABASE_URL") + .expect("Failed to load DATABASE_URL from environment"), ), database_url: None, }; - let (pool, _) = connect(conn_args, crate::db::ConnectMode::Write) - .expect("failed to connect to database"); - let conn = pool.get().expect("failed to get connection to database"); - - let nft_a_metadata_address = Borrowed("metadata_a"); - let nft_b_metadata_address = Borrowed("metadata_b"); - let nft_c_metadata_address = Borrowed("metadata_c"); - let nft_d_metadata_address = Borrowed("metadata_d"); - let nft_d_purchase_id = Some( - Uuid::parse_str("00000000-0000-0000-0000-000000000009").expect("failed to parse UUID"), - ); - let collection_metadata_address = Borrowed("collection_a"); - let auction_house_address = Borrowed("auction_house_a"); - let seller_address = Borrowed("seller_a"); - let buyer_address = Borrowed("buyer_a"); - - insert_into(metadata_collection_keys::table) - .values(vec![ - models::MetadataCollectionKey { - metadata_address: nft_a_metadata_address.clone(), - collection_address: collection_metadata_address.clone(), - verified: true, - }, - models::MetadataCollectionKey { - metadata_address: nft_b_metadata_address.clone(), - collection_address: collection_metadata_address.clone(), - verified: true, - }, - models::MetadataCollectionKey { - metadata_address: nft_c_metadata_address.clone(), - collection_address: collection_metadata_address.clone(), - verified: true, - }, - models::MetadataCollectionKey { - metadata_address: nft_d_metadata_address.clone(), - collection_address: collection_metadata_address.clone(), - verified: true, - }, - ]) - .on_conflict_do_nothing() - .execute(&conn) - .expect("failed to seed metadata_collection_keys"); - - insert_into(metadatas::table) - .values(vec![ - models::Metadata { - address: nft_a_metadata_address.clone(), - name: Borrowed("nft A"), - symbol: Borrowed("symbol"), - uri: Borrowed("http://example.com/nft-a-uri"), - seller_fee_basis_points: 100, - update_authority_address: Borrowed("update authority"), - mint_address: collection_metadata_address.clone(), - primary_sale_happened: true, - is_mutable: false, - edition_nonce: None, - edition_pda: Borrowed("nft edition pda"), - token_standard: None, - slot: Some(0), - burned: false, - }, - models::Metadata { - address: nft_b_metadata_address.clone(), - name: Borrowed("nft B"), - symbol: Borrowed("symbol"), - uri: Borrowed("http://example.com/nft-b-uri"), - seller_fee_basis_points: 100, - update_authority_address: Borrowed("update authority"), - mint_address: collection_metadata_address.clone(), - primary_sale_happened: true, - is_mutable: false, - edition_nonce: None, - edition_pda: Borrowed("nft edition pda"), - token_standard: None, - slot: Some(0), - burned: false, - }, - models::Metadata { - address: nft_c_metadata_address.clone(), - name: Borrowed("nft C"), - symbol: Borrowed("symbol"), - uri: Borrowed("http://example.com/nft-c-uri"), - seller_fee_basis_points: 100, - update_authority_address: Borrowed("update authority"), - mint_address: collection_metadata_address.clone(), - primary_sale_happened: true, - is_mutable: false, - edition_nonce: None, - edition_pda: Borrowed("nft edition pda"), - token_standard: None, - slot: Some(0), - burned: false, - }, - models::Metadata { - address: nft_d_metadata_address.clone(), - name: Borrowed("nft D"), - symbol: Borrowed("symbol"), - uri: Borrowed("http://example.com/nft-d-uri"), - seller_fee_basis_points: 100, - update_authority_address: Borrowed("update authority"), - mint_address: collection_metadata_address.clone(), - primary_sale_happened: true, - is_mutable: false, - edition_nonce: None, - edition_pda: Borrowed("nft edition pda"), - token_standard: None, - slot: Some(0), - burned: false, - }, - models::Metadata { - address: collection_metadata_address.clone(), - name: Borrowed("collection name"), - symbol: Borrowed("symbol"), - uri: Borrowed("http://example.com/collection-uri"), - seller_fee_basis_points: 100, - update_authority_address: Borrowed("update authority"), - mint_address: Borrowed("collection mint"), - primary_sale_happened: true, - is_mutable: false, - edition_nonce: None, - edition_pda: Borrowed("collection edition pda"), - token_standard: None, - slot: Some(0), - burned: false, - }, - ]) - .on_conflict_do_nothing() - .execute(&conn) - .expect("failed to seed metadatas"); - - insert_into(auction_houses::table) - .values(vec![models::AuctionHouse { - address: auction_house_address.clone(), - treasury_mint: Borrowed("So11111111111111111111111111111111111111112"), - auction_house_treasury: Borrowed("treasury"), - treasury_withdrawal_destination: Borrowed("treasury withdrawal"), - fee_withdrawal_destination: Borrowed("fee withdrawal"), - authority: Borrowed("auction house authority"), - creator: Borrowed("auction house creator"), - bump: 0, - treasury_bump: 0, - fee_payer_bump: 0, - seller_fee_basis_points: 100, - requires_sign_off: false, - can_change_sale_price: false, - auction_house_fee_account: Borrowed("auction house fee account"), - }]) - .on_conflict_do_nothing() - .execute(&conn) - .expect("failed to seed auction_houses"); - - insert_into(listings::table) - .values(vec![ - models::Listing { - id: Some( - Uuid::parse_str("00000000-0000-0000-0000-000000000001") - .expect("failed to parse UUID"), - ), - trade_state: Borrowed("nft_a trade state"), - auction_house: auction_house_address.clone(), - seller: seller_address.clone(), - metadata: nft_a_metadata_address.clone(), - purchase_id: None, - price: 1, - token_size: 1, - trade_state_bump: 0, - created_at: NaiveDate::from_ymd(2020, 1, 2).and_hms(0, 0, 0), - canceled_at: None, - slot: 0, - write_version: Some(0), - }, - models::Listing { - id: Some( - Uuid::parse_str("00000000-0000-0000-0000-000000000002") - .expect("failed to parse UUID"), - ), - trade_state: Borrowed("nft_b trade state"), - auction_house: auction_house_address.clone(), - seller: seller_address.clone(), - metadata: nft_b_metadata_address.clone(), - purchase_id: None, - price: 1, - token_size: 1, - trade_state_bump: 0, - created_at: NaiveDate::from_ymd(2020, 1, 2).and_hms(0, 0, 0), - canceled_at: None, - slot: 0, - write_version: Some(0), - }, - models::Listing { - id: Some( - Uuid::parse_str("00000000-0000-0000-0000-000000000003") - .expect("failed to parse UUID"), - ), - trade_state: Borrowed("nft_c trade state"), - auction_house: auction_house_address.clone(), - seller: seller_address.clone(), - metadata: nft_c_metadata_address.clone(), - purchase_id: None, - price: 1, - token_size: 1, - trade_state_bump: 0, - created_at: NaiveDate::from_ymd(2020, 1, 2).and_hms(0, 0, 0), - canceled_at: None, - slot: 0, - write_version: Some(0), - }, - models::Listing { - id: Some( - Uuid::parse_str("00000000-0000-0000-0000-000000000004") - .expect("failed to parse UUID"), - ), - trade_state: Borrowed("nft_d trade state"), - auction_house: auction_house_address.clone(), - seller: seller_address.clone(), - metadata: nft_d_metadata_address.clone(), - purchase_id: nft_d_purchase_id.clone(), - price: 1, - token_size: 1, - trade_state_bump: 0, - created_at: NaiveDate::from_ymd(2020, 1, 2).and_hms(0, 0, 0), - canceled_at: None, - slot: 0, - write_version: Some(0), - }, - ]) - .on_conflict_do_nothing() - .execute(&conn) - .expect("failed to seed purchases"); - - insert_into(purchases::table) - .values(vec![models::Purchase { - id: nft_d_purchase_id.clone(), - buyer: buyer_address.clone(), - seller: seller_address.clone(), - auction_house: auction_house_address.clone(), - metadata: nft_d_metadata_address.clone(), - token_size: 1, - price: 1, - created_at: NaiveDate::from_ymd(2020, 1, 2).and_hms(0, 0, 0), - slot: 0, - write_version: None, - }]) - .on_conflict_do_nothing() - .execute(&conn) - .expect("failed to seed purchases"); - - insert_into(metadata_jsons::table) - .values(vec![ - models::MetadataJson { - metadata_address: nft_a_metadata_address, - fingerprint: Borrowed(&Vec::::new()), - updated_at: NaiveDate::from_ymd(2020, 1, 2).and_hms(0, 0, 0), - description: Some(Borrowed("nft A description")), - image: Some(Borrowed("http://example.com/nft-a-image")), - animation_url: Some(Borrowed("http://example.com/nft-a-animation")), - external_url: Some(Borrowed("http://example.com/nft-a-external")), - category: Some(Borrowed("nft A category")), - raw_content: Borrowed( - &serde_json::from_str("{}") - .expect("Failed to deserialize metadata content"), - ), - model: Some(Borrowed("model")), - fetch_uri: Borrowed("http://example.com/nft-a-fetch-uri"), - slot: 0, - write_version: 0, - }, - models::MetadataJson { - metadata_address: nft_b_metadata_address, - fingerprint: Borrowed(&Vec::::new()), - updated_at: NaiveDate::from_ymd(2020, 1, 2).and_hms(0, 0, 0), - description: Some(Borrowed("nft B description")), - image: Some(Borrowed("http://example.com/nft-b-image")), - animation_url: Some(Borrowed("http://example.com/nft-b-animation")), - external_url: Some(Borrowed("http://example.com/nft-b-external")), - category: Some(Borrowed("nft B category")), - raw_content: Borrowed( - &serde_json::from_str("{}") - .expect("Failed to deserialize metadata content"), - ), - model: Some(Borrowed("model")), - fetch_uri: Borrowed("http://example.com/nft-b-fetch-uri"), - slot: 0, - write_version: 0, - }, - models::MetadataJson { - metadata_address: nft_c_metadata_address, - fingerprint: Borrowed(&Vec::::new()), - updated_at: NaiveDate::from_ymd(2020, 1, 2).and_hms(0, 0, 0), - description: Some(Borrowed("nft C description")), - image: Some(Borrowed("http://example.com/nft-c-image")), - animation_url: Some(Borrowed("http://example.com/nft-c-animation")), - external_url: Some(Borrowed("http://example.com/nft-c-external")), - category: Some(Borrowed("nft C category")), - raw_content: Borrowed( - &serde_json::from_str("{}") - .expect("Failed to deserialize metadata content"), - ), - model: Some(Borrowed("model")), - fetch_uri: Borrowed("http://example.com/nft-c-fetch-uri"), - slot: 0, - write_version: 0, - }, - models::MetadataJson { - metadata_address: nft_d_metadata_address, - fingerprint: Borrowed(&Vec::::new()), - updated_at: NaiveDate::from_ymd(2020, 1, 2).and_hms(0, 0, 0), - description: Some(Borrowed("nft C description")), - image: Some(Borrowed("http://example.com/nft-c-image")), - animation_url: Some(Borrowed("http://example.com/nft-c-animation")), - external_url: Some(Borrowed("http://example.com/nft-c-external")), - category: Some(Borrowed("nft B category")), - raw_content: Borrowed( - &serde_json::from_str("{}") - .expect("Failed to deserialize metadata content"), - ), - model: Some(Borrowed("model")), - fetch_uri: Borrowed("http://example.com/nft-c-fetch-uri"), - slot: 0, - write_version: 0, - }, - models::MetadataJson { - metadata_address: collection_metadata_address, - fingerprint: Borrowed(&Vec::::new()), - updated_at: NaiveDate::from_ymd(2020, 1, 2).and_hms(0, 0, 0), - description: Some(Borrowed("collection description")), - image: Some(Borrowed("http://example.com/collection-image")), - animation_url: Some(Borrowed("http://example.com/collection-animation")), - external_url: Some(Borrowed("http://example.com/collection-external")), - category: Some(Borrowed("collection category")), - raw_content: Borrowed( - &serde_json::from_str("{}") - .expect("Failed to deserialize metadata content"), - ), - model: Some(Borrowed("model")), - fetch_uri: Borrowed("http://example.com/collection-fetch-uri"), - slot: 0, - write_version: 0, - }, - ]) - .on_conflict_do_nothing() - .execute(&conn) - .expect("failed to seed metadata_jsons"); - - pool + let res = super::connect(conn_args, super::ConnectMode::Write { migrate: true }) + .expect("Failed to connect to database"); + + super::migrate( + &*res + .pool + .get() + .expect("Failed to acquire database connection"), + "test seed migrations", + embedded_migrations::run_with_output, + ) + .expect("Seed migrations failed"); + + res } lazy_static::lazy_static! { - pub static ref DATABASE: super::Pool = initialize(); + static ref DATABASE: super::ConnectResult = initialize(); + } + + #[must_use] + pub fn connect() -> super::PooledConnection { + DATABASE + .pool + .get() + .expect("Failed to acquire database connection") + } + + pub mod prelude { + pub use super::connect; + pub use crate::{db::custom_types::*, prelude::*}; } } diff --git a/crates/core/src/db/queries/collections.rs b/crates/core/src/db/queries/collections.rs index 5ecb5cf56..0fff8d824 100644 --- a/crates/core/src/db/queries/collections.rs +++ b/crates/core/src/db/queries/collections.rs @@ -173,43 +173,41 @@ fn make_by_market_cap_query_string(order_direction: OrderDirection) -> String { #[cfg(test)] mod tests { - use chrono::{TimeZone, Utc}; - - use crate::db::test::DATABASE; + use crate::db::test::prelude::*; #[test] fn test_collections_featured_by_marketcap_returns_non_empty() { - let pool = &DATABASE; + let conn = connect(); let result = super::by_market_cap( - &pool.get().expect("failed to aquire database connection"), + &conn, None::>, - crate::db::custom_types::OrderDirection::Desc, + OrderDirection::Desc, Utc.ymd(1901, 1, 1).and_hms(0, 0, 0), Utc.ymd(3000, 1, 1).and_hms(0, 0, 0), 50, 0, ) - .expect("failed query"); + .unwrap(); - assert!(!result.is_empty(), "expected at least one row"); + assert!(!result.is_empty(), "Expected at least one row"); } #[test] fn test_collections_featured_by_volume_returns_non_empty() { - let pool = &DATABASE; + let conn = connect(); let result = super::by_volume( - &pool.get().expect("failed to aquire database connection"), + &conn, None::>, - crate::db::custom_types::OrderDirection::Desc, + OrderDirection::Desc, Utc.ymd(1901, 1, 1).and_hms(0, 0, 0), Utc.ymd(3000, 1, 1).and_hms(0, 0, 0), 50, 0, ) - .expect("failed query"); + .unwrap(); - assert!(!result.is_empty(), "expected at least one row"); + assert!(!result.is_empty(), "Expected at least one row"); } } diff --git a/crates/core/src/db/queries/nft_count.rs b/crates/core/src/db/queries/nft_count.rs index 3c9833b99..1ee7d580e 100644 --- a/crates/core/src/db/queries/nft_count.rs +++ b/crates/core/src/db/queries/nft_count.rs @@ -306,16 +306,12 @@ pub fn store_creators( #[cfg(test)] mod tests { - use crate::db::test::DATABASE; + use crate::db::test::prelude::*; #[test] fn test_store_creators_minimal_passes() { - let pool = &DATABASE; + let conn = connect(); - let _ = super::store_creators( - &pool.get().expect("failed to aquire database connection"), - Vec::::new(), - ) - .expect("failed query"); + std::mem::drop(super::store_creators(&conn, Vec::::new()).unwrap()); } } diff --git a/crates/core/test_migrations/2022-08-04-200854_seed_nfts/down.sql b/crates/core/test_migrations/2022-08-04-200854_seed_nfts/down.sql new file mode 100644 index 000000000..d6df2ac2f --- /dev/null +++ b/crates/core/test_migrations/2022-08-04-200854_seed_nfts/down.sql @@ -0,0 +1,27 @@ +delete from metadatas where address = any(array [ + 'meta_address_0', + 'meta_address_1', + 'meta_address_2', + 'meta_address_3', + 'collection_address' +]); + +delete from metadata_jsons where metadata_address = any(array [ + 'meta_address_0', + 'meta_address_1', + 'meta_address_2', + 'meta_address_3', + 'collection_address' +]); + +delete from metadata_collection_keys where collection_address = any(array [ + 'collection_mint' +]); + +delete from current_metadata_owners where mint_address = any(array [ + 'mint_0', + 'mint_1', + 'mint_2', + 'mint_3', + 'collection_mint' +]); diff --git a/crates/core/test_migrations/2022-08-04-200854_seed_nfts/up.sql b/crates/core/test_migrations/2022-08-04-200854_seed_nfts/up.sql new file mode 100644 index 000000000..262294505 --- /dev/null +++ b/crates/core/test_migrations/2022-08-04-200854_seed_nfts/up.sql @@ -0,0 +1,198 @@ +insert into metadatas values ( + 'meta_address_0', -- address + 'Metadata 0', -- name + 'NFT', -- symbol + 'https://example.com/0.json', -- uri + 1000, -- seller_fee_basis_points + 'updater', -- update_authority_address + 'mint_0', -- mint_address + false, -- primary_sale_happened + false, -- is_mutable + 1337, -- edition_nonce + 'edition', -- edition_pda + 'NonFungible', -- token_standard + 1, -- slot + false -- burned +), ( + 'meta_address_1', -- address + 'Metadata 1', -- name + 'NFT', -- symbol + 'https://example.com/1.json', -- uri + 1000, -- seller_fee_basis_points + 'updater', -- update_authority_address + 'mint_1', -- mint_address + false, -- primary_sale_happened + false, -- is_mutable + 1337, -- edition_nonce + 'edition', -- edition_pda + 'NonFungible', -- token_standard + 1, -- slot + false -- burned +), ( + 'meta_address_2', -- address + 'Metadata 2', -- name + 'NFT', -- symbol + 'https://example.com/2.json', -- uri + 1000, -- seller_fee_basis_points + 'updater', -- update_authority_address + 'mint_2', -- mint_address + false, -- primary_sale_happened + false, -- is_mutable + 1337, -- edition_nonce + 'edition', -- edition_pda + 'NonFungible', -- token_standard + 1, -- slot + false -- burned +), ( + 'meta_address_3', -- address + 'Metadata 3', -- name + 'NFT', -- symbol + 'https://example.com/3.json', -- uri + 1000, -- seller_fee_basis_points + 'updater', -- update_authority_address + 'mint_3', -- mint_address + false, -- primary_sale_happened + false, -- is_mutable + 1337, -- edition_nonce + 'edition', -- edition_pda + 'NonFungible', -- token_standard + 1, -- slot + false -- burned +), ( + 'collection_address', -- address + 'Collection', -- name + 'COLL', -- symbol + 'https://example.com/c.json', -- uri + 1000, -- seller_fee_basis_points + 'updater', -- update_authority_address + 'collection_mint', -- mint_address + false, -- primary_sale_happened + false, -- is_mutable + 1337, -- edition_nonce + 'Edition', -- edition_pda + 'NonFungible', -- token_standard + 1, -- slot + false -- burned +); + +insert into metadata_jsons values ( + 'meta_address_0', -- metadata_address + '00', -- fingerprint + '2020-01-01', -- updated_at + 'An NFT', -- description + 'https://example.com/0.jpg', -- image + 'https://example.com/0.mkv', -- animation_url + 'https://example.com/0', -- external_url + 'test', -- category + '{}', -- raw_content + 'seeded', -- model + 'https://example.com/0.json', -- fetch_uri + 1, -- slot + 0 -- write_version +), ( + 'meta_address_1', -- metadata_address + '01', -- fingerprint + '2020-01-01', -- updated_at + 'An NFT', -- description + 'https://example.com/1.jpg', -- image + 'https://example.com/1.mkv', -- animation_url + 'https://example.com/1', -- external_url + 'test', -- category + '{}', -- raw_content + 'seeded', -- model + 'https://example.com/1.json', -- fetch_uri + 1, -- slot + 0 -- write_version +), ( + 'meta_address_2', -- metadata_address + '02', -- fingerprint + '2020-01-01', -- updated_at + 'An NFT', -- description + 'https://example.com/2.jpg', -- image + 'https://example.com/2.mkv', -- animation_url + 'https://example.com/2', -- external_url + 'test', -- category + '{}', -- raw_content + 'seeded', -- model + 'https://example.com/2.json', -- fetch_uri + 1, -- slot + 0 -- write_version +), ( + 'meta_address_3', -- metadata_address + '03', -- fingerprint + '2020-01-01', -- updated_at + 'An NFT', -- description + 'https://example.com/3.jpg', -- image + 'https://example.com/3.mkv', -- animation_url + 'https://example.com/3', -- external_url + 'test', -- category + '{}', -- raw_content + 'seeded', -- model + 'https://example.com/3.json', -- fetch_uri + 1, -- slot + 0 -- write_version +), ( + 'collection_address', -- metadata_address + 'c0', -- fingerprint + '2020-01-01', -- updated_at + 'An NFT collection', -- description + 'https://example.com/c.jpg', -- image + 'https://example.com/c.mkv', -- animation_url + 'https://example.com/c', -- external_url + 'test', -- category + '{}', -- raw_content + 'seeded', -- model + 'https://example.com/c.json', -- fetch_uri + 1, -- slot + 0 -- write_version +); + +insert into metadata_collection_keys values ( + 'meta_address_0', -- metadata_address + 'collection_mint', -- collection_address + true -- verified +), ( + 'meta_address_1', -- metadata_address + 'collection_mint', -- collection_address + true -- verified +), ( + 'meta_address_2', -- metadata_address + 'collection_mint', -- collection_address + true -- verified +), ( + 'meta_address_3', -- metadata_address + 'collection_mint', -- collection_address + true -- verified +); + +insert into current_metadata_owners values ( + 'mint_0', -- mint_address + 'meta_owner', -- owner_address + 'ata_owner', -- token_account_address + '2020-01-01', -- updated_at + 1 -- slot +), ( + 'mint_1', -- mint_address + 'meta_owner', -- owner_address + 'ata_owner', -- token_account_address + '2020-01-01', -- updated_at + 1 -- slot +), ( + 'mint_2', -- mint_address + 'meta_owner', -- owner_address + 'ata_owner', -- token_account_address + '2020-01-01', -- updated_at + 1 -- slot +), ( + 'mint_3', -- mint_address + 'meta_owner', -- owner_address + 'ata_owner', -- token_account_address + '2020-01-01', -- updated_at + 1 -- slot +), ( + 'collection_mint', -- mint_address + 'meta_owner', -- owner_address + 'ata_owner', -- token_account_address + '2020-01-01', -- updated_at + 1 -- slot +); diff --git a/crates/core/test_migrations/2022-08-04-205234_seed_auction_houses/down.sql b/crates/core/test_migrations/2022-08-04-205234_seed_auction_houses/down.sql new file mode 100644 index 000000000..66539b761 --- /dev/null +++ b/crates/core/test_migrations/2022-08-04-205234_seed_auction_houses/down.sql @@ -0,0 +1,14 @@ +delete from auction_houses where address = any(array [ + 'ah_address' +]); + +delete from listings where id = any(array [ + '00000000-0000-0000-0001-000000000000'::uuid, + '00000000-0000-0000-0001-000000000001', + '00000000-0000-0000-0001-000000000002', + '00000000-0000-0000-0001-000000000003' +]); + +delete from purchases where id = any(array [ + '00000000-0000-0000-0002-000000000003'::uuid +]); diff --git a/crates/core/test_migrations/2022-08-04-205234_seed_auction_houses/up.sql b/crates/core/test_migrations/2022-08-04-205234_seed_auction_houses/up.sql new file mode 100644 index 000000000..ad2823764 --- /dev/null +++ b/crates/core/test_migrations/2022-08-04-205234_seed_auction_houses/up.sql @@ -0,0 +1,96 @@ +insert into auction_houses values ( + 'ah_address', -- address + 'So11111111111111111111111111111111111111112', -- treasury_mint + 'ah_treasury', -- auction_house_treasury + 'ah_withdrawal', -- treasury_withdrawal_destination + 'ah_fee_withdrawal', -- fee_withdrawal_destination + 'ah_authority', -- authority + 'ah_creator', -- creator + 0, -- bump + 0, -- treasury_bump + 0, -- fee_payer_bump + 1000, -- seller_fee_basis_points + true, -- requires_sign_off + false, -- can_change_sale_price + 'ah_fees' -- auction_house_fee_account +); + +insert into listings values ( + '00000000-0000-0000-0001-000000000000', -- id + 'meta_trade_state_0', -- trade_state + 'ah_address', -- auction_house + 'meta_owner', -- seller + 'meta_address_0', -- metadata + null, -- purchase_id + 1, -- price + 1, -- token_size + 0, -- trade_state_bump + '2020-01-01', -- created_at + null, -- canceled_at + 1, -- slot + 0, -- write_version + 'market_program', -- marketplace_program + null -- expiry +), ( + '00000000-0000-0000-0001-000000000001', -- id + 'meta_trade_state_1', -- trade_state + 'ah_address', -- auction_house + 'meta_owner', -- seller + 'meta_address_1', -- metadata + null, -- purchase_id + 1, -- price + 1, -- token_size + 0, -- trade_state_bump + '2020-01-01', -- created_at + null, -- canceled_at + 1, -- slot + 0, -- write_version + 'market_program', -- marketplace_program + null -- expiry +), ( + '00000000-0000-0000-0001-000000000002', -- id + 'meta_trade_state_2', -- trade_state + 'ah_address', -- auction_house + 'meta_owner', -- seller + 'meta_address_2', -- metadata + null, -- purchase_id + 1, -- price + 1, -- token_size + 0, -- trade_state_bump + '2020-01-01', -- created_at + null, -- canceled_at + 1, -- slot + 0, -- write_version + 'market_program', -- marketplace_program + null -- expiry +), ( + '00000000-0000-0000-0001-000000000003', -- id + 'meta_trade_state_3', -- trade_state + 'ah_address', -- auction_house + 'meta_owner', -- seller + 'meta_address_3', -- metadata + '00000000-0000-0000-0002-000000000003', -- purchase_id + 1, -- price + 1, -- token_size + 0, -- trade_state_bump + '2020-01-01', -- created_at + null, -- canceled_at + 1, -- slot + 0, -- write_version + 'market_program', -- marketplace_program + null -- expiry +); + +insert into purchases values ( + '00000000-0000-0000-0002-000000000003', -- id + 'meta_buyer', -- buyer + 'meta_owner', -- seller + 'ah_address', -- auction_house + 'meta_address_3', -- metadata + 1, -- token_size + 1, -- price + '2020-01-01', -- created_at + 1, -- slot + 0, -- write_version + 'market_program' -- marketplace_program +); diff --git a/diesel.sh b/diesel.sh index 95057c1e4..67299313d 100755 --- a/diesel.sh +++ b/diesel.sh @@ -1,6 +1,7 @@ #!/bin/bash set -e +cd "$(dirname "$0")" for f in '' .dev .local; do f="./.env$f" @@ -13,6 +14,6 @@ done export DATABASE_URL -cd "$(dirname "$0")"/crates/core +cd crates/core -diesel "$@" +exec diesel "$@" diff --git a/test-migration.sh b/test-migration.sh new file mode 100755 index 000000000..3b4f3888d --- /dev/null +++ b/test-migration.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +set -e + +cd "$(dirname "$0")" + +exec ./diesel.sh migration --migration-dir test_migrations "$@"