Skip to content

Commit 21169fc

Browse files
committed
fix: ignore a deleted branch in queries + delete branch entity after cleanup
1 parent 6a39a5b commit 21169fc

File tree

11 files changed

+54
-11
lines changed

11 files changed

+54
-11
lines changed

backend/data/src/main/kotlin/io/tolgee/development/testDataBuilder/data/KeysTestData.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import io.tolgee.model.UserAccount
99
import io.tolgee.model.enums.ProjectPermissionType
1010
import io.tolgee.model.key.Key
1111
import io.tolgee.model.key.Tag
12+
import java.util.Date
1213

1314
class KeysTestData {
1415
lateinit var enOnlyUserAccount: UserAccount
@@ -119,6 +120,17 @@ class KeysTestData {
119120
branch = self
120121
}
121122
}
123+
124+
// deleted branch with the same name
125+
addBranch {
126+
name = "dev"
127+
archivedAt = Date(1759833439)
128+
}.build {
129+
addKey {
130+
name = "first_key"
131+
branch = self
132+
}
133+
}
122134
}
123135

124136
addUserAccountWithoutOrganization {

backend/data/src/main/kotlin/io/tolgee/development/testDataBuilder/data/TranslationsTestData.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import io.tolgee.model.enums.TranslationState
1818
import io.tolgee.model.key.Key
1919
import io.tolgee.model.key.screenshotReference.KeyInScreenshotPosition
2020
import io.tolgee.model.translation.Translation
21+
import java.util.Date
2122

2223
class TranslationsTestData {
2324
lateinit var project: Project
@@ -89,6 +90,17 @@ class TranslationsTestData {
8990
}
9091
}
9192

93+
// deleted branch
94+
addBranch {
95+
name = "from-default"
96+
project = this@project.self
97+
archivedAt = Date(1759834567)
98+
}.build {
99+
addBasicKey().apply {
100+
branch = self
101+
}
102+
}
103+
92104
val zKeyBuilder =
93105
addKey {
94106
name = "Z key"

backend/data/src/main/kotlin/io/tolgee/model/Project.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import jakarta.validation.constraints.Pattern
4343
import jakarta.validation.constraints.Size
4444
import org.hibernate.annotations.ColumnDefault
4545
import org.hibernate.annotations.Filter
46+
import org.hibernate.annotations.SQLRestriction
4647
import org.springframework.beans.factory.ObjectFactory
4748
import org.springframework.beans.factory.annotation.Autowired
4849
import org.springframework.beans.factory.annotation.Configurable
@@ -145,6 +146,7 @@ class Project(
145146
var slackConfigs: MutableList<SlackConfig> = mutableListOf()
146147

147148
@OneToMany(fetch = FetchType.LAZY, cascade = [CascadeType.PERSIST], mappedBy = "project")
149+
@SQLRestriction("archived_at IS NULL")
148150
var branches: MutableList<Branch> = mutableListOf()
149151

150152
@ColumnDefault("true")

backend/data/src/main/kotlin/io/tolgee/repository/KeyRepository.kt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ interface KeyRepository : JpaRepository<Key, Long> {
4040
k.name = :name
4141
and k.project.id = :projectId
4242
and (ns.name = :namespace or (ns is null and :namespace is null))
43-
and (br.name = :branch or (:branch is null and (br is null or br.isDefault)))
43+
and ((br.name = :branch and br.archivedAt is null) or (:branch is null and (br is null or br.isDefault)))
4444
""",
4545
)
4646
fun getByNameAndNamespace(
@@ -64,7 +64,8 @@ interface KeyRepository : JpaRepository<Key, Long> {
6464
left join k.keyMeta km
6565
left join k.namespace ns
6666
left join k.branch br
67-
where k.project.id = :projectId order by k.id
67+
where k.project.id = :projectId and (br is null or br.archivedAt is null)
68+
order by k.id
6869
""",
6970
)
7071
fun getAllByProjectIdSortedById(projectId: Long): List<KeyView>
@@ -158,13 +159,13 @@ interface KeyRepository : JpaRepository<Key, Long> {
158159
left join k.namespace ns
159160
left join k.branch b
160161
where k.project.id = :projectId
161-
and (k.branch.name = :branch or (:branch is null and (b is null or b.isDefault)))
162+
and ((b.name = :branch and b.archivedAt is null) or (:branch is null and (b is null or b.isDefault)))
162163
""",
163164
countQuery = """
164165
select count(k) from Key k
165166
left join k.branch b
166167
where k.project.id = :projectId
167-
and (k.branch.name = :branch or (:branch is null and (b is null or b.isDefault)))
168+
and ((b.name = :branch and b.archivedAt is null) or (:branch is null and (b is null or b.isDefault)))
168169
""",
169170
)
170171
fun getAllByProjectId(
@@ -290,14 +291,14 @@ interface KeyRepository : JpaRepository<Key, Long> {
290291
left join fetch k.translations t
291292
left join fetch k.keyMeta km
292293
left join fetch k.namespace ns
293-
left join fetch k.branch br
294+
left join fetch k.branch b
294295
left join fetch t.labels l
295296
left join fetch km.tags tg
296297
left join fetch t.comments c
297298
left join fetch c.author ca
298299
where k.project.id = :projectId
299300
and k.name = :keyName
300-
and (br.name = :branchName or (:branchName is null and (br is null or br.isDefault)))
301+
and ((b.name = :branchName and b.archivedAt is null) or (:branchName is null and (b is null or b.isDefault)))
301302
"""
302303
)
303304
fun findPrefetchedByNameAndBranch(projectId: Long, keyName: String, branchName: String?): Key?

backend/data/src/main/kotlin/io/tolgee/repository/TranslationRepository.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ interface TranslationRepository : JpaRepository<Translation, Long> {
2828
left join km.tags kmt
2929
join t.language l
3030
where t.key.project.id = :projectId
31-
and (b.name = :branch or (:branch is null and (b is null or b.isDefault)))
31+
and ((b.name = :branch and b.archivedAt is null) or (:branch is null and (b is null or b.isDefault)))
3232
and l.tag in :languages
3333
and ((n.name is null and :namespace is null) or n.name = :namespace)
3434
and (:filterTags is null or kmt.name in :filterTags)

backend/data/src/main/kotlin/io/tolgee/service/bigMeta/BigMetaService.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,10 @@ class BigMetaService(
236236
cb.equal(root.get(Key_.name), key.keyName),
237237
cb.equalNullable(namespace.get(Namespace_.name), key.namespace),
238238
cb.or(
239-
cb.equalNullable(branch.get(Branch_.name), key.branch),
239+
cb.and(
240+
cb.equalNullable(branch.get(Branch_.name), key.branch),
241+
cb.isNull(branch.get(Branch_.archivedAt)),
242+
),
240243
cb.or(
241244
cb.isNull(root.get(Key_.branch)),
242245
cb.isTrue(branch.get(Branch_.isDefault)),

backend/data/src/main/kotlin/io/tolgee/service/export/dataProvider/ExportDataProvider.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,12 @@ class ExportDataProvider(
157157

158158
private fun filterBranch() {
159159
if (exportParams.filterBranch != null) {
160-
whereConditions.add(cb.equal(branchJoin.get(Branch_.name), exportParams.filterBranch))
160+
whereConditions.add(
161+
cb.and(
162+
cb.equal(branchJoin.get(Branch_.name), exportParams.filterBranch),
163+
cb.isNull(branchJoin.get(Branch_.archivedAt)),
164+
)
165+
)
161166
} else {
162167
whereConditions.add(
163168
cb.or(

backend/data/src/main/kotlin/io/tolgee/service/queryBuilders/LanguageStatsProvider.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package io.tolgee.service.queryBuilders
33
import io.tolgee.model.Language_
44
import io.tolgee.model.Project
55
import io.tolgee.model.Project_
6+
import io.tolgee.model.branching.Branch_
67
import io.tolgee.model.enums.TranslationState
78
import io.tolgee.model.key.Key
89
import io.tolgee.model.key.Key_
@@ -107,7 +108,7 @@ open class LanguageStatsProvider(
107108
subquery.where(
108109
cb.or(
109110
cb.isNull(join.get(Key_.branch)),
110-
cb.isTrue(branchJoin.get(io.tolgee.model.branching.Branch_.isDefault)),
111+
cb.isTrue(branchJoin.get(Branch_.isDefault)),
111112
),
112113
)
113114
}

backend/data/src/main/kotlin/io/tolgee/service/queryBuilders/translationViewBuilder/QueryGlobalFiltering.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,12 @@ class QueryGlobalFiltering(
274274
)
275275
)
276276
} else {
277-
queryBase.whereConditions.add(cb.equal(branchJoin.get(Branch_.name), cb.literal(params.branch)))
277+
queryBase.whereConditions.add(
278+
cb.and(
279+
cb.equal(branchJoin.get(Branch_.name), cb.literal(params.branch)),
280+
cb.isNull(branchJoin.get(Branch_.archivedAt)),
281+
)
282+
)
278283
}
279284
}
280285
}

ee/backend/app/src/main/kotlin/io/tolgee/ee/service/branching/BranchCleanupService.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,6 @@ class BranchCleanupService(
6262
}
6363
page++
6464
}
65+
branchRepository.delete(branch)
6566
}
6667
}

0 commit comments

Comments
 (0)