Skip to content

Commit 2147a1d

Browse files
committed
Implement ALTER TABLE ... SET TABLESPACE ... as a pgloader clause.
This allows creating tables in any target tablespace rather than the default one, and is supported for the various sources having support for the ALTER TABLE clause already.
1 parent f28f8e5 commit 2147a1d

File tree

10 files changed

+38
-7
lines changed

10 files changed

+38
-7
lines changed

docs/ref/mssql.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ actions are *SET SCHEMA*, *RENAME TO*, and *SET*::
139139
ALTER TABLE NAMES MATCHING 'film' IN SCHEMA 'dbo' RENAME TO 'films'
140140
141141
ALTER TABLE NAMES MATCHING ~/./ IN SCHEMA 'dbo' SET (fillfactor='40')
142+
143+
ALTER TABLE NAMES MATCHING ~/./ IN SCHEMA 'dbo' SET TABLESPACE 'tlbspc'
142144

143145
You can use as many such rules as you need. The list of tables to be
144146
migrated is searched in pgloader memory against the *ALTER TABLE* matching
@@ -153,6 +155,9 @@ schema. In case of a name change, the mapping is kept and reused in the
153155
The *SET ()* action takes effect as a *WITH* clause for the `CREATE TABLE`
154156
command that pgloader will run when it has to create a table.
155157

158+
The *SET TABLESPACE* action takes effect as a *TABLESPACE* clause for the
159+
`CREATE TABLE` command that pgloader will run when it has to create a table.
160+
156161
The matching is done in pgloader itself, with a Common Lisp regular
157162
expression lib, so doesn't depend on the *LIKE* implementation of MS SQL,
158163
nor on the lack of support for regular expressions in the engine.

docs/ref/mysql.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,8 @@ actions are *SET SCHEMA*, *RENAME TO*, and *SET*::
509509
510510
ALTER TABLE NAMES MATCHING ~/./ SET (fillfactor='40')
511511

512+
ALTER TABLE NAMES MATCHING ~/./ SET TABLESPACE 'pg_default'
513+
512514
You can use as many such rules as you need. The list of tables to be
513515
migrated is searched in pgloader memory against the *ALTER TABLE* matching
514516
rules, and for each command pgloader stops at the first matching criteria
@@ -522,6 +524,9 @@ schema. In case of a name change, the mapping is kept and reused in the
522524
The *SET ()* action takes effect as a *WITH* clause for the `CREATE TABLE`
523525
command that pgloader will run when it has to create a table.
524526

527+
The *SET TABLESPACE* action takes effect as a *TABLESPACE* clause for the
528+
`CREATE TABLE` command that pgloader will run when it has to create a table.
529+
525530
MySQL Migration: limitations
526531
----------------------------
527532

docs/ref/pgsql.rst

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -346,16 +346,18 @@ ALTER TABLE NAMES MATCHING
346346
^^^^^^^^^^^^^^^^^^^^^^^^^^
347347

348348
Introduce a comma separated list of table names or *regular expressions*
349-
that you want to target in the pgloader *ALTER TABLE* command. The only two
350-
available actions are *SET SCHEMA* and *RENAME TO*, both take a quoted
351-
string as parameter::
349+
that you want to target in the pgloader *ALTER TABLE* command. Available
350+
actions are *SET SCHEMA*, *RENAME TO*, and *SET*::
352351

353352
ALTER TABLE NAMES MATCHING ~/_list$/, 'sales_by_store', ~/sales_by/
353+
IN SCHEMA 'public'
354354
SET SCHEMA 'mv'
355355
356-
ALTER TABLE NAMES MATCHING 'film' RENAME TO 'films'
356+
ALTER TABLE NAMES MATCHING 'film' IN SCHEMA 'public' RENAME TO 'films'
357357
358-
ALTER TABLE NAMES MATCHING ~/./ SET (fillfactor='40')
358+
ALTER TABLE NAMES MATCHING ~/./ IN SCHEMA 'public' SET (fillfactor='40')
359+
360+
ALTER TABLE NAMES MATCHING ~/./ IN SCHEMA 'public' SET TABLESPACE 'pg_default'
359361

360362
You can use as many such rules as you need. The list of tables to be
361363
migrated is searched in pgloader memory against the *ALTER TABLE* matching
@@ -370,6 +372,9 @@ schema. In case of a name change, the mapping is kept and reused in the
370372
The *SET ()* action takes effect as a *WITH* clause for the `CREATE TABLE`
371373
command that pgloader will run when it has to create a table.
372374

375+
The *SET TABLESPACE* action takes effect as a *TABLESPACE* clause for the
376+
`CREATE TABLE` command that pgloader will run when it has to create a table.
377+
373378
PostgreSQL Migration: limitations
374379
---------------------------------
375380

src/package.lisp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
#:table-oid
9595
#:table-comment
9696
#:table-storage-parameter-list
97+
#:table-tablespace
9798
#:table-field-list
9899
#:table-column-list
99100
#:table-index-list

src/parsers/command-alter-table.lisp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,14 @@
4747
(bind (((_ _ parameters _) stmt))
4848
(list #'pgloader.catalog::alter-table-set-storage-parameters parameters))))
4949

50+
(defrule set-tablespace (and kw-set kw-tablespace quoted-namestring)
51+
(:lambda (stmt)
52+
(list #'pgloader.catalog::alter-table-set-tablespace (third stmt))))
53+
5054
(defrule alter-table-action (or rename-to
5155
set-schema
52-
set-storage-parameters))
56+
set-storage-parameters
57+
set-tablespace))
5358

5459
(defrule alter-table-command (and alter-table-names-matching
5560
(? in-schema)

src/parsers/command-keywords.lisp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
(def-keyword-rule "with")
2727
(def-keyword-rule "when")
2828
(def-keyword-rule "set")
29+
(def-keyword-rule "tablespace")
2930
(def-keyword-rule "database")
3031
(def-keyword-rule "messages")
3132
(def-keyword-rule "matches")

src/pgsql/pgsql-ddl.lisp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@
9292
(alexandria:alist-plist
9393
(table-storage-parameter-list table))))
9494

95+
(when (table-tablespace table)
96+
(format s "~%TABLESPACE ~a" (table-tablespace table)))
97+
9598
(format s ";~%"))))
9699

97100
(defmethod format-drop-sql ((table table) &key (stream nil) cascade (if-exists t))

src/utils/alter-table.lisp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@
7575
"Alter the storage parameters of TABLE."
7676
(setf (table-storage-parameter-list table) parameters))
7777

78+
(defun alter-table-set-tablespace (table tablespace)
79+
"Alter the tablespace slot of TABLE"
80+
(setf (table-tablespace table) tablespace))
81+
7882

7983
;;;
8084
;;; Apply the match rules as given by the parser to a table name.

src/utils/catalog.lisp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@
4747
(defstruct schema source-name name catalog in-search-path
4848
table-list view-list extension-list sqltype-list)
4949

50-
(defstruct table source-name name schema oid comment storage-parameter-list
50+
(defstruct table source-name name schema oid comment
51+
storage-parameter-list tablespace
5152
;; field is for SOURCE
5253
;; column is for TARGET
5354
;; citus is an extra slot for citus support

test/mysql/my.load

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ load database
99
quote identifiers
1010

1111
ALTER SCHEMA 'pgloader' RENAME TO 'mysql'
12+
ALTER TABLE NAMES MATCHING ~/./ SET TABLESPACE 'pg_default'
1213

1314
CAST column utilisateurs__Yvelines2013-06-28.sexe
1415
to text drop not null using empty-string-to-null,

0 commit comments

Comments
 (0)