Skip to content

Commit

Permalink
fix: sqlite test
Browse files Browse the repository at this point in the history
  • Loading branch information
tea-artist committed Mar 20, 2024
1 parent 4cae37f commit 2936312
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 40 deletions.
11 changes: 1 addition & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,16 +248,7 @@ Giving non-techy people the ability to create their software sounds exciting. Bu

In essence, Teable isn't just another no-code solution, it's a comprehensive answer to the evolving demands of modern software development, ensuring that everyone, regardless of their technical proficiency, has a platform tailored to their needs.

## 🌟 Star distribution

<a href="https://next.ossinsight.io/widgets/official/analyze-repo-stars-map?repo_id=560299175&activity=stars" target="_blank" style="display: block" align="center">
<picture>
<source media="(prefers-color-scheme: dark)" srcset="https://next.ossinsight.io/widgets/official/analyze-repo-stars-map/thumbnail.png?repo_id=560299175&activity=stars&image_size=auto&color_scheme=dark" width="721" height="auto">
<img alt="Star Geographical Distribution of teableio/teable" src="https://next.ossinsight.io/widgets/official/analyze-repo-stars-map/thumbnail.png?repo_id=560299175&activity=stars&image_size=auto&color_scheme=light" width="721" height="auto">
</picture>
</a>

## :heart: Sponsors
## Sponsors :heart:

If you are enjoying some this project in your company, I'd really appreciate a [sponsorship](https://github.com/sponsors/teableio), a [coffee](https://ko-fi.com/teable) or a dropped star.
That gives me some more time to improve it to the next level.
Expand Down
9 changes: 7 additions & 2 deletions apps/nestjs-backend/src/db-provider/db.provider.interface.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { DriverClient, IAggregationField, IFilter, ISortItem } from '@teable/core';
import type { Prisma } from '@teable/db-main-prisma';
import type { Knex } from 'knex';
import type { IFieldInstance } from '../features/field/model/factory';
import type { SchemaType } from '../features/field/util';
Expand Down Expand Up @@ -29,15 +30,19 @@ export interface IDbProvider {

dropTable(tableName: string): string;

checkColumnExist(tableName: string, columnName: string): string;

renameColumn(tableName: string, oldName: string, newName: string): string[];

dropColumn(tableName: string, columnName: string): string[];

// sql response format: { name: string }[], name for columnName.
columnInfo(tableName: string): string;

checkColumnExist(
tableName: string,
columnName: string,
prisma: Prisma.TransactionClient
): Promise<boolean>;

dropColumnAndIndex(tableName: string, columnName: string, indexName: string): string[];

modifyColumnSchema(tableName: string, columnName: string, schemaType: SchemaType): string[];
Expand Down
20 changes: 13 additions & 7 deletions apps/nestjs-backend/src/db-provider/postgres.provider.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Logger } from '@nestjs/common';
import type { IAggregationField, IFilter, ISortItem } from '@teable/core';
import { DriverClient } from '@teable/core';
import type { PrismaClient } from '@teable/db-main-prisma';
import type { Knex } from 'knex';
import type { IFieldInstance } from '../features/field/model/factory';
import type { SchemaType } from '../features/field/util';
Expand Down Expand Up @@ -45,14 +46,19 @@ export class PostgresProvider implements IDbProvider {
return this.knex.raw('DROP TABLE ??', [tableName]).toQuery();
}

checkColumnExist(tableName: string, columnName: string): string {
const [schemaName, dbTableName] = this.splitTableName(tableName);
return this.knex
.raw(
'SELECT EXISTS (SELECT FROM information_schema.columns WHERE table_schema = ? AND table_name = ? AND column_name = ?) AS exists',
[schemaName, dbTableName, columnName]
)
async checkColumnExist(
tableName: string,
columnName: string,
prisma: PrismaClient
): Promise<boolean> {
const sql = this.knex
.raw('SELECT EXISTS (SELECT 1 FROM PRAGMA table_info(??) WHERE name = ?) AS exists', [
tableName,
columnName,
])
.toQuery();
const res = await prisma.$queryRawUnsafe<{ exists: boolean }[]>(sql);
return res[0].exists;
}

renameColumn(tableName: string, oldName: string, newName: string): string[] {
Expand Down
16 changes: 9 additions & 7 deletions apps/nestjs-backend/src/db-provider/sqlite.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { Logger } from '@nestjs/common';
import type { IAggregationField, IFilter, ISortItem } from '@teable/core';
import { DriverClient } from '@teable/core';
import type { PrismaClient } from '@teable/db-main-prisma';
import type { Knex } from 'knex';
import type { IFieldInstance } from '../features/field/model/factory';
import type { SchemaType } from '../features/field/util';
Expand Down Expand Up @@ -41,13 +42,14 @@ export class SqliteProvider implements IDbProvider {
return this.knex.raw('DROP TABLE ??', [tableName]).toQuery();
}

checkColumnExist(tableName: string, columnName: string): string {
return this.knex
.raw('SELECT EXISTS (SELECT 1 FROM pragma_table_info(??) WHERE name = ??) AS exists;', [
tableName,
columnName,
])
.toQuery();
async checkColumnExist(
tableName: string,
columnName: string,
prisma: PrismaClient
): Promise<boolean> {
const sql = this.columnInfo(tableName);
const columns = await prisma.$queryRawUnsafe<{ name: string }[]>(sql);
return columns.some((column) => column.name === columnName);
}

renameColumn(tableName: string, oldName: string, newName: string): string[] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ export class RecordOpenApiService {
) {
const dbTableName = await this.recordService.getDbTableName(tableId);

console.log('orderRo', orderRo);
const indexField = await this.viewService.getOrCreateViewIndexField(
dbTableName,
orderRo.viewId
Expand Down
9 changes: 5 additions & 4 deletions apps/nestjs-backend/src/features/record/record.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,10 +334,11 @@ export class RecordService implements IAdapterService {

async getBasicOrderIndexField(dbTableName: string, viewId: string | undefined) {
const columnName = `${ROW_ORDER_FIELD_PREFIX}_${viewId}`;
const exitSql = this.dbProvider.checkColumnExist(dbTableName, columnName);
const exists = await this.prismaService
.$queryRawUnsafe<{ exists: boolean }[]>(exitSql)
.then((res) => res[0].exists);
const exists = await this.dbProvider.checkColumnExist(
dbTableName,
columnName,
this.prismaService.txClient()
);

if (exists) {
return columnName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ export class ViewOpenApiService {
newValue: new Date().toISOString(),
});

await this.prismaService.$tx(async () => {
await this.prismaService.$tx(async (prisma) => {
await this.viewService.updateViewByOps(tableId, viewId, [ops]);
for (let i = 0; i < recordIds.length; i++) {
const recordId = recordIds[i];
Expand All @@ -450,7 +450,7 @@ export class ViewOpenApiService {
})
.where('__id', recordId)
.toQuery();
await this.prismaService.$executeRawUnsafe(updateRecordSql);
await prisma.$executeRawUnsafe(updateRecordSql);
}
});
},
Expand Down
9 changes: 5 additions & 4 deletions apps/nestjs-backend/src/features/view/view.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,11 @@ export class ViewService implements IReadonlyAdapterService {

async existIndex(dbTableName: string, viewId: string) {
const columnName = this.getRowIndexFieldName(viewId);
const exitSql = this.dbProvider.checkColumnExist(dbTableName, columnName);
const exists = await this.prismaService
.$queryRawUnsafe<{ exists: boolean }[]>(exitSql)
.then((res) => res[0].exists);
const exists = await this.dbProvider.checkColumnExist(
dbTableName,
columnName,
this.prismaService.txClient()
);

if (exists) {
return columnName;
Expand Down
2 changes: 1 addition & 1 deletion apps/nestjs-backend/test/order-update.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe('order update', () => {
await deleteTable(baseId, table.id);
});

it('should update view order', async () => {
it('should update record order', async () => {
const viewId = table.views[0].id;
const record1 = { id: table.records[0].id };
const record2 = { id: table.records[1].id };
Expand Down
9 changes: 7 additions & 2 deletions packages/openapi/src/view/update-record-order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ import { registerRoute, urlBuilder } from '../utils';
import { z } from '../zod';

export const updateRecordOrdersRoSchema = z.object({
anchorId: z.string(),
anchorId: z.string().openapi({
description: 'Id of the record that you want to move other records around',
}),
position: z.enum(['before', 'after']),
recordIds: z.string().array(),
recordIds: z.string().array().max(1000).openapi({
description: 'Ids of those records you want to move',
maxLength: 1000,
}),
});

export type IUpdateRecordOrdersRo = z.infer<typeof updateRecordOrdersRoSchema>;
Expand Down

0 comments on commit 2936312

Please sign in to comment.