esx-framework / esx_property

GNU General Public License v3.0
10 stars 25 forks source link

Database Row Check & Creation #26

Open tepelekas opened 9 months ago

tepelekas commented 9 months ago

I noticed every time my server start's the script esx_property creates a row in my datastore_data table on the database that is empty and null. I tryed and fixed that to check if the row exists then to not create it and create the row only if not exist.

`CreateThread(function() Wait(3000) PropertiesRefresh()

MySQL.query("ALTER TABLE `users` ADD COLUMN IF NOT EXISTS `last_property` LONGTEXT NULL", function(result)
    if result.affectedRows > 0 then
        print("[^2INFO^7] Added ^5last_property^7 column to users table")
    end
end)

-- Check if datastore table exists before inserting values.
if MySQL.scalar.await("SELECT EXISTS (SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_TYPE LIKE 'BASE TABLE' AND TABLE_NAME = 'datastore')") > 0 then
    if MySQL.scalar.await("SELECT COUNT(*) FROM `datastore` WHERE name = 'property'") == 0 then
        MySQL.insert("INSERT INTO `datastore` (name, label, shared) VALUES ('property', 'Property', 1)", function(affectedRows)
            if affectedRows > 0 then
                print("[^2INFO^7] Added ^5Property^7 into ^5datastore^7 table")
            end
        end)
    else
        print("[^2INFO^7] Row with name ^5Property^7 already exists in the ^5datastore^7 table")
    end

    if MySQL.scalar.await("SELECT COUNT(*) FROM `datastore_data` WHERE name = 'property'") == 0 then
        MySQL.insert("INSERT INTO `datastore_data` (name, owner, data) VALUES ('property', NULL, '{}')", function(affectedRows)
            if affectedRows > 0 then
                print("[^2INFO^7] Added ^5Property^7 into ^5datastore_data^7 table")
            end
        end)
    else
        print("[^2INFO^7] Row with name ^5Property^7 already exists in the '^5datastore_data' table")
    end
end

end)`

Esegovic commented 4 months ago

This fix issue above.

if MySQL.scalar.await("SELECT EXISTS (SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME = 'datastore')") > 0 then
    if MySQL.scalar.await("SELECT COUNT(*) FROM datastore WHERE name = 'property'") == 0 then
        MySQL.insert("INSERT INTO `datastore` (name, label, shared) VALUES ('property', 'Property', 1)", function(affectedRows)
            if affectedRows > 0 then
                print("[^2INFO^7] Added ^5Property^7 into ^5datastore^7 table")
            end
        end)
    end

    if MySQL.scalar.await("SELECT COUNT(*) FROM datastore_data WHERE name = 'property'") == 0 then
        MySQL.insert("INSERT INTO `datastore_data` (name, owner, data) VALUES ('property', NULL, '{}')", function(affectedRows)
            if affectedRows > 0 then
                print("[^2INFO^7] Added ^5Property^7 into ^5datastore_data^7 table")
            end
        end)
    end
  end