66 lines
2.9 KiB
Go
66 lines
2.9 KiB
Go
package environment
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"os"
|
|
"testing"
|
|
)
|
|
|
|
func TestOwnedPublicSchemaUpgrade(t *testing.T) {
|
|
databaseURL := os.Getenv("CREATORHUB_POSTGRES_TEST_URL")
|
|
if databaseURL == "" {
|
|
t.Skip("set CREATORHUB_POSTGRES_TEST_URL")
|
|
}
|
|
for _, upgrade := range []bool{false, true} {
|
|
name := "fresh and repeated startup"
|
|
if upgrade {
|
|
name = "already applied listener migration preserves data"
|
|
}
|
|
t.Run(name, func(t *testing.T) {
|
|
ctx := context.Background()
|
|
testURL := isolatedDatabaseURL(t, databaseURL)
|
|
store := openFullyMigratedHub(t, ctx, testURL)
|
|
store.Close()
|
|
db, err := sql.Open("pgx", testURL)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer db.Close()
|
|
if upgrade {
|
|
// Reproduce the deployed schema: 1052 belongs to listener alignment,
|
|
// not the public-work fields needed by the account-list query.
|
|
_, err := db.Exec(`
|
|
DELETE FROM schema_migration WHERE version=1053;
|
|
ALTER TABLE creator_work DROP COLUMN IF EXISTS is_public;
|
|
ALTER TABLE creator_collection_checkpoint DROP COLUMN IF EXISTS public_work_count;
|
|
ALTER TABLE creator_collection_checkpoint DROP COLUMN IF EXISTS public_work_count_in_progress;
|
|
INSERT INTO social_account(account_id,name,credential_provider,credential_key,platform,platform_account_key)
|
|
VALUES('public-upgrade-account','保留的账号','os_keyring','creatorhub/public-upgrade','douyin','public-upgrade-uid');
|
|
INSERT INTO creator_work(work_id,platform,work_key,source_type,source_id,title)
|
|
VALUES('public-upgrade-owned','douyin','public-upgrade-owned','owned','public-upgrade-account','保留的自有作品'),
|
|
('public-upgrade-competitor','douyin','public-upgrade-competitor','competitor','public-upgrade-competitor','保留的竞品作品');
|
|
`)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
assertDatabaseCount(t, db, `SELECT count(*) FROM schema_migration WHERE version=1052`, 1)
|
|
}
|
|
for range 2 {
|
|
store = openFullyMigratedHub(t, ctx, testURL)
|
|
store.Close()
|
|
assertDatabaseCount(t, db, `SELECT count(*) FROM schema_migration WHERE version IN (1052,1053)`, 2)
|
|
assertDatabaseCount(t, db, `SELECT count(*) FROM information_schema.columns WHERE table_schema=current_schema()
|
|
AND ((table_name='creator_work' AND column_name='is_public') OR
|
|
(table_name='creator_collection_checkpoint' AND column_name IN ('public_work_count','public_work_count_in_progress')))`, 3)
|
|
assertListenerSchema(t, db)
|
|
if upgrade {
|
|
assertDatabaseCount(t, db, `SELECT count(*) FROM social_account WHERE account_id='public-upgrade-account' AND name='保留的账号'`, 1)
|
|
assertDatabaseCount(t, db, `SELECT count(*) FROM creator_work WHERE work_id='public-upgrade-owned' AND title='保留的自有作品' AND NOT is_public`, 1)
|
|
assertDatabaseCount(t, db, `SELECT count(*) FROM creator_work WHERE work_id='public-upgrade-competitor' AND title='保留的竞品作品' AND is_public`, 1)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|