DDL Data Source Questions

Answered

I'm looking into database versioning using the DDL Data Source function.

1. Is it possible to organize the generated SQL scripts into folders based on schema & object type like the Database Explorer does? Right now, everything is under schema.

2. If I wish to add script to initialize a table with data, it should not be placed in the generated script files because it will be overwritten. Correct?

3. The general workflow is to create, modify, delete database objects using the GUI tools & let the DDL Data Source files auto sync if turned on. Correct? The workflow is NOT to modify DDL Data Source script files & push to the database. Right? Or can it work both ways?

0
3 comments

Hi!
1. Yes, that part is scriptable.
In Data Source settings there is an option to choose layout script:


You can add your layout scripts here:


2. Yes, now there is no way to store this scripts inside "generated" root. That seems to be a reasonable feature. Now this is just a conceptual problem how to identify the files that should not be recreated

3. There is no "auto sync" in your meaning. DDL Data Source is about mapping files to lets say the tree in database view, so auto sync setting is about updates of that tree from files. Synchronisation between real data source and ddl one (configured by ddl mapping) is manual both ways - you decide when to dump real db to disk, or when to restore from disk to db.
We have ui tools to edit both types of object, or you can manually edit code (but this will be regenerated in other "style" if you'll dump the same changes from db some day)

1

Hi Kyle,

Have you figure it out how to organize the generated SQL scripts into folders based on schema & object type? Can you share your groovy script?

Alexander Kass, is there any documentation for packages?

How to get the type?

0

Ok.

I figured it out.

If anyone needs that, here it is:

import com.intellij.database.model.DasObjectWithSource
import com.intellij.database.model.DasObject
import com.intellij.database.model.DasSchemaChild
import com.intellij.database.model.ObjectKind
import com.intellij.database.util.DasUtil
import com.intellij.database.util.ObjectPath

LAYOUT.ignoreDependencies = true
LAYOUT.baseName { ctx -> baseName(ctx.object) }
LAYOUT.fileScope { path -> fileScope(path) }


def baseName(obj) {
def schema = DasUtil.getSchema(obj)
def file = fileName(obj)
def type = objectType(obj)
if (schema.isEmpty()) {
return file
}
else {
return sanitize(schema) + "/" + type + "/" + file
}
}


def objectType(obj) {
def objType = obj.getKind()
if(objType == ObjectKind.ROUTINE){
objType = obj.getRoutineType()}

def objName = objType
switch(objType){
case "table":
objName = "Tables";
break;
case "index":
objName = "Indexes";
break;
case "table-type":
objName = "User Defined Types/Table Types";
break;
case "alias-type":
objName = "User Defined Types/Data Types";
break;
case "PROC":
objName = "Stored Procedures";
break;
case "TAB_FUN":
objName = "Functions/Table";
break;
case "SCALAR_FUN":
objName = "Functions/Scalar";
break;
default:
objName = objType
break;
}

return objName
}

def fileName(obj) {
for (def cur = obj; cur != null; cur = cur.dasParent) {
if (storeSeparately(cur)) return sanitize(cur.name)
}
return sanitize(obj.name)
}

def fileScope(path) {
def root = path.getName(0).toString()
if (root.endsWith(".sql")) return null
return ObjectPath.create(root, ObjectKind.SCHEMA)
}

def storeSeparately(obj) {
return obj instanceof DasObjectWithSource || obj instanceof DasSchemaChild
}

def sanitize(name) {
return name.replace('/', 'slash')
}

It will create a folder structure like:

1

Please sign in to leave a comment.