Skip to content

Commit

Permalink
fix: fix dropping table
Browse files Browse the repository at this point in the history
  • Loading branch information
nichenqin committed Oct 21, 2024
1 parent e9da736 commit f2bf79b
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@
hasFilter && "bg-orange-50",
)}
use:props.resize
data-field-id={cell.id}
>
{#if cell.id === "$select" && !$hasRecord}
<Checkbox checked={false} disabled />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export class DashboardOnTableDeletedEventHandle implements IEventHandler<TableDe

const spec = new DashboardTableIdSpecification(tableId)
const dashboards = await this.dashboardRepository.find(spec)
console.log(dashboards)

for (const dashboard of dashboards) {
const spec = dashboard.$onTableDeleted(tableId)
Expand Down
2 changes: 1 addition & 1 deletion packages/persistence/src/table/table.filter-visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ export class TableFilterVisitor extends AbstractQBVisitor<TableDo> implements IT
const subQuery = this.qb
.selectFrom("undb_reference_id_mapping")
.select(["foreign_table_id"])
.where((eb) => eb.eb("table_id", "=", spec.tableId.value))
.where((eb) => eb.eb("table_id", "=", spec.tableId.value).or(eb.eb("foreign_table_id", "=", spec.tableId.value)))
const cond = this.eb.eb("id", "in", subQuery)
this.addCond(cond)
}
Expand Down
51 changes: 50 additions & 1 deletion packages/persistence/src/table/table.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,56 @@ export class TableRepository implements ITableRepository {
.onConflict((ob) => ob.doNothing())
.execute()

for (const view of table.views.views) {
await trx
.insertInto("undb_table_id_mapping")
.values({
table_id: table.id.value,
subject_id: view.id.value,
})
.onConflict((ob) => ob.doNothing())
.execute()
}

for (const form of table.forms?.props ?? []) {
await trx
.insertInto("undb_table_id_mapping")
.values({
table_id: table.id.value,
subject_id: form.id,
})
.onConflict((ob) => ob.doNothing())
.execute()
}

for (const field of table.schema.fields) {
if (field.type === "rollup") {
const referenceField = field.getReferenceField(table)
const option = field.option.unwrap()
await trx
.insertInto("undb_rollup_id_mapping")
.values({
field_id: option.rollupFieldId,
table_id: referenceField.foreignTableId,
rollup_id: field.id.value,
rollup_table_id: table.id.value,
})
.onConflict((ob) => ob.doNothing())
.execute()
} else if (field.type === "reference" && field.symmetricFieldId) {
await trx
.insertInto("undb_reference_id_mapping")
.values({
field_id: field.id.value,
table_id: table.id.value,
symmetric_field_id: field.symmetricFieldId,
foreign_table_id: field.foreignTableId,
})
.onConflict((ob) => ob.doNothing())
.execute()
}
}

await this.underlyingTableService.create(table)
await this.outboxService.save(table)
}
Expand Down Expand Up @@ -200,7 +250,6 @@ export class TableRepository implements ITableRepository {
.execute()

await this.underlyingTableService.delete(table)
console.log("save", table.domainEvents)
await this.outboxService.save(table)
}
}
8 changes: 4 additions & 4 deletions packages/persistence/src/tables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ export const referenceIdMapping = sqliteTable(
tableId: text("table_id")
.notNull()
.references(() => tables.id),
rollupId: text("symmetric_field_id").notNull(),
rollupTableId: text("foreign_table_id")
symmetricFieldId: text("symmetric_field_id").notNull(),
foreignTableId: text("foreign_table_id")
.notNull()
.references(() => tables.id),
},
Expand All @@ -110,8 +110,8 @@ export const referenceIdMapping = sqliteTable(
uniqueIndex: unique("reference_id_mapping_unique_idx").on(
table.fieldId,
table.tableId,
table.rollupId,
table.rollupTableId,
table.symmetricFieldId,
table.foreignTableId,
),
}
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { injectContext,type IContext } from "@undb/context"
import { injectContext, type IContext } from "@undb/context"
import { singleton } from "@undb/di"
import { createLogger } from "@undb/logger"
import type { TableComositeSpecification,TableDo } from "@undb/table"
import type { TableComositeSpecification, TableDo } from "@undb/table"
import type { CompiledQuery } from "kysely"
import { getAnonymousTransaction,getCurrentTransaction } from "../ctx"
import { getAnonymousTransaction, getCurrentTransaction } from "../ctx"
import { JoinTable } from "./reference/join-table"
import { UnderlyingTable } from "./underlying-table"
import { UnderlyingTableFieldVisitor } from "./underlying-table-field.visitor"
Expand Down Expand Up @@ -53,7 +53,7 @@ export class UnderlyingTableService {
const referenceFields = table.schema.getReferenceFields()
for (const field of referenceFields) {
const joinTable = new JoinTable(table, field)
await trx.schema.dropTable(joinTable.getTableName()).execute()
await trx.schema.dropTable(joinTable.getTableName()).ifExists().execute()
}
}

Expand Down

0 comments on commit f2bf79b

Please sign in to comment.