@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/main.mjs CHANGED
@@ -1,120 +1,125 @@
1
- var pe=Object.defineProperty;var a=(e,t)=>pe(e,"name",{value:t,configurable:!0});import{SqlBuilder as $}from"@ez4/pgsql";import{Index as be}from"@ez4/database";import{isArraySchema as ue,isEnumSchema as le,isObjectSchema as ge,isScalarSchema as de,
2
- isStringSchema as ye,isTupleSchema as fe,SchemaType as y}from"@ez4/schema";import{
3
- isAnyNumber as he}from"@ez4/utils";var R=a(e=>!!(e.nullable||e.optional),"isOptionalColumn"),P=a((e,t)=>{if(de(e)||
4
- ge(e)||ue(e)||fe(e)){let{definitions:n}=e;switch(typeof n?.default){case"boolean":case"\
5
- number":return`${n.default}`;case"string":return`'${n.default}'`;case"object":return`\
6
- '${JSON.stringify(n.default)}'`}}if(le(e)){let{definitions:n}=e;if(n?.default)return`\
7
- '${n.default}'`}if(ye(e)&&t)switch(e.format){case"date-time":case"date":case"tim\
8
- e":return"now()";case"uuid":return"gen_random_uuid()"}if(R(e))return"null"},"get\
9
- ColumnDefault"),N=a((e,t)=>{switch(e.type){case y.Array:case y.Object:case y.Reference:case y.
10
- Union:case y.Tuple:return"jsonb";case y.Boolean:return"boolean";case y.Enum:return"\
11
- text";case y.Number:return xe(e,t);case y.String:return Se(e)}},"getColumnType"),
12
- xe=a((e,t)=>e.format==="integer"?t?"bigserial":"bigint":"decimal","getColumNumbe\
13
- rType"),Se=a(e=>{switch(e.format){case"uuid":case"time":case"date":return e.format;case"\
14
- date-time":return"timestamptz";default:let t=e.definitions?.maxLength;return he(
15
- t)?`varchar(${t})`:"text"}},"getColumnTextType");var v=a((e,t,n,s)=>{let o=e.table(t).create().missing();for(let r in n.properties){
16
- let i=n.properties[r],p=s[r]?.type===be.Primary,c=!R(i),u=N(i,p),l=P(i,p);o.column(
17
- r,u,c,l).missing()}return{query:o.build()}},"prepareCreateTable"),E=a((e,t,n)=>({
18
- query:e.table(t).rename(n).existing().build()}),"prepareRenameTable"),M=a((e,t)=>({
19
- query:e.table(t).drop().existing().cascade().build()}),"prepareDeleteTable");import{Index as K}from"@ez4/database";var g=a((e,t)=>{let[n]=e.select().rawColumn("1").from("pg_constraint").where({conname:e.
20
- rawString(t)}).build();return n},"getCheckConstraintQuery"),A=a((e,t,n)=>{let[s]=e.
1
+ var X=Object.defineProperty;var m=(e,c)=>X(e,"name",{value:c,configurable:!0});import{SqlBuilder as L}from"@ez4/pgsql";import{Index as ae}from"@ez4/database";import{isArraySchema as Y,isEnumSchema as Z,isObjectSchema as ee,isScalarSchema as te,
2
+ isStringSchema as re,isTupleSchema as ne,SchemaType as C}from"@ez4/schema";import{
3
+ isAnyNumber as oe}from"@ez4/utils";var A=m(e=>!!(e.nullable||e.optional),"isOptionalColumn"),z=m((e,c)=>{if(te(e)||
4
+ ee(e)||Y(e)||ne(e)){let{definitions:d}=e;switch(typeof d?.default){case"boolean":case"\
5
+ number":return`${d.default}`;case"string":return`'${d.default}'`;case"object":return`\
6
+ '${JSON.stringify(d.default)}'`}}if(Z(e)){let{definitions:d}=e;if(d?.default)return`\
7
+ '${d.default}'`}if(re(e)&&c)switch(e.format){case"date-time":case"date":case"tim\
8
+ e":return"now()";case"uuid":return"gen_random_uuid()"}if(A(e))return"null"},"get\
9
+ ColumnDefault"),D=m((e,c)=>{switch(e.type){case C.Array:case C.Object:case C.Reference:case C.
10
+ Union:case C.Tuple:return"jsonb";case C.Boolean:return"boolean";case C.Enum:return"\
11
+ text";case C.Number:return se(e,c);case C.String:return ie(e)}},"getColumnType"),
12
+ se=m((e,c)=>e.format==="integer"?c?"bigserial":"bigint":"decimal","getColumNumbe\
13
+ rType"),ie=m(e=>{switch(e.format){case"uuid":case"time":case"date":return e.format;case"\
14
+ date-time":return"timestamptz";default:let c=e.definitions?.maxLength;return oe(
15
+ c)?`varchar(${c})`:"text"}},"getColumnTextType");var U;(y=>(y.prepareCreate=m((g,n,p,u)=>{let o=g.table(n).create().missing();for(let t in p.
16
+ properties){let r=p.properties[t],a=u[t]?.type===ae.Primary,l=!A(r),i=D(r,a),f=z(
17
+ r,a);o.column(t,i,l,f).missing()}return{query:o.build()}},"prepareCreate"),y.prepareRename=
18
+ m((g,n,p)=>({query:g.table(n).rename(p).existing().build()}),"prepareRename"),y.
19
+ prepareDelete=m((g,n)=>({query:g.table(n).drop().existing().cascade().build()}),
20
+ "prepareDelete")))(U||={});import{Index as V}from"@ez4/database";var x=m((e,c)=>{let[d]=e.select().rawColumn("1").from("pg_constraint").where({conname:e.
21
+ rawString(c)}).build();return d},"getCheckConstraintQuery"),K=m((e,c,d)=>{let[y]=e.
21
22
  select().rawColumn("1").from("information_schema.columns").where({column_name:e.
22
- rawString(n),table_name:e.rawString(t)}).build();return s},"getCheckColumnQuery");var L=a((e,t,n,s)=>{let o=e.table(t).alter().existing();for(let r in s){let i=s[r],
23
- p=n[r]?.type===K.Primary,c=!R(i),u=N(i,p),l=P(i,p);o.add(r,u,c,l).missing()}return{
24
- query:o.build()}},"prepareCreateColumns"),V=a((e,t,n,s,o)=>{let r=[];for(let i in o){
25
- let{update:m,nested:p}=o[i],c=n.properties[i],l=s[i]?.type===K.Primary,S=p?.definitions?.
26
- update?.default,T=m?.optional??m?.nullable,x=m?.type,C=e.table(t).alter().existing().
27
- column(i);x!==void 0&&C.type(N(c,l)),T!==void 0&&C.optional(T),S!==void 0&&C.default(
28
- P(c,l)),!C.empty&&r.push({check:A(e,t,i),query:C.build()})}return r},"prepareUpd\
29
- ateColumns"),G=a((e,t,n)=>{let s=[];for(let o in n){let r=n[o],i=e.table(t).alter().
30
- existing().rename(o,r);s.push({check:A(e,t,o),query:i.build()})}return s},"prepa\
31
- reRenameColumns"),J=a((e,t,n)=>{let s=e.table(t).alter().existing();for(let o in n)
32
- s.drop(o).existing();return{query:s.build()}},"prepareDeleteColumns");import{isEnumSchema as h}from"@ez4/schema";import{toSnakeCase as qe}from"@ez4/utils";var I=a((e,t)=>`${j(e,t)}_pk`,"getPrimaryKeyName"),O=a((e,t)=>`${j(e,t)}_uk`,"ge\
33
- tUniqueKeyName"),k=a((e,t)=>`${j(e,t)}_sk`,"getSecondaryKeyName"),b=a((e,t)=>`${j(
34
- e,t)}_fk`,"getRelationName"),d=a((e,t)=>`${j(e,t)}_ck`,"getConstraintName"),j=a(
35
- (e,t)=>`${e}_${qe(t.replaceAll(":","_"))}`,"getName");var D=a((e,t,n)=>{let s=[];for(let o in n){let r=n[o];if(!h(r))continue;let i=d(
36
- t,o);s.push({check:g(e,i),query:z(e,t,i,o,r.options).build()})}return s},"prepar\
37
- eCreateConstraints"),F=a((e,t,n,s,o)=>{let r=[];for(let i in o){let{update:m,create:p,
38
- remove:c}=o[i];if(c){let u=s.properties[i];if(h(u)){let l=d(t,i);r.push({query:Q(
39
- e,t,l).build()})}}if(m){let u=s.properties[i],l=n.properties[i];if(h(u)&&h(l)){let S=d(
40
- t,i);r.push({query:Q(e,t,S).build()},{query:z(e,t,S,i,l.options).build()})}}if(p){
41
- let u=n.properties[i];if(h(u)){let l=d(t,i);r.push({check:g(e,l),query:z(e,t,l,i,
42
- u.options).build()})}}}return r},"prepareUpdateConstraints"),H=a((e,t,n,s)=>{let o=[];
43
- for(let r in s){let i=s[r];if(!h(i))continue;let m=d(t,r),p=d(n,r),c=e.table(n).
44
- alter().existing().constraint(m).rename(p);o.push({check:g(e,p),query:c.build()})}
45
- return o},"prepareRenameConstraints"),W=a((e,t,n,s)=>{let o=[];for(let r in s){let i=s[r],
46
- m=n[i];if(!h(m))continue;let p=d(t,r),c=d(t,i),u=e.table(t).alter().existing().constraint(
47
- p).rename(c);o.push({check:g(e,c),query:u.build()})}return o},"prepareRenameCons\
48
- traintColumns"),X=a((e,t,n)=>{let s=[];for(let o in n){let r=n[o];if(!h(r))continue;
49
- let i=d(t,o);s.push({query:Q(e,t,i).build()})}return s},"prepareDeleteConstraint\
50
- s"),Q=a((e,t,n)=>e.table(t).alter().existing().constraint(n).drop().existing(),"\
51
- getDeleteConstraintQuery"),z=a((e,t,n,s,o)=>{let r=o.map(({value:i})=>`${i}`);return e.
52
- table(t).alter().existing().constraint(n).check({[s]:{isIn:r}})},"getCreateConst\
53
- raintQuery");import{getTableName as Ce}from"@ez4/pgclient/library";import{IsNullishSchema as Re}from"@ez4/schema";
54
- import{Index as B}from"@ez4/database";var U=a((e,t,n,s)=>{let o=[];for(let r in s){let i=s[r];if(i.targetIndex===B.Primary)
55
- continue;let{[i.targetColumn]:m}=n.properties,p=b(t,r),c=!!Re(m);o.push({check:g(
56
- e,p),query:ne(e,t,p,i,c).build()})}return o},"prepareCreateRelations"),Y=a((e,t,n,s)=>{
57
- let o=[];for(let r in n){let i=n[r];if(i.targetIndex===B.Primary)continue;let m=s[i.
58
- targetColumn]?.update,p=m?.optional??m?.nullable;if(p===void 0)continue;let c=b(
59
- t,r);o.push({query:te(e,t,c).build()},{query:ne(e,t,c,i,p).build()})}return o},"\
60
- prepareUpdateRelations"),Z=a((e,t,n,s)=>{let o=[];for(let r in s){if(s[r].targetIndex===
61
- B.Primary)continue;let m=b(t,r),p=b(n,r),c=e.table(n).alter().existing().constraint(
62
- m).rename(p);o.push({check:g(e,p),query:c.build()})}return o},"prepareRenameRela\
63
- tions"),ee=a((e,t,n)=>{let s=[];for(let o in n){if(n[o].targetIndex===B.Primary)
64
- continue;let i=b(t,o);s.push({query:te(e,t,i).build()})}return s},"prepareDelete\
65
- Relations"),te=a((e,t,n)=>e.table(t).alter().existing().constraint(n).drop().existing(),
66
- "getDeleteRelationQuery"),ne=a((e,t,n,s,o)=>{let{sourceAlias:r,sourceColumn:i,targetColumn:m}=s,
67
- p=Ce(r),c=e.table(t).alter().existing().constraint(n).foreign(m,p,[i]);return o?
68
- c.delete().null():c.delete().cascade(),c.update().cascade(),c},"getCreateRelatio\
69
- nQuery");import{SchemaType as w}from"@ez4/schema";import{Index as f}from"@ez4/database";var _=a((e,t,n,s,o)=>{let r={constraints:[],indexes:[]};for(let i in s){let{columns:m,
70
- type:p}=s[i];switch(p){default:throw new Error("Unsupported index type.");case f.
71
- Primary:{let c=I(t,i),u=e.table(t).alter().existing().constraint(c).primary(m);r.
72
- constraints.push({check:g(e,c),query:u.build()});break}case f.Unique:{let c=O(t,
73
- i),u=e.table(t).alter().existing().constraint(c).unique(m);r.constraints.push({check:g(
74
- e,c),query:u.build()});break}case f.Secondary:{let c=k(t,i),u=Pe(m,n),l=e.index(
75
- c).create(t,m).type(u).missing();o&&l.concurrent(),r.indexes.push({query:l.build()});
76
- break}}}return r},"prepareCreateIndexes"),re=a((e,t,n,s)=>{let o={constraints:[],
77
- indexes:[]};for(let r in s){let{type:i}=s[r];switch(i){default:throw new Error("\
78
- Unsupported index type.");case f.Primary:{let m=I(t,r),p=I(n,r),c=e.table(n).alter().
79
- existing().constraint(m).rename(p);o.constraints.push({check:g(e,p),query:c.build()});
80
- break}case f.Unique:{let m=O(t,r),p=O(n,r),c=e.table(n).alter().existing().constraint(
81
- m).rename(p);o.constraints.push({check:g(e,p),query:c.build()});break}case f.Secondary:{
82
- let m=k(t,r),p=k(n,r),c=e.index(m).rename(p).existing();o.indexes.push({query:c.
83
- build()});break}}}return o},"prepareRenameIndexes"),oe=a((e,t,n)=>{let s={constraints:[],
84
- indexes:[]};for(let o in n){let{type:r}=n[o];switch(r){default:throw new Error("\
85
- Unsupported index type.");case f.Primary:{let i=I(t,o),m=e.table(t).alter().existing().
86
- constraint(i).drop().existing();s.constraints.push({query:m.build()});break}case f.
87
- Unique:{let i=O(t,o),m=e.table(t).alter().existing().constraint(i).drop().existing();
88
- s.constraints.push({query:m.build()});break}case f.Secondary:{let i=k(t,o),m=e.index(
89
- i).drop().existing().concurrent();s.indexes.push({query:m.build()});break}}}return s},
90
- "prepareDeleteIndexes"),Pe=a((e,t)=>{if(e.length===1){let[n]=e;switch(t.properties[n]?.
91
- type){case w.Object:case w.Union:case w.Array:case w.Tuple:return"GIN"}}return"B\
92
- TREE"},"getIndexType");import{deepEqual as ce,deepCompareObject as Ne,isAnyObject as se}from"@ez4/utils";
93
- import{isRepositoryTable as ie}from"@ez4/pgclient/library";import{isAnySchema as ae}from"@ez4/schema";var me=a((e,t)=>Ne(e,t,{onRename:a((n,s)=>!se(n)||!se(s)?!1:ie(n)&&ie(s)?Ie(n.schema,
94
- s.schema):ae(n)&&ae(s)?Oe(n,s):!1,"onRename")}),"getTableRepositoryChanges"),Ie=a(
95
- (e,t)=>ce(Object.keys(e.properties),Object.keys(t.properties)),"canRenameTable"),
96
- Oe=a((e,t)=>ce(e,t,{depth:1,include:{type:!0,definitions:!0,nullable:!0,format:!0}}),
97
- "canRenameColumn");var ke=a(e=>{let t=new $,n={tables:[],constraints:[],relations:[],indexes:[]};for(let s in e){
98
- let{name:o,schema:r,indexes:i,relations:m}=e[s];q(n,_(t,o,r,i,!1)),n.tables.push(
99
- v(t,o,r,i)),n.constraints.push(...D(t,o,r.properties)),n.relations.push(...U(t,o,
100
- r,m))}return n},"getCreateQueries"),Nt=a((e,t)=>{let n=me(e,t),s=new $,o={tables:[],
101
- constraints:[],relations:[],indexes:[]};if(n.create&&q(o,ke(n.create)),n.rename)
102
- for(let r in n.rename){let i=n.rename[r],m=e[i].indexes,p=e[i].relations,c=e[i].
103
- schema;q(o,re(s,r,i,m)),o.constraints.push(...H(s,r,i,c.properties)),o.relations.
104
- push(...Z(s,r,i,p)),o.tables.push(E(s,r,i))}if(n.nested)for(let r in n.nested){let{
105
- nested:i}=n.nested[r];if(!i)continue;let{schema:m,relations:p,indexes:c}=i,u=m?.
106
- nested?.properties,l=e[r].indexes,S=e[r].relations,T=t[r].schema,x=e[r].schema;u?.
107
- create&&(o.tables.push(L(s,r,l,u.create)),o.constraints.push(...D(s,r,u.create))),
108
- u?.nested&&(o.tables.push(...V(s,r,x,l,u.nested)),o.constraints.push(...F(s,r,x,
109
- T,u.nested)),o.relations.push(...Y(s,r,S,u.nested))),u?.rename&&(o.tables.push(...G(
110
- s,r,u.rename)),o.constraints.push(...W(s,r,x.properties,u.rename))),u?.remove&&(o.
111
- tables.push(J(s,r,u.remove)),o.constraints.push(...X(s,r,u.remove))),c?.create&&
112
- q(o,_(s,r,x,c.create,!0)),c?.remove&&q(o,oe(s,r,c.remove)),p?.create&&o.relations.
113
- push(...U(s,r,x,p.create)),p?.remove&&o.relations.push(...ee(s,r,p.remove))}return n.
114
- remove&&q(o,je(n.remove)),o},"getUpdateQueries"),je=a(e=>{let t=new $,n={tables:[],
115
- constraints:[],relations:[],indexes:[]};for(let s in e){let{name:o}=e[s];n.tables.
116
- push(M(t,o))}return n},"getDeleteQueries"),q=a((e,t)=>{t.indexes&&e.indexes.push(
117
- ...t.indexes),t.constraints&&e.constraints.push(...t.constraints),t.relations&&e.
118
- relations.push(...t.relations),t.tables&&e.tables.push(...t.tables)},"combineMig\
119
- rationQueries");export{ke as getCreateQueries,je as getDeleteQueries,Nt as getUpdateQueries};
23
+ rawString(d),table_name:e.rawString(c)}).build();return y},"getCheckColumnQuery");var I;(g=>(g.prepareCreate=m((n,p,u,o)=>{let t=n.table(p).alter().existing();for(let r in o){
24
+ let s=o[r],l=u[r]?.type===V.Primary,i=!A(s),f=D(s,l),h=z(s,l);t.add(r,f,i,h).missing()}
25
+ return{query:t.build()}},"prepareCreate"),g.prepareUpdate=m((n,p,u,o,t)=>{let r=[];
26
+ for(let s in t){let{update:a,nested:l}=t[s],i=u.properties[s],h=o[s]?.type===V.Primary,
27
+ P=l?.definitions?.update?.default,S=a?.optional??a?.nullable,b=a?.type,N=n.table(
28
+ p).alter().existing().column(s);b!==void 0&&N.type(D(i,h)),S!==void 0&&N.optional(
29
+ S),P!==void 0&&N.default(z(i,h)),!N.empty&&r.push({check:K(n,p,s),query:N.build()})}
30
+ return r},"prepareUpdate"),g.prepareRename=m((n,p,u)=>{let o=[];for(let t in u){
31
+ let r=u[t],s=n.table(p).alter().existing().rename(t,r);o.push({check:K(n,p,t),query:s.
32
+ build()})}return o},"prepareRename"),g.prepareDelete=m((n,p,u)=>{let o=n.table(p).
33
+ alter().existing();for(let t in u)o.drop(t).existing();return{query:o.build()}},
34
+ "prepareDelete")))(I||={});import{isEnumSchema as k}from"@ez4/schema";import{toSnakeCase as ce}from"@ez4/utils";var M=m((e,c)=>`${$(e,c)}_pk`,"getPrimaryKeyName"),_=m((e,c)=>`${$(e,c)}_uk`,"ge\
35
+ tUniqueKeyName"),Q=m((e,c)=>`${$(e,c)}_sk`,"getSecondaryKeyName"),j=m((e,c)=>`${$(
36
+ e,c)}_fk`,"getRelationName"),q=m((e,c)=>`${$(e,c)}_ck`,"getConstraintName"),$=m(
37
+ (e,c)=>`${e}_${ce(c.replaceAll(":","_"))}`,"getName");var O;(u=>{u.prepareCreate=m((o,t,r)=>{let s=[];for(let a in r){let l=r[a];if(!k(
38
+ l))continue;let i=q(t,a);s.push({check:x(o,i),query:p(o,t,i,a,l.options).build()})}
39
+ return s},"prepareCreate"),u.prepareUpdate=m((o,t,r,s,a)=>{let l=[];for(let i in a){
40
+ let{update:f,create:h,remove:P}=a[i];if(P){let S=s.properties[i];if(k(S)){let b=q(
41
+ t,i);l.push({query:n(o,t,b).build()})}}if(f){let S=s.properties[i],b=r.properties[i];
42
+ if(k(S)&&k(b)){let N=q(t,i);l.push({query:n(o,t,N).build()},{query:p(o,t,N,i,b.options).
43
+ build()})}}if(h){let S=r.properties[i];if(k(S)){let b=q(t,i);l.push({check:x(o,b),
44
+ query:p(o,t,b,i,S.options).build()})}}}return l},"prepareUpdate"),u.prepareRenameTable=
45
+ m((o,t,r,s)=>{let a=[];for(let l in s){let i=s[l];if(!k(i))continue;let f=q(t,l),
46
+ h=q(r,l),P=o.table(r).alter().existing().constraint(f).rename(h);a.push({check:x(
47
+ o,h),query:P.build()})}return a},"prepareRenameTable"),u.prepareRenameColumns=m(
48
+ (o,t,r,s)=>{let a=[];for(let l in s){let i=s[l],f=r[i];if(!k(f))continue;let h=q(
49
+ t,l),P=q(t,i),S=o.table(t).alter().existing().constraint(h).rename(P);a.push({check:x(
50
+ o,P),query:S.build()})}return a},"prepareRenameColumns"),u.prepareDelete=m((o,t,r)=>{
51
+ let s=[];for(let a in r){let l=r[a];if(!k(l))continue;let i=q(t,a);s.push({query:n(
52
+ o,t,i).build()})}return s},"prepareDelete");let n=m((o,t,r)=>o.table(t).alter().
53
+ existing().constraint(r).drop().existing(),"getDeleteQuery"),p=m((o,t,r,s,a)=>{let l=a.
54
+ map(({value:i})=>`${i}`);return o.table(t).alter().existing().constraint(r).check(
55
+ {[s]:{isIn:l}})},"getCreateQuery")})(O||={});import{getTableName as pe}from"@ez4/pgclient/library";import{IsNullishSchema as me}from"@ez4/schema";
56
+ import{Index as v}from"@ez4/database";var B;(p=>{p.prepareCreate=m((u,o,t,r)=>{let s=[];for(let a in r){let l=r[a];if(l.
57
+ targetIndex===v.Primary)continue;let{[l.targetColumn]:i}=t.properties,f=j(o,a),h=!!me(
58
+ i);s.push({check:x(u,f),query:n(u,o,f,l,h).build()})}return s},"prepareCreate"),
59
+ p.prepareUpdate=m((u,o,t,r)=>{let s=[];for(let a in t){let l=t[a];if(l.targetIndex===
60
+ v.Primary)continue;let i=r[l.targetColumn]?.update,f=i?.optional??i?.nullable;if(f===
61
+ void 0)continue;let h=j(o,a);s.push({query:g(u,o,h).build()},{query:n(u,o,h,l,f).
62
+ build()})}return s},"prepareUpdate"),p.prepareRename=m((u,o,t,r)=>{let s=[];for(let a in r){
63
+ if(r[a].targetIndex===v.Primary)continue;let i=j(o,a),f=j(t,a),h=u.table(t).alter().
64
+ existing().constraint(i).rename(f);s.push({check:x(u,f),query:h.build()})}return s},
65
+ "prepareRename"),p.prepareDelete=m((u,o,t)=>{let r=[];for(let s in t){if(t[s].targetIndex===
66
+ v.Primary)continue;let l=j(o,s);r.push({query:g(u,o,l).build()})}return r},"prep\
67
+ areDelete");let g=m((u,o,t)=>u.table(o).alter().existing().constraint(t).drop().
68
+ existing(),"getDeleteQuery"),n=m((u,o,t,r,s)=>{let{sourceTable:a,sourceColumn:l,
69
+ targetColumn:i}=r,f=pe(a),h=u.table(o).alter().existing().constraint(t).foreign(
70
+ i,f,[l]);return s?h.delete().null():h.delete().cascade(),h.update().cascade(),h},
71
+ "getCreateQuery")})(B||={});import{SchemaType as E}from"@ez4/schema";import{Index as R}from"@ez4/database";var w;(g=>{g.prepareCreate=m((n,p,u,o,t)=>{let r={constraints:[],indexes:[]};for(let s in o){
72
+ let{columns:a,type:l}=o[s];switch(l){default:throw new Error("Unsupported index \
73
+ type.");case R.Primary:{let i=M(p,s),f=n.table(p).alter().existing().constraint(
74
+ i).primary(a);r.constraints.push({check:x(n,i),query:f.build()});break}case R.Unique:{
75
+ let i=_(p,s),f=n.table(p).alter().existing().constraint(i).unique(a);r.constraints.
76
+ push({check:x(n,i),query:f.build()});break}case R.Secondary:{let i=Q(p,s),f=y(a,
77
+ u),h=n.index(i).create(p,a).type(f).missing();t&&h.concurrent(),r.indexes.push({
78
+ query:h.build()});break}}}return r},"prepareCreate"),g.prepareRename=m((n,p,u,o)=>{
79
+ let t={constraints:[],indexes:[]};for(let r in o){let{type:s}=o[r];switch(s){default:
80
+ throw new Error("Unsupported index type.");case R.Primary:{let a=M(p,r),l=M(u,r),
81
+ i=n.table(u).alter().existing().constraint(a).rename(l);t.constraints.push({check:x(
82
+ n,l),query:i.build()});break}case R.Unique:{let a=_(p,r),l=_(u,r),i=n.table(u).alter().
83
+ existing().constraint(a).rename(l);t.constraints.push({check:x(n,l),query:i.build()});
84
+ break}case R.Secondary:{let a=Q(p,r),l=Q(u,r),i=n.index(a).rename(l).existing();
85
+ t.indexes.push({query:i.build()});break}}}return t},"prepareRename"),g.prepareDelete=
86
+ m((n,p,u)=>{let o={constraints:[],indexes:[]};for(let t in u){let{type:r}=u[t];switch(r){default:
87
+ throw new Error("Unsupported index type.");case R.Primary:{let s=M(p,t),a=n.table(
88
+ p).alter().existing().constraint(s).drop().existing();o.constraints.push({query:a.
89
+ build()});break}case R.Unique:{let s=_(p,t),a=n.table(p).alter().existing().constraint(
90
+ s).drop().existing();o.constraints.push({query:a.build()});break}case R.Secondary:{
91
+ let s=Q(p,t),a=n.index(s).drop().existing().concurrent();o.indexes.push({query:a.
92
+ build()});break}}}return o},"prepareDelete");let y=m((n,p)=>{if(n.length===1){let[
93
+ u]=n;switch(p.properties[u]?.type){case E.Object:case E.Union:case E.Array:case E.
94
+ Tuple:return"GIN"}}return"BTREE"},"getIndexType")})(w||={});import{deepEqual as H,deepCompareObject as ue,isAnyObject as G}from"@ez4/utils";
95
+ import{isTableMetadata as J}from"@ez4/pgclient/library";import{isAnySchema as F}from"@ez4/schema";var W=m((e,c)=>ue(e,c,{onRename:m((d,y)=>!G(d)||!G(y)?!1:J(d)&&J(y)?le(d.schema,
96
+ y.schema):F(d)&&F(y)?ge(d,y):!1,"onRename")}),"getTableRepositoryChanges"),le=m(
97
+ (e,c)=>H(Object.keys(e.properties),Object.keys(c.properties)),"canRenameTable"),
98
+ ge=m((e,c)=>H(e,c,{depth:1,include:{type:!0,definitions:!0,nullable:!0,format:!0}}),
99
+ "canRenameColumn");var de=m(e=>{let c=new L,d={tables:[],constraints:[],relations:[],indexes:[]};for(let y in e){
100
+ let{name:g,schema:n,indexes:p,relations:u}=e[y];d.tables.push(U.prepareCreate(c,
101
+ g,n,p)),d.constraints.push(...O.prepareCreate(c,g,n.properties)),d.relations.push(
102
+ ...B.prepareCreate(c,g,n,u)),T(d,w.prepareCreate(c,g,n,p,!1))}return d},"getCrea\
103
+ teQueries"),ut=m((e,c)=>{let d=W(e,c),y=new L,g={tables:[],constraints:[],relations:[],
104
+ indexes:[]};if(d.create&&T(g,de(d.create)),d.rename)for(let n in d.rename){let p=d.
105
+ rename[n],u=e[p].indexes,o=e[p].relations,t=e[p].schema;g.tables.push(U.prepareRename(
106
+ y,n,p)),g.constraints.push(...O.prepareRenameTable(y,n,p,t.properties)),g.relations.
107
+ push(...B.prepareRename(y,n,p,o)),T(g,w.prepareRename(y,n,p,u))}if(d.nested)for(let n in d.
108
+ nested){let{nested:p}=d.nested[n];if(!p)continue;let{schema:u,relations:o,indexes:t}=p,
109
+ r=u?.nested?.properties,s=e[n].indexes,a=e[n].relations,l=c[n].schema,i=e[n].schema;
110
+ r?.create&&(g.tables.push(I.prepareCreate(y,n,s,r.create)),g.constraints.push(...O.
111
+ prepareCreate(y,n,r.create))),r?.nested&&(g.tables.push(...I.prepareUpdate(y,n,i,
112
+ s,r.nested)),g.constraints.push(...O.prepareUpdate(y,n,i,l,r.nested)),g.relations.
113
+ push(...B.prepareUpdate(y,n,a,r.nested))),r?.rename&&(g.tables.push(...I.prepareRename(
114
+ y,n,r.rename)),g.constraints.push(...O.prepareRenameColumns(y,n,i.properties,r.rename))),
115
+ r?.remove&&(g.tables.push(I.prepareDelete(y,n,r.remove)),g.constraints.push(...O.
116
+ prepareDelete(y,n,r.remove))),t?.create&&T(g,w.prepareCreate(y,n,i,t.create,!0)),
117
+ t?.remove&&T(g,w.prepareDelete(y,n,t.remove)),o?.create&&g.relations.push(...B.prepareCreate(
118
+ y,n,i,o.create)),o?.remove&&g.relations.push(...B.prepareDelete(y,n,o.remove))}return d.
119
+ remove&&T(g,ye(d.remove)),g},"getUpdateQueries"),ye=m(e=>{let c=new L,d={tables:[],
120
+ constraints:[],relations:[],indexes:[]};for(let y in e){let{name:g}=e[y];d.tables.
121
+ push(U.prepareDelete(c,g))}return d},"getDeleteQueries"),T=m((e,c)=>{c.indexes&&
122
+ e.indexes.push(...c.indexes),c.constraints&&e.constraints.push(...c.constraints),
123
+ c.relations&&e.relations.push(...c.relations),c.tables&&e.tables.push(...c.tables)},
124
+ "combineMigrationQueries");export{de as getCreateQueries,ye as getDeleteQueries,ut as getUpdateQueries};
120
125
  //# sourceMappingURL=main.mjs.map
@@ -0,0 +1,20 @@
1
+ import type { PgIndexRepository } from '@ez4/pgclient/library';
2
+ import type { AnySchema, ObjectSchema } from '@ez4/schema';
3
+ import type { ObjectComparison } from '@ez4/utils';
4
+ import type { SqlBuilder } from '@ez4/pgsql';
5
+ export declare namespace ColumnQuery {
6
+ const prepareCreate: (builder: SqlBuilder, table: string, indexes: PgIndexRepository, columns: Record<string, AnySchema>) => {
7
+ query: string;
8
+ };
9
+ const prepareUpdate: (builder: SqlBuilder, table: string, schema: ObjectSchema, indexes: PgIndexRepository, changes: Record<string, ObjectComparison>) => {
10
+ check: string;
11
+ query: string;
12
+ }[];
13
+ const prepareRename: (builder: SqlBuilder, table: string, changes: Record<string, string>) => {
14
+ check: string;
15
+ query: string;
16
+ }[];
17
+ const prepareDelete: (builder: SqlBuilder, table: string, columns: Record<string, AnySchema>) => {
18
+ query: string;
19
+ };
20
+ }
@@ -0,0 +1,27 @@
1
+ import type { AnySchema, ObjectSchema, ObjectSchemaProperties } from '@ez4/schema';
2
+ import type { ObjectComparison } from '@ez4/utils';
3
+ import type { SqlBuilder } from '@ez4/pgsql';
4
+ export declare namespace ConstraintQuery {
5
+ const prepareCreate: (builder: SqlBuilder, table: string, columns: Record<string, AnySchema>) => {
6
+ check: string;
7
+ query: string;
8
+ }[];
9
+ const prepareUpdate: (builder: SqlBuilder, table: string, targetSchema: ObjectSchema, sourceSchema: ObjectSchema, changes: Record<string, ObjectComparison>) => ({
10
+ query: string;
11
+ check?: undefined;
12
+ } | {
13
+ check: string;
14
+ query: string;
15
+ })[];
16
+ const prepareRenameTable: (builder: SqlBuilder, fromTable: string, toTable: string, columns: Record<string, AnySchema>) => {
17
+ check: string;
18
+ query: string;
19
+ }[];
20
+ const prepareRenameColumns: (builder: SqlBuilder, table: string, columns: ObjectSchemaProperties, changes: Record<string, string>) => {
21
+ check: string;
22
+ query: string;
23
+ }[];
24
+ const prepareDelete: (builder: SqlBuilder, table: string, columns: Record<string, AnySchema>) => {
25
+ query: string;
26
+ }[];
27
+ }
@@ -0,0 +1,9 @@
1
+ export declare namespace DatabaseQueries {
2
+ const prepareCreate: (database: string) => {
3
+ check: string;
4
+ query: string;
5
+ };
6
+ const prepareDelete: (database: string) => {
7
+ query: string;
8
+ };
9
+ }
@@ -0,0 +1,11 @@
1
+ import type { PgIndexRepository } from '@ez4/pgclient/library';
2
+ import type { ObjectSchema } from '@ez4/schema';
3
+ import type { SqlBuilder } from '@ez4/pgsql';
4
+ import type { PgMigrationQueries } from '../types/query';
5
+ type IndexMigrationQueries = Pick<PgMigrationQueries, 'constraints' | 'indexes'>;
6
+ export declare namespace IndexQueries {
7
+ const prepareCreate: (builder: SqlBuilder, table: string, schema: ObjectSchema, indexes: PgIndexRepository, concurrent: boolean) => IndexMigrationQueries;
8
+ const prepareRename: (builder: SqlBuilder, fromTable: string, toTable: string, indexes: PgIndexRepository) => IndexMigrationQueries;
9
+ const prepareDelete: (builder: SqlBuilder, table: string, indexes: PgIndexRepository) => IndexMigrationQueries;
10
+ }
11
+ export {};
@@ -0,0 +1,18 @@
1
+ import type { PgRelationRepository } from '@ez4/pgclient/library';
2
+ import type { ObjectSchema } from '@ez4/schema';
3
+ import type { ObjectComparison } from '@ez4/utils';
4
+ import type { SqlBuilder } from '@ez4/pgsql';
5
+ export declare namespace RelationQuery {
6
+ const prepareCreate: (builder: SqlBuilder, table: string, schema: ObjectSchema, relations: PgRelationRepository) => {
7
+ check: string;
8
+ query: string;
9
+ }[];
10
+ const prepareUpdate: (builder: SqlBuilder, table: string, relations: PgRelationRepository, changes: Record<string, ObjectComparison>) => any[];
11
+ const prepareRename: (builder: SqlBuilder, fromTable: string, toTable: string, relations: PgRelationRepository) => {
12
+ check: string;
13
+ query: string;
14
+ }[];
15
+ const prepareDelete: (builder: SqlBuilder, table: string, relations: PgRelationRepository) => {
16
+ query: string;
17
+ }[];
18
+ }
@@ -0,0 +1,14 @@
1
+ import type { PgIndexRepository } from '@ez4/pgclient/library';
2
+ import type { ObjectSchema } from '@ez4/schema';
3
+ import type { SqlBuilder } from '@ez4/pgsql';
4
+ export declare namespace TableQuery {
5
+ const prepareCreate: (builder: SqlBuilder, table: string, schema: ObjectSchema, indexes: PgIndexRepository) => {
6
+ query: string;
7
+ };
8
+ const prepareRename: (builder: SqlBuilder, fromTable: string, toTable: string) => {
9
+ query: string;
10
+ };
11
+ const prepareDelete: (builder: SqlBuilder, table: string) => {
12
+ query: string;
13
+ };
14
+ }
@@ -1,5 +1,5 @@
1
1
  import type { PgTableRepository } from '@ez4/pgclient/library';
2
- import type { PgMigrationQueries } from '../types/query.js';
2
+ import type { PgMigrationQueries } from '../types/query';
3
3
  export declare const getCreateQueries: (target: PgTableRepository) => PgMigrationQueries;
4
4
  export declare const getUpdateQueries: (target: PgTableRepository, source: PgTableRepository) => PgMigrationQueries;
5
5
  export declare const getDeleteQueries: (target: PgTableRepository) => PgMigrationQueries;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ez4/pgmigration",
3
3
  "description": "EZ4: A PostgreSQL migration library",
4
- "version": "0.26.0",
4
+ "version": "0.27.0",
5
5
  "author": "Silas B.",
6
6
  "license": "MIT",
7
7
  "type": "module",
@@ -11,7 +11,7 @@
11
11
  "repository": {
12
12
  "type": "git",
13
13
  "url": "git+https://github.com/sbalmt/ez4.git",
14
- "directory": "packages/pgmigration"
14
+ "directory": "libraries/pgmigration"
15
15
  },
16
16
  "engines": {
17
17
  "node": ">=22.7"
@@ -29,25 +29,23 @@
29
29
  }
30
30
  },
31
31
  "workspaces": [
32
- "packages/*"
32
+ "contracts/*",
33
+ "libraries/*"
33
34
  ],
34
35
  "scripts": {
35
36
  "lint": "eslint --cache",
36
37
  "clean": "rm -f *.tsbuildinfo && rm -rf dist/*",
37
- "build": "tsc -p tsconfig.json && node tools/bundler.mjs",
38
- "test": "npm run test:types && node --test --import ../../tools/tsnode.mjs test/*.spec.ts",
39
- "test:client": "npm run test:types && node --test --import ../../tools/tsnode.mjs test/client-*.spec.ts",
40
- "test:only": "npm run test:types && node --test-only --test --import ../../tools/tsnode.mjs test/*.spec.ts",
41
- "test:types": "npm run build && tsc -p tsconfig.test.json",
38
+ "build": "tsc && node tools/bundler.mjs",
39
+ "test": "npm run build && ez4 test",
42
40
  "local:publish": "npm run build && npm run clean:registry && npm publish --registry http://localhost:4873",
43
41
  "clean:registry": "rm -rf ../../.registry/@ez4/pgmigration",
44
42
  "live:publish": "npm run build && npm publish --access public"
45
43
  },
46
44
  "dependencies": {
47
- "@ez4/database": "^0.26.0",
48
- "@ez4/pgsql": "^0.26.0",
49
- "@ez4/pgclient": "^0.26.0",
50
- "@ez4/schema": "^0.26.0",
51
- "@ez4/utils": "^0.26.0"
45
+ "@ez4/database": "^0.27.0",
46
+ "@ez4/pgsql": "^0.27.0",
47
+ "@ez4/pgclient": "^0.27.0",
48
+ "@ez4/schema": "^0.27.0",
49
+ "@ez4/utils": "^0.27.0"
52
50
  }
53
51
  }
@@ -1,18 +0,0 @@
1
- import type { PgIndexRepository } from '@ez4/pgclient/library';
2
- import type { AnySchema, ObjectSchema } from '@ez4/schema';
3
- import type { ObjectComparison } from '@ez4/utils';
4
- import type { SqlBuilder } from '@ez4/pgsql';
5
- export declare const prepareCreateColumns: (builder: SqlBuilder, table: string, indexes: PgIndexRepository, columns: Record<string, AnySchema>) => {
6
- query: string;
7
- };
8
- export declare const prepareUpdateColumns: (builder: SqlBuilder, table: string, schema: ObjectSchema, indexes: PgIndexRepository, changes: Record<string, ObjectComparison>) => {
9
- check: string;
10
- query: string;
11
- }[];
12
- export declare const prepareRenameColumns: (builder: SqlBuilder, table: string, changes: Record<string, string>) => {
13
- check: string;
14
- query: string;
15
- }[];
16
- export declare const prepareDeleteColumns: (builder: SqlBuilder, table: string, columns: Record<string, AnySchema>) => {
17
- query: string;
18
- };
@@ -1,25 +0,0 @@
1
- import type { AnySchema, ObjectSchema, ObjectSchemaProperties } from '@ez4/schema';
2
- import type { ObjectComparison } from '@ez4/utils';
3
- import type { SqlBuilder } from '@ez4/pgsql';
4
- export declare const prepareCreateConstraints: (builder: SqlBuilder, table: string, columns: Record<string, AnySchema>) => {
5
- check: string;
6
- query: string;
7
- }[];
8
- export declare const prepareUpdateConstraints: (builder: SqlBuilder, table: string, targetSchema: ObjectSchema, sourceSchema: ObjectSchema, changes: Record<string, ObjectComparison>) => ({
9
- query: string;
10
- check?: undefined;
11
- } | {
12
- check: string;
13
- query: string;
14
- })[];
15
- export declare const prepareRenameConstraints: (builder: SqlBuilder, fromTable: string, toTable: string, columns: Record<string, AnySchema>) => {
16
- check: string;
17
- query: string;
18
- }[];
19
- export declare const prepareRenameConstraintColumns: (builder: SqlBuilder, table: string, columns: ObjectSchemaProperties, changes: Record<string, string>) => {
20
- check: string;
21
- query: string;
22
- }[];
23
- export declare const prepareDeleteConstraints: (builder: SqlBuilder, table: string, columns: Record<string, AnySchema>) => {
24
- query: string;
25
- }[];
@@ -1,7 +0,0 @@
1
- export declare const prepareCreateDatabase: (database: string) => {
2
- check: string;
3
- query: string;
4
- };
5
- export declare const prepareDeleteDatabase: (database: string) => {
6
- query: string;
7
- };
@@ -1,9 +0,0 @@
1
- import type { PgIndexRepository } from '@ez4/pgclient/library';
2
- import type { ObjectSchema } from '@ez4/schema';
3
- import type { SqlBuilder } from '@ez4/pgsql';
4
- import type { PgMigrationQueries } from '../types/query.js';
5
- type IndexQueries = Pick<PgMigrationQueries, 'constraints' | 'indexes'>;
6
- export declare const prepareCreateIndexes: (builder: SqlBuilder, table: string, schema: ObjectSchema, indexes: PgIndexRepository, concurrent: boolean) => IndexQueries;
7
- export declare const prepareRenameIndexes: (builder: SqlBuilder, fromTable: string, toTable: string, indexes: PgIndexRepository) => IndexQueries;
8
- export declare const prepareDeleteIndexes: (builder: SqlBuilder, table: string, indexes: PgIndexRepository) => IndexQueries;
9
- export {};
@@ -1,16 +0,0 @@
1
- import type { PgRelationRepository } from '@ez4/pgclient/library';
2
- import type { ObjectSchema } from '@ez4/schema';
3
- import type { ObjectComparison } from '@ez4/utils';
4
- import type { SqlBuilder } from '@ez4/pgsql';
5
- export declare const prepareCreateRelations: (builder: SqlBuilder, table: string, schema: ObjectSchema, relations: PgRelationRepository) => {
6
- check: string;
7
- query: string;
8
- }[];
9
- export declare const prepareUpdateRelations: (builder: SqlBuilder, table: string, relations: PgRelationRepository, changes: Record<string, ObjectComparison>) => any[];
10
- export declare const prepareRenameRelations: (builder: SqlBuilder, fromTable: string, toTable: string, relations: PgRelationRepository) => {
11
- check: string;
12
- query: string;
13
- }[];
14
- export declare const prepareDeleteRelations: (builder: SqlBuilder, table: string, relations: PgRelationRepository) => {
15
- query: string;
16
- }[];
@@ -1,12 +0,0 @@
1
- import type { PgIndexRepository } from '@ez4/pgclient/library';
2
- import type { ObjectSchema } from '@ez4/schema';
3
- import type { SqlBuilder } from '@ez4/pgsql';
4
- export declare const prepareCreateTable: (builder: SqlBuilder, table: string, schema: ObjectSchema, indexes: PgIndexRepository) => {
5
- query: string;
6
- };
7
- export declare const prepareRenameTable: (builder: SqlBuilder, fromTable: string, toTable: string) => {
8
- query: string;
9
- };
10
- export declare const prepareDeleteTable: (builder: SqlBuilder, table: string) => {
11
- query: string;
12
- };