Sailsjs table needs to be updated automatically without using any cron - mysql

I am using SAILS JS and mysql adapter is being used. I have a model named as User with the following fields ID, USERNAME, EMAIL, ACTIVE_STATUS and CREATED_DATE.
By Default, active_status is set as 0. I want to update the status is 1 when created_date + 3 days is equal on Today.
Kindly suggest any possible ways to do this.

Hope you can use MySQL’s Event Scheduler
Activate it by: SET GLOBAL event_scheduler = ON;
Create event syntax:
CREATE EVENT `event_name`
ON SCHEDULE schedule
[ON COMPLETION [NOT] PRESERVE]
[ENABLE | DISABLE | DISABLE ON SLAVE]
DO BEGIN
-- event body
END;
The schedule can be assigned various settings, e.g.
Run once on a specific date/time:
AT ‘YYYY-MM-DD HH:MM.SS’
e.g. AT ‘2011-06-01 02:00.00’
Run once after a specific period has elapsed:
AT CURRENT_TIMESTAMP + INTERVAL n [HOUR|MONTH|WEEK|DAY|MINUTE]
e.g. AT CURRENT_TIMESTAMP + INTERVAL 1 DAY
Run at specific intervals forever:
EVERY n [HOUR|MONTH|WEEK|DAY|MINUTE]
e.g. EVERY 1 DAY
Run at specific intervals during a specific period:
EVERY n [HOUR|MONTH|WEEK|DAY|MINUTE] STARTS date ENDS date
e.g. EVERY 1 DAY STARTS CURRENT_TIMESTAMP + INTERVAL 1 WEEK ENDS ‘2012-01-01 00:00.00’
Example:
SET GLOBAL event_scheduler = ON; -- enable event scheduler.
SELECT ##event_scheduler; -- check whether event scheduler is ON/OFF
CREATE EVENT e_store_ts -- create your event
ON SCHEDULE
EVERY 24 HOURS -- run every 24 hours
DO
UPDATE myschema.users set active_status = 1
Refer: http://dev.mysql.com/doc/refman/5.5/en/create-event.html

Related

MySQL event or trigger to automatically update a value 30 minutes after last modify

I have in my DB (db_test) a table (tb_test) with 3 columns (id, test_field, timestamp_ins, timestamp_mod).
id is the 'primary key' with auto-increment attribute;
test_field is a char(1) that can contains only 2 values ('N' or 'S');
timestamp_ins is a datetime with current timestamp (not updating);
timestamp_mod is a datetime with current timestamp set to ON UPDATE CURRENT_TIMESTAMP().
I would like test_field automatically returns to default value ('N') 30 minutes after last modify of record indicated in timestamp_mod value.
I'm not expert in mysql, so I need help about this.
Is it possible using phpMyAdmin on a XAMPP virtual server?
--UPDATE--
Solved with this syntax:
CREATE EVENT IF NOT EXISTS test_event
ON SCHEDULE EVERY 1 MINUTE
DO
UPDATE ni0y2__test
SET test_field = DEFAULT
WHERE test_field < NOW() - INTERVAL 30 MINUTE
Only one doubt:
can this event make my DB performances worse?
I prefer putting the onus on the reader, not the table:
SELECT IF (timestamp_mod < NOW() - INTERVAL 30 MINUTE,
'N',
test_field) AS funky_field
FROM tb_test;
EVENTs have some overhead. Also, the above method will change at precisely 30 minutes; any attempt at using EVENT will be only approximately 30.
You could "hide" the IF(...) from the users by using a VIEW or Stored Function or a GENERATED column.

Mysql auto deleting event after 3 days

I have a table in my MYSQL database called sale as shown below:
I want to auto delete the sales that are 3 days old and not validated,
I create event but unfortunately it doesn't work here's my code :
CREATE EVENT `delete_sale` ON SCHEDULE EVERY 1 HOUR DO DELETE FROM sale WHERE sale.is_validated = 0 and sale.date < DATE_SUB(NOW(), INTERVAL 3 DAY)
My best guess is that your event is working, but it's only firing once then deactivating. To fix that, you should add the "ON XOMPLETION PRESERVE" line. Another possibility is that it's not loading properly because it can't quite parse your event code. To really hold MySQL's hand with this, try treating it like a multi statement event, changing the delimiter and adding BEGIN and END. Something like this:
DELIMITER &&
CREATE EVENT `delete_sale`
ON SCHEDULE EVERY 1 HOUR
ON COMPLETION PRESERVE
DO
BEGIN
DELETE FROM sale WHERE sale.is_validated = 0 and sale.date < DATE_SUB(NOW(), INTERVAL 3 DAY);
END &&
DELIMITER ;
the problem resolved by enabling event_scheduler with the following query SET GLOBAL event_scheduler = ON;

How To UPDATE 2 MySQL Tables With Server Event

I'm creating a website for a simple poll from registered users. Every month, registered users only allowed to vote 5x for a poll.
Now, i'm going to create a server event with HEIDI SQL on 2 tables which will reset their 5 times votes on each month to 0
Here's my exe code:
CREATE EVENT `resetvalue`
ON SCHEDULE
EVERY 1 MONTH STARTS '2017-10-01 00:00:00'
ON COMPLETION PRESERVE
ENABLE
COMMENT 'Im going to reset this every month'
DO BEGIN
UPDATE tbl_users SET statusmonth = 0
UPDATE tbl_poll SET status1 = 0
UPDATE tbl_poll SET status2 = 0
END
Is this the correct code?
Unfortunately, I asked my friend and then he gave another code that look like this:
DELIMITER $$
CREATE EVENT `dbname`.`resetvalue`
ON SCHEDULE EVERY 1 MONTH
STARTS '2011-06-21 01:00:00'
ON COMPLETION NOT PRESERVE
ENABLE
DO BEGIN
/(sql_statements)
END$$
DELIMITER ;
I got confused, which one is the correct code? What is a DELIMiTER anyway?
Below is a difference for your event with respect to your friend's event.
Your event will run on 1st of every month at 12:00 am and start date of event is 2017-10-01 00:00:00 but your friend's event will run on 21st at 12:00 am of every month and start date is 2011-06-21 01:00:00.
Your event will not deleted once event is completed. But your friend's event code will be removed once it will done. Here both events are never ending it's affect only if it's 5 times to run or you disable an event.
Other then this I think it's same. For more details you can refer MySQL event scheduler documentation.
https://dev.mysql.com/doc/refman/5.7/en/events-overview.html

Event doesn't start and returns error Data Truncated

I have one event in my database:
CREATE DEFINER=`root`#`localhost` EVENT `movedate`
ON SCHEDULE
EVERY 1 DAY STARTS '2015-01-08 00:00:05'
ON COMPLETION PRESERVE
ENABLE
COMMENT 'Move tasks to current date if production is not fnished at previ'
DO update production set deliverydate = now() where deliverydate < now() and progress <> 2
Problem is that event is not executed even in my.ini in section mysqld
is set
event_scheduler=on
If I run manually query:
update production set deliverydate = now() where deliverydate < now() and progress <> 2
then I get warnings like:
Data truncated for column 'DeliveryDate' at row 3
The same thing, same code works fine on my PC where is installed mysql.
What I should check to see what causes that event is not fired?

How to update column value after some time interval

I need to update a particular column value from Y to N after every 3 minutes.
Table Structure
`u_id` int(11) NOT NULL,
`status` varchar(11) NOT NULL
How can i change status N after 3 mins for each row in phpmyadmin
It seems like you are looking for something called EVENTS in MySQL.
You may try following steps to achieve.
SET GLOBAL event_scheduler = ON; -- enable event scheduler.
SELECT ##event_scheduler; -- check whether event scheduler is ON/OFF
CREATE EVENT e_store_ts -- create your event
ON SCHEDULE
EVERY 180 SECOND -- run every 180 secs (3 Min)
DO
UPDATE myschema.youtable set mycolumn='N' -- update this table
To see the events that created
SHOW EVENTS;
For more detail : Schedule SQL Query to execute on specific time interval
You can also create a corn job to run every 3 minutes, which is the most standard way of doing repetitive work.
Refer here which gives an easy explanation about how to create corn job.
The corn expression to run every 3 minutes is: 0 0/3 * 1/1 * ? *