Skip to content

Commit 3160548

Browse files
Remove support for the name field in migrations (#852)
Remove support for specifying a `name` field in migration files: ```yaml name: 01_some_migration # <-- no longer supported operations: - create_table: name: products columns: - name: id type: serial pk: true - name: name type: varchar(255) ``` The migration filename is the source of truth for the migration name. The `name` field was made optional and its use discouraged in the documentation in #744. This PR removes support for it entirely. ⚠️ **Warning** **This is a breaking change:** migration files that set the `name` field will not be runnable by `pgroll` until the `name` field is removed. Because of #812 `pgroll pull` and `pgroll migrate` will still work with migration histories that contain migrations with `name` fields. The easiest way to remove the `name` fields from all migrations in the history is to `pgroll pull` the migration history; this will serialize all migrations in the remote history, removing the any `name` fields. --- Closes #715 and #850
1 parent 052ab79 commit 3160548

File tree

8 files changed

+19
-44
lines changed

8 files changed

+19
-44
lines changed

docs/operations/README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@
55
YAML migration:
66

77
```yaml
8-
name: "0x_migration_name"
98
operations: [...]
109
```
1110
1211
JSON migration:
1312
1413
```json
1514
{
16-
"name": "0x_migration_name",
1715
"operations": [...]
1816
}
1917
```

docs/tutorial.md

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ With `pgroll` initialized, let's run our first migration. Here is a migration to
4242

4343
<YamlJsonTabs>
4444
```yaml
45-
name: "01_create_users_table"
4645
operations:
4746
- create_table:
4847
name: users
@@ -59,7 +58,6 @@ operations:
5958
```
6059
```json
6160
{
62-
"name": "01_create_users_table",
6361
"operations": [
6462
{
6563
"create_table": {
@@ -90,8 +88,6 @@ operations:
9088

9189
Take this file and save it as `sql/01_create_users_table.yaml`.
9290

93-
> Note: The `name` field is optional. If not provided, `pgroll` will use the filename (without the `.yaml` extension) as the migration name. In this example, the file is saved as `sql/01_create_users_table.yaml`, so the name would be `01_create_users_table` if not explicitly provided. It's recommended to write migrations without an explicit `name` field and allow the filename to determine the name of the migration.
94-
9591
The migration will create a `users` table with three columns. It is equivalent to the following SQL DDL statement:
9692

9793
```sql
@@ -152,7 +148,6 @@ Here is the `pgroll` migration that will perform the migration to make the `desc
152148

153149
<YamlJsonTabs>
154150
```yaml
155-
name: "02_user_description_set_nullable"
156151
operations:
157152
- alter_column:
158153
table: users
@@ -163,7 +158,6 @@ operations:
163158
```
164159
```json
165160
{
166-
"name": "02_user_description_set_nullable",
167161
"operations": [
168162
{
169163
"alter_column": {
@@ -179,8 +173,6 @@ operations:
179173
```
180174
</YamlJsonTabs>
181175

182-
> As mentioned earlier, the `name` field is optional. If the file is saved as `sql/02_user_description_set_nullable.yaml`, `pgroll` will use `02_user_description_set_nullable` as the migration name if the name field is not provided.
183-
184176
Save this migration as `sql/02_user_description_set_nullable.yaml` and start the migration:
185177

186178
```
@@ -478,7 +470,6 @@ Looking at the second of these items, rollbacks, let's see how to roll back a `p
478470

479471
<YamlJsonTabs>
480472
```yaml
481-
name: "03_add_is_active_column"
482473
operations:
483474
- add_column:
484475
table: users
@@ -490,7 +481,6 @@ operations:
490481
```
491482
```json
492483
{
493-
"name": "03_add_is_active_column",
494484
"operations": [
495485
{
496486
"add_column": {
@@ -508,7 +498,7 @@ operations:
508498
```
509499
</YamlJsonTabs>
510500

511-
> Again, the `name` field is optional. The filename will be used as the default name if not specified.
501+
Create this migration and save it as `sql/03_add_is_active_column.yaml`.
512502

513503
(the misspelling of `is_active` is intentional!)
514504

pkg/migrations/migrations.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ type RequiresSchemaRefreshOperation interface {
6868
type (
6969
Operations []Operation
7070
Migration struct {
71-
Name string `json:"name,omitempty"`
71+
Name string `json:"-"`
7272
Operations Operations `json:"operations"`
7373
}
7474
RawMigration struct {
75-
Name string `json:"name"`
75+
Name string `json:"-"`
7676
Operations json.RawMessage `json:"operations"`
7777
}
7878
)

pkg/migrations/op_common.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,8 @@ func ReadRawMigration(dir fs.FS, filename string) (*RawMigration, error) {
118118
return nil, fmt.Errorf("reading migration file: %w", err)
119119
}
120120

121-
if mig.Name == "" {
122-
// Extract base filename without extension as the default migration name
123-
mig.Name = strings.TrimSuffix(filepath.Base(filename), filepath.Ext(filename))
124-
}
121+
// Extract base filename wiahout extension as the migration name
122+
mig.Name = strings.TrimSuffix(filepath.Base(filename), filepath.Ext(filename))
125123

126124
return &mig, nil
127125
}

pkg/roll/unapplied_test.go

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"context"
77
"database/sql"
88
"encoding/json"
9-
"fmt"
109
"maps"
1110
"slices"
1211
"testing"
@@ -54,12 +53,11 @@ func TestUnappliedMigrations(t *testing.T) {
5453
ctx := context.Background()
5554

5655
// Unmarshal the first migration from the migrations directory
57-
var migration migrations.Migration
58-
err := json.Unmarshal(fs["01_migration_1.json"].Data, &migration)
56+
migration, err := migrations.ReadMigration(fs, "01_migration_1.json")
5957
require.NoError(t, err)
6058

6159
// Apply the first migration
62-
err = roll.Start(ctx, &migration, backfill.NewConfig())
60+
err = roll.Start(ctx, migration, backfill.NewConfig())
6361
require.NoError(t, err)
6462
err = roll.Complete(ctx)
6563
require.NoError(t, err)
@@ -87,11 +85,10 @@ func TestUnappliedMigrations(t *testing.T) {
8785

8886
// Unmarshal and apply all migrations from the migrations directory
8987
for _, filename := range slices.Sorted(maps.Keys(fs)) {
90-
var migration migrations.Migration
91-
err := json.Unmarshal(fs[filename].Data, &migration)
88+
migration, err := migrations.ReadMigration(fs, filename)
9289
require.NoError(t, err)
9390

94-
err = roll.Start(ctx, &migration, backfill.NewConfig())
91+
err = roll.Start(ctx, migration, backfill.NewConfig())
9592
require.NoError(t, err)
9693
err = roll.Complete(ctx)
9794
require.NoError(t, err)
@@ -284,12 +281,11 @@ func TestUnappliedMigrationsWithBaselines(t *testing.T) {
284281
require.NoError(t, err)
285282

286283
// Unmarshal the migration that comes after the baseline version
287-
var migration migrations.Migration
288-
err = json.Unmarshal(fs["03_migration_3.json"].Data, &migration)
284+
migration, err := migrations.ReadMigration(fs, "03_migration_3.json")
289285
require.NoError(t, err)
290286

291287
// Apply the migration
292-
err = roll.Start(ctx, &migration, backfill.NewConfig())
288+
err = roll.Start(ctx, migration, backfill.NewConfig())
293289
require.NoError(t, err)
294290
err = roll.Complete(ctx)
295291
require.NoError(t, err)
@@ -353,10 +349,9 @@ func TestUnappliedMigrationsWithBaselines(t *testing.T) {
353349
require.NoError(t, err)
354350

355351
// Apply a migration after the first baseline
356-
var migration migrations.Migration
357-
err = json.Unmarshal(fs["03_migration_3.json"].Data, &migration)
352+
migration, err := migrations.ReadMigration(fs, "03_migration_3.json")
358353
require.NoError(t, err)
359-
err = roll.Start(ctx, &migration, backfill.NewConfig())
354+
err = roll.Start(ctx, migration, backfill.NewConfig())
360355
require.NoError(t, err)
361356
err = roll.Complete(ctx)
362357
require.NoError(t, err)
@@ -412,12 +407,11 @@ func TestUnappliedMigrationsWithOldMigrationFormats(t *testing.T) {
412407
ctx := context.Background()
413408

414409
// Unmarshal the first migration from the migrations directory
415-
var migration migrations.Migration
416-
err := json.Unmarshal(fs["01_migration_1.json"].Data, &migration)
410+
migration, err := migrations.ReadMigration(fs, "01_migration_1.json")
417411
require.NoError(t, err)
418412

419413
// Apply the first migration
420-
err = roll.Start(ctx, &migration, backfill.NewConfig())
414+
err = roll.Start(ctx, migration, backfill.NewConfig())
421415
require.NoError(t, err)
422416
err = roll.Complete(ctx)
423417
require.NoError(t, err)
@@ -465,17 +459,15 @@ func exampleMigration(t *testing.T, name string) []byte {
465459
func unDeserializableMigration(t *testing.T, name string) []byte {
466460
t.Helper()
467461

468-
migJSON := fmt.Sprintf(`{
469-
"name": "%s",
462+
migJSON := `{
470463
"operations": [
471464
{
472465
"sql": {
473466
"upxxx": "SELECT 1"
474467
}
475468
}
476469
]
477-
}
478-
`, name)
470+
}`
479471

480472
return []byte(migJSON)
481473
}

pkg/state/history.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ func (s *State) SchemaHistory(ctx context.Context, schema string) ([]HistoryEntr
6262
if err != nil {
6363
return nil, err
6464
}
65+
mig.Name = name
6566

6667
entries = append(entries, HistoryEntry{
6768
Migration: mig,

pkg/state/state.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ func (s *State) GetActiveMigration(ctx context.Context, schema string) (*migrati
168168
if err != nil {
169169
return nil, fmt.Errorf("unable to unmarshal migration: %w", err)
170170
}
171+
migration.Name = name
171172

172173
return &migration, nil
173174
}

pkg/state/state_test.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"database/sql"
88
"encoding/json"
99
"fmt"
10-
"strings"
1110
"sync"
1211
"testing"
1312
"time"
@@ -260,10 +259,6 @@ func TestInferredMigration(t *testing.T) {
260259
for i, wantMigration := range tt.wantMigrations {
261260
gotMigration := gotMigrations[i]
262261

263-
// test there is a name for the migration, then remove it for the comparison
264-
assert.True(t, strings.HasPrefix(gotMigration.Name, "sql_") && len(gotMigration.Name) > 10)
265-
gotMigration.Name = ""
266-
267262
assert.Equal(t, wantMigration, gotMigration)
268263
}
269264
})

0 commit comments

Comments
 (0)