123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318 |
- package migrate
- import (
- "context"
- "golang-fave/engine/sqlw"
- )
- func Migrate_000000021(ctx context.Context, db *sqlw.DB, host string) error {
- // Drop foreign keys
- if _, err := db.Exec(ctx, `ALTER TABLE blog_cat_post_rel DROP FOREIGN KEY FK_blog_cat_post_rel_post_id;`); err != nil {
- return err
- }
- if _, err := db.Exec(ctx, `ALTER TABLE blog_cat_post_rel DROP FOREIGN KEY FK_blog_cat_post_rel_category_id;`); err != nil {
- return err
- }
- if _, err := db.Exec(ctx, `ALTER TABLE blog_cats DROP FOREIGN KEY FK_blog_cats_user;`); err != nil {
- return err
- }
- if _, err := db.Exec(ctx, `ALTER TABLE blog_posts DROP FOREIGN KEY FK_blog_posts_user;`); err != nil {
- return err
- }
- if _, err := db.Exec(ctx, `ALTER TABLE blog_posts DROP FOREIGN KEY FK_blog_posts_category;`); err != nil {
- return err
- }
- if _, err := db.Exec(ctx, `ALTER TABLE pages DROP FOREIGN KEY FK_pages_user;`); err != nil {
- return err
- }
- if _, err := db.Exec(ctx, `ALTER TABLE shop_cat_product_rel DROP FOREIGN KEY FK_shop_cat_product_rel_product_id;`); err != nil {
- return err
- }
- if _, err := db.Exec(ctx, `ALTER TABLE shop_cat_product_rel DROP FOREIGN KEY FK_shop_cat_product_rel_category_id;`); err != nil {
- return err
- }
- if _, err := db.Exec(ctx, `ALTER TABLE shop_cats DROP FOREIGN KEY FK_shop_cats_user;`); err != nil {
- return err
- }
- if _, err := db.Exec(ctx, `ALTER TABLE shop_filter_product_values DROP FOREIGN KEY FK_shop_filter_product_values_product_id;`); err != nil {
- return err
- }
- if _, err := db.Exec(ctx, `ALTER TABLE shop_filter_product_values DROP FOREIGN KEY FK_shop_filter_product_values_filter_value_id;`); err != nil {
- return err
- }
- if _, err := db.Exec(ctx, `ALTER TABLE shop_filters_values DROP FOREIGN KEY FK_shop_filters_values_filter_id;`); err != nil {
- return err
- }
- if _, err := db.Exec(ctx, `ALTER TABLE shop_orders DROP FOREIGN KEY FK_shop_orders_currency_id;`); err != nil {
- return err
- }
- if _, err := db.Exec(ctx, `ALTER TABLE shop_order_products DROP FOREIGN KEY FK_shop_order_products_order_id;`); err != nil {
- return err
- }
- if _, err := db.Exec(ctx, `ALTER TABLE shop_order_products DROP FOREIGN KEY FK_shop_order_products_product_id;`); err != nil {
- return err
- }
- if _, err := db.Exec(ctx, `ALTER TABLE shop_product_images DROP FOREIGN KEY FK_shop_product_images_product_id;`); err != nil {
- return err
- }
- if _, err := db.Exec(ctx, `ALTER TABLE shop_products DROP FOREIGN KEY FK_shop_products_user;`); err != nil {
- return err
- }
- if _, err := db.Exec(ctx, `ALTER TABLE shop_products DROP FOREIGN KEY FK_shop_products_currency;`); err != nil {
- return err
- }
- if _, err := db.Exec(ctx, `ALTER TABLE shop_products DROP FOREIGN KEY FK_shop_products_category;`); err != nil {
- return err
- }
- if _, err := db.Exec(ctx, `ALTER TABLE shop_products DROP FOREIGN KEY FK_shop_products_parent_id;`); err != nil {
- return err
- }
- // Rename tables
- if _, err := db.Exec(ctx, `RENAME TABLE blog_cat_post_rel TO fave_blog_cat_post_rel;`); err != nil {
- return err
- }
- if _, err := db.Exec(ctx, `RENAME TABLE blog_cats TO fave_blog_cats;`); err != nil {
- return err
- }
- if _, err := db.Exec(ctx, `RENAME TABLE blog_posts TO fave_blog_posts;`); err != nil {
- return err
- }
- if _, err := db.Exec(ctx, `RENAME TABLE notify_mail TO fave_notify_mail;`); err != nil {
- return err
- }
- if _, err := db.Exec(ctx, `RENAME TABLE pages TO fave_pages;`); err != nil {
- return err
- }
- if _, err := db.Exec(ctx, `RENAME TABLE shop_cat_product_rel TO fave_shop_cat_product_rel;`); err != nil {
- return err
- }
- if _, err := db.Exec(ctx, `RENAME TABLE shop_cats TO fave_shop_cats;`); err != nil {
- return err
- }
- if _, err := db.Exec(ctx, `RENAME TABLE shop_currencies TO fave_shop_currencies;`); err != nil {
- return err
- }
- if _, err := db.Exec(ctx, `RENAME TABLE shop_filter_product_values TO fave_shop_filter_product_values;`); err != nil {
- return err
- }
- if _, err := db.Exec(ctx, `RENAME TABLE shop_filters_values TO fave_shop_filters_values;`); err != nil {
- return err
- }
- if _, err := db.Exec(ctx, `RENAME TABLE shop_filters TO fave_shop_filters;`); err != nil {
- return err
- }
- if _, err := db.Exec(ctx, `RENAME TABLE shop_order_products TO fave_shop_order_products;`); err != nil {
- return err
- }
- if _, err := db.Exec(ctx, `RENAME TABLE shop_orders TO fave_shop_orders;`); err != nil {
- return err
- }
- if _, err := db.Exec(ctx, `RENAME TABLE shop_product_images TO fave_shop_product_images;`); err != nil {
- return err
- }
- if _, err := db.Exec(ctx, `RENAME TABLE shop_products TO fave_shop_products;`); err != nil {
- return err
- }
- if _, err := db.Exec(ctx, `RENAME TABLE users TO fave_users;`); err != nil {
- return err
- }
- // Add constraints
- if _, err := db.Exec(
- ctx,
- `ALTER TABLE fave_blog_cat_post_rel ADD CONSTRAINT FK_blog_cat_post_rel_post_id
- FOREIGN KEY (post_id) REFERENCES fave_blog_posts (id) ON DELETE RESTRICT;`,
- ); err != nil {
- return err
- }
- if _, err := db.Exec(
- ctx,
- `ALTER TABLE fave_blog_cat_post_rel ADD CONSTRAINT FK_blog_cat_post_rel_category_id
- FOREIGN KEY (category_id) REFERENCES fave_blog_cats (id) ON DELETE RESTRICT;`,
- ); err != nil {
- return err
- }
- if _, err := db.Exec(
- ctx,
- `ALTER TABLE fave_blog_cats ADD CONSTRAINT FK_blog_cats_user
- FOREIGN KEY (user) REFERENCES fave_users (id) ON DELETE RESTRICT;`,
- ); err != nil {
- return err
- }
- if _, err := db.Exec(
- ctx,
- `ALTER TABLE fave_blog_posts ADD CONSTRAINT FK_blog_posts_user
- FOREIGN KEY (user) REFERENCES fave_users (id) ON DELETE RESTRICT;`,
- ); err != nil {
- return err
- }
- if _, err := db.Exec(
- ctx,
- `ALTER TABLE fave_blog_posts ADD CONSTRAINT FK_blog_posts_category
- FOREIGN KEY (category) REFERENCES fave_blog_cats (id) ON DELETE RESTRICT;`,
- ); err != nil {
- return err
- }
- if _, err := db.Exec(
- ctx,
- `ALTER TABLE fave_pages ADD CONSTRAINT FK_pages_user
- FOREIGN KEY (user) REFERENCES fave_users (id) ON DELETE RESTRICT;`,
- ); err != nil {
- return err
- }
- if _, err := db.Exec(
- ctx,
- `ALTER TABLE fave_shop_cat_product_rel ADD CONSTRAINT FK_shop_cat_product_rel_product_id
- FOREIGN KEY (product_id) REFERENCES fave_shop_products (id) ON DELETE RESTRICT;`,
- ); err != nil {
- return err
- }
- if _, err := db.Exec(
- ctx,
- `ALTER TABLE fave_shop_cat_product_rel ADD CONSTRAINT FK_shop_cat_product_rel_category_id
- FOREIGN KEY (category_id) REFERENCES fave_shop_cats (id) ON DELETE RESTRICT;`,
- ); err != nil {
- return err
- }
- if _, err := db.Exec(
- ctx,
- `ALTER TABLE fave_shop_cats ADD CONSTRAINT FK_shop_cats_user
- FOREIGN KEY (user) REFERENCES fave_users (id) ON DELETE RESTRICT;`,
- ); err != nil {
- return err
- }
- if _, err := db.Exec(
- ctx,
- `ALTER TABLE fave_shop_filter_product_values ADD CONSTRAINT FK_shop_filter_product_values_product_id
- FOREIGN KEY (product_id) REFERENCES fave_shop_products (id) ON DELETE RESTRICT;`,
- ); err != nil {
- return err
- }
- if _, err := db.Exec(
- ctx,
- `ALTER TABLE fave_shop_filter_product_values ADD CONSTRAINT FK_shop_filter_product_values_filter_value_id
- FOREIGN KEY (filter_value_id) REFERENCES fave_shop_filters_values (id) ON DELETE RESTRICT;`,
- ); err != nil {
- return err
- }
- if _, err := db.Exec(
- ctx,
- `ALTER TABLE fave_shop_filters_values ADD CONSTRAINT FK_shop_filters_values_filter_id
- FOREIGN KEY (filter_id) REFERENCES fave_shop_filters (id) ON DELETE RESTRICT;`,
- ); err != nil {
- return err
- }
- if _, err := db.Exec(
- ctx,
- `ALTER TABLE fave_shop_orders ADD CONSTRAINT FK_shop_orders_currency_id
- FOREIGN KEY (currency_id) REFERENCES fave_shop_currencies (id) ON DELETE RESTRICT;`,
- ); err != nil {
- return err
- }
- if _, err := db.Exec(
- ctx,
- `ALTER TABLE fave_shop_order_products ADD CONSTRAINT FK_shop_order_products_order_id
- FOREIGN KEY (order_id) REFERENCES fave_shop_orders (id) ON DELETE RESTRICT;`,
- ); err != nil {
- return err
- }
- if _, err := db.Exec(
- ctx,
- `ALTER TABLE fave_shop_order_products ADD CONSTRAINT FK_shop_order_products_product_id
- FOREIGN KEY (product_id) REFERENCES fave_shop_products (id) ON DELETE RESTRICT;`,
- ); err != nil {
- return err
- }
- if _, err := db.Exec(
- ctx,
- `ALTER TABLE fave_shop_product_images ADD CONSTRAINT FK_shop_product_images_product_id
- FOREIGN KEY (product_id) REFERENCES fave_shop_products (id) ON DELETE RESTRICT;`,
- ); err != nil {
- return err
- }
- if _, err := db.Exec(
- ctx,
- `ALTER TABLE fave_shop_products ADD CONSTRAINT FK_shop_products_user
- FOREIGN KEY (user) REFERENCES fave_users (id) ON DELETE RESTRICT;`,
- ); err != nil {
- return err
- }
- if _, err := db.Exec(
- ctx,
- `ALTER TABLE fave_shop_products ADD CONSTRAINT FK_shop_products_currency
- FOREIGN KEY (currency) REFERENCES fave_shop_currencies (id) ON DELETE RESTRICT;`,
- ); err != nil {
- return err
- }
- if _, err := db.Exec(
- ctx,
- `ALTER TABLE fave_shop_products ADD CONSTRAINT FK_shop_products_category
- FOREIGN KEY (category) REFERENCES fave_shop_cats (id) ON DELETE RESTRICT;`,
- ); err != nil {
- return err
- }
- if _, err := db.Exec(
- ctx,
- `ALTER TABLE fave_shop_products ADD CONSTRAINT FK_shop_products_parent_id
- FOREIGN KEY (parent_id) REFERENCES fave_shop_products (id) ON DELETE RESTRICT;`,
- ); err != nil {
- return err
- }
- return nil
- }
|