8000 Add pg type type info to schema by agedemenli · Pull Request #592 · xataio/pgroll · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add pg type type info to schema #592

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pkg/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ type Column struct {

// Whether or not the column has been deleted in the virtual schema
Deleted bool `json:"-"`

// Postgres type type, e.g enum, composite, range
PostgresType string `json:"postgresType"`
}

// Index represents an index on a table
Expand Down
16 changes: 15 additions & 1 deletion pkg/state/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,21 @@ BEGIN
array_agg(e.enumlabel ORDER BY e.enumsortorder)
FROM pg_enum AS e
WHERE
e.enumtypid = tp.oid) AS enumValues FROM pg_attribute AS attr
e.enumtypid = tp.oid) AS enumValues, CASE WHEN tp.typtype = 'b' THEN
'base'
WHEN tp.typtype = 'c' THEN
'composite'
WHEN tp.typtype = 'd' THEN
'domain'
WHEN tp.typtype = 'e' THEN
'enum'
WHEN tp.typtype = 'p' THEN
'pseudo'
WHEN tp.typtype = 'r' THEN
'range'
WHEN tp.typtype = 'm' THEN
'multirange'
END AS postgresType FROM pg_attribute AS attr
INNER JOIN pg_type AS tp ON attr.atttypid = tp.oid
LEFT JOIN pg_attrdef AS def ON attr.attrelid = def.adrelid
AND attr.attnum = def.adnum
Expand Down
238 changes: 160 additions & 78 deletions pkg/state/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,9 +408,10 @@ func TestReadSchema(t *testing.T) {
Name: "table1",
Columns: map[string]*schema.Column{
"id& 10000 quot;: {
Name: "id",
Type: "integer",
Nullable: true,
Name: "id",
Type: "integer",
Nullable: true,
PostgresType: "base",
},
},
PrimaryKey: []string{},
Expand All @@ -432,10 +433,11 @@ func TestReadSchema(t *testing.T) {
Name: "table1",
Columns: map[string]*schema.Column{
"id": {
Name: "id",
Type: "integer",
Nullable: false,
Unique: true,
Name: "id",
Type: "integer",
Nullable: false,
Unique: true,
PostgresType: "base",
},
},
PrimaryKey: []string{},
Expand Down Expand Up @@ -470,14 +472,16 @@ func TestReadSchema(t *testing.T) {
Name: "table1",
Columns: map[string]*schema.Column{
"id": {
Name: "id",
Type: "integer",
Nullable: true,
Name: "id",
Type: "integer",
Nullable: true,
PostgresType: "base",
},
"name": {
Name: "name",
Type: "text",
Nullable: true,
Name: "name",
Type: "text",
Nullable: true,
PostgresType: "base",
},
},
PrimaryKey: []string{},
Expand Down Expand Up @@ -507,10 +511,11 @@ func TestReadSchema(t *testing.T) {
Name: "table1",
Columns: map[string]*schema.Column{
"id": {
Name: "id",
Type: "integer",
Nullable: false,
Unique: true,
Name: "id",
Type: "integer",
Nullable: false,
Unique: true,
PostgresType: "base",
},
},
PrimaryKey: []string{"id"},
Expand All @@ -531,9 +536,10 @@ func TestReadSchema(t *testing.T) {
Name: "table2",
Columns: map[string]*schema.Column{
"fk": {
Name: "fk",
Type: "integer",
Nullable: false,
Name: "fk",
Type: "integer",
Nullable: false,
PostgresType: "base",
},
},
PrimaryKey: []string{},
Expand Down Expand Up @@ -563,10 +569,11 @@ func TestReadSchema(t *testing.T) {
Name: "table1",
Columns: map[string]*schema.Column{
"id": {
Name: "id",
Type: "integer",
Nullable: false,
Unique: true,
Name: "id",
Type: "integer",
Nullable: false,
Unique: true,
PostgresType: "base",
},
},
PrimaryKey: []string{"id"},
Expand All @@ -587,9 +594,10 @@ func TestReadSchema(t *testing.T) {
Name: "table2",
Columns: map[string]*schema.Column{
"fk": {
Name: "fk",
Type: "integer",
Nullable: false,
Name: "fk",
Type: "integer",
Nullable: false,
PostgresType: "base",
},
},
PrimaryKey: []string{},
Expand Down Expand Up @@ -619,15 +627,17 @@ func TestReadSchema(t *testing.T) {
Name: "table1",
Columns: map[string]*schema.Column{
"id": {
Name: "id",
Type: "integer",
Nullable: false,
Unique: true,
Name: "id",
Type: "integer",
Nullable: false,
Unique: true,
PostgresType: "base",
},
"age": {
Name: "age",
Type: "integer",
Nullable: true,
Name: "age",
Type: "integer",
Nullable: true,
PostgresType: "base",
},
},
PrimaryKey: []string{"id"},
Expand Down Expand Up @@ -663,16 +673,18 @@ func TestReadSchema(t *testing.T) {
Name: "table1",
Columns: map[string]*schema.Column{
"id": {
Name: "id",
Type: "integer",
Nullable: false,
Unique: true,
Name: "id",
Type: "integer",
Nullable: false,
Unique: true,
PostgresType: "base",
},
"name": {
Name: "name",
Type: "text",
Unique: true,
Nullable: true,
Name: "name",
Type: "text",
Unique: true,
Nullable: true,
PostgresType: "base",
},
},
PrimaryKey: []string{"id"},
Expand Down Expand Up @@ -714,16 +726,18 @@ func TestReadSchema(t *testing.T) {
Name: "table1",
Columns: map[string]*schema.Column{
"id": {
Name: "id",
Type: "integer",
Nullable: false,
Unique: true,
Name: "id",
Type: "integer",
Nullable: false,
Unique: true,
PostgresType: "base",
},
"name": {
Name: "name",
Type: "text",
Nullable: true,
Unique: false,
Name: "name",
Type: "text",
Nullable: true,
Unique: false,
PostgresType: "base",
},
},
PrimaryKey: []string{"id"},
Expand Down Expand Up @@ -773,14 +787,16 @@ func TestReadSchema(t *testing.T) {
Name: "products",
Columns: map[string]*schema.Column{
"customer_id": {
Name: "customer_id",
Type: "integer",
Nullable: false,
Name: "customer_id",
Type: "integer",
Nullable: false,
PostgresType: "base",
},
"product_id": {
Name: "product_id",
Type: "integer",
Nullable: false,
Name: "product_id",
Type: "integer",
Nullable: false,
PostgresType: "base",
},
},
PrimaryKey: []string{"customer_id", "product_id"},
Expand All @@ -801,14 +817,16 @@ func TestReadSchema(t *testing.T) {
Name: "orders",
Columns: map[string]*schema.Column{
"customer_id": {
Name: "customer_id",
Type: "integer",
Nullable: false,
Name: "customer_id",
Type: "integer",
Nullable: false,
PostgresType: "base",
},
"product_id": {
Name: "product_id",
Type: "integer",
Nullable: false,
Name: "product_id",
Type: "integer",
Nullable: false,
PostgresType: "base",
},
},
PrimaryKey: []string{},
Expand Down Expand Up @@ -838,14 +856,16 @@ func TestReadSchema(t *testing.T) {
Name: "table1",
Columns: map[string]*schema.Column{
"a": {
Name: "a",
Type: "text",
Nullable: true,
Name: "a",
Type: "text",
Nullable: true,
PostgresType: "base",
},
"b": {
Name: "b",
Type: "text",
Nullable: true,
Name: "b",
Type: "text",
Nullable: true,
PostgresType: "base",
},
},
PrimaryKey: []string{},
Expand Down Expand Up @@ -875,9 +895,10 @@ func TestReadSchema(t *testing.T) {
Name: "table1",
Columns: map[string]*schema.Column{
"a": {
Name: "a",
Type: "public.email_type",
Nullable: true,
Name: "a",
Type: "public.email_type",
Nullable: true,
PostgresType: "domain",
},
},
PrimaryKey: []string{},
Expand All @@ -899,15 +920,76 @@ func TestReadSchema(t *testing.T) {
Name: "table1",
Columns: map[string]*schema.Column{
"name": {
Name: "name",
Type: "text",
Nullable: true,
Name: "name",
Type: "text",
Nullable: true,
PostgresType: "base",
},
"review": {
Name: "review",
Type: "public.review",
Nullable: true,
EnumValues: []string{"good", "bad", "ugly"},
Name: "review",
Type: "public.review",
Nullable: true,
EnumValues: []string{"good", "bad", "ugly"},
PostgresType: "enum",
},
},
PrimaryKey: []string{},
Indexes: map[string]*schema.Index{},
ForeignKeys: map[string]*schema.ForeignKey{},
CheckConstraints: map[string]*schema.CheckConstraint{},
UniqueConstraints: map[string]*schema.UniqueConstraint{},
},
},
},
},
{
name: "postgres type types",
createStmt: `
CREATE TYPE comptype AS (f1 int, f2 text);
CREATE TYPE review AS ENUM ('good', 'bad', 'ugly');
CREATE TYPE float8_range AS RANGE (subtype = float8, subtype_diff = float8mi);
CREATE DOMAIN us_postal_code AS TEXT
CHECK(
VALUE ~ '^\d{5}$'
OR VALUE ~ '^\d{5}-\d{4}$'
);
CREATE TABLE public.table1 (id bigint, comp_col comptype, enum_col review, range_col float8_range, domain_col us_postal_code);`,
wantSchema: &schema.Schema{
Name: "public",
Tables: map[string]*schema.Table{
"table1": {
Name: "table1",
Columns: map[string]*schema.Column{
"id": {
Name: "id",
Type: "bigint",
Nullable: true,
PostgresType: "base",
},
"comp_col": {
Name: "comp_col",
Type: "public.comptype",
Nullable: true,
PostgresType: "composite",
},
"enum_col": {
Name: "enum_col",
Type: "public.review",
Nullable: true,
PostgresType: "enum",
EnumValues: []string{"good", "bad", "ugly"},
},
"range_col": {
Name: "range_col",
Type: "public.float8_range",
Nullable: true,
PostgresType: "range",
},
"domain_col": {
Name: "domain_col",
Type: "public.us_postal_code",
Nullable: true,
PostgresType: "domain",
},
},
PrimaryKey: []string{},
Expand Down
0