@mikro-orm/mariadb 7.0.2-dev.9 → 7.0.2

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.
@@ -1,84 +1,84 @@
1
- import { MySqlSchemaHelper, } from '@mikro-orm/mysql';
1
+ import { MySqlSchemaHelper } from '@mikro-orm/mysql';
2
+ /** Schema introspection helper for MariaDB. */
2
3
  export class MariaDbSchemaHelper extends MySqlSchemaHelper {
3
- appendMySqlIndexSuffix(sql, index) {
4
- // MariaDB uses IGNORED instead of MySQL's INVISIBLE keyword
5
- if (index.invisible) {
6
- sql += ' ignored';
7
- }
8
- // MariaDB supports CLUSTERING=YES only with the Aria storage engine.
9
- // Using this option with InnoDB tables will have no effect (silently ignored).
10
- // See: https://mariadb.com/kb/en/create-index/#clustering-yes
11
- if (index.clustered) {
12
- sql += ' clustering=yes';
13
- }
14
- return sql;
4
+ appendMySqlIndexSuffix(sql, index) {
5
+ // MariaDB uses IGNORED instead of MySQL's INVISIBLE keyword
6
+ if (index.invisible) {
7
+ sql += ' ignored';
15
8
  }
16
- async loadInformationSchema(schema, connection, tables) {
17
- /* v8 ignore next */
18
- if (tables.length === 0) {
19
- return;
20
- }
21
- const columns = await this.getAllColumns(connection, tables);
22
- const indexes = await this.getAllIndexes(connection, tables);
23
- const checks = await this.getAllChecks(connection, tables, columns);
24
- const fks = await this.getAllForeignKeys(connection, tables);
25
- const enums = await this.getAllEnumDefinitions(connection, tables);
26
- for (const t of tables) {
27
- const key = this.getTableKey(t);
28
- const table = schema.addTable(t.table_name, t.schema_name, t.table_comment);
29
- const pks = await this.getPrimaryKeys(connection, indexes[key], table.name, table.schema);
30
- table.init(columns[key], indexes[key], checks[key], pks, fks[key], enums[key]);
31
- }
9
+ // MariaDB supports CLUSTERING=YES only with the Aria storage engine.
10
+ // Using this option with InnoDB tables will have no effect (silently ignored).
11
+ // See: https://mariadb.com/kb/en/create-index/#clustering-yes
12
+ if (index.clustered) {
13
+ sql += ' clustering=yes';
14
+ }
15
+ return sql;
16
+ }
17
+ async loadInformationSchema(schema, connection, tables) {
18
+ /* v8 ignore next */
19
+ if (tables.length === 0) {
20
+ return;
32
21
  }
33
- async getAllIndexes(connection, tables) {
34
- const sql = `select table_name as table_name, nullif(table_schema, schema()) as schema_name, index_name as index_name, non_unique as non_unique, column_name as column_name, index_type as index_type, sub_part as sub_part, collation as sort_order /*M!100600 , ignored as ignored */
22
+ const columns = await this.getAllColumns(connection, tables);
23
+ const indexes = await this.getAllIndexes(connection, tables);
24
+ const checks = await this.getAllChecks(connection, tables, columns);
25
+ const fks = await this.getAllForeignKeys(connection, tables);
26
+ const enums = await this.getAllEnumDefinitions(connection, tables);
27
+ for (const t of tables) {
28
+ const key = this.getTableKey(t);
29
+ const table = schema.addTable(t.table_name, t.schema_name, t.table_comment);
30
+ const pks = await this.getPrimaryKeys(connection, indexes[key], table.name, table.schema);
31
+ table.init(columns[key], indexes[key], checks[key], pks, fks[key], enums[key]);
32
+ }
33
+ }
34
+ async getAllIndexes(connection, tables) {
35
+ const sql = `select table_name as table_name, nullif(table_schema, schema()) as schema_name, index_name as index_name, non_unique as non_unique, column_name as column_name, index_type as index_type, sub_part as sub_part, collation as sort_order /*M!100600 , ignored as ignored */
35
36
  from information_schema.statistics where table_schema = database()
36
37
  and table_name in (${tables.map(t => this.platform.quoteValue(t.table_name)).join(', ')})
37
38
  order by schema_name, table_name, index_name, seq_in_index`;
38
- const allIndexes = await connection.execute(sql);
39
- const ret = {};
40
- for (const index of allIndexes) {
41
- const key = this.getTableKey(index);
42
- const indexDef = {
43
- columnNames: [index.column_name],
44
- keyName: index.index_name,
45
- unique: !index.non_unique,
46
- primary: index.index_name === 'PRIMARY',
47
- constraint: !index.non_unique,
48
- };
49
- // Capture column options (prefix length, sort order)
50
- if (index.sub_part != null || index.sort_order === 'D') {
51
- indexDef.columns = [
52
- {
53
- name: index.column_name,
54
- ...(index.sub_part != null && { length: index.sub_part }),
55
- ...(index.sort_order === 'D' && { sort: 'DESC' }),
56
- },
57
- ];
58
- }
59
- // Capture index type for fulltext and spatial indexes
60
- if (index.index_type === 'FULLTEXT') {
61
- indexDef.type = 'fulltext';
62
- }
63
- else if (index.index_type === 'SPATIAL') {
64
- /* v8 ignore next */
65
- indexDef.type = 'spatial';
66
- }
67
- // Capture ignored flag (MariaDB 10.6+, equivalent to MySQL's INVISIBLE)
68
- /* v8 ignore next */
69
- if (index.ignored === 'YES') {
70
- indexDef.invisible = true;
71
- }
72
- ret[key] ??= [];
73
- ret[key].push(indexDef);
74
- }
75
- for (const key of Object.keys(ret)) {
76
- ret[key] = await this.mapIndexes(ret[key]);
77
- }
78
- return ret;
39
+ const allIndexes = await connection.execute(sql);
40
+ const ret = {};
41
+ for (const index of allIndexes) {
42
+ const key = this.getTableKey(index);
43
+ const indexDef = {
44
+ columnNames: [index.column_name],
45
+ keyName: index.index_name,
46
+ unique: !index.non_unique,
47
+ primary: index.index_name === 'PRIMARY',
48
+ constraint: !index.non_unique,
49
+ };
50
+ // Capture column options (prefix length, sort order)
51
+ if (index.sub_part != null || index.sort_order === 'D') {
52
+ indexDef.columns = [
53
+ {
54
+ name: index.column_name,
55
+ ...(index.sub_part != null && { length: index.sub_part }),
56
+ ...(index.sort_order === 'D' && { sort: 'DESC' }),
57
+ },
58
+ ];
59
+ }
60
+ // Capture index type for fulltext and spatial indexes
61
+ if (index.index_type === 'FULLTEXT') {
62
+ indexDef.type = 'fulltext';
63
+ } else if (index.index_type === 'SPATIAL') {
64
+ /* v8 ignore next */
65
+ indexDef.type = 'spatial';
66
+ }
67
+ // Capture ignored flag (MariaDB 10.6+, equivalent to MySQL's INVISIBLE)
68
+ /* v8 ignore next */
69
+ if (index.ignored === 'YES') {
70
+ indexDef.invisible = true;
71
+ }
72
+ ret[key] ??= [];
73
+ ret[key].push(indexDef);
79
74
  }
80
- async getAllColumns(connection, tables) {
81
- const sql = `select table_name as table_name,
75
+ for (const key of Object.keys(ret)) {
76
+ ret[key] = await this.mapIndexes(ret[key]);
77
+ }
78
+ return ret;
79
+ }
80
+ async getAllColumns(connection, tables) {
81
+ const sql = `select table_name as table_name,
82
82
  nullif(table_schema, schema()) as schema_name,
83
83
  column_name as column_name,
84
84
  column_default as column_default,
@@ -94,69 +94,73 @@ export class MariaDbSchemaHelper extends MySqlSchemaHelper {
94
94
  ifnull(datetime_precision, character_maximum_length) length
95
95
  from information_schema.columns where table_schema = database() and table_name in (${tables.map(t => this.platform.quoteValue(t.table_name)).join(', ')})
96
96
  order by ordinal_position`;
97
- const allColumns = await connection.execute(sql);
98
- const str = (val) => (val != null ? '' + val : val);
99
- const extra = (val) => val.replace(/auto_increment|default_generated|(stored|virtual) generated/i, '').trim() || undefined;
100
- const ret = {};
101
- for (const col of allColumns) {
102
- const mappedType = this.platform.getMappedType(col.column_type);
103
- const tmp = this.normalizeDefaultValue(mappedType.compareAsType() === 'boolean' && ['0', '1'].includes(col.column_default)
104
- ? ['false', 'true'][+col.column_default]
105
- : col.column_default, col.length);
106
- const defaultValue = str(tmp === 'NULL' && col.is_nullable === 'YES' ? null : tmp);
107
- const key = this.getTableKey(col);
108
- const generated = col.generation_expression
109
- ? `${col.generation_expression.replaceAll(`\\'`, `'`)} ${col.extra.match(/stored generated/i) ? 'stored' : 'virtual'}`
110
- : undefined;
111
- ret[key] ??= [];
112
- ret[key].push({
113
- name: col.column_name,
114
- type: this.platform.isNumericColumn(mappedType)
115
- ? col.column_type.replace(/ unsigned$/, '').replace(/\(\d+\)$/, '')
116
- : col.column_type,
117
- mappedType,
118
- unsigned: col.column_type.endsWith(' unsigned'),
119
- length: col.length,
120
- default: this.wrap(defaultValue, mappedType),
121
- nullable: col.is_nullable === 'YES',
122
- primary: col.column_key === 'PRI',
123
- unique: col.column_key === 'UNI',
124
- autoincrement: col.extra === 'auto_increment',
125
- precision: col.numeric_precision,
126
- scale: col.numeric_scale,
127
- comment: col.column_comment,
128
- extra: extra(col.extra),
129
- generated,
130
- });
131
- }
132
- return ret;
97
+ const allColumns = await connection.execute(sql);
98
+ const str = val => (val != null ? '' + val : val);
99
+ const extra = val =>
100
+ val.replace(/auto_increment|default_generated|(stored|virtual) generated/i, '').trim() || undefined;
101
+ const ret = {};
102
+ for (const col of allColumns) {
103
+ const mappedType = this.platform.getMappedType(col.column_type);
104
+ const tmp = this.normalizeDefaultValue(
105
+ mappedType.compareAsType() === 'boolean' && ['0', '1'].includes(col.column_default)
106
+ ? ['false', 'true'][+col.column_default]
107
+ : col.column_default,
108
+ col.length,
109
+ );
110
+ const defaultValue = str(tmp === 'NULL' && col.is_nullable === 'YES' ? null : tmp);
111
+ const key = this.getTableKey(col);
112
+ const generated = col.generation_expression
113
+ ? `${col.generation_expression.replaceAll(`\\'`, `'`)} ${col.extra.match(/stored generated/i) ? 'stored' : 'virtual'}`
114
+ : undefined;
115
+ ret[key] ??= [];
116
+ ret[key].push({
117
+ name: col.column_name,
118
+ type: this.platform.isNumericColumn(mappedType)
119
+ ? col.column_type.replace(/ unsigned$/, '').replace(/\(\d+\)$/, '')
120
+ : col.column_type,
121
+ mappedType,
122
+ unsigned: col.column_type.endsWith(' unsigned'),
123
+ length: col.length,
124
+ default: this.wrap(defaultValue, mappedType),
125
+ nullable: col.is_nullable === 'YES',
126
+ primary: col.column_key === 'PRI',
127
+ unique: col.column_key === 'UNI',
128
+ autoincrement: col.extra === 'auto_increment',
129
+ precision: col.numeric_precision,
130
+ scale: col.numeric_scale,
131
+ comment: col.column_comment,
132
+ extra: extra(col.extra),
133
+ generated,
134
+ });
133
135
  }
134
- async getAllChecks(connection, tables, columns) {
135
- const sql = this.getChecksSQL(tables);
136
- const allChecks = await connection.execute(sql);
137
- const ret = {};
138
- for (const check of allChecks) {
139
- const key = this.getTableKey(check);
140
- const match = /^json_valid\(`(.*)`\)$/i.exec(check.expression);
141
- const col = columns?.[key]?.find(col => col.name === match?.[1]);
142
- if (col && match) {
143
- col.type = 'json';
144
- col.mappedType = this.platform.getMappedType('json');
145
- delete col.length;
146
- continue;
147
- }
148
- ret[key] ??= [];
149
- ret[key].push({
150
- name: check.name,
151
- columnName: check.column_name,
152
- definition: `check ${check.expression}`,
153
- expression: check.expression.replace(/^\((.*)\)$/, '$1'),
154
- });
155
- }
156
- return ret;
136
+ return ret;
137
+ }
138
+ async getAllChecks(connection, tables, columns) {
139
+ const sql = this.getChecksSQL(tables);
140
+ const allChecks = await connection.execute(sql);
141
+ const ret = {};
142
+ for (const check of allChecks) {
143
+ const key = this.getTableKey(check);
144
+ const match = /^json_valid\(`(.*)`\)$/i.exec(check.expression);
145
+ const col = columns?.[key]?.find(col => col.name === match?.[1]);
146
+ if (col && match) {
147
+ col.type = 'json';
148
+ col.mappedType = this.platform.getMappedType('json');
149
+ delete col.length;
150
+ continue;
151
+ }
152
+ ret[key] ??= [];
153
+ ret[key].push({
154
+ name: check.name,
155
+ columnName: check.column_name,
156
+ definition: `check ${check.expression}`,
157
+ expression: check.expression.replace(/^\((.*)\)$/, '$1'),
158
+ });
157
159
  }
158
- getChecksSQL(tables) {
159
- return `select
160
+ return ret;
161
+ }
162
+ getChecksSQL(tables) {
163
+ return `select
160
164
  tc.constraint_schema as table_schema,
161
165
  tc.table_name as table_name,
162
166
  tc.constraint_name as name,
@@ -165,8 +169,8 @@ export class MariaDbSchemaHelper extends MySqlSchemaHelper {
165
169
  from information_schema.check_constraints tc
166
170
  where tc.table_name in (${tables.map(t => this.platform.quoteValue(t.table_name)).join(', ')}) and tc.constraint_schema = database()
167
171
  order by tc.constraint_name`;
168
- }
169
- wrap(val, type) {
170
- return val;
171
- }
172
+ }
173
+ wrap(val, type) {
174
+ return val;
175
+ }
172
176
  }