Browse Source

Product can have different prices with attributes (database)

Vova Tkach 5 years ago
parent
commit
2cd7527ba3

+ 15 - 1
modules/module_index_act_mysql_setup.go

@@ -254,6 +254,7 @@ func (this *Modules) RegisterAction_IndexMysqlSetup() *Action {
 		if _, err = tx.Exec(
 		if _, err = tx.Exec(
 			`CREATE TABLE shop_products (
 			`CREATE TABLE shop_products (
 				id int(11) NOT NULL AUTO_INCREMENT COMMENT 'AI',
 				id int(11) NOT NULL AUTO_INCREMENT COMMENT 'AI',
+				parent_id int(11) DEFAULT NULL,
 				user int(11) NOT NULL COMMENT 'User id',
 				user int(11) NOT NULL COMMENT 'User id',
 				currency int(11) NOT NULL COMMENT 'Currency id',
 				currency int(11) NOT NULL COMMENT 'Currency id',
 				price float(8,2) NOT NULL COMMENT 'Product price',
 				price float(8,2) NOT NULL COMMENT 'Product price',
@@ -465,7 +466,7 @@ func (this *Modules) RegisterAction_IndexMysqlSetup() *Action {
 			return
 			return
 		}
 		}
 		if _, err = tx.Exec(
 		if _, err = tx.Exec(
-			`INSERT INTO settings (name, value) VALUES ('database_version', '000000009');`,
+			`INSERT INTO settings (name, value) VALUES ('database_version', '000000010');`,
 		); err != nil {
 		); err != nil {
 			tx.Rollback()
 			tx.Rollback()
 			wrap.MsgError(err.Error())
 			wrap.MsgError(err.Error())
@@ -741,6 +742,11 @@ func (this *Modules) RegisterAction_IndexMysqlSetup() *Action {
 			wrap.MsgError(err.Error())
 			wrap.MsgError(err.Error())
 			return
 			return
 		}
 		}
+		if _, err = tx.Exec(`ALTER TABLE shop_products ADD KEY FK_shop_products_parent_id (parent_id);`); err != nil {
+			tx.Rollback()
+			wrap.MsgError(err.Error())
+			return
+		}
 		if _, err = tx.Exec(`ALTER TABLE users ADD UNIQUE KEY email (email);`); err != nil {
 		if _, err = tx.Exec(`ALTER TABLE users ADD UNIQUE KEY email (email);`); err != nil {
 			tx.Rollback()
 			tx.Rollback()
 			wrap.MsgError(err.Error())
 			wrap.MsgError(err.Error())
@@ -876,6 +882,14 @@ func (this *Modules) RegisterAction_IndexMysqlSetup() *Action {
 			wrap.MsgError(err.Error())
 			wrap.MsgError(err.Error())
 			return
 			return
 		}
 		}
+		if _, err = tx.Exec(`
+			ALTER TABLE shop_products ADD CONSTRAINT FK_shop_products_parent_id
+			FOREIGN KEY (parent_id) REFERENCES shop_products (id) ON DELETE RESTRICT;
+		`); err != nil {
+			tx.Rollback()
+			wrap.MsgError(err.Error())
+			return
+		}
 
 
 		// Commit all changes
 		// Commit all changes
 		err = tx.Commit()
 		err = tx.Commit()

+ 1 - 0
support/migrate/000000001.go

@@ -15,4 +15,5 @@ var Migrations = map[string]func(*sqlw.DB, string) error{
 	"000000007": Migrate_000000007,
 	"000000007": Migrate_000000007,
 	"000000008": Migrate_000000008,
 	"000000008": Migrate_000000008,
 	"000000009": Migrate_000000009,
 	"000000009": Migrate_000000009,
+	"000000010": Migrate_000000010,
 }
 }

+ 27 - 0
support/migrate/000000010.go

@@ -0,0 +1,27 @@
+package migrate
+
+import (
+	"golang-fave/engine/sqlw"
+)
+
+func Migrate_000000010(db *sqlw.DB, host string) error {
+	// Changes
+	if _, err := db.Exec(`ALTER TABLE shop_products ADD COLUMN parent_id INT(11) DEFAULT NULL AFTER id;`); err != nil {
+		return err
+	}
+
+	// Indexes
+	if _, err := db.Exec(`ALTER TABLE shop_products ADD KEY FK_shop_products_parent_id (parent_id);`); err != nil {
+		return err
+	}
+
+	// References
+	if _, err := db.Exec(`
+		ALTER TABLE shop_products ADD CONSTRAINT FK_shop_products_parent_id
+		FOREIGN KEY (parent_id) REFERENCES shop_products (id) ON DELETE RESTRICT;
+	`); err != nil {
+		return err
+	}
+
+	return nil
+}

+ 3 - 0
support/schema.sql

@@ -84,6 +84,7 @@ CREATE TABLE shop_product_images (
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
 CREATE TABLE shop_products (
 CREATE TABLE shop_products (
 	id int(11) NOT NULL AUTO_INCREMENT COMMENT 'AI',
 	id int(11) NOT NULL AUTO_INCREMENT COMMENT 'AI',
+	parent_id int(11) DEFAULT NULL,
 	user int(11) NOT NULL COMMENT 'User id',
 	user int(11) NOT NULL COMMENT 'User id',
 	currency int(11) NOT NULL COMMENT 'Currency id',
 	currency int(11) NOT NULL COMMENT 'Currency id',
 	price float(8,2) NOT NULL COMMENT 'Product price',
 	price float(8,2) NOT NULL COMMENT 'Product price',
@@ -141,6 +142,7 @@ 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_user (user);
 ALTER TABLE shop_products ADD KEY FK_shop_products_currency (currency);
 ALTER TABLE shop_products ADD KEY FK_shop_products_currency (currency);
 ALTER TABLE shop_products ADD KEY FK_shop_products_category (category);
 ALTER TABLE shop_products ADD KEY FK_shop_products_category (category);
+ALTER TABLE shop_products ADD KEY FK_shop_products_parent_id (parent_id);
 ALTER TABLE users ADD UNIQUE KEY email (email);
 ALTER TABLE users ADD UNIQUE KEY email (email);
 
 
 # References
 # References
@@ -160,3 +162,4 @@ ALTER TABLE shop_product_images ADD CONSTRAINT FK_shop_product_images_product_id
 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_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;
 ALTER TABLE shop_products ADD CONSTRAINT FK_shop_products_currency FOREIGN KEY (currency) REFERENCES shop_currencies (id) ON DELETE RESTRICT;
 ALTER TABLE shop_products ADD CONSTRAINT FK_shop_products_category FOREIGN KEY (category) REFERENCES shop_cats (id) ON DELETE RESTRICT;
 ALTER TABLE shop_products ADD CONSTRAINT FK_shop_products_category FOREIGN KEY (category) REFERENCES shop_cats (id) ON DELETE RESTRICT;
+ALTER TABLE shop_products ADD CONSTRAINT FK_shop_products_parent_id FOREIGN KEY (parent_id) REFERENCES shop_products (id) ON DELETE RESTRICT;