@@ -41,9 +41,9 @@ metagration is a script defined entirely within the database, there is
41
41
no external migration tool or language.
42
42
43
43
Metagration ` script ` s are what move the database from one revision to
44
- the next. Forward metagration runs the "up" procedure, and backward
45
- metagration runs the "down" procedure to undo the "up" operation.
46
- Script procedures can be written in * any* supported stored procedure
44
+ the next. Each script has a forward "up" procedure, and optionally a
45
+ backward "down" procedure to undo the "up" operation. Script
46
+ procedures can be written in * any* supported stored procedure
47
47
language. Metagration strictly enforces the revision order of the
48
48
scripts applied.
49
49
@@ -59,7 +59,7 @@ counterparts:
59
59
--------
60
60
1
61
61
62
- This creates a new revision ` 1 ` . The function
62
+ This creates a new script with revision ` 1 ` . The function
63
63
` metagration.new_script(up[, down]) ` expands the up and down code into
64
64
dynamically created plpgsql functions. Once the script is created, it
65
65
can then be run with ` metagration.run() `
@@ -154,12 +154,12 @@ variable `i` in the `FOR` loops are declared in the `up_declare` and
154
154
SELECT new_script(
155
155
$up$
156
156
FOR i IN (SELECT * FROM generate_series(1, (args->>'target')::bigint, 1)) LOOP
157
- EXECUTE format('CREATE TABLE %I (id serial)', 'forks_ ' || i);
157
+ EXECUTE format('CREATE TABLE %I (id serial)', 'foo_ ' || i);
158
158
END LOOP
159
159
$up$,
160
160
$down$
161
161
FOR i IN (SELECT * FROM generate_series(1, (args->>'target')::bigint, 1)) LOOP
162
- EXECUTE format('DROP TABLE %I', 'forks_ ' || i);
162
+ EXECUTE format('DROP TABLE %I', 'foo_ ' || i);
163
163
END LOOP
164
164
$down$,
165
165
up_declare:='i bigint',
@@ -171,11 +171,11 @@ To run, pass an integer value for the `target` jsonb key in `args`:
171
171
# CALL metagration.run(args:=jsonb_build_object('target', 3));
172
172
# \dt+
173
173
List of relations
174
- Schema | Name | Type | Owner | Size | Description
175
- --------+--------- +-------+----------+---------+-------------
176
- public | forks_1 | table | postgres | 0 bytes |
177
- public | forks_2 | table | postgres | 0 bytes |
178
- public | forks_3 | table | postgres | 0 bytes |
174
+ Schema | Name | Type | Owner | Size | Description
175
+ --------+-------+-------+----------+---------+-------------
176
+ public | foo_1 | table | postgres | 0 bytes |
177
+ public | foo_2 | table | postgres | 0 bytes |
178
+ public | foo_3 | table | postgres | 0 bytes |
179
179
180
180
If your up script depends on ` args ` , it's likely your down scripts do
181
181
too. Pass them as well to revert or, get the args used in the up
@@ -200,26 +200,26 @@ migration from the `migration.log` table where they are saved.
200
200
201
201
The obvious question is, if metagrations are stored procedures that
202
202
makes DDL changes, who CREATEs the metagrations? They can be created
203
- programatically as shown above with ` new_script ` , or they can be
204
- import and exported using any SQL client or tool.
203
+ programatically as shown above with ` new_script ` or by inserting
204
+ directly into the ` metagraiton.script ` table. They can be imported and
205
+ exported using any PostgreSQL client or admin tool. Because
206
+ metagrations are in-database, they are dumped and restored when the
207
+ database is backed up.
205
208
206
209
You can still check your metagrations into source control and stream
207
210
them into a new database when you initialize it, then call
208
211
` metagrate.run() ` .
209
212
210
- Because metagrations are in-database, they are dumped and restored
211
- when the database is backed up.
212
-
213
- Since this process is * decoupled* from the actual migration, it can be
214
- done using any of the many database management tools for
215
- PostgreSQL. Because metagration scripts are stored procedures, they
216
- are stateless database objects that can be exported, imported, dropped
217
- and re-created as necessary.
213
+ Since this process of creating metagrations is * decoupled* from the
214
+ actual act of migration, it can be done using any of the many database
215
+ management tools for PostgreSQL. Because metagration scripts are
216
+ stored procedures, they are stateless database objects that can be
217
+ exported, imported, dropped and re-created as necessary.
218
218
219
219
A helpful tool for doing this ` metagration.export() ` . The ` export() `
220
220
function will generate SQL script file that ` CREATE OR REPLACE ` s the
221
- migration scripts. Simply capture the output of this function, for
222
- example with:
221
+ migration scripts and optionally clear and run them. Simply capture
222
+ the output of this function, for example with:
223
223
224
224
psql -A -t -U postgres -c 'select metagration.export()' > export_file.sql
225
225
0 commit comments