@ez4/pgmigration 0.26.0 → 0.27.0
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/dist/library.cjs +87 -92
- package/dist/library.d.ts +7 -7
- package/dist/library.mjs +85 -87
- package/dist/main.cjs +128 -123
- package/dist/main.d.ts +1 -1
- package/dist/main.mjs +123 -118
- package/dist/queries/columns.d.ts +20 -0
- package/dist/queries/constraints.d.ts +27 -0
- package/dist/queries/database.d.ts +9 -0
- package/dist/queries/indexes.d.ts +11 -0
- package/dist/queries/relations.d.ts +18 -0
- package/dist/queries/tables.d.ts +14 -0
- package/dist/service/queries.d.ts +1 -1
- package/package.json +11 -13
- package/dist/common/columns.d.ts +0 -18
- package/dist/common/constraints.d.ts +0 -25
- package/dist/common/database.d.ts +0 -7
- package/dist/common/indexes.d.ts +0 -9
- package/dist/common/relations.d.ts +0 -16
- package/dist/common/tables.d.ts +0 -12
package/dist/main.cjs
CHANGED
|
@@ -1,125 +1,130 @@
|
|
|
1
|
-
"use strict";var
|
|
2
|
-
typeof
|
|
3
|
-
|
|
4
|
-
module.exports=
|
|
5
|
-
e)||(0,
|
|
6
|
-
definitions:
|
|
7
|
-
default}`;case"string":return`'${
|
|
8
|
-
|
|
9
|
-
'${
|
|
10
|
-
date":case"time":return"now()";case"uuid":return"gen_random_uuid()"}if(
|
|
11
|
-
null"},"getColumnDefault"),
|
|
12
|
-
Object:case
|
|
13
|
-
return"jsonb";case
|
|
14
|
-
text";case
|
|
15
|
-
e)}},"getColumnType"),
|
|
16
|
-
ecimal","getColumNumberType"),
|
|
17
|
-
date":return e.format;case"date-time":return"timestamptz";default:let
|
|
18
|
-
maxLength;return(0,
|
|
19
|
-
let
|
|
20
|
-
column(
|
|
21
|
-
query:
|
|
22
|
-
|
|
23
|
-
|
|
1
|
+
"use strict";var L=Object.defineProperty;var oe=Object.getOwnPropertyDescriptor;var se=Object.getOwnPropertyNames;var ie=Object.prototype.hasOwnProperty;var m=(e,s)=>L(e,"name",{value:s,configurable:!0});var ae=(e,s)=>{for(var d in s)L(e,d,{get:s[d],enumerable:!0})},ce=(e,s,d,y)=>{if(s&&
|
|
2
|
+
typeof s=="object"||typeof s=="function")for(let u of se(s))!ie.call(e,u)&&u!==d&&
|
|
3
|
+
L(e,u,{get:()=>s[u],enumerable:!(y=oe(s,u))||y.enumerable});return e};var pe=e=>ce(L({},"__esModule",{value:!0}),e);var ye={};ae(ye,{getCreateQueries:()=>re,getDeleteQueries:()=>ne,getUpdateQueries:()=>de});
|
|
4
|
+
module.exports=pe(ye);var V=require("@ez4/pgsql");var X=require("@ez4/database");var x=require("@ez4/schema"),W=require("@ez4/utils");var z=m(e=>!!(e.nullable||e.optional),"isOptionalColumn"),D=m((e,s)=>{if((0,x.isScalarSchema)(
|
|
5
|
+
e)||(0,x.isObjectSchema)(e)||(0,x.isArraySchema)(e)||(0,x.isTupleSchema)(e)){let{
|
|
6
|
+
definitions:d}=e;switch(typeof d?.default){case"boolean":case"number":return`${d.
|
|
7
|
+
default}`;case"string":return`'${d.default}'`;case"object":return`'${JSON.stringify(
|
|
8
|
+
d.default)}'`}}if((0,x.isEnumSchema)(e)){let{definitions:d}=e;if(d?.default)return`\
|
|
9
|
+
'${d.default}'`}if((0,x.isStringSchema)(e)&&s)switch(e.format){case"date-time":case"\
|
|
10
|
+
date":case"time":return"now()";case"uuid":return"gen_random_uuid()"}if(z(e))return"\
|
|
11
|
+
null"},"getColumnDefault"),U=m((e,s)=>{switch(e.type){case x.SchemaType.Array:case x.SchemaType.
|
|
12
|
+
Object:case x.SchemaType.Reference:case x.SchemaType.Union:case x.SchemaType.Tuple:
|
|
13
|
+
return"jsonb";case x.SchemaType.Boolean:return"boolean";case x.SchemaType.Enum:return"\
|
|
14
|
+
text";case x.SchemaType.Number:return me(e,s);case x.SchemaType.String:return ue(
|
|
15
|
+
e)}},"getColumnType"),me=m((e,s)=>e.format==="integer"?s?"bigserial":"bigint":"d\
|
|
16
|
+
ecimal","getColumNumberType"),ue=m(e=>{switch(e.format){case"uuid":case"time":case"\
|
|
17
|
+
date":return e.format;case"date-time":return"timestamptz";default:let s=e.definitions?.
|
|
18
|
+
maxLength;return(0,W.isAnyNumber)(s)?`varchar(${s})`:"text"}},"getColumnTextType");var M;(y=>(y.prepareCreate=m((u,n,p,l)=>{let o=u.table(n).create().missing();for(let t in p.
|
|
19
|
+
properties){let r=p.properties[t],c=l[t]?.type===X.Index.Primary,g=!z(r),a=U(r,c),
|
|
20
|
+
f=D(r,c);o.column(t,a,g,f).missing()}return{query:o.build()}},"prepareCreate"),y.
|
|
21
|
+
prepareRename=m((u,n,p)=>({query:u.table(n).rename(p).existing().build()}),"prep\
|
|
22
|
+
areRename"),y.prepareDelete=m((u,n)=>({query:u.table(n).drop().existing().cascade().
|
|
23
|
+
build()}),"prepareDelete")))(M||={});var J=require("@ez4/database");var S=m((e,s)=>{let[d]=e.select().rawColumn("1").from("pg_constraint").where({conname:e.
|
|
24
|
+
rawString(s)}).build();return d},"getCheckConstraintQuery"),G=m((e,s,d)=>{let[y]=e.
|
|
24
25
|
select().rawColumn("1").from("information_schema.columns").where({column_name:e.
|
|
25
|
-
rawString(
|
|
26
|
-
|
|
27
|
-
return{query:
|
|
28
|
-
let{update:
|
|
29
|
-
definitions?.update?.default,
|
|
30
|
-
existing().column(i);
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
t).alter().existing().rename(r
|
|
34
|
-
"
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
if(
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
c.
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
{
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
(
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
(
|
|
117
|
-
|
|
118
|
-
push(...
|
|
119
|
-
|
|
120
|
-
n
|
|
121
|
-
n.
|
|
122
|
-
push(...
|
|
123
|
-
|
|
124
|
-
|
|
26
|
+
rawString(d),table_name:e.rawString(s)}).build();return y},"getCheckColumnQuery");var j;(u=>(u.prepareCreate=m((n,p,l,o)=>{let t=n.table(p).alter().existing();for(let r in o){
|
|
27
|
+
let i=o[r],g=l[r]?.type===J.Index.Primary,a=!z(i),f=U(i,g),h=D(i,g);t.add(r,f,a,
|
|
28
|
+
h).missing()}return{query:t.build()}},"prepareCreate"),u.prepareUpdate=m((n,p,l,o,t)=>{
|
|
29
|
+
let r=[];for(let i in t){let{update:c,nested:g}=t[i],a=l.properties[i],h=o[i]?.type===
|
|
30
|
+
J.Index.Primary,N=g?.definitions?.update?.default,q=c?.optional??c?.nullable,R=c?.
|
|
31
|
+
type,k=n.table(p).alter().existing().column(i);R!==void 0&&k.type(U(a,h)),q!==void 0&&
|
|
32
|
+
k.optional(q),N!==void 0&&k.default(D(a,h)),!k.empty&&r.push({check:G(n,p,i),query:k.
|
|
33
|
+
build()})}return r},"prepareUpdate"),u.prepareRename=m((n,p,l)=>{let o=[];for(let t in l){
|
|
34
|
+
let r=l[t],i=n.table(p).alter().existing().rename(t,r);o.push({check:G(n,p,t),query:i.
|
|
35
|
+
build()})}return o},"prepareRename"),u.prepareDelete=m((n,p,l)=>{let o=n.table(p).
|
|
36
|
+
alter().existing();for(let t in l)o.drop(t).existing();return{query:o.build()}},
|
|
37
|
+
"prepareDelete")))(j||={});var P=require("@ez4/schema");var Y=require("@ez4/utils");var _=m((e,s)=>`${v(e,s)}_pk`,"getPrimaryKeyName"),Q=m((e,s)=>`${v(e,s)}_uk`,"ge\
|
|
38
|
+
tUniqueKeyName"),$=m((e,s)=>`${v(e,s)}_sk`,"getSecondaryKeyName"),w=m((e,s)=>`${v(
|
|
39
|
+
e,s)}_fk`,"getRelationName"),b=m((e,s)=>`${v(e,s)}_ck`,"getConstraintName"),v=m(
|
|
40
|
+
(e,s)=>`${e}_${(0,Y.toSnakeCase)(s.replaceAll(":","_"))}`,"getName");var O;(l=>{l.prepareCreate=m((o,t,r)=>{let i=[];for(let c in r){let g=r[c];if(!(0,P.isEnumSchema)(
|
|
41
|
+
g))continue;let a=b(t,c);i.push({check:S(o,a),query:p(o,t,a,c,g.options).build()})}
|
|
42
|
+
return i},"prepareCreate"),l.prepareUpdate=m((o,t,r,i,c)=>{let g=[];for(let a in c){
|
|
43
|
+
let{update:f,create:h,remove:N}=c[a];if(N){let q=i.properties[a];if((0,P.isEnumSchema)(
|
|
44
|
+
q)){let R=b(t,a);g.push({query:n(o,t,R).build()})}}if(f){let q=i.properties[a],R=r.
|
|
45
|
+
properties[a];if((0,P.isEnumSchema)(q)&&(0,P.isEnumSchema)(R)){let k=b(t,a);g.push(
|
|
46
|
+
{query:n(o,t,k).build()},{query:p(o,t,k,a,R.options).build()})}}if(h){let q=r.properties[a];
|
|
47
|
+
if((0,P.isEnumSchema)(q)){let R=b(t,a);g.push({check:S(o,R),query:p(o,t,R,a,q.options).
|
|
48
|
+
build()})}}}return g},"prepareUpdate"),l.prepareRenameTable=m((o,t,r,i)=>{let c=[];
|
|
49
|
+
for(let g in i){let a=i[g];if(!(0,P.isEnumSchema)(a))continue;let f=b(t,g),h=b(r,
|
|
50
|
+
g),N=o.table(r).alter().existing().constraint(f).rename(h);c.push({check:S(o,h),
|
|
51
|
+
query:N.build()})}return c},"prepareRenameTable"),l.prepareRenameColumns=m((o,t,r,i)=>{
|
|
52
|
+
let c=[];for(let g in i){let a=i[g],f=r[a];if(!(0,P.isEnumSchema)(f))continue;let h=b(
|
|
53
|
+
t,g),N=b(t,a),q=o.table(t).alter().existing().constraint(h).rename(N);c.push({check:S(
|
|
54
|
+
o,N),query:q.build()})}return c},"prepareRenameColumns"),l.prepareDelete=m((o,t,r)=>{
|
|
55
|
+
let i=[];for(let c in r){let g=r[c];if(!(0,P.isEnumSchema)(g))continue;let a=b(t,
|
|
56
|
+
c);i.push({query:n(o,t,a).build()})}return i},"prepareDelete");let n=m((o,t,r)=>o.
|
|
57
|
+
table(t).alter().existing().constraint(r).drop().existing(),"getDeleteQuery"),p=m(
|
|
58
|
+
(o,t,r,i,c)=>{let g=c.map(({value:a})=>`${a}`);return o.table(t).alter().existing().
|
|
59
|
+
constraint(r).check({[i]:{isIn:g}})},"getCreateQuery")})(O||={});var Z=require("@ez4/pgclient/library"),ee=require("@ez4/schema"),E=require("@ez4/database");var I;(p=>{p.prepareCreate=m((l,o,t,r)=>{let i=[];for(let c in r){let g=r[c];if(g.
|
|
60
|
+
targetIndex===E.Index.Primary)continue;let{[g.targetColumn]:a}=t.properties,f=w(
|
|
61
|
+
o,c),h=!!(0,ee.IsNullishSchema)(a);i.push({check:S(l,f),query:n(l,o,f,g,h).build()})}
|
|
62
|
+
return i},"prepareCreate"),p.prepareUpdate=m((l,o,t,r)=>{let i=[];for(let c in t){
|
|
63
|
+
let g=t[c];if(g.targetIndex===E.Index.Primary)continue;let a=r[g.targetColumn]?.
|
|
64
|
+
update,f=a?.optional??a?.nullable;if(f===void 0)continue;let h=w(o,c);i.push({query:u(
|
|
65
|
+
l,o,h).build()},{query:n(l,o,h,g,f).build()})}return i},"prepareUpdate"),p.prepareRename=
|
|
66
|
+
m((l,o,t,r)=>{let i=[];for(let c in r){if(r[c].targetIndex===E.Index.Primary)continue;
|
|
67
|
+
let a=w(o,c),f=w(t,c),h=l.table(t).alter().existing().constraint(a).rename(f);i.
|
|
68
|
+
push({check:S(l,f),query:h.build()})}return i},"prepareRename"),p.prepareDelete=
|
|
69
|
+
m((l,o,t)=>{let r=[];for(let i in t){if(t[i].targetIndex===E.Index.Primary)continue;
|
|
70
|
+
let g=w(o,i);r.push({query:u(l,o,g).build()})}return r},"prepareDelete");let u=m(
|
|
71
|
+
(l,o,t)=>l.table(o).alter().existing().constraint(t).drop().existing(),"getDelet\
|
|
72
|
+
eQuery"),n=m((l,o,t,r,i)=>{let{sourceTable:c,sourceColumn:g,targetColumn:a}=r,f=(0,Z.getTableName)(
|
|
73
|
+
c),h=l.table(o).alter().existing().constraint(t).foreign(a,f,[g]);return i?h.delete().
|
|
74
|
+
null():h.delete().cascade(),h.update().cascade(),h},"getCreateQuery")})(I||={});var K=require("@ez4/schema"),C=require("@ez4/database");var T;(u=>{u.prepareCreate=m((n,p,l,o,t)=>{let r={constraints:[],indexes:[]};for(let i in o){
|
|
75
|
+
let{columns:c,type:g}=o[i];switch(g){default:throw new Error("Unsupported index \
|
|
76
|
+
type.");case C.Index.Primary:{let a=_(p,i),f=n.table(p).alter().existing().constraint(
|
|
77
|
+
a).primary(c);r.constraints.push({check:S(n,a),query:f.build()});break}case C.Index.
|
|
78
|
+
Unique:{let a=Q(p,i),f=n.table(p).alter().existing().constraint(a).unique(c);r.constraints.
|
|
79
|
+
push({check:S(n,a),query:f.build()});break}case C.Index.Secondary:{let a=$(p,i),
|
|
80
|
+
f=y(c,l),h=n.index(a).create(p,c).type(f).missing();t&&h.concurrent(),r.indexes.
|
|
81
|
+
push({query:h.build()});break}}}return r},"prepareCreate"),u.prepareRename=m((n,p,l,o)=>{
|
|
82
|
+
let t={constraints:[],indexes:[]};for(let r in o){let{type:i}=o[r];switch(i){default:
|
|
83
|
+
throw new Error("Unsupported index type.");case C.Index.Primary:{let c=_(p,r),g=_(
|
|
84
|
+
l,r),a=n.table(l).alter().existing().constraint(c).rename(g);t.constraints.push(
|
|
85
|
+
{check:S(n,g),query:a.build()});break}case C.Index.Unique:{let c=Q(p,r),g=Q(l,r),
|
|
86
|
+
a=n.table(l).alter().existing().constraint(c).rename(g);t.constraints.push({check:S(
|
|
87
|
+
n,g),query:a.build()});break}case C.Index.Secondary:{let c=$(p,r),g=$(l,r),a=n.index(
|
|
88
|
+
c).rename(g).existing();t.indexes.push({query:a.build()});break}}}return t},"pre\
|
|
89
|
+
pareRename"),u.prepareDelete=m((n,p,l)=>{let o={constraints:[],indexes:[]};for(let t in l){
|
|
90
|
+
let{type:r}=l[t];switch(r){default:throw new Error("Unsupported index type.");case C.Index.
|
|
91
|
+
Primary:{let i=_(p,t),c=n.table(p).alter().existing().constraint(i).drop().existing();
|
|
92
|
+
o.constraints.push({query:c.build()});break}case C.Index.Unique:{let i=Q(p,t),c=n.
|
|
93
|
+
table(p).alter().existing().constraint(i).drop().existing();o.constraints.push({
|
|
94
|
+
query:c.build()});break}case C.Index.Secondary:{let i=$(p,t),c=n.index(i).drop().
|
|
95
|
+
existing().concurrent();o.indexes.push({query:c.build()});break}}}return o},"pre\
|
|
96
|
+
pareDelete");let y=m((n,p)=>{if(n.length===1){let[l]=n;switch(p.properties[l]?.type){case K.SchemaType.
|
|
97
|
+
Object:case K.SchemaType.Union:case K.SchemaType.Array:case K.SchemaType.Tuple:return"\
|
|
98
|
+
GIN"}}return"BTREE"},"getIndexType")})(T||={});var B=require("@ez4/utils"),F=require("@ez4/pgclient/library"),H=require("@ez4/schema");var te=m((e,s)=>(0,B.deepCompareObject)(e,s,{onRename:m((d,y)=>!(0,B.isAnyObject)(
|
|
99
|
+
d)||!(0,B.isAnyObject)(y)?!1:(0,F.isTableMetadata)(d)&&(0,F.isTableMetadata)(y)?
|
|
100
|
+
le(d.schema,y.schema):(0,H.isAnySchema)(d)&&(0,H.isAnySchema)(y)?ge(d,y):!1,"onR\
|
|
101
|
+
ename")}),"getTableRepositoryChanges"),le=m((e,s)=>(0,B.deepEqual)(Object.keys(e.
|
|
102
|
+
properties),Object.keys(s.properties)),"canRenameTable"),ge=m((e,s)=>(0,B.deepEqual)(
|
|
103
|
+
e,s,{depth:1,include:{type:!0,definitions:!0,nullable:!0,format:!0}}),"canRename\
|
|
104
|
+
Column");var re=m(e=>{let s=new V.SqlBuilder,d={tables:[],constraints:[],relations:[],indexes:[]};
|
|
105
|
+
for(let y in e){let{name:u,schema:n,indexes:p,relations:l}=e[y];d.tables.push(M.
|
|
106
|
+
prepareCreate(s,u,n,p)),d.constraints.push(...O.prepareCreate(s,u,n.properties)),
|
|
107
|
+
d.relations.push(...I.prepareCreate(s,u,n,l)),A(d,T.prepareCreate(s,u,n,p,!1))}return d},
|
|
108
|
+
"getCreateQueries"),de=m((e,s)=>{let d=te(e,s),y=new V.SqlBuilder,u={tables:[],constraints:[],
|
|
109
|
+
relations:[],indexes:[]};if(d.create&&A(u,re(d.create)),d.rename)for(let n in d.
|
|
110
|
+
rename){let p=d.rename[n],l=e[p].indexes,o=e[p].relations,t=e[p].schema;u.tables.
|
|
111
|
+
push(M.prepareRename(y,n,p)),u.constraints.push(...O.prepareRenameTable(y,n,p,t.
|
|
112
|
+
properties)),u.relations.push(...I.prepareRename(y,n,p,o)),A(u,T.prepareRename(y,
|
|
113
|
+
n,p,l))}if(d.nested)for(let n in d.nested){let{nested:p}=d.nested[n];if(!p)continue;
|
|
114
|
+
let{schema:l,relations:o,indexes:t}=p,r=l?.nested?.properties,i=e[n].indexes,c=e[n].
|
|
115
|
+
relations,g=s[n].schema,a=e[n].schema;r?.create&&(u.tables.push(j.prepareCreate(
|
|
116
|
+
y,n,i,r.create)),u.constraints.push(...O.prepareCreate(y,n,r.create))),r?.nested&&
|
|
117
|
+
(u.tables.push(...j.prepareUpdate(y,n,a,i,r.nested)),u.constraints.push(...O.prepareUpdate(
|
|
118
|
+
y,n,a,g,r.nested)),u.relations.push(...I.prepareUpdate(y,n,c,r.nested))),r?.rename&&
|
|
119
|
+
(u.tables.push(...j.prepareRename(y,n,r.rename)),u.constraints.push(...O.prepareRenameColumns(
|
|
120
|
+
y,n,a.properties,r.rename))),r?.remove&&(u.tables.push(j.prepareDelete(y,n,r.remove)),
|
|
121
|
+
u.constraints.push(...O.prepareDelete(y,n,r.remove))),t?.create&&A(u,T.prepareCreate(
|
|
122
|
+
y,n,a,t.create,!0)),t?.remove&&A(u,T.prepareDelete(y,n,t.remove)),o?.create&&u.relations.
|
|
123
|
+
push(...I.prepareCreate(y,n,a,o.create)),o?.remove&&u.relations.push(...I.prepareDelete(
|
|
124
|
+
y,n,o.remove))}return d.remove&&A(u,ne(d.remove)),u},"getUpdateQueries"),ne=m(e=>{
|
|
125
|
+
let s=new V.SqlBuilder,d={tables:[],constraints:[],relations:[],indexes:[]};for(let y in e){
|
|
126
|
+
let{name:u}=e[y];d.tables.push(M.prepareDelete(s,u))}return d},"getDeleteQueries"),
|
|
127
|
+
A=m((e,s)=>{s.indexes&&e.indexes.push(...s.indexes),s.constraints&&e.constraints.
|
|
128
|
+
push(...s.constraints),s.relations&&e.relations.push(...s.relations),s.tables&&e.
|
|
129
|
+
tables.push(...s.tables)},"combineMigrationQueries");0&&(module.exports={getCreateQueries,getDeleteQueries,getUpdateQueries});
|
|
125
130
|
//# sourceMappingURL=main.cjs.map
|
package/dist/main.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from './service/queries
|
|
1
|
+
export * from './service/queries';
|