Browse Source

Shop product images table

Vova Tkach 5 years ago
parent
commit
1a4d711e41

+ 1 - 0
modules/module_index_act_cypress.go

@@ -46,6 +46,7 @@ func (this *Modules) RegisterAction_IndexCypressReset() *Action {
 				shop_filter_product_values,
 				shop_filters,
 				shop_filters_values,
+				shop_product_images,
 				shop_products,
 				users
 			;`,

+ 31 - 1
modules/module_index_act_mysql_setup.go

@@ -237,6 +237,18 @@ func (this *Modules) RegisterAction_IndexMysqlSetup() *Action {
 			return
 		}
 
+		// Table: shop_product_images
+		if _, err = tx.Exec(
+			`CREATE TABLE shop_product_images (
+				product_id int(11) NOT NULL,
+				filename varchar(255) NOT NULL
+			) ENGINE=InnoDB DEFAULT CHARSET=utf8;`,
+		); err != nil {
+			tx.Rollback()
+			wrap.MsgError(err.Error())
+			return
+		}
+
 		// Table: shop_products
 		if _, err = tx.Exec(
 			`CREATE TABLE shop_products (
@@ -443,7 +455,7 @@ func (this *Modules) RegisterAction_IndexMysqlSetup() *Action {
 			return
 		}
 		if _, err = tx.Exec(
-			`INSERT INTO settings (name, value) VALUES ('database_version', '000000004');`,
+			`INSERT INTO settings (name, value) VALUES ('database_version', '000000006');`,
 		); err != nil {
 			tx.Rollback()
 			wrap.MsgError(err.Error())
@@ -678,6 +690,16 @@ func (this *Modules) RegisterAction_IndexMysqlSetup() *Action {
 			wrap.MsgError(err.Error())
 			return
 		}
+		if _, err = tx.Exec(`ALTER TABLE shop_product_images ADD UNIQUE KEY product_filename (product_id,filename) USING BTREE;`); err != nil {
+			tx.Rollback()
+			wrap.MsgError(err.Error())
+			return
+		}
+		if _, err = tx.Exec(`ALTER TABLE shop_product_images ADD KEY FK_shop_product_images_product_id (product_id);`); err != nil {
+			tx.Rollback()
+			wrap.MsgError(err.Error())
+			return
+		}
 		if _, err = tx.Exec(`ALTER TABLE shop_products ADD UNIQUE KEY alias (alias);`); err != nil {
 			tx.Rollback()
 			wrap.MsgError(err.Error())
@@ -788,6 +810,14 @@ func (this *Modules) RegisterAction_IndexMysqlSetup() *Action {
 			wrap.MsgError(err.Error())
 			return
 		}
+		if _, err = tx.Exec(`
+			ALTER TABLE shop_product_images ADD CONSTRAINT FK_shop_product_images_product_id
+			FOREIGN KEY (product_id) REFERENCES shop_products (id) ON DELETE RESTRICT;
+		`); err != nil {
+			tx.Rollback()
+			wrap.MsgError(err.Error())
+			return
+		}
 		if _, err = tx.Exec(`
 			ALTER TABLE shop_products ADD CONSTRAINT FK_shop_products_user
 			FOREIGN KEY (user) REFERENCES users (id) ON DELETE RESTRICT;

+ 1 - 0
support/migrate/000000001.go

@@ -11,4 +11,5 @@ var Migrations = map[string]func(*sqlw.DB, string) error{
 	"000000003": Migrate_000000003,
 	"000000004": Migrate_000000004,
 	"000000005": Migrate_000000005,
+	"000000006": Migrate_000000006,
 }

+ 35 - 0
support/migrate/000000006.go

@@ -0,0 +1,35 @@
+package migrate
+
+import (
+	"golang-fave/engine/sqlw"
+)
+
+func Migrate_000000006(db *sqlw.DB, host string) error {
+	// Table: shop_product_images
+	if _, err := db.Exec(
+		`CREATE TABLE shop_product_images (
+			product_id int(11) NOT NULL,
+			filename varchar(255) NOT NULL
+		) ENGINE=InnoDB DEFAULT CHARSET=utf8;`,
+	); err != nil {
+		return err
+	}
+
+	// Indexes
+	if _, err := db.Exec(`ALTER TABLE shop_product_images ADD UNIQUE KEY product_filename (product_id,filename) USING BTREE;`); err != nil {
+		return err
+	}
+	if _, err := db.Exec(`ALTER TABLE shop_product_images ADD KEY FK_shop_product_images_product_id (product_id);`); err != nil {
+		return err
+	}
+
+	// References
+	if _, err := db.Exec(`
+		ALTER TABLE shop_product_images ADD CONSTRAINT FK_shop_product_images_product_id
+		FOREIGN KEY (product_id) REFERENCES shop_products (id) ON DELETE RESTRICT;
+	`); err != nil {
+		return err
+	}
+
+	return nil
+}

+ 7 - 0
support/schema.sql

@@ -77,6 +77,10 @@ CREATE TABLE shop_filters_values (
 	name varchar(255) NOT NULL COMMENT 'Value name',
 	PRIMARY KEY (id)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
+CREATE TABLE shop_product_images (
+	product_id int(11) NOT NULL,
+	filename varchar(255) NOT NULL
+) ENGINE=InnoDB DEFAULT CHARSET=utf8;
 CREATE TABLE shop_products (
 	id int(11) NOT NULL AUTO_INCREMENT COMMENT 'AI',
 	user int(11) NOT NULL COMMENT 'User id',
@@ -126,6 +130,8 @@ ALTER TABLE shop_filter_product_values ADD KEY FK_shop_filter_product_values_fil
 ALTER TABLE shop_filters ADD KEY name (name);
 ALTER TABLE shop_filters_values ADD KEY FK_shop_filters_values_filter_id (filter_id);
 ALTER TABLE shop_filters_values ADD KEY name (name);
+ALTER TABLE shop_product_images ADD UNIQUE KEY product_filename (product_id,filename) USING BTREE;
+ALTER TABLE shop_product_images ADD KEY FK_shop_product_images_product_id (product_id);
 ALTER TABLE shop_products ADD UNIQUE KEY alias (alias);
 ALTER TABLE shop_products ADD KEY FK_shop_products_user (user);
 ALTER TABLE shop_products ADD KEY FK_shop_products_currency (currency);
@@ -143,5 +149,6 @@ ALTER TABLE shop_cats ADD CONSTRAINT FK_shop_cats_user FOREIGN KEY (user) REFERE
 ALTER TABLE shop_filter_product_values ADD CONSTRAINT FK_shop_filter_product_values_product_id FOREIGN KEY (product_id) REFERENCES shop_products (id) ON DELETE RESTRICT;
 ALTER TABLE shop_filter_product_values ADD CONSTRAINT FK_shop_filter_product_values_filter_value_id FOREIGN KEY (filter_value_id) REFERENCES shop_filters_values (id) ON DELETE RESTRICT;
 ALTER TABLE shop_filters_values ADD CONSTRAINT FK_shop_filters_values_filter_id FOREIGN KEY (filter_id) REFERENCES shop_filters (id) ON DELETE RESTRICT;
+ALTER TABLE shop_product_images ADD CONSTRAINT FK_shop_product_images_product_id FOREIGN KEY (product_id) REFERENCES shop_products (id) ON DELETE RESTRICT;
 ALTER TABLE shop_products ADD CONSTRAINT FK_shop_products_user FOREIGN KEY (user) REFERENCES users (id) ON DELETE RESTRICT;
 ALTER TABLE shop_products ADD CONSTRAINT FK_shop_products_currency FOREIGN KEY (currency) REFERENCES shop_currencies (id) ON DELETE RESTRICT;