Skip to content
Open
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
13 changes: 12 additions & 1 deletion packages/migra/src/changes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import * as schemainspect from '@pgkit/schemainspect'
import {asa, isa, BaseInspectedSelectable, pg} from '@pgkit/schemainspect'
import {Statements} from './statements'
import {sortKeys, differences} from './util'
import {sortKeys, differences, isEqual} from './util'

const {InspectedConstraint, InspectedEnum, InspectedExtension} = pg

Expand Down Expand Up @@ -398,6 +398,17 @@ function get_table_changes({
const rls_alter = v.alter_rls_statement
statements.append(rls_alter)
}

// Check for reloptions changes
// Sort arrays before comparison since order doesn't matter for reloptions
const before_opts = (before.reloptions || []).slice().sort()
const after_opts = (v.reloptions || []).slice().sort()
if (!isEqual(before_opts, after_opts) && v.is_table) {
const alter_reloptions = v.alter_reloptions_statement
if (alter_reloptions) {
statements.append(alter_reloptions)
}
}
}

// const [seq_created, seq_dropped, seq_modified, _] = differences(sequences_from, sequences_target)
Expand Down
12 changes: 12 additions & 0 deletions packages/migra/test/NEW_FIXTURES/viewreloptions/a.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
create table test_table(id serial primary key, name text);

create table test_table_with_options(id serial primary key, name text) with (fillfactor = 80);

create view test_view_without_options as select name from test_table;

create view test_view_with_options with (security_barrier = true) as select name from test_table;

create materialized view test_matview_without_options as select name from test_table;

create materialized view test_matview_with_options with (fillfactor = 90, autovacuum_enabled = false) as select name from test_table;

Empty file.
24 changes: 24 additions & 0 deletions packages/migra/test/NEW_FIXTURES/viewreloptions/b.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
create table test_table(id serial primary key, name text);

-- Same table with different reloptions
create table test_table_with_options(id serial primary key, name text) with (fillfactor = 70, parallel_workers = 4);

-- Add a new table with reloptions
create table test_table_new_with_options(id serial primary key, name text) with (fillfactor = 75, autovacuum_enabled = false);

create view test_view_without_options as select name from test_table;

-- Same view with different reloptions
create view test_view_with_options with (security_barrier = false) as select name from test_table;

-- Add a new view with reloptions
create view test_view_new_with_options with (security_barrier = true) as select id from test_table;

create materialized view test_matview_without_options as select name from test_table;

-- Same materialized view with different reloptions
create materialized view test_matview_with_options with (fillfactor = 85, autovacuum_enabled = true) as select name from test_table;

-- Add a new materialized view with reloptions
create materialized view test_matview_new_with_options with (parallel_workers = 2) as select id from test_table;

29 changes: 29 additions & 0 deletions packages/migra/test/NEW_FIXTURES/viewreloptions/expected.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
create sequence "public"."test_table_new_with_options_id_seq";

drop materialized view if exists "public"."test_matview_with_options";

create table "public"."test_table_new_with_options" (
"id" integer not null default nextval('test_table_new_with_options_id_seq'::regclass),
"name" text
) with (fillfactor = 75, autovacuum_enabled = false);

alter table "public"."test_table_with_options" set (fillfactor = 70, parallel_workers = 4);

alter sequence "public"."test_table_new_with_options_id_seq" owned by "public"."test_table_new_with_options"."id";

CREATE UNIQUE INDEX test_table_new_with_options_pkey ON public.test_table_new_with_options USING btree (id);

alter table "public"."test_table_new_with_options" add constraint "test_table_new_with_options_pkey" PRIMARY KEY using index "test_table_new_with_options_pkey";

create materialized view "public"."test_matview_new_with_options" with (parallel_workers = 2) as SELECT test_table.id
FROM test_table;

create or replace view "public"."test_view_new_with_options" with (security_barrier = true) as SELECT test_table.id
FROM test_table;

create materialized view "public"."test_matview_with_options" with (fillfactor = 85, autovacuum_enabled = true) as SELECT test_table.name
FROM test_table;

create or replace view "public"."test_view_with_options" with (security_barrier = false) as SELECT test_table.name
FROM test_table;

Empty file.
6 changes: 4 additions & 2 deletions packages/schemainspect/queries/pg/sql/relations.sql
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ r as (
c.relforcerowsecurity::boolean as forcerowsecurity,
c.relpersistence as persistence,
c.relpages as page_size_estimate,
c.reltuples as row_count_estimate
c.reltuples as row_count_estimate,
c.reloptions as reloptions
from
pg_catalog.pg_class c
inner join pg_catalog.pg_namespace n
Expand Down Expand Up @@ -92,7 +93,8 @@ select
r.forcerowsecurity,
r.persistence,
r.page_size_estimate,
r.row_count_estimate
r.row_count_estimate,
r.reloptions
FROM
r
left join pg_catalog.pg_attribute a
Expand Down
2 changes: 2 additions & 0 deletions packages/schemainspect/src/inspected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ export interface BaseInspectedSelectableOptions {
rowsecurity?: boolean
forcerowsecurity?: boolean
persistence?: any
reloptions?: string[] | null
}

export abstract class BaseInspectedSelectable extends AutoThisAssigner<
Expand Down Expand Up @@ -311,6 +312,7 @@ export abstract class BaseInspectedSelectable extends AutoThisAssigner<
'partition_def',
'rowsecurity',
'persistence',
'reloptions',
])
}
}
31 changes: 27 additions & 4 deletions packages/schemainspect/src/pg/obj.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const resource_text = (relativePath: string): string => {

const CREATE_TABLE = `
create {}table {} ({}
){}{};
){}{}{};
`
const CREATE_TABLE_SUBCLASS = `
create {}table {} partition of {} {};
Expand Down Expand Up @@ -153,6 +153,10 @@ export class InspectedSelectable extends BaseInspectedSelectable {
get create_statement(): string {
const n = this.quoted_full_name
let create_statement: string
let reloptions_clause = ''
if (this.reloptions && this.reloptions.length > 0) {
reloptions_clause = ` with (${this.reloptions.join(', ')})`
}

if (['r', 'p'].includes(this.relationtype)) {
if (this.is_partitioning_child_table) {
Expand Down Expand Up @@ -180,18 +184,18 @@ export class InspectedSelectable extends BaseInspectedSelectable {
inherits_clause = ` inherits (${this.parent_table})`
}

create_statement = format(CREATE_TABLE, this.persistence_modifier, n, colspec, partition_key, inherits_clause)
create_statement = format(CREATE_TABLE, this.persistence_modifier, n, colspec, partition_key, inherits_clause, reloptions_clause)
}
} else
switch (this.relationtype) {
case 'v': {
create_statement = `create or replace view ${n} as ${this.definition}\n`
create_statement = `create or replace view ${n}${reloptions_clause} as ${this.definition}\n`

break
}

case 'm': {
create_statement = `create materialized view ${n} as ${this.definition}\n`
create_statement = `create materialized view ${n}${reloptions_clause} as ${this.definition}\n`

break
}
Expand Down Expand Up @@ -342,6 +346,24 @@ export class InspectedSelectable extends BaseInspectedSelectable {
const keyword = this.is_unlogged ? 'unlogged' : 'logged'
return this.alter_table_statement(`set ${keyword}`)
}

get alter_reloptions_clause(): string | null {
if (this.reloptions && this.reloptions.length > 0) {
return `set (${this.reloptions.join(', ')})`
}
return null
}

get alter_reloptions_statement(): string | null {
if (!this.is_alterable) {
return null
}
const clause = this.alter_reloptions_clause
if (clause) {
return this.alter_table_statement(clause)
}
return null
}
}

export interface InspectedFunctionParams {
Expand Down Expand Up @@ -1603,6 +1625,7 @@ export class PostgreSQL extends DBInspector {
rowsecurity: f.rowsecurity,
forcerowsecurity: f.forcerowsecurity,
persistence: f.persistence,
reloptions: f.reloptions,
})

// const RELATIONTYPES = {
Expand Down
Loading