Skip to content

Commit

Permalink
fix: comment blueprint
Browse files Browse the repository at this point in the history
  • Loading branch information
LinboLen committed Jul 13, 2024
1 parent f2911e7 commit a97abd0
Show file tree
Hide file tree
Showing 11 changed files with 530 additions and 209 deletions.
3 changes: 2 additions & 1 deletion libs/fedaco/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
}
],
"@typescript-eslint/consistent-type-imports": ["warn", {}],
"@typescript-eslint/no-unsafe-declaration-merging": ["off"]
"@typescript-eslint/no-unsafe-declaration-merging": ["off"],
"@typescript-eslint/no-empty-function": ["off"]
}
},
{
Expand Down
4 changes: 4 additions & 0 deletions libs/fedaco/src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,10 @@ export class Connection extends mixinManagesTransactions(class {
return grammar;
}

public async getServerVersion(): Promise<string> {
return await (await this.getPdo()).execute('select version() as version')
}

causedByConcurrencyError(e: Error) {
console.warn(`catch an error check whether is concurrency error.
if it's raised in transaction you can report this error ${e.message} to make this function more better.`);
Expand Down
2 changes: 1 addition & 1 deletion libs/fedaco/src/connection/mysql-connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class MysqlConnection extends Connection {
// return (await this.getPdo()).getAttribute('ATTR_SERVER_VERSION').includes('MariaDB');
}

public async getServerVersion() {
public async getServerVersion(): Promise<string> {
await this.isMaria();
return this._version;
}
Expand Down
10 changes: 9 additions & 1 deletion libs/fedaco/src/schema/blueprint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class Blueprint {
public _after: string;

/*Create a new schema blueprint.*/
public constructor(table: string, callback: Function | null = null, prefix: string = '') {
public constructor(table: string, callback: (blueprint: Blueprint) => void | null = null, prefix: string = '') {
this.table = table;
this.prefix = prefix;
if (!isBlank(callback)) {
Expand Down Expand Up @@ -693,6 +693,14 @@ export class Blueprint {
return this.addCommand(type, {index, columns, algorithm});
}

/**
* Add a comment to the table.
*
*/
public comment(comment: string) {
return this.addCommand('tableComment', {comment});
}

/*Create a new drop index command on the blueprint.*/
protected dropIndexCommand(command: string, type: string, index: string | any[]) {
let columns = [];
Expand Down
150 changes: 0 additions & 150 deletions libs/fedaco/src/schema/grammar/change-column.ts

This file was deleted.

13 changes: 7 additions & 6 deletions libs/fedaco/src/schema/grammar/postgres-schema-grammar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import { isBlank, isString } from '@gradii/nanofn';
import type { Connection } from '../../connection';
import { raw } from '../../query-builder/ast-factory';
import type { Blueprint } from '../blueprint';
import type { ColumnDefinition } from '../column-definition';
import type { ForeignKeyDefinition } from '../foreign-key-definition';
Expand Down Expand Up @@ -442,7 +443,7 @@ export class PostgresSchemaGrammar extends SchemaGrammar {
return `comment on table ${
this.wrapTable(blueprint)
} is ${
`'${command.comment(/'/g, '\'\'',)}'`
`'${command.comment.replace(/'+/g, '\'\'',)}'`
}`;
}

Expand Down Expand Up @@ -604,11 +605,11 @@ export class PostgresSchemaGrammar extends SchemaGrammar {

/*Create the column definition for a timestamp type.*/
protected typeTimestamp(column: ColumnDefinition) {
const columnType = `timestamp${
isBlank(column.precision) ?
'' : `(${column.precision})`
} without time zone`;
return column.useCurrent ? '"ColumnType default CURRENT_TIMESTAMP" ' : columnType;
if (column.useCurrent) {
column.withDefault(raw('CURRENT_TIMESTAMP'));
}

return 'timestamp' + (isBlank(column.precision) ? '' : `(${column.precision})`) + ' without time zone';
}

/*Create the column definition for a timestamp (with time zone) type.*/
Expand Down
47 changes: 0 additions & 47 deletions libs/fedaco/src/schema/grammar/rename-column.ts

This file was deleted.

4 changes: 2 additions & 2 deletions libs/fedaco/src/schema/grammar/schema-grammar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ export class SchemaGrammar extends BaseGrammar {
public compileRenameColumn(blueprint: Blueprint, command: ColumnDefinition,
connection: Connection): Promise<string> | string {
return `alter table ${
this.wrapTable(blueprint)}
rename column ${
this.wrapTable(blueprint)
} rename column ${
this.wrap(command.from)} to ${this.wrap(command.to)}`;
}

Expand Down
2 changes: 1 addition & 1 deletion libs/fedaco/src/schema/grammar/sqlite-schema-grammar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ export class SqliteSchemaGrammar extends SchemaGrammar {
connection: Connection) {
const table = this.wrapTable(blueprint);
const columns = this.prefixArray('drop column', this.wrapArray(command.columns));
return columns.map(it => `alter table ${table}${it}`);
return columns.map(it => `alter table ${table} ${it}`);
}

/*Compile a drop unique key command.*/
Expand Down
22 changes: 22 additions & 0 deletions libs/fedaco/test/database-abstract-schema-grammar.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { SchemaGrammar } from '../src/schema/grammar/schema-grammar';

describe("test database abstract schema grammar", () => {

it("create database", () => {
const grammar = new class extends SchemaGrammar {
}();

expect(()=>{
// @ts-ignore
grammar.compileCreateDatabase("foo", {});
}).toThrowError('LogicException This database driver does not support creating databases.')
});
it("drop database if exists", () => {
const grammar = new class extends SchemaGrammar {
}();
expect(()=>{
// @ts-ignore
grammar.compileDropDatabaseIfExists("foo");
}).toThrowError('LogicException This database driver does not support dropping databases.')
});
});
Loading

0 comments on commit a97abd0

Please sign in to comment.