refactor: simplify dataset source ownership
This commit is contained in:
@@ -33,8 +33,6 @@ func TestInitDatasetCreatesDefaultSourcesWhenTableIsEmpty(t *testing.T) {
|
||||
require.True(t, sources[index].Enabled)
|
||||
require.Equal(t, root.ID, sources[index].OwnerID)
|
||||
require.Equal(t, root.Identity, sources[index].OwnerIdentity)
|
||||
require.Equal(t, root.ID, sources[index].CreatedBy)
|
||||
require.Equal(t, root.Identity, sources[index].CreatedByIdentity)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,10 +42,10 @@ func TestInitDatasetAddsMissingDefaultsWithoutChangingCustomSources(t *testing.T
|
||||
var root models.SaUser
|
||||
require.NoError(t, database.Where("email = ?", rootUsername).First(&root).Error)
|
||||
existing := models.SaDatasetSource{
|
||||
CreatedBy: root.ID,
|
||||
Name: "Existing source",
|
||||
Kind: "manual",
|
||||
Enabled: true,
|
||||
OwnerID: root.ID,
|
||||
Name: "Existing source",
|
||||
Kind: "manual",
|
||||
Enabled: true,
|
||||
}
|
||||
require.NoError(t, database.Create(&existing).Error)
|
||||
|
||||
@@ -70,20 +68,33 @@ func TestInitDatasetBackfillsLegacySourceOwners(t *testing.T) {
|
||||
PasswordHash: "hash", Role: "user",
|
||||
}
|
||||
require.NoError(t, database.Create(&other).Error)
|
||||
require.NoError(t, database.Exec(
|
||||
"ALTER TABLE sa_dataset_sources ADD COLUMN created_by integer NOT NULL DEFAULT 0",
|
||||
).Error)
|
||||
require.NoError(t, database.Exec(
|
||||
"ALTER TABLE sa_dataset_sources ADD COLUMN created_by_identity text",
|
||||
).Error)
|
||||
source := models.SaDatasetSource{
|
||||
CreatedBy: other.ID, Name: "Existing source", Kind: "manual", Enabled: true,
|
||||
OwnerID: other.ID, Name: "Existing source", Kind: "manual", Enabled: true,
|
||||
}
|
||||
require.NoError(t, database.Create(&source).Error)
|
||||
require.NoError(t, database.Model(&source).Updates(map[string]any{
|
||||
"owner_id": 0, "owner_identity": "",
|
||||
}).Error)
|
||||
require.NoError(t, database.Exec(`
|
||||
UPDATE sa_dataset_sources
|
||||
SET owner_id = 0, owner_identity = '', created_by = ?, created_by_identity = ?
|
||||
WHERE id = ?
|
||||
`, other.ID, other.Identity, source.ID).Error)
|
||||
|
||||
require.NoError(t, InitDataset(database))
|
||||
|
||||
require.NoError(t, database.First(&source, source.ID).Error)
|
||||
require.Equal(t, other.ID, source.OwnerID)
|
||||
require.Equal(t, other.Identity, source.OwnerIdentity)
|
||||
require.Equal(t, other.ID, source.CreatedBy)
|
||||
hasCreatedBy, err := hasDatasetSourceColumn(database, "created_by")
|
||||
require.NoError(t, err)
|
||||
require.False(t, hasCreatedBy)
|
||||
hasCreatedByIdentity, err := hasDatasetSourceColumn(database, "created_by_identity")
|
||||
require.NoError(t, err)
|
||||
require.False(t, hasCreatedByIdentity)
|
||||
}
|
||||
|
||||
func TestInitDatasetRejectsMissingRootInsteadOfAssigningDefaultsToAnotherUser(t *testing.T) {
|
||||
@@ -115,11 +126,11 @@ func TestInitDatasetRepairsLegacyRSSHubURLsForRootOnly(t *testing.T) {
|
||||
require.NoError(t, database.Create(&other).Error)
|
||||
|
||||
rootLegacy := models.SaDatasetSource{
|
||||
CreatedBy: root.ID, Name: "财联社-热门", Kind: "rss",
|
||||
OwnerID: root.ID, Name: "财联社-热门", Kind: "rss",
|
||||
URL: "https://rsshub.app/cls/hot", Enabled: true,
|
||||
}
|
||||
otherLegacy := models.SaDatasetSource{
|
||||
CreatedBy: other.ID, Name: "财联社-热门", Kind: "rss",
|
||||
OwnerID: other.ID, Name: "财联社-热门", Kind: "rss",
|
||||
URL: "https://rsshub.app/cls/hot", Enabled: true,
|
||||
}
|
||||
require.NoError(t, database.Create(&rootLegacy).Error)
|
||||
@@ -142,7 +153,7 @@ func TestInitDatasetPreparesCollectedItemUniqueness(t *testing.T) {
|
||||
var root models.SaUser
|
||||
require.NoError(t, database.Where("email = ?", rootUsername).First(&root).Error)
|
||||
source := models.SaDatasetSource{
|
||||
CreatedBy: root.ID, Name: "Existing feed", Kind: "rss",
|
||||
OwnerID: root.ID, Name: "Existing feed", Kind: "rss",
|
||||
URL: "https://example.com/feed", Enabled: true,
|
||||
}
|
||||
require.NoError(t, database.Create(&source).Error)
|
||||
|
||||
Reference in New Issue
Block a user