11import { escapeQuotes , exportFieldComment , parseDefault } from "./shared" ;
22import { dbToTypes } from "../../data/datatypes" ;
33
4+ const getSchemaAndName = ( ident ) => {
5+ if ( ident . includes ( '.' ) ) {
6+ return ident . split ( '.' , 2 )
7+ }
8+
9+ return [ "public" , ident ]
10+ }
11+
12+ /**
13+ * @param {string } ident
14+ */
15+ const formatWithSchema = ( ident ) => {
16+ const [ schema , name ] = getSchemaAndName ( ident )
17+ return `"${ schema } "."${ name } "`
18+ }
19+
420export function toPostgres ( diagram ) {
21+ const schemas = new Set ( )
22+ const schemaSources = [ ...diagram . types , ...diagram . enums , ...diagram . tables ]
23+ schemaSources . forEach ( ( v ) => {
24+ const [ schema ] = getSchemaAndName ( v . name )
25+ schemas . add ( schema )
26+ } )
27+ const schemaStatements = [ ...schemas ] . map ( ( s ) => {
28+ return `CREATE SCHEMA IF NOT EXISTS "${ s } "`
29+ } ) . join ( ";\n" ) + ";\n\n"
530 const enumStatements = diagram . enums
631 . map (
732 ( e ) =>
8- `CREATE TYPE " ${ e . name } " AS ENUM (\n${ e . values
33+ `CREATE TYPE ${ formatWithSchema ( e . name ) } AS ENUM (\n${ e . values
934 . map ( ( v ) => `\t'${ v } '` )
1035 . join ( ",\n" ) } \n);\n`,
1136 )
@@ -14,11 +39,11 @@ export function toPostgres(diagram) {
1439 const typeStatements = diagram . types
1540 . map (
1641 ( type ) =>
17- `CREATE TYPE ${ type . name } AS (\n${ type . fields
42+ `CREATE TYPE ${ formatWithSchema ( type . name ) } AS (\n${ type . fields
1843 . map ( ( f ) => `\t${ f . name } ${ f . type } ` )
1944 . join ( ",\n" ) } \n);\n\n${
2045 type . comment ?. trim ( )
21- ? `COMMENT ON TYPE " ${ type . name } " IS '${ escapeQuotes ( type . comment ) } ';\n`
46+ ? `COMMENT ON TYPE ${ formatWithSchema ( type . name ) } IS '${ escapeQuotes ( type . comment ) } ';\n`
2247 : ""
2348 } `,
2449 )
@@ -28,7 +53,7 @@ export function toPostgres(diagram) {
2853 . map ( ( table ) => {
2954 const inheritsClause =
3055 Array . isArray ( table . inherits ) && table . inherits . length > 0
31- ? `\n) INHERITS (${ table . inherits . map ( ( parent ) => `" ${ parent } "` ) . join ( ", " ) } )`
56+ ? `\n) INHERITS (${ table . inherits . map ( ( parent ) => formatWithSchema ( parent ) ) . join ( ", " ) } )`
3257 : "\n)" ;
3358
3459 const fieldDefinitions = table . fields
@@ -41,7 +66,7 @@ export function toPostgres(diagram) {
4166 } ${ field . isArray ? " ARRAY" : "" } ${ field . notNull ? " NOT NULL" : "" } ${
4267 field . unique ? " UNIQUE" : ""
4368 } ${ field . increment ? " GENERATED BY DEFAULT AS IDENTITY" : "" } ${
44- field . default ?. trim ( )
69+ field . default && field . default ?. trim ( )
4570 ? ` DEFAULT ${ parseDefault ( field , diagram . database ) } `
4671 : ""
4772 } ${
@@ -61,12 +86,12 @@ export function toPostgres(diagram) {
6186
6287 const commentStatements = [
6388 table . comment ?. trim ( )
64- ? `COMMENT ON TABLE " ${ table . name } " IS '${ escapeQuotes ( table . comment ) } ';`
89+ ? `COMMENT ON TABLE ${ formatWithSchema ( table . name ) } IS '${ escapeQuotes ( table . comment ) } ';`
6590 : "" ,
6691 ...table . fields
6792 . map ( ( field ) =>
6893 field . comment ?. trim ( )
69- ? `COMMENT ON COLUMN " ${ table . name } " ."${ field . name } " IS '${ escapeQuotes ( field . comment ) } ';`
94+ ? `COMMENT ON COLUMN ${ formatWithSchema ( table . name ) } ."${ field . name } " IS '${ escapeQuotes ( field . comment ) } ';`
7095 : "" ,
7196 )
7297 . filter ( Boolean ) ,
@@ -75,13 +100,13 @@ export function toPostgres(diagram) {
75100 const indexStatements = table . indices
76101 . map (
77102 ( i ) =>
78- `CREATE ${ i . unique ? "UNIQUE " : "" } INDEX "${ i . name } "\nON " ${ table . name } " (${ i . fields
103+ `CREATE ${ i . unique ? "UNIQUE " : "" } INDEX "${ i . name } "\nON ${ formatWithSchema ( table . name ) } (${ i . fields
79104 . map ( ( f ) => `"${ f } "` )
80105 . join ( ", " ) } );`,
81106 )
82107 . join ( "\n" ) ;
83108
84- return `CREATE TABLE IF NOT EXISTS " ${ table . name } " (\n${ fieldDefinitions } ${ primaryKeyClause } ${ inheritsClause } ;\n\n${ commentStatements } \n${ indexStatements } ` ;
109+ return `CREATE TABLE IF NOT EXISTS ${ formatWithSchema ( table . name ) } (\n${ fieldDefinitions } ${ primaryKeyClause } ${ inheritsClause } ;\n\n${ commentStatements } \n${ indexStatements } ` ;
85110 } )
86111 . join ( "\n\n" ) ;
87112
@@ -96,12 +121,13 @@ export function toPostgres(diagram) {
96121
97122 if ( ! startTable || ! endTable || ! startField || ! endField ) return "" ;
98123
99- return `ALTER TABLE " ${ startTable . name } " \nADD FOREIGN KEY("${ startField . name } ") REFERENCES " ${ endTable . name } " ("${ endField . name } ")\nON UPDATE ${ r . updateConstraint . toUpperCase ( ) } ON DELETE ${ r . deleteConstraint . toUpperCase ( ) } ;` ;
124+ return `ALTER TABLE ${ formatWithSchema ( startTable . name ) } \nADD FOREIGN KEY("${ startField . name } ") REFERENCES ${ formatWithSchema ( endTable . name ) } ("${ endField . name } ")\nON UPDATE ${ r . updateConstraint . toUpperCase ( ) } ON DELETE ${ r . deleteConstraint . toUpperCase ( ) } ;` ;
100125 } )
101126 . filter ( Boolean )
102127 . join ( "\n" ) ;
103128
104129 return [
130+ schemaStatements ,
105131 enumStatements ,
106132 enumStatements . trim ( ) && typeStatements
107133 ? "\n" + typeStatements
0 commit comments