Skip to content
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

add MS SQL Server dialect. #595

Merged
merged 80 commits into from
Sep 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
80 commits
Select commit Hold shift + click to select a range
57620cf
add mssql dev dependency.
igalklebanov Jul 17, 2023
beee7c9
Merge branch 'master' into mssql-dialect
igalklebanov Jul 18, 2023
c84ea10
add mssql to docker-compose.yml.
igalklebanov Jul 18, 2023
d4d7ab9
config? @ pg driver.
igalklebanov Jul 18, 2023
e51b57d
initial dialect implementation.
igalklebanov Jul 19, 2023
dbc6243
Merge branch 'master' into mssql-dialect
igalklebanov Jul 19, 2023
a6dfbbc
fix browser tests by getting tedious stuff in config.
igalklebanov Jul 19, 2023
1a97d6f
Merge branch 'master' into mssql-dialect
igalklebanov Jul 21, 2023
f9adb7f
introduce mssql to test-setup.
igalklebanov Jul 22, 2023
945fe29
get dialect to a working state.
igalklebanov Jul 22, 2023
fd01e77
introduce mssql to insert test suite.
igalklebanov Jul 22, 2023
b5fbb96
introduce mssql to select test suite.
igalklebanov Jul 22, 2023
3b08a91
add mssql to test scripts.
igalklebanov Jul 22, 2023
e4540bf
make tests pass type-checks.
igalklebanov Jul 22, 2023
c006c62
Merge branch 'master' into mssql-dialect
igalklebanov Jul 22, 2023
57cd036
fix new tests after update branch.
igalklebanov Jul 22, 2023
1b725bc
introduce mssql to update test suite.
igalklebanov Jul 22, 2023
dabc2d1
fix mysql test expectation.
igalklebanov Jul 22, 2023
facd918
introduce mssql to delete test suite.
igalklebanov Jul 22, 2023
042b397
introduce mssql to with test suite.
igalklebanov Jul 22, 2023
86e220a
introduce mssql to with schema test suite.
igalklebanov Jul 23, 2023
f5a3815
introduce mssql to where test suite.
igalklebanov Jul 23, 2023
cc91823
Merge branch 'master' into mssql-dialect
igalklebanov Jul 23, 2023
e41fcef
test mssql vs. selectNoFrom.
igalklebanov Jul 23, 2023
e72124c
introduce mssql to set operation test suite.
igalklebanov Jul 23, 2023
ccfa1f4
fix error rejections.
igalklebanov Jul 24, 2023
1508457
introduce mssql to schema module test suite.
igalklebanov Jul 24, 2023
0099d60
Merge branch 'master' into mssql-dialect
igalklebanov Jul 27, 2023
c5ab5df
remove mssql, add tedious and tarn.
igalklebanov Jul 29, 2023
bf5f93e
initial pool implementation using tarn.
igalklebanov Jul 29, 2023
32bda8e
fix failing where tests.
igalklebanov Jul 29, 2023
ae17118
fix compilation errors at select tests.
igalklebanov Jul 29, 2023
a8f4a3c
introduce mssql to schema module test suite, pt. 2.
igalklebanov Jul 29, 2023
09f47af
compile mssql alter table alter column properly.
igalklebanov Jul 31, 2023
ce55989
introduce mssql to schema module test suite, pt. 3.
igalklebanov Jul 31, 2023
cfe611d
fix failing where test.
igalklebanov Jul 31, 2023
d5531b6
introduce mssql to sanitize identifiers test suite.
igalklebanov Aug 2, 2023
f7e7b7d
introduce mssql to raw sql test suite.
igalklebanov Aug 2, 2023
5e6e2bb
introduce mssql to raw query test suite.
igalklebanov Aug 2, 2023
f438909
introduce mssql to order by test suite.
igalklebanov Aug 2, 2023
9d87ac4
Merge branch 'master' into mssql-dialect
igalklebanov Aug 3, 2023
3268674
introduce mssql to join test suite.
igalklebanov Aug 3, 2023
e067376
introduce mssql to having test suite.
igalklebanov Aug 3, 2023
60559e2
introduce mssql into group by test suite.
igalklebanov Aug 3, 2023
cc4cf34
empty and/or works everywhere.
igalklebanov Aug 3, 2023
ebcca8b
introduce mssql to expression test suite.
igalklebanov Aug 3, 2023
1328b0b
introduce mssql to execute test suite.
igalklebanov Aug 3, 2023
aae2c40
introduce mssql to deduplicate joins test suite.
igalklebanov Aug 3, 2023
b393ba7
introduce mssql to coalesce test suite.
igalklebanov Aug 3, 2023
23b0535
introduce mssql to clear test suite.
igalklebanov Aug 3, 2023
f9771c3
introduce mssql case test suite.
igalklebanov Aug 3, 2023
9f82a6d
introduce mssql to camel case test suite.
igalklebanov Aug 3, 2023
fb8741c
introduce mssql to aggregate function test suite.
igalklebanov Aug 4, 2023
a69f3ad
lift only.
igalklebanov Aug 4, 2023
40b228d
connection validate shouldn't trigger kysely stuff.
igalklebanov Aug 4, 2023
62ef70e
introduce mssql to transaction test suite, pt. 1.
igalklebanov Aug 4, 2023
a85be59
Merge branch 'master' into mssql-dialect
igalklebanov Aug 4, 2023
fa3494b
fix introspect tests.
igalklebanov Aug 4, 2023
772f6a2
remove mssql types.
igalklebanov Aug 4, 2023
66d7655
add @types/tedious dev dep.
igalklebanov Aug 4, 2023
00df597
introduce mssql to transaction test suite, pt. 2.
igalklebanov Aug 4, 2023
989de4a
introduce mssql to introspect test suite, pt. 1.
igalklebanov Aug 5, 2023
c34f900
Merge branch 'master' into mssql-dialect
igalklebanov Aug 5, 2023
aa8fdb9
introduce mssql to introspect test suite, pt. 2.
igalklebanov Aug 5, 2023
0b52cb0
fix default to column introspection @ mssql.
igalklebanov Aug 5, 2023
efb0297
autoincrementing columns are technically columns with default values.
igalklebanov Aug 6, 2023
9489d69
dont use ifNotExists in mssql migrations.
igalklebanov Aug 6, 2023
f221a98
introduce mssql to migration test suite.
igalklebanov Aug 6, 2023
6938047
Merge branch 'master' into mssql-dialect
igalklebanov Aug 8, 2023
a07213b
implement streaming.
igalklebanov Aug 9, 2023
ece054d
Merge branch 'master' into mssql-dialect
igalklebanov Aug 10, 2023
ff422f2
fix schema tests.
igalklebanov Aug 11, 2023
8bb0c0e
rename DIALECTS_WITH_MSSQL back to DIALECTS.
igalklebanov Aug 11, 2023
a00831b
misc adjustments and js docs.
igalklebanov Aug 12, 2023
5a1aa1d
Merge branch 'master' into mssql-dialect
igalklebanov Aug 12, 2023
36c86e7
fix wording of `supportsCreateIfNotExists`.
igalklebanov Aug 12, 2023
d8fdd97
Merge branch 'master' into mssql-dialect
igalklebanov Aug 29, 2023
bb9f7d7
Merge branch 'master' into mssql-dialect
igalklebanov Sep 1, 2023
6afab44
Merge branch 'master' into mssql-dialect
igalklebanov Sep 9, 2023
e883b22
add mssql to key value set variant test case.
igalklebanov Sep 9, 2023
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
Prev Previous commit
Next Next commit
introduce mssql to introspect test suite, pt. 2.
  • Loading branch information
igalklebanov committed Aug 5, 2023
commit aa8fdb99c6a01790e45ddd5159c0b3cc79c21fff
264 changes: 261 additions & 3 deletions src/dialect/mssql/mssql-introspector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import {
SchemaMetadata,
TableMetadata,
} from '../database-introspector.js'
import {
DEFAULT_MIGRATION_LOCK_TABLE,
DEFAULT_MIGRATION_TABLE,
} from '../../migration/migrator.js'
import { freeze } from '../../util/object-utils.js'

export class MssqlIntrospector implements DatabaseIntrospector {
readonly #db: Kysely<MssqlSysTables>
Expand All @@ -19,13 +24,132 @@ export class MssqlIntrospector implements DatabaseIntrospector {
}

async getTables(
options?: DatabaseMetadataOptions | undefined
options: DatabaseMetadataOptions = { withInternalKyselyTables: false }
): Promise<TableMetadata[]> {
throw new Error('Not implemented')
const rawColumns = await this.#db
.selectFrom('sys.tables as tables')
.leftJoin(
'sys.schemas as table_schemas',
'table_schemas.schema_id',
'tables.schema_id'
)
.innerJoin(
'sys.columns as columns',
'columns.object_id',
'tables.object_id'
)
.innerJoin(
'sys.types as types',
'types.system_type_id',
'columns.system_type_id'
)
.leftJoin(
'sys.schemas as type_schemas',
'type_schemas.schema_id',
'types.schema_id'
)
.$if(!options.withInternalKyselyTables, (qb) =>
qb
.where('tables.name', '!=', DEFAULT_MIGRATION_TABLE)
.where('tables.name', '!=', DEFAULT_MIGRATION_LOCK_TABLE)
)
.select([
'tables.name as table_name',
(eb) =>
eb
.ref('tables.type')
.$castTo<
| MssqlSysTables['sys.tables']['type']
| MssqlSysTables['sys.views']['type']
>()
.as('table_type'),
'table_schemas.name as table_schema_name',
'columns.generated_always_type_desc as column_generated_always_type',
'columns.is_computed as column_is_computed',
'columns.is_identity as column_is_identity',
'columns.is_nullable as column_is_nullable',
'columns.is_rowguidcol as column_is_rowguidcol',
'columns.name as column_name',
'types.is_nullable as type_is_nullable',
'types.name as type_name',
'type_schemas.name as type_schema_name',
])
.unionAll(
this.#db
.selectFrom('sys.views as views')
.leftJoin(
'sys.schemas as view_schemas',
'view_schemas.schema_id',
'views.schema_id'
)
.innerJoin(
'sys.columns as columns',
'columns.object_id',
'views.object_id'
)
.innerJoin(
'sys.types as types',
'types.system_type_id',
'columns.system_type_id'
)
.leftJoin(
'sys.schemas as type_schemas',
'type_schemas.schema_id',
'types.schema_id'
)
.select([
'views.name as table_name',
'views.type as table_type',
'view_schemas.name as table_schema_name',
'columns.generated_always_type_desc as column_generated_always_type',
'columns.is_computed as column_is_computed',
'columns.is_identity as column_is_identity',
'columns.is_nullable as column_is_nullable',
'columns.is_rowguidcol as column_is_rowguidcol',
'columns.name as column_name',
'types.is_nullable as type_is_nullable',
'types.name as type_name',
'type_schemas.name as type_schema_name',
])
)
.orderBy('table_schema_name')
.orderBy('table_name')
.orderBy('column_name')
.execute()

const tableDictionary: Record<string, TableMetadata> = {}

for (const rawColumn of rawColumns) {
const key = `${rawColumn.table_schema_name}.${rawColumn.table_name}`

const table = (tableDictionary[key] ||= freeze({
columns: [],
isView: rawColumn.table_type === 'V ',
name: rawColumn.table_name,
schema: rawColumn.table_schema_name ?? undefined,
}))

table.columns.push(
freeze({
dataType: rawColumn.type_name,
dataTypeSchema: rawColumn.type_schema_name ?? undefined,
hasDefaultValue:
rawColumn.column_generated_always_type !== 'NOT_APPLICABLE' ||
rawColumn.column_is_computed ||
rawColumn.column_is_rowguidcol,
isAutoIncrementing: rawColumn.column_is_identity,
isNullable:
rawColumn.column_is_nullable && rawColumn.type_is_nullable,
name: rawColumn.column_name,
})
)
}

return Object.values(tableDictionary)
}

async getMetadata(
options?: DatabaseMetadataOptions | undefined
options?: DatabaseMetadataOptions
): Promise<DatabaseMetadata> {
return {
tables: await this.getTables(options),
Expand All @@ -34,7 +158,141 @@ export class MssqlIntrospector implements DatabaseIntrospector {
}

interface MssqlSysTables {
'sys.columns': {
// collation_name: string | null
// column_encryption_key_database_name: null
// column_encryption_key_id: null
column_id: number
// default_object_id: number
// encryption_algorithm_name: null
// encryption_type: null
// encryption_type_desc: null
// generated_always_type: number
generated_always_type_desc: string
// graph_type: number
// graph_type_desc: string
// is_ansi_padded: boolean
// is_column_set: boolean
is_computed: boolean
// is_data_deletion_filter_column: boolean
// is_dropped_ledger_column: boolean
// is_dts_replicated: boolean
// is_filestream: boolean
// is_hidden: boolean
is_identity: boolean
// is_masked: boolean
// is_merge_published: boolean
// is_non_sql_subscribed: boolean
is_nullable: boolean
// is_replicated: boolean
is_rowguidcol: boolean
// is_sparse: boolean
// is_xml_document: boolean
// ledger_view_column_type: null
// ledger_view_column_type_desc: null
// max_length: number
name: string
object_id: number
// precision: number
// rule_object_id: number
// scale: number
system_type_id: number
}
'sys.schemas': {
name: string
// principal_id: number
schema_id: number
}
'sys.tables': {
// create_date: Date
// data_retention_period: number
// data_retention_period_unit: number
// data_retention_period_unit_desc: string
// durability: number
// durability_desc: string
// filestream_data_space_id: number | null
// has_replication_filter: boolean
// has_unchecked_assembly_data: boolean
// history_retention_period: null
// history_retention_period_unit: null
// history_retention_period_unit_desc: null
// history_table_id: null
// is_dropped_ledger_table: boolean
// is_edge: boolean
// is_external: boolean
// is_filetable: boolean
// is_memory_optimized: boolean
// is_merge_published: boolean
// is_ms_shipped: boolean
// is_node: boolean
// is_published: boolean
// is_remote_data_archive_enabled: boolean
// is_replicated: boolean
// is_schema_published: boolean
// is_sync_tran_subscribed: boolean
// is_tracked_by_cdc: boolean
// large_value_types_out_of_row: boolean
// ledger_type: number
// ledger_type_desc: string
// ledger_view_id: null
// lob_data_space_id: number
// lock_escalation: number
// lock_escalation_desc: string
// lock_on_bulk_load: boolean
// max_column_id_used: number
// modify_date: Date
name: string
object_id: number
// parent_object_id: number
// principal_id: number | null
schema_id: number
// temporal_type: number
// temporal_type_desc: string
// text_in_row_limit: number
type: 'U '
// type_desc: 'USER_TABLE'
// uses_ansi_nulls: boolean
}
'sys.types': {
// collation_name: string | null
// default_object_id: number
// is_assembly_type: boolean
is_nullable: boolean
// is_table_type: boolean
// is_user_defined: boolean
// max_length: number
name: string
// precision: number
// principal_id: number | null
// rule_object_id: number
// scale: number
schema_id: number
system_type_id: number
// user_type_id: number
}
'sys.views': {
// create_date: Date
// has_opaque_metadata: boolean
// has_replication_filter: boolean
// has_snapshot: boolean
// has_unchecked_assembly_data: boolean
// is_date_correlation_view: boolean
// is_dropped_ledger_view: boolean
// is_msh_shipped: boolean
// is_published: boolean
// is_replicated: boolean
// is_schema_published: boolean
// is_tracked_by_cdc: boolean
// ledger_view_type: number
// ledger_view_type_desc: string
// modify_date: Date
name: string
object_id: number
// parent_object_id: number
// principal_id: number | null
schema_id: number
type: 'V '
// type_desc: 'VIEW'
// with_check_option: boolean
}
}
Loading