symfony 3 doctrine porting data from mysql to postgres - mysql

I have a website build with Symfony3 and MySQL.
Now I must migrate from MySQL to Postgres for specials needs.
I've found this tool for migrate it automatically (nmig), and all it was all done, no problem found! YEAH!
I've changed my app/config/parameters.yml as symfony documentation tells.
Cache cleared!
But when I try to execute a simple query from symfony form index, I get this error:
An exception occurred while executing
SELECT DISTINCT id_0, MIN(sclr_25) AS dctrn_minrownum
FROM (
SELECT c0_.ID AS id_0,
c0_.RAG_SOCIALE AS rag_sociale_1,
c0_.INDIRIZZO AS indirizzo_2,
c0_.CIVICO AS civico_3,
c0_.CITTA AS citta_4,
c0_.PROVINCIA AS provincia_5,
c0_.NAZIONE AS nazione_6,
c0_.SETTORE AS settore_7,
c0_.REFERENTE1 AS referente1_8,
c0_.REFERENTE2 AS referente2_9,
c0_.CAP AS cap_10,
c0_.EMAIL1 AS email1_11,
c0_.EMAIL2 AS email2_12,
c0_.PIVA AS piva_13,
c0_.PATHLOGO AS pathlogo_14,
c0_.TELEFONO AS telefono_15,
c0_.NOTE AS note_16,
c0_.VALORE_OFFERTE_TOTALI AS valore_offerte_totali_17,
c0_.VALORE_OFF_ORD_ATTIVE AS valore_off_ord_attive_18,
c0_.VALORE_FATTURE_TOTALI AS valore_fatture_totali_19,
c0_.BANCA_APPOGGIO AS banca_appoggio_20,
c0_.CONDIZIONI_PAGAMENTO AS condizioni_pagamento_21,
c0_.RELATIVEID AS relativeid_22,
c0_.created_at AS created_at_23,
c0_.updated_at AS updated_at_24,
ROW_NUMBER() OVER(ORDER BY c0_.ID DESC) AS sclr_25
FROM clienti c0_
) dctrn_result
GROUP BY id_0
ORDER BY dctrn_minrownum ASC
LIMIT 10
OFFSET 0
**SQLSTATE[42703]: Undefined column: 7 ERRORE: la colonna c0_.id non esiste
LINE 1: ...d_0, MIN(sclr_25) AS dctrn_minrownum FROM (SELECT c0_.ID AS ...
^
500 Internal Server Error - InvalidFieldNameException**
Same error if I try this query into sqleditor: same error!
What's wrong on my configuration??
Thx

Related

PHP query works in localhost but breaks on live server

As the title suggests, I have a query that works fine in localhost but on my live environment I get a 500 server error. My PHP version locally is 7.4 but the live server uses 7.3.
The error I get is
PHP Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation:
1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL
server version for the right syntax to use near '(partition by prop_slug order by
prop_client) as rn
FROM listing_details
JOI' at line 4 in /hermes/bosnaweb23a/b60/ipg.site/site/index.php:23
Stack trace:
#0 /hermes/bosnaweb23a/b60/ipg.site/site/index.php(23): PDOStatement->execute()
#1 {m
My query looks like the below, but as I said this works fine in the local environment.
$get_listings = $db->prepare('SELECT *
FROM (
SELECT *,
row_number() over(partition by prop_slug order by prop_client) as rn
FROM listing_details
JOIN prop_gallery
ON prop_gallery.prop_gallery_id = listing_details.prop_slug
WHERE prop_slug LIKE prop_gallery_id OR prop_gallery_id LIKE prop_slug
AND listing_details.prop_mandate = 1
) x
where rn = 1');
$get_listings->execute();
$listings = $get_listings->fetchAll();
if (!$listings) {
echo 'Error: No Listings.';
}

WordPress database error log yet Blog loads and looks fine

I'm trying to hunt down an error that keeps appearing in my Plesk log for a Wordpress install I have. It uses the theme Colornews and I'm able to load the blog and it appears to look as expected. The error shows in the log as this:
AH01071: Got error 'PHP message: WordPress database error Table
'blog_database.wp_commentsINNER' doesn't exist for query
\n\t\t\t\t\tSELECT wpcoms.*, wpposts.ID, wpposts.comment_count,
wpposts.post_title\n\t\t\t\t\tFROM (\n\t\t\t\t\t\tSELECT
*,\n\t\t\t\t\t\t\t#num := if(#post_id = comment_post_ID, #num + 1, 1) as row_number,\n\t\t\t\t\t\t\t#post_id := comment_post_ID as
cpID\n\t\t\t\t\t\tFROM wp_commentsINNER JOIN wp_posts
wpposts\n\t\t\t\t\t\t\t\tON (comment_post_ID =
wpposts.ID\n\t\t\t\t\t\t\t\t\tAND (wpposts.post_status = 'publish' OR
wpposts.post_status = 'inherit') \n\t\t\t\t\t\t\t\t\tAND
wpposts.post_password = '' AND wpposts.post_type = 'post'
)\n\t\t\t\t\t\t\tWHERE comment_approved = 1 AND comment_type = ''
ORDER BY comment_post_ID DESC, comment_ID DESC \n\t\t\t\t\t) as
wpcoms\n\t\t\t\t\t\tWHERE wpcoms.row_number <= 3\n\t\t\t\t\t\t\tORDER
BY wpcoms.comment_date DESC\n\t\t\t\t\t\tLIMIT 3 made by
require('wp-blog-header.php'),
require_once('wp-includes/template-loader.php'),
include('/themes/colornews/index.php'), get_sidebar, locate_template,
load_template, require_once('/themes/colornews/sidebar.php'),
dynamic_sidebar, W...\n'
Its a long shot but wondered if anyone could point me in the right direction to fix the error.
Thanks
Chris
The error is self-explanatory. There's an error in the SQL query:
wp_commentsINNER
the space is missing between the table name and the INNER. Locate where that query is executed and fix it.

#1327 - Undeclared variable: table_name

I was rolling some queries into a stored procedure and I hit the #1327 - Undeclared variable error ... the odd thing though is that the variable it claims is undeclared is actually a table name.
Working through the problem
So I extracted the bit of the procedure where it was falling over and tried to run it as a normal SQL query directly on the database through PHPMyAdmin... same thing. After much tinkering it seems to be where I'm joining another table.
If I run the query on a single table, it's fine, like this:
SET #i_channel_id = 3;
SET #i_product_id = 90;
SELECT
`product_status_to_channel`.`status_code` INTO #s_status_code
FROM `product_status_to_channel`
WHERE `product_status_to_channel`.`channel_id` = #i_channel_id
AND `product_status_to_channel`.`product_id` = #i_product_id
ORDER BY IF(`product_status_to_channel`.`date` IS NULL, 1, 0) ASC,
`product_status_to_channel`.`date` DESC
LIMIT 0, 1;
SELECT #s_status_code AS status_code;
Which outputs 'LIVE' as the status_code in PHPMyAdmin - which is fine.
However, when I try and JOIN to the message table to find the associated status message, I get the error: #1327 - Undeclared variable: product_status_to_channel_lang ... but product_status_to_channel_lang is a table?!
SET #i_channel_id = 3;
SET #i_language_id = 3;
SET #i_product_id = 90;
SELECT
`product_status_to_channel`.`status_code` INTO #s_status_code,
`product_status_to_channel_lang`.`string` INTO #s_status_message
FROM `product_status_to_channel`
LEFT JOIN `product_status_to_channel_lang`
ON `product_status_to_channel`.`product_status_to_channel_id` = `product_status_to_channel_lang`.`product_status_to_channel_id`
AND `product_status_to_channel_lang`.`language_id` = #i_language_id
WHERE `product_status_to_channel`.`channel_id` = #i_channel_id
AND `product_status_to_channel`.`product_id` = #i_product_id
ORDER BY IF(`product_status_to_channel`.`date` IS NULL, 1, 0) ASC, `product_status_to_channel`.`date` DESC
LIMIT 0, 1;
SELECT #s_status_code AS status_code, #s_status_message AS status_message;
Is it trying to evaluate product_status_to_channel_lang.product_status_to_channel_id as a variable on the JOIN?
LEFT JOIN `product_status_to_channel_lang`
ON `product_status_to_channel`.`product_status_to_channel_id` = `product_status_to_channel_lang`.`product_status_to_channel_id`
I assume I'm overlooking something obvious?
I've tried this on both:
a Win7 box running xampp with MySQL 5.5.27 - MySQL Community Server (GPL)
a Debian box running MySQL 5.1.73-1-log - (Debian)
Never mind - it was something obvious:
SELECT
`product_status_to_channel`.`status_code` INTO #s_status_code,
`product_status_to_channel_lang`.`string` INTO #s_status_message
Should be:
SELECT
`product_status_to_channel`.`status_code`,
`product_status_to_channel_lang`.`string`
INTO
#s_status_code,
#s_status_message
... it must be Friday, it took literally a couple of hours to see that.
... INTO #s_status_code,
`product_status_to_channel_lang`.`string` ...
^That's where it's trying to assign product_status_to_channel_lang to being a variable into which to put data.

arel union and latest conversation from messages

I need a list of messages where each one is the most recent in the "conversation" between the current user and each other user.
The same query is described in this question
The code I have so far is:
t1 = Arel::Table.new(:messages, :as => 't1')
t2 = Arel::Table.new(:messages, :as => 't2')
convs1 = t1.
project(
t1[:receiver_user_id].as('other_user_id'),
t1[:receiver_user_id].as('receiver_user_id'),
t1[:sender_user_id].as('sender_user_id'),
t1[:created_at].as('created_at')
).
where(t1[:sender_user_id].eq(user.id))
convs2 = t2.project(
t2[:sender_user_id].as('other_user_id'),
t2[:receiver_user_id].as('receiver_user_id'),
t2[:sender_user_id].as('sender_user_id'),
t2[:created_at].as('created_at')
).
where(t2[:receiver_user_id].eq(user.id))
conv = convs1.union(convs2)
First off, I get an error:
ActiveRecord::StatementInvalid: Mysql2::Error: You have an error in your SQL syntax; check \
the manual that corresponds to your MySQL server version for the right syntax to use near \
'UNION SELECT `t2`...
This works if I manually replace "UNION" with "UNION ALL" in the sql produced below.
conv.to_sql from the above code produces:
SELECT `t1`.`receiver_user_id` AS other_user_id,
`t1`.`receiver_user_id` AS receiver_user_id, `
t1`.`sender_user_id` AS sender_user_id,
`t1`.`created_at` AS created_at
FROM `messages` `t1`
WHERE `t1`.`sender_user_id` = 50
UNION
SELECT `t2`.`sender_user_id` AS other_user_id,
`t2`.`receiver_user_id` AS receiver_user_id,
`t2`.`sender_user_id` AS sender_user_id,
`t2`.`created_at` AS created_at
FROM `messages` `t2`
WHERE `t2`.`receiver_user_id` = 50
Any idea why there's a MySQL UNION error. Is it an arel bug?
Secondly, any help with completing the query would be much appreciated.
Update:
Using Arel::Nodes::Union.new works
I think this is more probably a mysql fault, this is a mySQL error text. Something similar is discussed in here, but not exactly this issue.
Try to migrate to another sql server, and check again, or if union all works then use this:
conv = convs1.union(convs2, :all)
Based on documentation.
The problem is actually the parentheses in the sql. It works if I run:
Message.find_by_sql conv.to_sql.delete('()')
which removes the leading "(" and trailing ")"
Weird.. I don't know how I would chain this to complete the query. (Arel::Nodes::Union doesn't have a group method). This is Rails 3.1.4
I had a similar problem and solved it as follows:
def last_messages
Message.find_by_sql("
SELECT messages.*,
(IF(recipient_id = #{id}, 0,1)) as outlast,
users.avatar_name,
users.name
FROM messages
INNER JOIN users
ON users.id=(IF(recipient_id = #{id}, sender_id,recipient_id))
WHERE messages.id IN
( SELECT max(id)
FROM messages
WHERE recipient_id = #{id} OR sender_id = #{id}
GROUP BY (IF(recipient_id = #{id}, sender_id, recipient_id))
)
ORDER BY messages.id DESC")
end
This is the code I used instead in the end
all_msgs = Message.where("messages.sender_user_id = ? OR messages.receiver_user_id = ?",
user.id, user.id)
msg_ids = all_msgs.select("sender_user_id, receiver_user_id, max(id) as max_id")
.group(:sender_user_id, :receiver_user_id).map { |m| m.max_id }
all_msgs = all_msgs.where(:id => msg_ids)

Track SQL queries that timeout

Is it possible to query SQL Server 2008 R2 for a list of queries that have timedout?
I'm trying to find out which parts of our app are falling over, but for boring and bad reasons that won't get fixed, we have no application level logging.
In lieu of getting some application logging put in, how can I find out which queries are timing out?
to get all the time-outs you will need to use the profiler, however you can run some queries that may help:
show top 10 high CPU queries:
SELECT TOP 10
total_worker_time/execution_count AS Avg_CPU_Time
,execution_count
,total_elapsed_time/execution_count as AVG_Run_Time
,(SELECT
SUBSTRING(text,statement_start_offset/2,(CASE
WHEN statement_end_offset = -1 THEN LEN(CONVERT(nvarchar(max), text)) * 2
ELSE statement_end_offset
END -statement_start_offset)/2
) FROM sys.dm_exec_sql_text(sql_handle)
) AS query_text
FROM sys.dm_exec_query_stats
ORDER BY Avg_CPU_Time DESC
this query will show cahced query plans that "SCAN", change comments for other things, can add filters for UseCount, EstimatedCost, EstimatedCPU, Estimated Rows
;WITH XMLNAMESPACES(DEFAULT N'http://schemas.microsoft.com/sqlserver/2004/07/showplan')
, CachedPlans AS
(SELECT
RelOp.op.value(N'../../#NodeId', N'int') AS ParentOperationID
,RelOp.op.value(N'#NodeId', N'int') AS OperationID
,RelOp.op.value(N'#PhysicalOp', N'varchar(50)') AS PhysicalOperator
,RelOp.op.value(N'#LogicalOp', N'varchar(50)') AS LogicalOperator
,RelOp.op.value(N'#EstimatedTotalSubtreeCost ', N'float') AS EstimatedCost
,RelOp.op.value(N'#EstimateIO', N'float') AS EstimatedIO
,RelOp.op.value(N'#EstimateCPU', N'float') AS EstimatedCPU
,RelOp.op.value(N'#EstimateRows', N'float') AS EstimatedRows
,cp.plan_handle AS PlanHandle
,qp.query_plan AS QueryPlan
,st.TEXT AS QueryText
,cp.cacheobjtype AS CacheObjectType
,cp.objtype AS ObjectType
,cp.usecounts AS UseCounts
FROM sys.dm_exec_cached_plans cp
CROSS APPLY sys.dm_exec_sql_text(cp.plan_handle) st
CROSS APPLY sys.dm_exec_query_plan(cp.plan_handle) qp
CROSS APPLY qp.query_plan.nodes(N'//RelOp') RelOp (op)
)
SELECT
PlanHandle
,ParentOperationID
,OperationID
,PhysicalOperator
,LogicalOperator
,UseCounts
,CacheObjectType
,ObjectType
,EstimatedCost
,EstimatedIO
,EstimatedCPU
,EstimatedRows
,QueryText
FROM CachedPlans
WHERE CacheObjectType = N'Compiled Plan'
AND PhysicalOperator IN ('nothing will ever match this one!'
--,'Assert'
--,'Bitmap'
--,'Clustered Index Delete'
--,'Clustered Index Insert'
,'Clustered Index Scan'
--,'Clustered Index Seek'
--,'Clustered Index Update'
--,'Compute Scalar'
--,'Concatenation'
--,'Constant Scan'
,'Deleted Scan'
--,'Filter'
--,'Hash Match'
,'Index Scan'
--,'Index Seek'
--,'Index Spool'
,'Inserted Scan'
--,'Merge Join'
--,'Nested Loops'
--,'Parallelism'
,'Parameter Table Scan'
--,'RID Lookup'
--,'Segment'
--,'Sequence Project'
--,'Sort'
--,'Stream Aggregate'
--,'Table Delete'
--,'Table Insert'
,'Table Scan'
--,'Table Spool'
--,'Table Update'
--,'Table-valued function'
--,'Top'
)