New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Beautify generated SQL for migrations #6685
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { PrimaryGeneratedColumn, Entity, Column, CreateDateColumn } from "../../../../src"; | ||
|
||
@Entity() | ||
export class Post { | ||
|
||
@PrimaryGeneratedColumn() | ||
id?: number; | ||
|
||
@Column() | ||
title: string; | ||
|
||
@CreateDateColumn() | ||
readonly createdAt?: Date; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Column } from "../../../../src/decorator/columns/Column"; | ||
import { PrimaryColumn } from "../../../../src/decorator/columns/PrimaryColumn"; | ||
import { Entity } from "../../../../src/decorator/entity/Entity"; | ||
|
||
@Entity() | ||
export class Username { | ||
@PrimaryColumn() | ||
username: string; | ||
|
||
@Column() | ||
email: string; | ||
|
||
@Column() | ||
something: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import sinon from "sinon"; | ||
import { ConnectionOptions, ConnectionOptionsReader, DatabaseType } from "../../../src"; | ||
import { setupTestingConnections, createTestingConnections, closeTestingConnections, reloadTestingDatabases } from "../../utils/test-utils"; | ||
import { Username } from "./entity/Username"; | ||
import { CommandUtils } from "../../../src/commands/CommandUtils"; | ||
import { MigrationGenerateCommand } from "../../../src/commands/MigrationGenerateCommand"; | ||
import { Post } from "./entity/Post"; | ||
import { resultsTemplates } from "./results-templates"; | ||
|
||
describe("github issues > #4415 allow beautify generated migrations", () => { | ||
let connectionOptions: ConnectionOptions[]; | ||
let createFileStub: sinon.SinonStub; | ||
let getConnectionOptionsStub: sinon.SinonStub; | ||
let migrationGenerateCommand: MigrationGenerateCommand; | ||
let connectionOptionsReader: ConnectionOptionsReader; | ||
let baseConnectionOptions: ConnectionOptions; | ||
|
||
const enabledDrivers = [ | ||
"postgres", | ||
"mssql", | ||
"mysql", | ||
"mariadb", | ||
"sqlite", | ||
"better-sqlite3", | ||
"oracle", | ||
"cockroachdb" | ||
] as DatabaseType[]; | ||
|
||
// simulate args: `npm run typeorm migration:run -- -n test-migration -d test-directory` | ||
const testHandlerArgs = (options: Record<string, any>) => ({ | ||
"$0": "test", | ||
"_": ["test"], | ||
"name": "test-migration", | ||
"dir": "test-directory", | ||
...options | ||
}); | ||
|
||
before(async () => { | ||
// clean out db from any prior tests in case previous state impacts the generated migrations | ||
const connections = await createTestingConnections({ | ||
entities: [], | ||
enabledDrivers | ||
}); | ||
await reloadTestingDatabases(connections); | ||
await closeTestingConnections(connections); | ||
|
||
connectionOptions = await setupTestingConnections({ | ||
entities: [Username, Post], | ||
enabledDrivers | ||
}); | ||
connectionOptionsReader = new ConnectionOptionsReader(); | ||
migrationGenerateCommand = new MigrationGenerateCommand(); | ||
createFileStub = sinon.stub(CommandUtils, "createFile"); | ||
}); | ||
after(() => createFileStub.restore()); | ||
|
||
it("writes regular migration file when no option is passed", async () => { | ||
for (const connectionOption of connectionOptions) { | ||
createFileStub.resetHistory(); | ||
baseConnectionOptions = await connectionOptionsReader.get(connectionOption.name as string); | ||
getConnectionOptionsStub = sinon.stub(ConnectionOptionsReader.prototype, "get").resolves({ | ||
...baseConnectionOptions, | ||
entities: [Username, Post] | ||
}); | ||
|
||
await migrationGenerateCommand.handler(testHandlerArgs({ | ||
"connection": connectionOption.name | ||
})); | ||
|
||
// compare against control test strings in results-templates.ts | ||
for (const control of resultsTemplates[connectionOption.type as string].control) { | ||
sinon.assert.calledWith( | ||
createFileStub, | ||
sinon.match(/test-directory.*test-migration.ts/), | ||
sinon.match(control) | ||
); | ||
} | ||
|
||
getConnectionOptionsStub.restore(); | ||
} | ||
}); | ||
|
||
it("writes pretty printed file when pretty option is passed", async () => { | ||
for (const connectionOption of connectionOptions) { | ||
createFileStub.resetHistory(); | ||
baseConnectionOptions = await connectionOptionsReader.get(connectionOption.name as string); | ||
getConnectionOptionsStub = sinon.stub(ConnectionOptionsReader.prototype, "get").resolves({ | ||
...baseConnectionOptions, | ||
entities: [Username, Post] | ||
}); | ||
|
||
await migrationGenerateCommand.handler(testHandlerArgs({ | ||
"connection": connectionOption.name, | ||
"pretty": true | ||
})); | ||
|
||
// compare against "pretty" test strings in results-templates.ts | ||
for (const pretty of resultsTemplates[connectionOption.type as string].pretty) { | ||
sinon.assert.calledWith( | ||
createFileStub, | ||
sinon.match(/test-directory.*test-migration.ts/), | ||
sinon.match(pretty) | ||
); | ||
} | ||
getConnectionOptionsStub.restore(); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
export const resultsTemplates: Record<string, any> = { | ||
|
||
postgres: { | ||
control: [ | ||
`CREATE TABLE "post" ("id" SERIAL NOT NULL, "title" character varying NOT NULL, "createdAt" TIMESTAMP NOT NULL DEFAULT now(), CONSTRAINT "PK_be5fda3aac270b134ff9c21cdee" PRIMARY KEY ("id"))`, | ||
`CREATE TABLE "username" ("username" character varying NOT NULL, "email" character varying NOT NULL, "something" character varying NOT NULL, CONSTRAINT "PK_b39ad32e514b17e90c93988888a" PRIMARY KEY ("username"))` | ||
], | ||
pretty: [ | ||
` | ||
CREATE TABLE "post" ( | ||
"id" SERIAL NOT NULL, | ||
"title" character varying NOT NULL, | ||
"createdAt" TIMESTAMP NOT NULL DEFAULT now(), | ||
CONSTRAINT "PK_be5fda3aac270b134ff9c21cdee" PRIMARY KEY ("id") | ||
) | ||
`, | ||
` | ||
CREATE TABLE "username" ( | ||
"username" character varying NOT NULL, | ||
"email" character varying NOT NULL, | ||
"something" character varying NOT NULL, | ||
CONSTRAINT "PK_b39ad32e514b17e90c93988888a" PRIMARY KEY ("username") | ||
) | ||
` | ||
] | ||
}, | ||
|
||
mssql: { | ||
control: [ | ||
`CREATE TABLE "post" ("id" int NOT NULL IDENTITY(1,1), "title" nvarchar(255) NOT NULL, "createdAt" datetime2 NOT NULL CONSTRAINT "DF_fb91bea2d37140a877b775e6b2a" DEFAULT getdate(), CONSTRAINT "PK_be5fda3aac270b134ff9c21cdee" PRIMARY KEY ("id"))`, | ||
`CREATE TABLE "username" ("username" nvarchar(255) NOT NULL, "email" nvarchar(255) NOT NULL, "something" nvarchar(255) NOT NULL, CONSTRAINT "PK_b39ad32e514b17e90c93988888a" PRIMARY KEY ("username"))` | ||
], | ||
pretty: [ | ||
` | ||
CREATE TABLE "post" ( | ||
"id" int NOT NULL IDENTITY(1, 1), | ||
"title" nvarchar(255) NOT NULL, | ||
"createdAt" datetime2 NOT NULL CONSTRAINT "DF_fb91bea2d37140a877b775e6b2a" DEFAULT getdate(), | ||
CONSTRAINT "PK_be5fda3aac270b134ff9c21cdee" PRIMARY KEY ("id") | ||
) | ||
`, | ||
` | ||
CREATE TABLE "username" ( | ||
"username" nvarchar(255) NOT NULL, | ||
"email" nvarchar(255) NOT NULL, | ||
"something" nvarchar(255) NOT NULL, | ||
CONSTRAINT "PK_b39ad32e514b17e90c93988888a" PRIMARY KEY ("username") | ||
) | ||
` | ||
] | ||
}, | ||
|
||
sqlite: { | ||
control: [ | ||
`CREATE TABLE "post" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar NOT NULL, "createdAt" datetime NOT NULL DEFAULT (datetime('now')))`, | ||
`CREATE TABLE "username" ("username" varchar PRIMARY KEY NOT NULL, "email" varchar NOT NULL, "something" varchar NOT NULL)`, | ||
], | ||
pretty: [ | ||
` | ||
CREATE TABLE "post" ( | ||
"id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, | ||
"title" varchar NOT NULL, | ||
"createdAt" datetime NOT NULL DEFAULT (datetime('now')) | ||
) | ||
`, | ||
` | ||
CREATE TABLE "username" ( | ||
"username" varchar PRIMARY KEY NOT NULL, | ||
"email" varchar NOT NULL, | ||
"something" varchar NOT NULL | ||
) | ||
` | ||
] | ||
}, | ||
|
||
mysql: { | ||
control: [ | ||
`CREATE TABLE \`post\` (\`id\` int NOT NULL AUTO_INCREMENT, \`title\` varchar(255) NOT NULL, \`createdAt\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), PRIMARY KEY (\`id\`)) ENGINE=InnoDB`, | ||
`CREATE TABLE \`username\` (\`username\` varchar(255) NOT NULL, \`email\` varchar(255) NOT NULL, \`something\` varchar(255) NOT NULL, PRIMARY KEY (\`username\`)) ENGINE=InnoDB` | ||
], | ||
pretty: [ | ||
` | ||
CREATE TABLE \`post\` ( | ||
\`id\` int NOT NULL AUTO_INCREMENT, | ||
\`title\` varchar(255) NOT NULL, | ||
\`createdAt\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), | ||
PRIMARY KEY (\`id\`) | ||
) ENGINE = InnoDB | ||
`, | ||
` | ||
CREATE TABLE \`username\` ( | ||
\`username\` varchar(255) NOT NULL, | ||
\`email\` varchar(255) NOT NULL, | ||
\`something\` varchar(255) NOT NULL, | ||
PRIMARY KEY (\`username\`) | ||
) ENGINE = InnoDB | ||
` | ||
] | ||
}, | ||
|
||
oracle: { | ||
control: [ | ||
`CREATE TABLE "post" ("id" number GENERATED BY DEFAULT AS IDENTITY, "title" varchar2(255) NOT NULL, "createdAt" timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL, CONSTRAINT "PK_be5fda3aac270b134ff9c21cdee" PRIMARY KEY ("id"))`, | ||
`CREATE TABLE "username" ("username" varchar2(255) NOT NULL, "email" varchar2(255) NOT NULL, "something" varchar2(255) NOT NULL, CONSTRAINT "PK_b39ad32e514b17e90c93988888a" PRIMARY KEY ("username"))` | ||
], | ||
pretty: [ | ||
` | ||
CREATE TABLE "post" ( | ||
"id" number GENERATED BY DEFAULT AS IDENTITY, | ||
"title" varchar2(255) NOT NULL, | ||
"createdAt" timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL, | ||
CONSTRAINT "PK_be5fda3aac270b134ff9c21cdee" PRIMARY KEY ("id") | ||
) | ||
`, | ||
` | ||
CREATE TABLE "username" ( | ||
"username" varchar2(255) NOT NULL, | ||
"email" varchar2(255) NOT NULL, | ||
"something" varchar2(255) NOT NULL, | ||
CONSTRAINT "PK_b39ad32e514b17e90c93988888a" PRIMARY KEY ("username") | ||
) | ||
` | ||
] | ||
}, | ||
|
||
cockroachdb: { | ||
control: [ | ||
`CREATE SEQUENCE "post_id_seq"`, | ||
`CREATE TABLE "post" ("id" INT DEFAULT nextval('"post_id_seq"') NOT NULL, "title" varchar NOT NULL, "createdAt" timestamptz NOT NULL DEFAULT now(), CONSTRAINT "PK_be5fda3aac270b134ff9c21cdee" PRIMARY KEY ("id"))`, | ||
`CREATE TABLE "username" ("username" varchar NOT NULL, "email" varchar NOT NULL, "something" varchar NOT NULL, CONSTRAINT "PK_b39ad32e514b17e90c93988888a" PRIMARY KEY ("username"))` | ||
], | ||
pretty: [ | ||
` | ||
CREATE SEQUENCE "post_id_seq" | ||
`, | ||
` | ||
CREATE TABLE "post" ( | ||
"id" INT DEFAULT nextval('"post_id_seq"') NOT NULL, | ||
"title" varchar NOT NULL, | ||
"createdAt" timestamptz NOT NULL DEFAULT now(), | ||
CONSTRAINT "PK_be5fda3aac270b134ff9c21cdee" PRIMARY KEY ("id") | ||
) | ||
`, | ||
` | ||
CREATE TABLE "username" ( | ||
"username" varchar NOT NULL, | ||
"email" varchar NOT NULL, | ||
"something" varchar NOT NULL, | ||
CONSTRAINT "PK_b39ad32e514b17e90c93988888a" PRIMARY KEY ("username") | ||
) | ||
` | ||
] | ||
}, | ||
|
||
get mariadb() { return this.mysql; }, | ||
get "better-sqlite3"() { return this.sqlite; }, | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it a safe import? Don't they have it exported on
@sqltools/formatter
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately the TS compiler's unhappy with that (
Could not find a declaration file for module '@sqltools/formatter'
).. The declaration file is in there but I think it's unable to pick it up from the top level folder.Require statement works ok, if you prefer that?
const { format } = require("@sqltools/formatter");
I'm a bit newb-ish on how the packaging works though - here's the contents of
node_modules/@sqltools/formatter
if you can spot anything in that structure:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
okay, lets merge it as it is... but I bet if there is something wrong with typing on
@sqltools/formatter
they will release a new version with change and it can break our stuff... so I would probably fix this package version for now.