如何为PostgreSQL建立构建系统 [英] How to make build system for PostgreSQL

查看:145
本文介绍了如何为PostgreSQL建立构建系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为MS SQL Server创建构建系统很容易:

Making build system for MS SQL Server is easy:

{
    "cmd": ["sqlcmd", "-S", ".", "-i", "$file"],
    "selector": "source.sql",
    "shell": true
}

对于PostgreSQL,我尝试过这样:

and for PostgreSQL I tried this:

{
    "env": {"PGPASSWORD": "password"},
    "cmd": ["C:/PostgreSQL/9.3/bin/psql.exe", "-U", "postgres", "-f", "$file"],
    "selector": "source.postgresql",
    "shell": true
}

但是,使用MS SQL时,我可以使用 USE从脚本中引用数据库关键字,Postgre中没有这样的功能,因此上面的构建无法正常工作,而且在我看来,我必须对每个数据库进行硬编码以构建在这种情况下毫无意义的系统。

But, while with MS SQL I can reference database from the script with USE keyword, there is no such feature in Postgre so above build can't work, and it seems to me I'll must hardcode each database to build system which is non-sensical in this case.

有什么想法可以做到这一点-在任意PostgreSQL数据库上从Sublime Text运行sql脚本吗?

Any ideas how to make this work - run sql script from Sublime Text on arbitrary PostgreSQL database?

推荐答案

构建系统的 cmd的内容数组必须是您在命令行上键入的内容才能运行该命令-数据库也不例外。我对Postgres内部知识一无所知,但是如果您需要在命令行中指定数据库名称,则必须在构建系统中进行操作。

The contents of the build system's "cmd" array need to be what you would type on the command line to run that command - DBs are no exception. I don't know anything about Postgres internals, but if you need to specify the DB's name on the command line, then you'll have to do it in the build system.

但是,除了可能存在数十个构建系统之外,还有另一种方法-在 .sublime-project 文件。根据文档 .sublime-project 文件可以具有三个基本组:文件夹 设置 build_systems 。要创建项目,请打开要包含在侧边栏中的文件夹,然后单击 Project->。将项目另存为... 。输入其名称,然后将其保存在合理的位置。然后,选择 Project->编辑项目 ,它将使用JSON语法打开 .sublime-project 文件。它应该看起来像这样:

However, there is an alternative to having potentially dozens of build systems lying around - defining the build system in a .sublime-project file. According to the documentation, the .sublime-project file can have three base groups: "folders", "settings", and "build_systems". To create a project, open the folder(s) you want to include in the sidebar, then click on Project -> Save Project As.... Put in its name and save it in a logical place. Then, select Project -> Edit Project, which will open up the .sublime-project file with JSON syntax. It should look something like this:

{
    "folders":
    [
        {
            "follow_symlinks": true,
            "path": "C:\\Users\\MattDMo\\Development\\DB\\my_postgres_db1"
            // by default, Sublime uses double-backslashes :(
        }
    ],
}

在<后面的方括号后添加 build_systems:标识符code>文件夹 ,并放入您的构建系统中:

Add a "build_systems": identifier after the closing square bracket from "folders", and put in your build system:

{
    "folders":
    [
        {
            "follow_symlinks": true,
            "path": "C:\\Users\\MattDMo\\Development\\DB\\my_postgres_db1"
        }
    ],
    "build_systems":
    [
        {
            "name": "my_postgres_db1",
            "cmd": ["C:/PostgreSQL/9.3/bin/psql.exe", "-W", "-U", "postgres", "-d", "my_postgres_db1", "-f", "$file"],
            // you can use either double backslashes or forward slashes on Windows. Forward is better :)
            "selector": "source.postgresql",
            "shell": true
        }
    ]
}

,您应该已经准备就绪。有了模板之后,您可以根据需要创建任意数量的文件副本,根据需要自定义数据库名称和构建系统名称。 名称 字段将显示在 Tools->构建系统 菜单,您可以直接选择它,也可以使用 自动 t还有其他任何带有选择器: source.postgresql 的构建系统。

and you should be all set. Now that you have your template, you can make as many copies of the file as you want, customizing the DB name and build system name as needed. The "name" field will show up in the Tools -> Build System menu, and you can either select it outright, or use Automatic if you don't have any other build systems with "selector": "source.postgresql" in them.

它确实涉及每个数据库都有一点工作,在切换数据库时,您必须记住要切换活动项目(我只为每个活动的项目打开一个窗口),但除此之外,它还可以解决您的问题。问题。您会注意到,我删除了 env:{ PGPASSWORD:密码},行(无论如何,它都应该放在方括号中),而是添加了 psql.exe -W 命令行选项以提示输入密码。无法加密 .sublime-project 文件,因此任何可以读取这些文件的人都将看到您服务器的密码。我不知道是否使用 shell:true 导入外壳程序的环境变量,我怀疑不是,但是您必须进行测试。运行构建系统将是一回事,但这将使您的整个系统更加安全。当然,如果您不希望/需要额外的安全性,可以随时恢复,但是如果它是面向公众的服务器,我就不需要。

It does involve a little bit of work for each DB that you have, and you'll have to remember to switch the active project when switching DBs (I just keep one window open for each project I have active), but other than that it should solve your problem. You'll note that I removed the "env": {"PGPASSWORD": "password"}, line (it should have been in square brackets, anyways) and instead added the -W command-line option to psql.exe to prompt for the password. There's no way of encrypting .sublime-project files, so anybody that can read them will see your server's password. I don't know if using "shell": true imports your shell's environment variables or not, I suspect it doesn't, but you'll have to test. It'll be one thing to enter when running the build system, but it will make your overall system more secure. Of course, feel free to revert if you don't want/need the extra security, but if it's a public-facing server I wouldn't.

祝您好运!

这篇关于如何为PostgreSQL建立构建系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆