Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/mariadb-introspection-compat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@prisma/studio-core": patch
---

Fix MySQL introspection failing on MariaDB. The adapter now detects MariaDB via `select version()` and uses a dedicated tables query that returns one row per column and groups the result on the client, avoiding `json_arrayagg` (missing before MariaDB 10.5), JSON casts (invalid syntax on MariaDB), and any server-side string aggregation that would be truncated at `group_concat_max_len`. Introspection also parses string-encoded `json_arrayagg` payloads returned by some MySQL transports.
1 change: 1 addition & 0 deletions FEATURES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Each adapter handles introspection, querying, inserts, updates, and deletes whil
Studio introspects connected databases to build schemas, tables, columns, relationships, filter operators, and timezone metadata.
This gives users an accurate live model of the database and keeps table navigation grounded in current structure.
A fresh Studio mount performs this discovery once, while actual adapter or database-availability changes invalidate cached metadata and load it again.
The MySQL adapter detects MariaDB servers via `select version()` and switches to a MariaDB-compatible tables query that returns one row per column and groups the result on the client, so introspection works on all supported MariaDB versions where `json_arrayagg` or JSON casts are unavailable and cannot be truncated by `group_concat_max_len`.

## Deployable Prisma Postgres Demo

Expand Down
82 changes: 76 additions & 6 deletions data/mysql-core/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,16 @@ import {
getUpdateRefetchQuery,
} from "./dml";
import {
detectMySQLServerFlavor,
getMariaDBTablesQuery,
getServerVersionQuery,
getTablesQuery,
getTimezoneQuery,
groupMariaDBTablesQueryResult,
mockTablesQuery,
mockTimezoneQuery,
type MySQLServerFlavor,
normalizeTablesQueryResult,
} from "./introspection";
import { lintMySQLWithExplainFallback } from "./sql-lint";

Expand Down Expand Up @@ -148,18 +154,82 @@ export function createMySQLAdapter(
}
}

let cachedServerFlavor: MySQLServerFlavor | null = null;

async function detectServerFlavor(
options: Parameters<Adapter["introspect"]>[0],
): Promise<MySQLServerFlavor> {
if (cachedServerFlavor) {
return cachedServerFlavor;
}

try {
const [error, versions] = await executor.execute(
getServerVersionQuery(otherRequirements),
options,
);

if (error) {
// fall back to the MySQL introspection SQL without caching, so the
// next introspection retries the detection.
return "mysql";
}

cachedServerFlavor = detectMySQLServerFlavor(versions[0]?.version);

return cachedServerFlavor;
} catch {
return "mysql";
}
}

/**
* Fetches table metadata using the tables query that matches the server
* flavor. MariaDB gets a one-row-per-column query grouped on the client, so
* results can never be truncated by `group_concat_max_len`.
*/
async function fetchTables(
serverFlavor: MySQLServerFlavor,
options: Parameters<Adapter["introspect"]>[0],
): Promise<{
query: Query;
result: Either<Error, QueryResult<typeof getTablesQuery>>;
}> {
if (serverFlavor === "mariadb") {
const query = getMariaDBTablesQuery(otherRequirements);
const [error, rows] = await executor.execute(query, options);

return {
query,
result: error ? [error] : [null, groupMariaDBTablesQueryResult(rows)],
};
}

const query = getTablesQuery(otherRequirements);
const [error, rows] = await executor.execute(query, options);

return {
query,
result: error ? [error] : [null, normalizeTablesQueryResult(rows)],
};
}

async function introspectDatabase(
options: Parameters<Adapter["introspect"]>[0],
): Promise<Either<AdapterError, AdapterIntrospectResult>> {
try {
const tablesQuery = getTablesQuery(otherRequirements);
const serverFlavor = await detectServerFlavor(options);
const timezoneQuery = getTimezoneQuery(otherRequirements);

const [[tablesError, tables], [timezoneError, timezones]] =
await Promise.all([
executor.execute(tablesQuery, options),
executor.execute(timezoneQuery, options),
]);
const [tablesFetch, [timezoneError, timezones]] = await Promise.all([
fetchTables(serverFlavor, options),
executor.execute(timezoneQuery, options),
]);

const {
query: tablesQuery,
result: [tablesError, tables],
} = tablesFetch;

if (tablesError) {
return createMySQLAdapterError({
Expand Down
Loading