@sqldoc/ns-deprecated 0.0.1 → 0.0.3
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.
- package/package.json +11 -9
- package/src/index.ts +61 -53
- package/src/__tests__/deprecated.test.ts +0 -139
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"type": "module",
|
|
3
3
|
"name": "@sqldoc/ns-deprecated",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.3",
|
|
5
5
|
"description": "Deprecation namespace for sqldoc -- marks SQL objects as deprecated with COMMENT ON statements",
|
|
6
6
|
"exports": {
|
|
7
7
|
".": {
|
|
@@ -14,17 +14,19 @@
|
|
|
14
14
|
"types": "./src/index.ts",
|
|
15
15
|
"files": [
|
|
16
16
|
"src",
|
|
17
|
+
"!src/__tests__",
|
|
17
18
|
"package.json"
|
|
18
19
|
],
|
|
20
|
+
"scripts": {
|
|
21
|
+
"test": "node --test 'src/__tests__/**/*.test.ts'",
|
|
22
|
+
"typecheck": "tsc --noEmit"
|
|
23
|
+
},
|
|
19
24
|
"peerDependencies": {
|
|
20
|
-
"@sqldoc/core": "
|
|
25
|
+
"@sqldoc/core": "workspace:*"
|
|
21
26
|
},
|
|
22
27
|
"devDependencies": {
|
|
23
|
-
"
|
|
24
|
-
"
|
|
25
|
-
"@sqldoc/
|
|
26
|
-
},
|
|
27
|
-
"scripts": {
|
|
28
|
-
"test": "vitest run"
|
|
28
|
+
"@sqldoc/core": "workspace:*",
|
|
29
|
+
"typescript": "catalog:",
|
|
30
|
+
"@sqldoc/test-utils": "workspace:*"
|
|
29
31
|
}
|
|
30
|
-
}
|
|
32
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,32 +1,5 @@
|
|
|
1
1
|
import type { NamespacePlugin, TagContext, TagOutput } from '@sqldoc/core'
|
|
2
|
-
|
|
3
|
-
function commentOnSql(target: string, qualifiedName: string, message: string): string {
|
|
4
|
-
return `COMMENT ON ${target} ${qualifiedName} IS '${message}';`
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
function targetKeyword(target: string): string {
|
|
8
|
-
switch (target) {
|
|
9
|
-
case 'table':
|
|
10
|
-
return 'TABLE'
|
|
11
|
-
case 'column':
|
|
12
|
-
return 'COLUMN'
|
|
13
|
-
case 'view':
|
|
14
|
-
return 'VIEW'
|
|
15
|
-
case 'function':
|
|
16
|
-
return 'FUNCTION'
|
|
17
|
-
case 'type':
|
|
18
|
-
return 'TYPE'
|
|
19
|
-
default:
|
|
20
|
-
return target.toUpperCase()
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
function qualifiedName(ctx: TagContext): string {
|
|
25
|
-
if (ctx.target === 'column') {
|
|
26
|
-
return `"${ctx.objectName}"."${ctx.columnName}"`
|
|
27
|
-
}
|
|
28
|
-
return `"${ctx.objectName}"`
|
|
29
|
-
}
|
|
2
|
+
import { escapeString, quoteIdentifier } from '@sqldoc/core'
|
|
30
3
|
|
|
31
4
|
const plugin: NamespacePlugin = {
|
|
32
5
|
apiVersion: 1,
|
|
@@ -49,45 +22,80 @@ const plugin: NamespacePlugin = {
|
|
|
49
22
|
},
|
|
50
23
|
|
|
51
24
|
onTag(ctx: TagContext): TagOutput | undefined {
|
|
52
|
-
const { tag } = ctx
|
|
53
|
-
const
|
|
54
|
-
const
|
|
55
|
-
|
|
56
|
-
|
|
25
|
+
const { tag, target, objectName, columnName, dialect } = ctx
|
|
26
|
+
const q = (name: string) => quoteIdentifier(name, dialect)
|
|
27
|
+
const esc = (s: string) => escapeString(s, dialect)
|
|
28
|
+
|
|
29
|
+
const docTarget = target === 'column' ? { object: objectName, column: columnName } : { object: objectName }
|
|
57
30
|
|
|
31
|
+
// Determine the deprecation message and docs value
|
|
32
|
+
let message: string
|
|
33
|
+
let docsValue: string
|
|
58
34
|
switch (tag.name) {
|
|
59
35
|
case '$self':
|
|
60
|
-
case null:
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
columns: [{ header: 'Status', ...docTarget, value: 'Deprecated' }],
|
|
65
|
-
},
|
|
66
|
-
}
|
|
67
|
-
}
|
|
36
|
+
case null:
|
|
37
|
+
message = 'DEPRECATED'
|
|
38
|
+
docsValue = 'Deprecated'
|
|
39
|
+
break
|
|
68
40
|
case 'replace': {
|
|
69
41
|
const args = tag.args as unknown[]
|
|
70
42
|
const newName = args[0] as string
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
columns: [{ header: 'Status', ...docTarget, value: `Deprecated → ${newName}` }],
|
|
75
|
-
},
|
|
76
|
-
}
|
|
43
|
+
message = `DEPRECATED: use ${newName} instead`
|
|
44
|
+
docsValue = `Deprecated \u2192 ${newName}`
|
|
45
|
+
break
|
|
77
46
|
}
|
|
78
47
|
case 'remove': {
|
|
79
48
|
const args = tag.args as unknown[]
|
|
80
49
|
const date = args[0] as string
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
columns: [{ header: 'Status', ...docTarget, value: `Remove after ${date}` }],
|
|
85
|
-
},
|
|
86
|
-
}
|
|
50
|
+
message = `DEPRECATED: scheduled for removal after ${date}`
|
|
51
|
+
docsValue = `Remove after ${date}`
|
|
52
|
+
break
|
|
87
53
|
}
|
|
88
54
|
default:
|
|
89
55
|
return undefined
|
|
90
56
|
}
|
|
57
|
+
|
|
58
|
+
const docs = {
|
|
59
|
+
columns: [{ header: 'Status', ...docTarget, value: docsValue }],
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Generate dialect-specific SQL
|
|
63
|
+
switch (dialect) {
|
|
64
|
+
case 'postgres': {
|
|
65
|
+
const keyword = target.toUpperCase()
|
|
66
|
+
const name = target === 'column' ? `${q(objectName)}.${q(columnName!)}` : q(objectName)
|
|
67
|
+
return {
|
|
68
|
+
sql: [{ sql: `COMMENT ON ${keyword} ${name} IS ${esc(message)};` }],
|
|
69
|
+
docs,
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
case 'mysql': {
|
|
74
|
+
if (target === 'table') {
|
|
75
|
+
return {
|
|
76
|
+
sql: [{ sql: `ALTER TABLE ${q(objectName)} COMMENT = ${esc(message)};` }],
|
|
77
|
+
docs,
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
if (target === 'column') {
|
|
81
|
+
const colType = ctx.columnType ?? 'TEXT'
|
|
82
|
+
return {
|
|
83
|
+
sql: [
|
|
84
|
+
{
|
|
85
|
+
sql: `ALTER TABLE ${q(objectName)} MODIFY COLUMN ${q(columnName!)} ${colType} COMMENT ${esc(message)};`,
|
|
86
|
+
},
|
|
87
|
+
],
|
|
88
|
+
docs,
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
// MySQL cannot comment on views, functions, or types -- docs-only output
|
|
92
|
+
return { sql: [], docs }
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
case 'sqlite':
|
|
96
|
+
// SQLite has no comment support -- docs-only output
|
|
97
|
+
return { sql: [], docs }
|
|
98
|
+
}
|
|
91
99
|
},
|
|
92
100
|
}
|
|
93
101
|
|
|
@@ -1,139 +0,0 @@
|
|
|
1
|
-
import type { TagContext } from '@sqldoc/core'
|
|
2
|
-
import { describe, expect, it } from 'vitest'
|
|
3
|
-
import plugin from '../index'
|
|
4
|
-
|
|
5
|
-
function makeCtx(overrides: Partial<TagContext> = {}): TagContext {
|
|
6
|
-
return {
|
|
7
|
-
target: 'table',
|
|
8
|
-
objectName: 'users',
|
|
9
|
-
tag: { name: '$self', args: {} },
|
|
10
|
-
namespaceTags: [],
|
|
11
|
-
siblingTags: [],
|
|
12
|
-
fileTags: [],
|
|
13
|
-
astNode: null,
|
|
14
|
-
fileStatements: [],
|
|
15
|
-
config: {},
|
|
16
|
-
filePath: 'test.sql',
|
|
17
|
-
...overrides,
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
describe('ns-deprecated plugin', () => {
|
|
22
|
-
it('exports apiVersion === 1', () => {
|
|
23
|
-
expect(plugin.apiVersion).toBe(1)
|
|
24
|
-
})
|
|
25
|
-
|
|
26
|
-
it('exports name === "deprecated"', () => {
|
|
27
|
-
expect(plugin.name).toBe('deprecated')
|
|
28
|
-
})
|
|
29
|
-
|
|
30
|
-
it('has $self, replace, and remove tag entries', () => {
|
|
31
|
-
expect(plugin.tags).toHaveProperty('$self')
|
|
32
|
-
expect(plugin.tags).toHaveProperty('replace')
|
|
33
|
-
expect(plugin.tags).toHaveProperty('remove')
|
|
34
|
-
})
|
|
35
|
-
|
|
36
|
-
describe('onTag', () => {
|
|
37
|
-
it('@deprecated on a table generates COMMENT ON TABLE', () => {
|
|
38
|
-
const ctx = makeCtx({
|
|
39
|
-
target: 'table',
|
|
40
|
-
objectName: 'users',
|
|
41
|
-
tag: { name: '$self', args: {} },
|
|
42
|
-
})
|
|
43
|
-
const result = plugin.onTag!(ctx) as any
|
|
44
|
-
expect(result.sql).toEqual([{ sql: `COMMENT ON TABLE "users" IS 'DEPRECATED';` }])
|
|
45
|
-
})
|
|
46
|
-
|
|
47
|
-
it('@deprecated with null tag name generates COMMENT ON TABLE', () => {
|
|
48
|
-
const ctx = makeCtx({
|
|
49
|
-
target: 'table',
|
|
50
|
-
objectName: 'users',
|
|
51
|
-
tag: { name: null, args: {} },
|
|
52
|
-
})
|
|
53
|
-
const result = plugin.onTag!(ctx) as any
|
|
54
|
-
expect(result.sql).toEqual([{ sql: `COMMENT ON TABLE "users" IS 'DEPRECATED';` }])
|
|
55
|
-
})
|
|
56
|
-
|
|
57
|
-
it('@deprecated on a column generates COMMENT ON COLUMN with qualified name', () => {
|
|
58
|
-
const ctx = makeCtx({
|
|
59
|
-
target: 'column',
|
|
60
|
-
objectName: 'users',
|
|
61
|
-
columnName: 'email',
|
|
62
|
-
tag: { name: '$self', args: {} },
|
|
63
|
-
})
|
|
64
|
-
const result = plugin.onTag!(ctx) as any
|
|
65
|
-
expect(result.sql).toEqual([{ sql: `COMMENT ON COLUMN "users"."email" IS 'DEPRECATED';` }])
|
|
66
|
-
})
|
|
67
|
-
|
|
68
|
-
it('@deprecated on a view generates COMMENT ON VIEW', () => {
|
|
69
|
-
const ctx = makeCtx({
|
|
70
|
-
target: 'view',
|
|
71
|
-
objectName: 'active_users',
|
|
72
|
-
tag: { name: '$self', args: {} },
|
|
73
|
-
})
|
|
74
|
-
const result = plugin.onTag!(ctx) as any
|
|
75
|
-
expect(result.sql).toEqual([{ sql: `COMMENT ON VIEW "active_users" IS 'DEPRECATED';` }])
|
|
76
|
-
})
|
|
77
|
-
|
|
78
|
-
it('@deprecated on a function generates COMMENT ON FUNCTION', () => {
|
|
79
|
-
const ctx = makeCtx({
|
|
80
|
-
target: 'function',
|
|
81
|
-
objectName: 'get_user',
|
|
82
|
-
tag: { name: '$self', args: {} },
|
|
83
|
-
})
|
|
84
|
-
const result = plugin.onTag!(ctx) as any
|
|
85
|
-
expect(result.sql).toEqual([{ sql: `COMMENT ON FUNCTION "get_user" IS 'DEPRECATED';` }])
|
|
86
|
-
})
|
|
87
|
-
|
|
88
|
-
it('@deprecated on a type generates COMMENT ON TYPE', () => {
|
|
89
|
-
const ctx = makeCtx({
|
|
90
|
-
target: 'type',
|
|
91
|
-
objectName: 'status_enum',
|
|
92
|
-
tag: { name: '$self', args: {} },
|
|
93
|
-
})
|
|
94
|
-
const result = plugin.onTag!(ctx) as any
|
|
95
|
-
expect(result.sql).toEqual([{ sql: `COMMENT ON TYPE "status_enum" IS 'DEPRECATED';` }])
|
|
96
|
-
})
|
|
97
|
-
|
|
98
|
-
it('@deprecated.replace generates COMMENT with replacement suggestion', () => {
|
|
99
|
-
const ctx = makeCtx({
|
|
100
|
-
target: 'table',
|
|
101
|
-
objectName: 'old_users',
|
|
102
|
-
tag: { name: 'replace', args: ['accounts'] },
|
|
103
|
-
})
|
|
104
|
-
const result = plugin.onTag!(ctx) as any
|
|
105
|
-
expect(result.sql).toEqual([{ sql: `COMMENT ON TABLE "old_users" IS 'DEPRECATED: use accounts instead';` }])
|
|
106
|
-
})
|
|
107
|
-
|
|
108
|
-
it('@deprecated.remove generates COMMENT with removal date', () => {
|
|
109
|
-
const ctx = makeCtx({
|
|
110
|
-
target: 'table',
|
|
111
|
-
objectName: 'legacy_data',
|
|
112
|
-
tag: { name: 'remove', args: ['2025-06-01'] },
|
|
113
|
-
})
|
|
114
|
-
const result = plugin.onTag!(ctx) as any
|
|
115
|
-
expect(result.sql).toEqual([
|
|
116
|
-
{ sql: `COMMENT ON TABLE "legacy_data" IS 'DEPRECATED: scheduled for removal after 2025-06-01';` },
|
|
117
|
-
])
|
|
118
|
-
})
|
|
119
|
-
|
|
120
|
-
it('@deprecated.replace on a column generates correct qualified COMMENT', () => {
|
|
121
|
-
const ctx = makeCtx({
|
|
122
|
-
target: 'column',
|
|
123
|
-
objectName: 'users',
|
|
124
|
-
columnName: 'name',
|
|
125
|
-
tag: { name: 'replace', args: ['full_name'] },
|
|
126
|
-
})
|
|
127
|
-
const result = plugin.onTag!(ctx) as any
|
|
128
|
-
expect(result.sql).toEqual([{ sql: `COMMENT ON COLUMN "users"."name" IS 'DEPRECATED: use full_name instead';` }])
|
|
129
|
-
})
|
|
130
|
-
})
|
|
131
|
-
|
|
132
|
-
describe('validation', () => {
|
|
133
|
-
it('@deprecated tags have no validate function (metadata only)', () => {
|
|
134
|
-
expect(plugin.tags.$self!.validate).toBeUndefined()
|
|
135
|
-
expect(plugin.tags.replace.validate).toBeUndefined()
|
|
136
|
-
expect(plugin.tags.remove.validate).toBeUndefined()
|
|
137
|
-
})
|
|
138
|
-
})
|
|
139
|
-
})
|