@cedarjs/codemods 3.0.0-canary.13619 → 3.0.0-canary.13624
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -24,6 +24,7 @@ async function getPrismaV7Context() {
|
|
|
24
24
|
const schemaPath = await getSchemaPath(paths.api.prismaConfig);
|
|
25
25
|
const provider = detectProvider(schemaPath);
|
|
26
26
|
const isSqlite = provider === "sqlite";
|
|
27
|
+
const isPostgres = provider === "postgresql" || provider === "postgres";
|
|
27
28
|
const dbPathTs = path.join(paths.api.lib, "db.ts");
|
|
28
29
|
const dbPathJs = path.join(paths.api.lib, "db.js");
|
|
29
30
|
let dbFilePath = null;
|
|
@@ -32,7 +33,7 @@ async function getPrismaV7Context() {
|
|
|
32
33
|
} else if (fs.existsSync(dbPathJs)) {
|
|
33
34
|
dbFilePath = dbPathJs;
|
|
34
35
|
}
|
|
35
|
-
return { isSqlite, paths, dbFilePath };
|
|
36
|
+
return { provider, isSqlite, isPostgres, paths, dbFilePath };
|
|
36
37
|
}
|
|
37
38
|
async function prismaV7() {
|
|
38
39
|
const context = await getPrismaV7Context();
|
|
@@ -45,13 +46,17 @@ async function prismaV7() {
|
|
|
45
46
|
targetPaths: [context.dbFilePath],
|
|
46
47
|
parser: "ts",
|
|
47
48
|
options: {
|
|
48
|
-
isSqlite
|
|
49
|
+
isSqlite,
|
|
50
|
+
isPostgres: context.isPostgres,
|
|
51
|
+
silent: true
|
|
49
52
|
}
|
|
50
53
|
});
|
|
51
54
|
}
|
|
52
55
|
await rewriteRemainingImports();
|
|
53
|
-
if (isSqlite) {
|
|
54
|
-
await updateApiPackageJson(path.join(paths.api.base, "package.json")
|
|
56
|
+
if (isSqlite || context.isPostgres) {
|
|
57
|
+
await updateApiPackageJson(path.join(paths.api.base, "package.json"), {
|
|
58
|
+
provider: context.provider
|
|
59
|
+
});
|
|
55
60
|
}
|
|
56
61
|
await updateTsConfigs({
|
|
57
62
|
apiTsConfig: path.join(paths.api.base, "tsconfig.json"),
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import path from "node:path";
|
|
2
|
-
import
|
|
2
|
+
import { styleText } from "node:util";
|
|
3
3
|
import runTransform from "../../../lib/runTransform.js";
|
|
4
4
|
import { getPrismaV7Context } from "./prismaV7.js";
|
|
5
5
|
import rewriteRemainingImports from "./rewriteRemainingImports.js";
|
|
@@ -10,152 +10,144 @@ import { updatePrismaConfig } from "./updatePrismaConfig.js";
|
|
|
10
10
|
import runUpdateSchemaFile from "./updateSchemaFile.js";
|
|
11
11
|
import { updateTsConfigs } from "./updateTsConfigs.js";
|
|
12
12
|
const command = "prisma-v7";
|
|
13
|
-
const description = "(v3.x) Upgrades your Cedar app to use Prisma v7 \
|
|
13
|
+
const description = "(v3.x) Upgrades your Cedar app to use Prisma v7 \u2013 updates schema.prisma, db.ts, prisma.config.cjs, and related config files";
|
|
14
|
+
function step(label, output) {
|
|
15
|
+
console.log(` ${styleText("green", "\u2714")} ${styleText("bold", label)}`);
|
|
16
|
+
console.log(` \u2192 ${output}`);
|
|
17
|
+
}
|
|
14
18
|
const handler = async () => {
|
|
15
19
|
const context = await getPrismaV7Context();
|
|
16
|
-
const { paths, isSqlite, dbFilePath } = context;
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
20
|
+
const { paths, isSqlite, isPostgres, provider, dbFilePath } = context;
|
|
21
|
+
console.log(styleText("bold", "\u276F Prisma v7 Migration"));
|
|
22
|
+
const { results: schemaResults } = await runUpdateSchemaFile();
|
|
23
|
+
if (schemaResults.length === 0) {
|
|
24
|
+
step("Update schema.prisma", "Skipped. No schema.prisma found");
|
|
25
|
+
} else {
|
|
26
|
+
for (const result of schemaResults) {
|
|
27
|
+
if (result.status === "skipped") {
|
|
28
|
+
step("Update schema.prisma", `Skipped. ${result.path} not found`);
|
|
29
|
+
} else if (result.status === "unmodified") {
|
|
30
|
+
step("Update schema.prisma", "No changes needed (already migrated)");
|
|
31
|
+
} else {
|
|
32
|
+
step("Update schema.prisma", `Updated ${result.path}`);
|
|
33
|
+
}
|
|
34
|
+
for (const warning of result.warnings) {
|
|
35
|
+
console.warn(`
|
|
32
36
|
\u26A0\uFE0F ${warning}`);
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
} else if (result === "unmodified") {
|
|
44
|
-
setOutput("No changes needed (already has datasource block)");
|
|
45
|
-
} else {
|
|
46
|
-
setOutput(`Updated ${paths.api.prismaConfig}`);
|
|
47
|
-
}
|
|
48
|
-
}),
|
|
49
|
-
task3("Update api/src/lib/db.{ts,js}", async ({ setOutput }) => {
|
|
50
|
-
if (!dbFilePath) {
|
|
51
|
-
setOutput(
|
|
52
|
-
"Skipped \u2014 no api/src/lib/db.ts or api/src/lib/db.js found"
|
|
53
|
-
);
|
|
54
|
-
return;
|
|
55
|
-
}
|
|
56
|
-
await runTransform({
|
|
57
|
-
transformPath: path.join(import.meta.dirname, "updateDbFile.js"),
|
|
58
|
-
targetPaths: [dbFilePath],
|
|
59
|
-
parser: "ts",
|
|
60
|
-
options: {
|
|
61
|
-
isSqlite
|
|
62
|
-
}
|
|
63
|
-
});
|
|
64
|
-
setOutput(`Updated ${dbFilePath}`);
|
|
65
|
-
if (!isSqlite) {
|
|
66
|
-
console.log(
|
|
67
|
-
"\n\u2139\uFE0F Non-SQLite database detected. The import paths in db.ts have\n been updated, but no driver adapter was added. If you want to\n use a Prisma driver adapter (recommended), add one manually.\n See: https://www.prisma.io/docs/orm/overview/databases/database-drivers"
|
|
68
|
-
);
|
|
69
|
-
}
|
|
70
|
-
})
|
|
71
|
-
],
|
|
72
|
-
{ concurrency: 1 }
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
const prismaConfigResult = await updatePrismaConfig(paths.api.prismaConfig);
|
|
41
|
+
if (prismaConfigResult === "skipped") {
|
|
42
|
+
step("Update prisma.config.cjs", "Skipped. prisma.config.cjs not found");
|
|
43
|
+
} else if (prismaConfigResult === "unmodified") {
|
|
44
|
+
step(
|
|
45
|
+
"Update prisma.config.cjs",
|
|
46
|
+
"No changes needed (already has datasource block)"
|
|
73
47
|
);
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
}
|
|
82
|
-
),
|
|
83
|
-
task3("Update api/package.json", async ({ setOutput }) => {
|
|
84
|
-
if (!isSqlite) {
|
|
85
|
-
setOutput(
|
|
86
|
-
"Skipped \u2014 non-SQLite project. Add your own driver adapter package."
|
|
87
|
-
);
|
|
88
|
-
return;
|
|
89
|
-
}
|
|
90
|
-
const pkgPath = path.join(paths.api.base, "package.json");
|
|
91
|
-
const result = await updateApiPackageJson(pkgPath);
|
|
92
|
-
if (result === "skipped") {
|
|
93
|
-
setOutput("Skipped \u2014 api/package.json not found");
|
|
94
|
-
} else if (result === "unmodified") {
|
|
95
|
-
setOutput("No changes needed (adapter already installed)");
|
|
96
|
-
} else {
|
|
97
|
-
setOutput(`Updated ${pkgPath}`);
|
|
98
|
-
}
|
|
99
|
-
}),
|
|
100
|
-
task3("Update tsconfig.json files", async ({ setOutput }) => {
|
|
101
|
-
const results = await updateTsConfigs({
|
|
102
|
-
apiTsConfig: path.join(paths.api.base, "tsconfig.json"),
|
|
103
|
-
scriptsTsConfig: path.join(paths.base, "scripts", "tsconfig.json"),
|
|
104
|
-
webTsConfig: path.join(paths.web.base, "tsconfig.json")
|
|
105
|
-
});
|
|
106
|
-
const updated = Object.entries(results).filter(([, status]) => status === "updated").map(([name]) => name);
|
|
107
|
-
if (updated.length === 0) {
|
|
108
|
-
setOutput("No changes needed");
|
|
109
|
-
} else {
|
|
110
|
-
setOutput(`Updated: ${updated.join(", ")}`);
|
|
111
|
-
}
|
|
112
|
-
}),
|
|
113
|
-
task3("Update .gitignore", async ({ setOutput }) => {
|
|
114
|
-
const gitignorePath = path.join(paths.base, ".gitignore");
|
|
115
|
-
const result = await updateGitignore(gitignorePath);
|
|
116
|
-
if (result === "skipped") {
|
|
117
|
-
setOutput("Skipped \u2014 .gitignore not found");
|
|
118
|
-
} else if (result === "unmodified") {
|
|
119
|
-
setOutput("No changes needed");
|
|
120
|
-
} else {
|
|
121
|
-
setOutput(`Updated ${gitignorePath}`);
|
|
122
|
-
}
|
|
123
|
-
}),
|
|
124
|
-
task3("Update .env.defaults", async ({ setOutput }) => {
|
|
125
|
-
const envDefaultsPath = path.join(paths.base, ".env.defaults");
|
|
126
|
-
const result = await updateEnvDefaults(envDefaultsPath);
|
|
127
|
-
if (result === "skipped") {
|
|
128
|
-
setOutput("Skipped \u2014 .env.defaults not found");
|
|
129
|
-
} else if (result === "unmodified") {
|
|
130
|
-
setOutput("No changes needed");
|
|
131
|
-
} else {
|
|
132
|
-
setOutput(`Updated ${envDefaultsPath}`);
|
|
133
|
-
}
|
|
134
|
-
const dotEnvWarning = checkDotEnv(path.join(paths.base, ".env"));
|
|
135
|
-
if (dotEnvWarning) {
|
|
136
|
-
console.warn(`
|
|
137
|
-
\u26A0\uFE0F ${dotEnvWarning}`);
|
|
138
|
-
}
|
|
139
|
-
})
|
|
140
|
-
],
|
|
141
|
-
{ concurrency: Infinity }
|
|
48
|
+
} else {
|
|
49
|
+
step("Update prisma.config.cjs", `Updated ${paths.api.prismaConfig}`);
|
|
50
|
+
}
|
|
51
|
+
if (!dbFilePath) {
|
|
52
|
+
step(
|
|
53
|
+
"Update api/src/lib/db.{ts,js}",
|
|
54
|
+
"Skipped. No api/src/lib/db.ts or api/src/lib/db.js found"
|
|
142
55
|
);
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
" 5. Add a Prisma driver adapter for your database to api/src/lib/db.ts",
|
|
153
|
-
" See: https://www.prisma.io/docs/orm/overview/databases/database-drivers"
|
|
154
|
-
);
|
|
56
|
+
} else {
|
|
57
|
+
await runTransform({
|
|
58
|
+
transformPath: path.join(import.meta.dirname, "updateDbFile.js"),
|
|
59
|
+
targetPaths: [dbFilePath],
|
|
60
|
+
parser: "ts",
|
|
61
|
+
options: {
|
|
62
|
+
isSqlite,
|
|
63
|
+
isPostgres,
|
|
64
|
+
silent: true
|
|
155
65
|
}
|
|
156
|
-
setOutput("\n\n" + steps.join("\n"));
|
|
157
66
|
});
|
|
67
|
+
step("Update api/src/lib/db.{ts,js}", `Updated ${dbFilePath}`);
|
|
68
|
+
if (!isSqlite && !isPostgres) {
|
|
69
|
+
const installationUrl = "https://www.prisma.io/docs/orm/prisma-client/setup-and-configuration/introduction#installation";
|
|
70
|
+
console.log(
|
|
71
|
+
` \u2139\uFE0F Non-SQLite database detected. The import paths in db.ts have been updated,
|
|
72
|
+
but no driver adapter was added. You'll need to add one manually.
|
|
73
|
+
See: ${installationUrl}`
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
await rewriteRemainingImports();
|
|
78
|
+
step("Rewrite remaining @prisma/client imports", "Done");
|
|
79
|
+
if (!isSqlite && !isPostgres) {
|
|
80
|
+
step(
|
|
81
|
+
"Update api/package.json",
|
|
82
|
+
"Skipped. Unsupported provider. Add your own driver adapter package."
|
|
83
|
+
);
|
|
84
|
+
} else {
|
|
85
|
+
const pkgPath = path.join(paths.api.base, "package.json");
|
|
86
|
+
const pkgResult = await updateApiPackageJson(pkgPath, { provider });
|
|
87
|
+
if (pkgResult === "skipped") {
|
|
88
|
+
step("Update api/package.json", "Skipped. api/package.json not found");
|
|
89
|
+
} else if (pkgResult === "unmodified") {
|
|
90
|
+
step(
|
|
91
|
+
"Update api/package.json",
|
|
92
|
+
"No changes needed (adapter already installed)"
|
|
93
|
+
);
|
|
94
|
+
} else {
|
|
95
|
+
step("Update api/package.json", `Updated ${pkgPath}`);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
const tsConfigResults = await updateTsConfigs({
|
|
99
|
+
apiTsConfig: path.join(paths.api.base, "tsconfig.json"),
|
|
100
|
+
scriptsTsConfig: path.join(paths.base, "scripts", "tsconfig.json"),
|
|
101
|
+
webTsConfig: path.join(paths.web.base, "tsconfig.json")
|
|
158
102
|
});
|
|
103
|
+
const updatedTsConfigs = Object.entries(tsConfigResults).filter(([, status]) => status === "updated").map(([name]) => name);
|
|
104
|
+
if (updatedTsConfigs.length === 0) {
|
|
105
|
+
step("Update tsconfig.json files", "No changes needed");
|
|
106
|
+
} else {
|
|
107
|
+
step(
|
|
108
|
+
"Update tsconfig.json files",
|
|
109
|
+
`Updated: ${updatedTsConfigs.join(", ")}`
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
const gitignorePath = path.join(paths.base, ".gitignore");
|
|
113
|
+
const gitignoreResult = await updateGitignore(gitignorePath);
|
|
114
|
+
if (gitignoreResult === "skipped") {
|
|
115
|
+
step("Update .gitignore", "Skipped. .gitignore not found");
|
|
116
|
+
} else if (gitignoreResult === "unmodified") {
|
|
117
|
+
step("Update .gitignore", "No changes needed");
|
|
118
|
+
} else {
|
|
119
|
+
step("Update .gitignore", `Updated ${gitignorePath}`);
|
|
120
|
+
}
|
|
121
|
+
const envDefaultsPath = path.join(paths.base, ".env.defaults");
|
|
122
|
+
const envDefaultsResult = await updateEnvDefaults(envDefaultsPath);
|
|
123
|
+
if (envDefaultsResult === "skipped") {
|
|
124
|
+
step("Update .env.defaults", "Skipped. .env.defaults not found");
|
|
125
|
+
} else if (envDefaultsResult === "unmodified") {
|
|
126
|
+
step("Update .env.defaults", "No changes needed");
|
|
127
|
+
} else {
|
|
128
|
+
step("Update .env.defaults", `Updated ${envDefaultsPath}`);
|
|
129
|
+
}
|
|
130
|
+
const dotEnvWarning = checkDotEnv(path.join(paths.base, ".env"));
|
|
131
|
+
if (dotEnvWarning) {
|
|
132
|
+
console.warn(`
|
|
133
|
+
\u26A0\uFE0F ${dotEnvWarning}`);
|
|
134
|
+
}
|
|
135
|
+
const nextSteps = [];
|
|
136
|
+
if (!isSqlite && !isPostgres) {
|
|
137
|
+
nextSteps.push(
|
|
138
|
+
" 1. Add a Prisma driver adapter for your database to api/src/lib/db.ts",
|
|
139
|
+
" See: https://www.prisma.io/docs/orm/core-concepts/supported-databases/database-drivers"
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
const offset = !isSqlite && !isPostgres ? 1 : 0;
|
|
143
|
+
nextSteps.push(
|
|
144
|
+
` ${offset + 1}. Run \`yarn install\` to install new dependencies`,
|
|
145
|
+
` ${offset + 2}. Run \`yarn cedar prisma generate\` to generate the new Prisma client`,
|
|
146
|
+
` ${offset + 3}. Run \`yarn cedar prisma migrate dev\` to verify migrations work`,
|
|
147
|
+
` ${offset + 4}. Run \`yarn cedar lint --fix\` to fix import ordering issues etc`
|
|
148
|
+
);
|
|
149
|
+
console.log(styleText("bold", "\n Next steps:"));
|
|
150
|
+
console.log(nextSteps.join("\n"));
|
|
159
151
|
};
|
|
160
152
|
export {
|
|
161
153
|
command,
|
|
@@ -3,34 +3,54 @@ const ADAPTER_PACKAGE = "@prisma/adapter-better-sqlite3";
|
|
|
3
3
|
const SQLITE_PACKAGE = "better-sqlite3";
|
|
4
4
|
const ADAPTER_VERSION = "^7.0.0";
|
|
5
5
|
const SQLITE_VERSION = "^12.0.0";
|
|
6
|
-
|
|
6
|
+
const PG_ADAPTER_PACKAGE = "@prisma/adapter-pg";
|
|
7
|
+
const PG_PACKAGE = "pg";
|
|
8
|
+
const PG_ADAPTER_VERSION = "^7.0.0";
|
|
9
|
+
const PG_VERSION = "^8.0.0";
|
|
10
|
+
function transformApiPackageJson(source, {
|
|
11
|
+
provider = "sqlite",
|
|
12
|
+
adapterVersion = ADAPTER_VERSION,
|
|
13
|
+
sqliteVersion = SQLITE_VERSION,
|
|
14
|
+
pgAdapterVersion = PG_ADAPTER_VERSION,
|
|
15
|
+
pgVersion = PG_VERSION
|
|
16
|
+
} = {}) {
|
|
7
17
|
const pkg = JSON.parse(source);
|
|
8
|
-
|
|
9
|
-
|
|
18
|
+
const isPostgres = provider === "postgresql" || provider === "postgres";
|
|
19
|
+
if (isPostgres) {
|
|
20
|
+
if (pkg.dependencies?.[PG_ADAPTER_PACKAGE]) {
|
|
21
|
+
return source;
|
|
22
|
+
}
|
|
23
|
+
pkg.dependencies = {
|
|
24
|
+
...pkg.dependencies ?? {},
|
|
25
|
+
[PG_ADAPTER_PACKAGE]: pgAdapterVersion,
|
|
26
|
+
[PG_PACKAGE]: pgVersion
|
|
27
|
+
};
|
|
28
|
+
} else {
|
|
29
|
+
if (pkg.dependencies?.[ADAPTER_PACKAGE]) {
|
|
30
|
+
return source;
|
|
31
|
+
}
|
|
32
|
+
pkg.dependencies = {
|
|
33
|
+
...pkg.dependencies ?? {},
|
|
34
|
+
[ADAPTER_PACKAGE]: adapterVersion,
|
|
35
|
+
[SQLITE_PACKAGE]: sqliteVersion
|
|
36
|
+
};
|
|
10
37
|
}
|
|
11
|
-
pkg.dependencies = {
|
|
12
|
-
...pkg.dependencies ?? {},
|
|
13
|
-
[ADAPTER_PACKAGE]: adapterVersion,
|
|
14
|
-
[SQLITE_PACKAGE]: sqliteVersion
|
|
15
|
-
};
|
|
16
38
|
const indentMatch = source.match(/^(\s+)"/m);
|
|
17
39
|
const indent = indentMatch ? indentMatch[1] : " ";
|
|
18
40
|
return JSON.stringify(pkg, null, indent.length) + "\n";
|
|
19
41
|
}
|
|
20
|
-
async function updateApiPackageJson(packageJsonPath,
|
|
42
|
+
async function updateApiPackageJson(packageJsonPath, options = {}) {
|
|
21
43
|
if (!fs.existsSync(packageJsonPath)) {
|
|
22
44
|
return "skipped";
|
|
23
45
|
}
|
|
24
46
|
const source = fs.readFileSync(packageJsonPath, "utf-8");
|
|
25
47
|
const pkg = JSON.parse(source);
|
|
26
|
-
|
|
48
|
+
const isPostgres = options.provider === "postgresql" || options.provider === "postgres";
|
|
49
|
+
const sentinelPackage = isPostgres ? PG_ADAPTER_PACKAGE : ADAPTER_PACKAGE;
|
|
50
|
+
if (pkg.dependencies?.[sentinelPackage]) {
|
|
27
51
|
return "unmodified";
|
|
28
52
|
}
|
|
29
|
-
const transformed = transformApiPackageJson(
|
|
30
|
-
source,
|
|
31
|
-
adapterVersion,
|
|
32
|
-
sqliteVersion
|
|
33
|
-
);
|
|
53
|
+
const transformed = transformApiPackageJson(source, options);
|
|
34
54
|
if (transformed === source) {
|
|
35
55
|
return "unmodified";
|
|
36
56
|
}
|
|
@@ -40,6 +60,10 @@ async function updateApiPackageJson(packageJsonPath, adapterVersion = ADAPTER_VE
|
|
|
40
60
|
export {
|
|
41
61
|
ADAPTER_PACKAGE,
|
|
42
62
|
ADAPTER_VERSION,
|
|
63
|
+
PG_ADAPTER_PACKAGE,
|
|
64
|
+
PG_ADAPTER_VERSION,
|
|
65
|
+
PG_PACKAGE,
|
|
66
|
+
PG_VERSION,
|
|
43
67
|
SQLITE_PACKAGE,
|
|
44
68
|
SQLITE_VERSION,
|
|
45
69
|
transformApiPackageJson,
|
|
@@ -3,11 +3,14 @@ const OLD_PRISMA_CLIENT = "@prisma/client";
|
|
|
3
3
|
function transform(file, api, options = {}) {
|
|
4
4
|
const j = api.jscodeshift;
|
|
5
5
|
const root = j(file.source);
|
|
6
|
-
const
|
|
6
|
+
const isSqlite = options["isSqlite"] !== false;
|
|
7
|
+
const isPostgres = options["isPostgres"] === true;
|
|
8
|
+
const hasNewClientPath = root.find(j.ImportDeclaration, { source: { value: NEW_CLIENT_PATH } }).length > 0;
|
|
9
|
+
const hasPgAdapter = root.find(j.ImportDeclaration, { source: { value: "@prisma/adapter-pg" } }).length > 0;
|
|
10
|
+
const alreadyMigrated = hasNewClientPath && (!isPostgres || hasPgAdapter);
|
|
7
11
|
if (alreadyMigrated) {
|
|
8
12
|
return file.source;
|
|
9
13
|
}
|
|
10
|
-
const isSqlite = options["isSqlite"] !== false;
|
|
11
14
|
let didTransform = false;
|
|
12
15
|
root.find(j.ImportDeclaration, { source: { value: OLD_PRISMA_CLIENT } }).forEach((nodePath) => {
|
|
13
16
|
nodePath.node.source = j.stringLiteral(NEW_CLIENT_PATH);
|
|
@@ -20,9 +23,93 @@ function transform(file, api, options = {}) {
|
|
|
20
23
|
nodePath.node.source = j.stringLiteral(NEW_CLIENT_PATH);
|
|
21
24
|
didTransform = true;
|
|
22
25
|
});
|
|
23
|
-
if (!didTransform) {
|
|
26
|
+
if (!didTransform && !isPostgres) {
|
|
24
27
|
return file.source;
|
|
25
28
|
}
|
|
29
|
+
if (isPostgres) {
|
|
30
|
+
const hasAdapterImport2 = root.find(j.ImportDeclaration, {
|
|
31
|
+
source: { value: "@prisma/adapter-pg" }
|
|
32
|
+
}).length > 0;
|
|
33
|
+
if (!hasAdapterImport2) {
|
|
34
|
+
const clientImport2 = root.find(j.ImportDeclaration, {
|
|
35
|
+
source: { value: NEW_CLIENT_PATH }
|
|
36
|
+
});
|
|
37
|
+
const adapterImport = j.importDeclaration(
|
|
38
|
+
[j.importSpecifier(j.identifier("PrismaPg"), j.identifier("PrismaPg"))],
|
|
39
|
+
j.stringLiteral("@prisma/adapter-pg")
|
|
40
|
+
);
|
|
41
|
+
clientImport2.insertBefore(adapterImport);
|
|
42
|
+
}
|
|
43
|
+
const hasAdapter2 = root.find(j.VariableDeclarator, {
|
|
44
|
+
id: { type: "Identifier", name: "adapter" }
|
|
45
|
+
}).length > 0;
|
|
46
|
+
const prismaClientNewExpr2 = root.find(j.NewExpression, {
|
|
47
|
+
callee: { type: "Identifier", name: "PrismaClient" }
|
|
48
|
+
});
|
|
49
|
+
if (prismaClientNewExpr2.length > 0 && !hasAdapter2) {
|
|
50
|
+
const prismaClientDeclaration = prismaClientNewExpr2.closest(
|
|
51
|
+
j.VariableDeclaration
|
|
52
|
+
);
|
|
53
|
+
const adapterDecl = j.variableDeclaration("const", [
|
|
54
|
+
j.variableDeclarator(
|
|
55
|
+
j.identifier("adapter"),
|
|
56
|
+
j.newExpression(j.identifier("PrismaPg"), [
|
|
57
|
+
j.objectExpression([
|
|
58
|
+
j.objectProperty(
|
|
59
|
+
j.identifier("connectionString"),
|
|
60
|
+
j.memberExpression(
|
|
61
|
+
j.memberExpression(
|
|
62
|
+
j.identifier("process"),
|
|
63
|
+
j.identifier("env")
|
|
64
|
+
),
|
|
65
|
+
j.identifier("DATABASE_URL")
|
|
66
|
+
)
|
|
67
|
+
)
|
|
68
|
+
])
|
|
69
|
+
])
|
|
70
|
+
)
|
|
71
|
+
]);
|
|
72
|
+
prismaClientDeclaration.insertBefore(adapterDecl);
|
|
73
|
+
}
|
|
74
|
+
root.find(j.NewExpression, {
|
|
75
|
+
callee: { type: "Identifier", name: "PrismaClient" }
|
|
76
|
+
}).forEach((nodePath) => {
|
|
77
|
+
const args = nodePath.node.arguments;
|
|
78
|
+
if (args.length === 0) {
|
|
79
|
+
nodePath.node.arguments = [
|
|
80
|
+
j.objectExpression([
|
|
81
|
+
Object.assign(
|
|
82
|
+
j.objectProperty(
|
|
83
|
+
j.identifier("adapter"),
|
|
84
|
+
j.identifier("adapter")
|
|
85
|
+
),
|
|
86
|
+
{ shorthand: true }
|
|
87
|
+
)
|
|
88
|
+
])
|
|
89
|
+
];
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
const firstArg = args[0];
|
|
93
|
+
if (firstArg.type !== "ObjectExpression") {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
const hasAdapterProp = firstArg.properties.some(
|
|
97
|
+
(prop) => prop.type === "ObjectProperty" && prop.key.type === "Identifier" && prop.key.name === "adapter"
|
|
98
|
+
);
|
|
99
|
+
if (!hasAdapterProp) {
|
|
100
|
+
firstArg.properties.push(
|
|
101
|
+
Object.assign(
|
|
102
|
+
j.objectProperty(
|
|
103
|
+
j.identifier("adapter"),
|
|
104
|
+
j.identifier("adapter")
|
|
105
|
+
),
|
|
106
|
+
{ shorthand: true }
|
|
107
|
+
)
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
return root.toSource({ quote: "single" });
|
|
112
|
+
}
|
|
26
113
|
if (!isSqlite) {
|
|
27
114
|
return root.toSource({ quote: "single" });
|
|
28
115
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cedarjs/codemods",
|
|
3
|
-
"version": "3.0.0-canary.
|
|
3
|
+
"version": "3.0.0-canary.13624+5a683e227",
|
|
4
4
|
"description": "Codemods to ease upgrading a CedarJS Project",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -26,10 +26,10 @@
|
|
|
26
26
|
"dependencies": {
|
|
27
27
|
"@babel/cli": "7.28.6",
|
|
28
28
|
"@babel/core": "^7.26.10",
|
|
29
|
-
"@babel/parser": "7.29.
|
|
29
|
+
"@babel/parser": "7.29.2",
|
|
30
30
|
"@babel/plugin-transform-typescript": "^7.26.8",
|
|
31
31
|
"@babel/traverse": "7.29.0",
|
|
32
|
-
"@cedarjs/project-config": "3.0.0-canary.
|
|
32
|
+
"@cedarjs/project-config": "3.0.0-canary.13624",
|
|
33
33
|
"@svgr/core": "8.1.0",
|
|
34
34
|
"@svgr/plugin-jsx": "8.1.0",
|
|
35
35
|
"@vscode/ripgrep": "1.17.1",
|
|
@@ -47,7 +47,7 @@
|
|
|
47
47
|
"yargs": "17.7.2"
|
|
48
48
|
},
|
|
49
49
|
"devDependencies": {
|
|
50
|
-
"@cedarjs/framework-tools": "3.0.0-canary.
|
|
50
|
+
"@cedarjs/framework-tools": "3.0.0-canary.13624",
|
|
51
51
|
"@types/babel__core": "7.20.5",
|
|
52
52
|
"@types/jscodeshift": "17.3.0",
|
|
53
53
|
"@types/yargs": "17.0.35",
|
|
@@ -60,5 +60,5 @@
|
|
|
60
60
|
"publishConfig": {
|
|
61
61
|
"access": "public"
|
|
62
62
|
},
|
|
63
|
-
"gitHead": "
|
|
63
|
+
"gitHead": "5a683e2277e88ed83a16a0b7c443c47ea2f4d7b6"
|
|
64
64
|
}
|