All Projects → frou → Pg2go

frou / Pg2go

PostgreSQL script that generates Go struct definitions for all tables in a database

Labels

Projects that are alternatives of or similar to Pg2go

Postgresql Anyarray
PostgeSQL extension adding highly desirable, data-type independent array functionality.
Stars: ✭ 91 (-21.55%)
Mutual labels:  plpgsql
Postgraphile Lambda Example
Simple serverless / Lambda example with caching and lightweight bundle
Stars: ✭ 104 (-10.34%)
Mutual labels:  plpgsql
Triprecord
咔咔出行——基于高德地图API,Vue+Express实现的移动端webapp,服务器受到攻击网站暂时下掉了
Stars: ✭ 1,495 (+1188.79%)
Mutual labels:  plpgsql
Pg permission
A simple set of views to see ALL permissions in a PostgreSQL database
Stars: ✭ 92 (-20.69%)
Mutual labels:  plpgsql
Civicscape
A new standard for real-time policing.
Stars: ✭ 101 (-12.93%)
Mutual labels:  plpgsql
Aws Database Migration Samples
A set of sample database and associated items to allow customers to among other things follow along with published database migration recipes.
Stars: ✭ 105 (-9.48%)
Mutual labels:  plpgsql
Evergreen
Evergreen ILS
Stars: ✭ 85 (-26.72%)
Mutual labels:  plpgsql
Vocabulary V5.0
PALLAS: Build process for OMOP Standardized Vocabularies. Currently not available as independent release. Therefore, do not clone or try to replicate. It is work in progress and not ready for replication.
Stars: ✭ 114 (-1.72%)
Mutual labels:  plpgsql
Plpgunit
PostgreSQL Unit Testing Framework
Stars: ✭ 102 (-12.07%)
Mutual labels:  plpgsql
Common schema
DBA's framework for MySQL
Stars: ✭ 108 (-6.9%)
Mutual labels:  plpgsql
Qt Client
This repository contains the source code for the Desktop client. The Desktop client is built using the Qt framework for C++. The client can be extended or customized using JavaScript. This client is used by all editions of xTuple ERP.
Stars: ✭ 93 (-19.83%)
Mutual labels:  plpgsql
Periods
PERIODs and SYSTEM VERSIONING for PostgreSQL
Stars: ✭ 101 (-12.93%)
Mutual labels:  plpgsql
Screampay
screamPay聚合支付,一个强大到让你尖叫的聚合支付系统,使用Java开发,spring-boot架构,已接入环讯、九派、杉德等主流支付渠道,可直接用于生产环境。
Stars: ✭ 107 (-7.76%)
Mutual labels:  plpgsql
Pg Semver
A semantic version data type for PostgreSQL
Stars: ✭ 91 (-21.55%)
Mutual labels:  plpgsql
Bible Database
Bible databases as XML, JSON, SQL & SQLITE3 Database format for various languages. Developers can download it freely for their development works. Freely received, freely give.
Stars: ✭ 111 (-4.31%)
Mutual labels:  plpgsql
Cartodb Postgresql
PostgreSQL extension for CartoDB
Stars: ✭ 90 (-22.41%)
Mutual labels:  plpgsql
Serverless Postgraphql
Serverless GraphQL endpoint for PostgresSQL using AWS, serverless and PostGraphQL
Stars: ✭ 105 (-9.48%)
Mutual labels:  plpgsql
Scaledger
A double-entry accounting database with a typed GraphQL API
Stars: ✭ 115 (-0.86%)
Mutual labels:  plpgsql
Node Sqlcipher
SQLCipher bindings for Node
Stars: ✭ 114 (-1.72%)
Mutual labels:  plpgsql
Panda Cloud
Base on SpringCloud MicroService Framework
Stars: ✭ 108 (-6.9%)
Mutual labels:  plpgsql

pg2go is a PostgreSQL script that generates Go struct definitions for all tables in a database.

It is designed to be run directly against a database using the official psql command, and the output of that redirected to a new .go source file.

Here is an example Unix shell session demonstrating this (it interacts with a hypothetical database called blogdb):

DB=blogdb
OUT_FILE="types_$DB.go"

echo "package main" >"$OUT_FILE"
psql --quiet --tuples-only --no-align --dbname "$DB" --file pg2go.sql >>"$OUT_FILE"

goimports -w "$OUT_FILE" || gofmt -w "$OUT_FILE"

Finally, we can peek at the contents of the generated .go file using the head command:

head -n 22 types_blogdb.go
package main

import (
    "database/sql"
    "time"
)

type Author struct {
    ID         int       `db:"id" json:"id"`
    Created    time.Time `db:"created" json:"created"`
    Name       string    `db:"name" json:"name"`
    Admin      bool      `db:"admin" json:"admin"`
    LoginEmail string    `db:"login_email" json:"login_email"`
    LoginSalt  []byte    `db:"login_salt" json:"login_salt"`
    LoginKey   []byte    `db:"login_key" json:"login_key"`
}

type Comment struct {
    ID      int       `db:"id" json:"id"`
    Created time.Time `db:"created" json:"created"`
    Post    int       `db:"post" json:"post"`
    Author  int       `db:"author" json:"author"`

Notes

Using the goimports command rather than the standard gofmt command to format the resultant file has the benefit of automatically adding import statements for packages if and only if they are used in what was generated (e.g. "time" for time.Time & "database/sql" for sql.NullString).

Struct fields are tagged db:"..." for package sqlx to pick up on, should you wish to use it. Similarly, json:"..." for the standard library package encoding/json.

An attempt is made to singularize plural table names.

If NEED_GO_TYPE_FOR_... shows up in the resultant file then add a case for that type name to the type_pg2go function in pg2go.sql.

If the tables you're interested in aren't in the default 'public' schema of your database, then search and replace that text in pg2go.sql.

Support for Enums

When the database schema has something like:

CREATE TYPE post_status AS ENUM ('draft', 'live', 'retracted');

CREATE TABLE post (
  -- ...
  status post_status NOT NULL
  -- ...
);

The resultant file will contain:

type Post struct {
    // ...
    Status string `db:"status" json:"status"` // Postgres enum. Use with the PostStatus* constants.
    // ...
}

// ...

const (
    PostStatusDraft     = "draft"
    PostStatusLive      = "live"
    PostStatusRetracted = "retracted"
)

A value other than one of those constants will be rejected by Postgres when performing an INSERT or UPDATE.

License

The MIT License

Copyright (c) 2015 Duncan Holm

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].