Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/lib/generators/python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,10 +319,10 @@ export class PythonGenerator extends BaseGenerator {
const filename = `${timestamp}_${name}.py`;

const upStatements = upSql
.map((sql) => ` op.execute("""${sql}""")`)
.map((sql) => ` op.execute(${JSON.stringify(sql)})`)
.join("\n");
const downStatements = (downSql || [])
.map((sql) => ` op.execute("""${sql}""")`)
.map((sql) => ` op.execute(${JSON.stringify(sql)})`)
.join("\n");

const content = `"""${name}"""
Expand Down
47 changes: 47 additions & 0 deletions test/lib/generators/python.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { expect, describe, test, beforeAll } from "@jest/globals";
import { spawnSync } from "child_process";
import { PythonGenerator } from "../../../src/lib/generators/python";
import { GeneratorConfig, Entity, Relationship } from "../../../src/lib/types";

Expand Down Expand Up @@ -567,4 +568,50 @@ describe("PythonGenerator", () => {
expect(files).toEqual([]);
});
});

describe("migration generation (cli#148)", () => {
const hasPython = spawnSync("python3", ["--version"]).status === 0;

// Run the migration under a stub alembic.op that records each statement.
const runMigration = `
import json, sys, types
calls = []
alembic = types.ModuleType("alembic")
alembic.op = types.SimpleNamespace(execute=calls.append)
sys.modules["alembic"] = alembic
exec(sys.stdin.read())
upgrade()
downgrade()
print(json.dumps(calls))
`;

(hasPython ? test : test.skip)(
"writes a migration Python runs with the SQL unchanged",
async () => {
const upSql = [
// ends in a quoted identifier
'ALTER TABLE "task" DROP COLUMN "title"',
// backslash
String.raw`ALTER TABLE "task" ALTER COLUMN "path" SET DEFAULT 'C:\tmp'`,
// newlines
'CREATE TABLE "note" (\n "id" SERIAL NOT NULL\n)',
];
const downSql = ['DROP TABLE "user"'];
const files = await generator.generateMigration({
entities: [],
relationshipMap: {},
upSql,
downSql,
});

expect(files).toHaveLength(1);
const result = spawnSync("python3", ["-c", runMigration], {
input: files[0].content,
encoding: "utf8",
});
expect(result.stderr).toBe("");
expect(JSON.parse(result.stdout)).toEqual([...upSql, ...downSql]);
}
);
});
});