diff --git a/docs/docs/03-cli/03-create.md b/docs/docs/03-cli/03-create.md index e1b2e5b..bba9bef 100644 --- a/docs/docs/03-cli/03-create.md +++ b/docs/docs/03-cli/03-create.md @@ -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')") } ``` @@ -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'") + }, }) } ``` diff --git a/e2e_test.go b/e2e_test.go index 82ce566..ade05bd 100644 --- a/e2e_test.go +++ b/e2e_test.go @@ -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" @@ -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) { diff --git a/pkg/schema/base/base.go b/pkg/schema/base/base.go index adb853f..20be43e 100644 --- a/pkg/schema/base/base.go +++ b/pkg/schema/base/base.go @@ -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()), @@ -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()), @@ -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()), diff --git a/pkg/schema/options.go b/pkg/schema/options.go index 3275a52..9bb97aa 100644 --- a/pkg/schema/options.go +++ b/pkg/schema/options.go @@ -305,6 +305,8 @@ type ExtensionOptions struct { // IfNotExists add IF NOT EXISTS to the query. IfNotExists bool + + Reversible *DropExtensionOptions } func (e ExtensionOptions) EventName() string { @@ -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 { diff --git a/pkg/schema/pg/postgres.go b/pkg/schema/pg/postgres.go index d9682fb..5a18607 100644 --- a/pkg/schema/pg/postgres.go +++ b/pkg/schema/pg/postgres.go @@ -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" @@ -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 } @@ -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)) diff --git a/pkg/schema/pg/postgres_test.go b/pkg/schema/pg/postgres_test.go index ab8def3..e80ad57 100644 --- a/pkg/schema/pg/postgres_test.go +++ b/pkg/schema/pg/postgres_test.go @@ -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" @@ -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 ( @@ -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}) } @@ -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()) @@ -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()) @@ -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() { diff --git a/pkg/schema/pg/testdata/TestPostgres_AddExtension/with_schema.snap.txt b/pkg/schema/pg/testdata/TestPostgres_AddExtension/with_schema.snap.txt index c4c1cc7..5bf43de 100644 --- a/pkg/schema/pg/testdata/TestPostgres_AddExtension/with_schema.snap.txt +++ b/pkg/schema/pg/testdata/TestPostgres_AddExtension/with_schema.snap.txt @@ -1,2 +1,2 @@ -DROP EXTENSION IF EXISTS "hstore" +DROP EXTENSION IF EXISTS "hstore" CASCADE CREATE EXTENSION "hstore" SCHEMA tst_pg_add_extension diff --git a/pkg/schema/pg/testdata/TestPostgres_AddExtension/without_schema.snap.txt b/pkg/schema/pg/testdata/TestPostgres_AddExtension/without_schema.snap.txt index 4bf53ed..0dcb87d 100644 --- a/pkg/schema/pg/testdata/TestPostgres_AddExtension/without_schema.snap.txt +++ b/pkg/schema/pg/testdata/TestPostgres_AddExtension/without_schema.snap.txt @@ -1,2 +1,2 @@ -DROP EXTENSION IF EXISTS "hstore" +DROP EXTENSION IF EXISTS "hstore" CASCADE CREATE EXTENSION "hstore" diff --git a/pkg/templates/init_create_table.go.tmpl b/pkg/templates/init_create_table.go.tmpl index cb60a29..dd36299 100644 --- a/pkg/templates/init_create_table.go.tmpl +++ b/pkg/templates/init_create_table.go.tmpl @@ -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 }) \ No newline at end of file diff --git a/pkg/templates/init_create_table_base.go.tmpl b/pkg/templates/init_create_table_base.go.tmpl index 9ee11c5..c1f5df1 100644 --- a/pkg/templates/init_create_table_base.go.tmpl +++ b/pkg/templates/init_create_table_base.go.tmpl @@ -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 )`) } \ No newline at end of file diff --git a/testdata/Test_2e2_postgres/migration_with_change_migrations_with_change/20240517080505_schema_version.snap.sql b/testdata/Test_2e2_postgres/migration_with_change_migrations_with_change/20240517080505_schema_version.snap.sql index 2b85935..1336e26 100644 --- a/testdata/Test_2e2_postgres/migration_with_change_migrations_with_change/20240517080505_schema_version.snap.sql +++ b/testdata/Test_2e2_postgres/migration_with_change_migrations_with_change/20240517080505_schema_version.snap.sql @@ -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 ); @@ -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); -- diff --git a/testdata/Test_2e2_postgres/migration_with_change_migrations_with_change/20240518071740_create_user.snap.sql b/testdata/Test_2e2_postgres/migration_with_change_migrations_with_change/20240518071740_create_user.snap.sql index 2c4863d..d30a0b1 100644 --- a/testdata/Test_2e2_postgres/migration_with_change_migrations_with_change/20240518071740_create_user.snap.sql +++ b/testdata/Test_2e2_postgres/migration_with_change_migrations_with_change/20240518071740_create_user.snap.sql @@ -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 ); @@ -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); -- diff --git a/testdata/Test_2e2_postgres/migration_with_change_migrations_with_change/20240518071842_add_index_user_email.snap.sql b/testdata/Test_2e2_postgres/migration_with_change_migrations_with_change/20240518071842_add_index_user_email.snap.sql index 9351e07..9e3a3e0 100644 --- a/testdata/Test_2e2_postgres/migration_with_change_migrations_with_change/20240518071842_add_index_user_email.snap.sql +++ b/testdata/Test_2e2_postgres/migration_with_change_migrations_with_change/20240518071842_add_index_user_email.snap.sql @@ -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 ); @@ -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); -- diff --git a/testdata/Test_2e2_postgres/migration_with_change_migrations_with_change/20240518071938_custom_seed.snap.sql b/testdata/Test_2e2_postgres/migration_with_change_migrations_with_change/20240518071938_custom_seed.snap.sql index 9351e07..9e3a3e0 100644 --- a/testdata/Test_2e2_postgres/migration_with_change_migrations_with_change/20240518071938_custom_seed.snap.sql +++ b/testdata/Test_2e2_postgres/migration_with_change_migrations_with_change/20240518071938_custom_seed.snap.sql @@ -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 ); @@ -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); -- diff --git a/testdata/Test_2e2_postgres/migration_with_change_migrations_with_change/20240527192300_enum.snap.sql b/testdata/Test_2e2_postgres/migration_with_change_migrations_with_change/20240527192300_enum.snap.sql index 7be627c..04a6105 100644 --- a/testdata/Test_2e2_postgres/migration_with_change_migrations_with_change/20240527192300_enum.snap.sql +++ b/testdata/Test_2e2_postgres/migration_with_change_migrations_with_change/20240527192300_enum.snap.sql @@ -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 ); @@ -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); -- diff --git a/testdata/Test_2e2_postgres/migration_with_classic_migrations_with_classic/20240517080505_schema_version.snap.sql b/testdata/Test_2e2_postgres/migration_with_classic_migrations_with_classic/20240517080505_schema_version.snap.sql index 90b17f2..6b36507 100644 --- a/testdata/Test_2e2_postgres/migration_with_classic_migrations_with_classic/20240517080505_schema_version.snap.sql +++ b/testdata/Test_2e2_postgres/migration_with_classic_migrations_with_classic/20240517080505_schema_version.snap.sql @@ -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 ); @@ -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); -- diff --git a/testdata/Test_2e2_postgres/migration_with_classic_migrations_with_classic/20240518071740_create_user.snap.sql b/testdata/Test_2e2_postgres/migration_with_classic_migrations_with_classic/20240518071740_create_user.snap.sql index acb143c..4740945 100644 --- a/testdata/Test_2e2_postgres/migration_with_classic_migrations_with_classic/20240518071740_create_user.snap.sql +++ b/testdata/Test_2e2_postgres/migration_with_classic_migrations_with_classic/20240518071740_create_user.snap.sql @@ -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 ); @@ -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); -- diff --git a/testdata/Test_2e2_postgres/migration_with_classic_migrations_with_classic/20240518071842_add_index_user_email.snap.sql b/testdata/Test_2e2_postgres/migration_with_classic_migrations_with_classic/20240518071842_add_index_user_email.snap.sql index 6d36193..bb1559c 100644 --- a/testdata/Test_2e2_postgres/migration_with_classic_migrations_with_classic/20240518071842_add_index_user_email.snap.sql +++ b/testdata/Test_2e2_postgres/migration_with_classic_migrations_with_classic/20240518071842_add_index_user_email.snap.sql @@ -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 ); @@ -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); -- diff --git a/testdata/Test_2e2_postgres/migration_with_classic_migrations_with_classic/20240518071938_custom_seed.snap.sql b/testdata/Test_2e2_postgres/migration_with_classic_migrations_with_classic/20240518071938_custom_seed.snap.sql index 6d36193..bb1559c 100644 --- a/testdata/Test_2e2_postgres/migration_with_classic_migrations_with_classic/20240518071938_custom_seed.snap.sql +++ b/testdata/Test_2e2_postgres/migration_with_classic_migrations_with_classic/20240518071938_custom_seed.snap.sql @@ -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 ); @@ -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); -- diff --git a/testdata/Test_2e2_postgres/migration_with_classic_migrations_with_classic/20240527192355_enum.snap.sql b/testdata/Test_2e2_postgres/migration_with_classic_migrations_with_classic/20240527192355_enum.snap.sql index 24c9fcc..8a1631e 100644 --- a/testdata/Test_2e2_postgres/migration_with_classic_migrations_with_classic/20240527192355_enum.snap.sql +++ b/testdata/Test_2e2_postgres/migration_with_classic_migrations_with_classic/20240527192355_enum.snap.sql @@ -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 ); @@ -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); -- diff --git a/testdata/e2e/pg/migrations_with_change/20240517080505_schema_version.go b/testdata/e2e/pg/migrations_with_change/20240517080505_schema_version.go index b74c375..5cdec2a 100644 --- a/testdata/e2e/pg/migrations_with_change/20240517080505_schema_version.go +++ b/testdata/e2e/pg/migrations_with_change/20240517080505_schema_version.go @@ -1,16 +1,17 @@ package migrations import ( + "time" + "github.com/alexisvisco/amigo/pkg/schema" "github.com/alexisvisco/amigo/pkg/schema/pg" - "time" ) type Migration20240517080505SchemaVersion struct{} func (m Migration20240517080505SchemaVersion) Change(s *pg.Schema) { s.CreateTable("migrations_with_change.mig_schema_versions", func(s *pg.PostgresTableDef) { - s.String("id") + s.String("version", schema.ColumnOptions{PrimaryKey: true}) }, schema.TableOptions{IfNotExists: true}) } diff --git a/testdata/e2e/pg/migrations_with_classic/20240517080505_schema_version.go b/testdata/e2e/pg/migrations_with_classic/20240517080505_schema_version.go index e18a71c..9b6d0ad 100644 --- a/testdata/e2e/pg/migrations_with_classic/20240517080505_schema_version.go +++ b/testdata/e2e/pg/migrations_with_classic/20240517080505_schema_version.go @@ -1,16 +1,17 @@ package migrations import ( + "time" + "github.com/alexisvisco/amigo/pkg/schema" "github.com/alexisvisco/amigo/pkg/schema/pg" - "time" ) type Migration20240517080505SchemaVersion struct{} func (m Migration20240517080505SchemaVersion) Up(s *pg.Schema) { s.CreateTable("migrations_with_classic.mig_schema_versions", func(s *pg.PostgresTableDef) { - s.String("id") + s.String("version", schema.ColumnOptions{PrimaryKey: true}) }, schema.TableOptions{IfNotExists: true}) }