Skip to content

Commit

Permalink
change: id to version in the table migration
Browse files Browse the repository at this point in the history
  • Loading branch information
alexisvisco committed Oct 31, 2024
1 parent 926cead commit d70ceed
Show file tree
Hide file tree
Showing 22 changed files with 65 additions and 49 deletions.
12 changes: 7 additions & 5 deletions docs/docs/03-cli/03-create.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ Suppose you have this migration:
```go
func (m Migration20240502155033SchemaVersion) Change(s *pg.Schema) {
s.CreateTable("public.mig_schema_versions", func(s *pg.PostgresTableDef) {
s.String("id")
s.String("version")
})

s.Exec("INSERT INTO public.mig_schema_versions (id) VALUES ('1')")
s.Exec("INSERT INTO public.mig_schema_versions (version) VALUES ('1')")
}
```

Expand All @@ -96,15 +96,17 @@ To avoid this problem, you need to use the `Reversible` method.
```go
func (m Migration20240502155033SchemaVersion) Change(s *pg.Schema) {
s.CreateTable("public.mig_schema_versions", func(s *pg.PostgresTableDef) {
s.String("id")
s.String("version")
})

s.Reversible(schema.Directions{
Up: func() {
s.Exec("INSERT INTO public.mig_schema_versions (id) VALUES ('1')")
s.Exec("INSERT INTO public.mig_schema_versions (version) VALUES ('1')")
},

Down: func() { },
Down: func() {
s.Exec("DELETE FROM public.mig_schema_versions WHERE version = '1'")
},
})
}
```
Expand Down
7 changes: 4 additions & 3 deletions e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ package main
import (
"database/sql"
"fmt"
"os"
"path"
"testing"

"github.com/alexisvisco/amigo/pkg/amigo"
"github.com/alexisvisco/amigo/pkg/amigoctx"
"github.com/alexisvisco/amigo/pkg/schema"
Expand All @@ -15,9 +19,6 @@ import (
_ "github.com/jackc/pgx/v5/stdlib"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"os"
"path"
"testing"
)

func Test_2e2_postgres(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions pkg/schema/base/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (p *Schema) rollbackMode() *Schema {
// AddVersion adds a new version to the schema_migrations table.
// This function is not reversible.
func (p *Schema) AddVersion(version string) {
sql := `INSERT INTO {version_table} (id) VALUES ({version})`
sql := `INSERT INTO {version_table} (version) VALUES ({version})`

replacer := utils.Replacer{
"version_table": utils.StrFunc(p.Context.MigratorOptions.SchemaVersionTable.String()),
Expand All @@ -67,7 +67,7 @@ func (p *Schema) AddVersion(version string) {
// RemoveVersion removes a version from the schema_migrations table.
// This function is not reversible.
func (p *Schema) RemoveVersion(version string) {
sql := `DELETE FROM {version_table} WHERE id = {version}`
sql := `DELETE FROM {version_table} WHERE version = {version}`

replacer := utils.Replacer{
"version_table": utils.StrFunc(p.Context.MigratorOptions.SchemaVersionTable.String()),
Expand All @@ -85,7 +85,7 @@ func (p *Schema) RemoveVersion(version string) {

// FindAppliedVersions returns all the applied versions in the schema_migrations table.
func (p *Schema) FindAppliedVersions() []string {
sql := `SELECT id FROM {version_table} ORDER BY id ASC`
sql := `SELECT version FROM {version_table} ORDER BY version ASC`

replacer := utils.Replacer{
"version_table": utils.StrFunc(p.Context.MigratorOptions.SchemaVersionTable.String()),
Expand Down
4 changes: 4 additions & 0 deletions pkg/schema/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,8 @@ type ExtensionOptions struct {

// IfNotExists add IF NOT EXISTS to the query.
IfNotExists bool

Reversible *DropExtensionOptions
}

func (e ExtensionOptions) EventName() string {
Expand All @@ -323,6 +325,8 @@ type DropExtensionOptions struct {

// Reversible will allow the migrator to reverse the operation by creating the extension.
Reversible *ExtensionOptions

Cascade bool
}

func (e DropExtensionOptions) EventName() string {
Expand Down
10 changes: 8 additions & 2 deletions pkg/schema/pg/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pg

import (
"fmt"

"github.com/alexisvisco/amigo/pkg/schema"
"github.com/alexisvisco/amigo/pkg/schema/base"
"github.com/alexisvisco/amigo/pkg/types"
Expand Down Expand Up @@ -71,7 +72,11 @@ func (p *Schema) AddExtension(name string, option ...schema.ExtensionOptions) {
options.ExtensionName = p.toExtension(name)

if p.Context.MigrationDirection == types.MigrationDirectionDown {
p.rollbackMode().DropExtension(options.ExtensionName, schema.DropExtensionOptions{IfExists: true})
extensionOptions := schema.DropExtensionOptions{IfExists: true}
if options.Reversible != nil {
extensionOptions.Cascade = options.Reversible.Cascade
}
p.rollbackMode().DropExtension(options.ExtensionName, extensionOptions)
return
}

Expand Down Expand Up @@ -131,11 +136,12 @@ func (p *Schema) DropExtension(name string, opt ...schema.DropExtensionOptions)
return
}

sql := `DROP EXTENSION {if_exists} "{name}"`
sql := `DROP EXTENSION {if_exists} "{name}" {cascade}`

replacer := utils.Replacer{
"if_exists": utils.StrFuncPredicate(options.IfExists, "IF EXISTS"),
"name": utils.StrFunc(p.toExtension(options.ExtensionName)),
"cascade": utils.StrFuncPredicate(options.Cascade, "CASCADE"),
}

_, err := p.TX.ExecContext(p.Context.Context, replacer.Replace(sql))
Expand Down
17 changes: 9 additions & 8 deletions pkg/schema/pg/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import (
"context"
"database/sql"
"fmt"
"log/slog"
"os"
"strings"
"testing"

"github.com/alexisvisco/amigo/pkg/schema"
"github.com/alexisvisco/amigo/pkg/utils"
"github.com/alexisvisco/amigo/pkg/utils/dblog"
Expand All @@ -12,10 +17,6 @@ import (
_ "github.com/jackc/pgx/v5/stdlib"
sqldblogger "github.com/simukti/sqldb-logger"
"github.com/stretchr/testify/require"
"log/slog"
"os"
"strings"
"testing"
)

var (
Expand All @@ -39,7 +40,7 @@ var (

func versionTable(schemaName string, s *Schema) {
s.CreateTable(schema.Table("mig_schema_version", schemaName), func(s *PostgresTableDef) {
s.String("id")
s.String("version")
}, schema.TableOptions{IfNotExists: true})
}

Expand Down Expand Up @@ -111,7 +112,7 @@ func TestPostgres_AddExtension(t *testing.T) {
t.Run("with schema", func(t *testing.T) {
p, r, schemaName := baseTest(t, "select 1", sc)

p.DropExtension("hstore", schema.DropExtensionOptions{IfExists: true})
p.DropExtension("hstore", schema.DropExtensionOptions{IfExists: true, Cascade: true})
p.AddExtension("hstore", schema.ExtensionOptions{Schema: schemaName})

testutils.AssertSnapshotDiff(t, r.FormatRecords())
Expand All @@ -120,7 +121,7 @@ func TestPostgres_AddExtension(t *testing.T) {
t.Run("without schema", func(t *testing.T) {
p, r, _ := baseTest(t, "select 1", sc, 1)

p.DropExtension("hstore", schema.DropExtensionOptions{IfExists: true})
p.DropExtension("hstore", schema.DropExtensionOptions{IfExists: true, Cascade: true})
p.AddExtension("hstore", schema.ExtensionOptions{})

testutils.AssertSnapshotDiff(t, r.FormatRecords())
Expand All @@ -129,7 +130,7 @@ func TestPostgres_AddExtension(t *testing.T) {
t.Run("with IfNotExists", func(t *testing.T) {
p, _, schemaName := baseTest(t, "select 1", sc, 2)

p.DropExtension("hstore", schema.DropExtensionOptions{IfExists: true})
p.DropExtension("hstore", schema.DropExtensionOptions{IfExists: true, Cascade: true})
p.AddExtension("hstore", schema.ExtensionOptions{Schema: schemaName})

require.Panics(t, func() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
DROP EXTENSION IF EXISTS "hstore"
DROP EXTENSION IF EXISTS "hstore" CASCADE
CREATE EXTENSION "hstore" SCHEMA tst_pg_add_extension
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
DROP EXTENSION IF EXISTS "hstore"
DROP EXTENSION IF EXISTS "hstore" CASCADE
CREATE EXTENSION "hstore"
2 changes: 1 addition & 1 deletion pkg/templates/init_create_table.go.tmpl
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
s.CreateTable("{{ .Name }}", func(s *pg.PostgresTableDef) {
s.String("id")
s.String("version", schema.ColumnOptions{ PrimaryKey: true })
}, schema.TableOptions{ IfNotExists: true })
2 changes: 1 addition & 1 deletion pkg/templates/init_create_table_base.go.tmpl
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
s.Exec(`CREATE TABLE IF NOT EXISTS {{ .Name }} ( "id" VARCHAR(255) NOT NULL PRIMARY KEY )`)
s.Exec(`CREATE TABLE IF NOT EXISTS {{ .Name }} ( "version" VARCHAR(255) NOT NULL PRIMARY KEY )`)
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ SET default_table_access_method = heap;
--

CREATE TABLE migrations_with_change.mig_schema_versions (
id text NOT NULL
version text NOT NULL
);


Expand All @@ -32,7 +32,7 @@ CREATE TABLE migrations_with_change.mig_schema_versions (
--

ALTER TABLE ONLY migrations_with_change.mig_schema_versions
ADD CONSTRAINT mig_schema_versions_pkey PRIMARY KEY (id);
ADD CONSTRAINT mig_schema_versions_pkey PRIMARY KEY (version);


--
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ SET default_table_access_method = heap;
--

CREATE TABLE migrations_with_change.mig_schema_versions (
id text NOT NULL
version text NOT NULL
);


Expand Down Expand Up @@ -72,7 +72,7 @@ ALTER TABLE ONLY migrations_with_change.users ALTER COLUMN id SET DEFAULT nextva
--

ALTER TABLE ONLY migrations_with_change.mig_schema_versions
ADD CONSTRAINT mig_schema_versions_pkey PRIMARY KEY (id);
ADD CONSTRAINT mig_schema_versions_pkey PRIMARY KEY (version);


--
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ SET default_table_access_method = heap;
--

CREATE TABLE migrations_with_change.mig_schema_versions (
id text NOT NULL
version text NOT NULL
);


Expand Down Expand Up @@ -72,7 +72,7 @@ ALTER TABLE ONLY migrations_with_change.users ALTER COLUMN id SET DEFAULT nextva
--

ALTER TABLE ONLY migrations_with_change.mig_schema_versions
ADD CONSTRAINT mig_schema_versions_pkey PRIMARY KEY (id);
ADD CONSTRAINT mig_schema_versions_pkey PRIMARY KEY (version);


--
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ SET default_table_access_method = heap;
--

CREATE TABLE migrations_with_change.mig_schema_versions (
id text NOT NULL
version text NOT NULL
);


Expand Down Expand Up @@ -72,7 +72,7 @@ ALTER TABLE ONLY migrations_with_change.users ALTER COLUMN id SET DEFAULT nextva
--

ALTER TABLE ONLY migrations_with_change.mig_schema_versions
ADD CONSTRAINT mig_schema_versions_pkey PRIMARY KEY (id);
ADD CONSTRAINT mig_schema_versions_pkey PRIMARY KEY (version);


--
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ SET default_table_access_method = heap;
--

CREATE TABLE migrations_with_change.mig_schema_versions (
id text NOT NULL
version text NOT NULL
);


Expand Down Expand Up @@ -82,7 +82,7 @@ ALTER TABLE ONLY migrations_with_change.users ALTER COLUMN id SET DEFAULT nextva
--

ALTER TABLE ONLY migrations_with_change.mig_schema_versions
ADD CONSTRAINT mig_schema_versions_pkey PRIMARY KEY (id);
ADD CONSTRAINT mig_schema_versions_pkey PRIMARY KEY (version);


--
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ SET default_table_access_method = heap;
--

CREATE TABLE migrations_with_classic.mig_schema_versions (
id text NOT NULL
version text NOT NULL
);


Expand All @@ -32,7 +32,7 @@ CREATE TABLE migrations_with_classic.mig_schema_versions (
--

ALTER TABLE ONLY migrations_with_classic.mig_schema_versions
ADD CONSTRAINT mig_schema_versions_pkey PRIMARY KEY (id);
ADD CONSTRAINT mig_schema_versions_pkey PRIMARY KEY (version);


--
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ SET default_table_access_method = heap;
--

CREATE TABLE migrations_with_classic.mig_schema_versions (
id text NOT NULL
version text NOT NULL
);


Expand Down Expand Up @@ -72,7 +72,7 @@ ALTER TABLE ONLY migrations_with_classic.users ALTER COLUMN id SET DEFAULT nextv
--

ALTER TABLE ONLY migrations_with_classic.mig_schema_versions
ADD CONSTRAINT mig_schema_versions_pkey PRIMARY KEY (id);
ADD CONSTRAINT mig_schema_versions_pkey PRIMARY KEY (version);


--
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ SET default_table_access_method = heap;
--

CREATE TABLE migrations_with_classic.mig_schema_versions (
id text NOT NULL
version text NOT NULL
);


Expand Down Expand Up @@ -72,7 +72,7 @@ ALTER TABLE ONLY migrations_with_classic.users ALTER COLUMN id SET DEFAULT nextv
--

ALTER TABLE ONLY migrations_with_classic.mig_schema_versions
ADD CONSTRAINT mig_schema_versions_pkey PRIMARY KEY (id);
ADD CONSTRAINT mig_schema_versions_pkey PRIMARY KEY (version);


--
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ SET default_table_access_method = heap;
--

CREATE TABLE migrations_with_classic.mig_schema_versions (
id text NOT NULL
version text NOT NULL
);


Expand Down Expand Up @@ -72,7 +72,7 @@ ALTER TABLE ONLY migrations_with_classic.users ALTER COLUMN id SET DEFAULT nextv
--

ALTER TABLE ONLY migrations_with_classic.mig_schema_versions
ADD CONSTRAINT mig_schema_versions_pkey PRIMARY KEY (id);
ADD CONSTRAINT mig_schema_versions_pkey PRIMARY KEY (version);


--
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ SET default_table_access_method = heap;
--

CREATE TABLE migrations_with_classic.mig_schema_versions (
id text NOT NULL
version text NOT NULL
);


Expand Down Expand Up @@ -82,7 +82,7 @@ ALTER TABLE ONLY migrations_with_classic.users ALTER COLUMN id SET DEFAULT nextv
--

ALTER TABLE ONLY migrations_with_classic.mig_schema_versions
ADD CONSTRAINT mig_schema_versions_pkey PRIMARY KEY (id);
ADD CONSTRAINT mig_schema_versions_pkey PRIMARY KEY (version);


--
Expand Down
Loading

0 comments on commit d70ceed

Please sign in to comment.