Datamapper unable to find table - ruby

I have an app running on my Raspberry Pi with Postgresql 9.1.
My first iteration was to add weather records into a table called "Weathers". That was successful.
My next iteration is to use psychopg2 to write records from Python into a different table called "weather". That also is succcessful.
What should also be successful is to change the Weather class in my app to the new fields. But DataMapper returns a mapping error:
#records = Weather.all(:order => :timestamp.desc)
ArgumentError at /
+options [:order]+ entry :timestamp does not map to a property in Weather
Rereading the datamapper.org docs suggests it's something to do with my table name so I migrated over the older "weathers" table into another called "older" and dropped the "weathers" table. But DataMapper still fails to find this table.
My Postgres environment with a truncated view of the target table is:
List of relations
Schema | Name | Type | Owner
--------+-----------+-------+-------
public | customers | table | pi
public | older | table | pi
public | systemlog | table | pi
public | weather | table | pi
(4 rows)
Table "public.weather"
Column | Type | Modifiers | Storage | Description
-----------------------------+-----------------------------+------------------------------------------------------+----------+-------------
id | integer | not null default nextval('weather_id_seq'::regclass) | plain |
timestamp | timestamp without time zone | default now() | plain |
currentwindspeed | real | | plain |
bmp180temperature | integer | | plain |
Indexes:
"weather_pkey" PRIMARY KEY, btree (id)
Has OIDs: no
My Datamapper class is:
class Weather
include DataMapper::Resource
property :id, Serial
property :bmp180temperature, String
property :insidehumidity, String
property :totalrain, String
property :currentwinddirection, String
property :currentWindSpeed, String
property :timestamp, DateTime
end
DataMapper.finalize
Weather.auto_upgrade!
Since this is Ruby I fired-up IRb, required the Datamapper gem and got:
records = Weather.all
DataObjects::SyntaxError: ERROR: column "bmp180temperature" does not exist
LINE 1: SELECT "id", "bmp180temperature", "insidehumidity", "totalra...
^
(code: 50360452, sql state: 42703, query: SELECT "id", "bmp180temperature", "insidehumidity", "totalrain", "currentwinddirection", "current_wind_speed", "timestamp" FROM "weathers" ORDER BY "id", uri: postgres:pi#localhost/postgres?scheme=postgres&user=pi&password=pw&host=localhost&port=&path=/postgres&query=&fragment=&adapter=postgres)
from /var/lib/gems/1.9.1/gems/dm-do-adapter-1.2.0/lib/dm-do-adapter/adapter.rb:147:in `execute_reader'
from /var/lib/gems/1.9.1/gems/dm-do-adapter-1.2.0/lib/dm-do-adapter/adapter.rb:147:in `block in read'
from /var/lib/gems/1.9.1/gems/dm-do-adapter-1.2.0/lib/dm-do-adapter/adapter.rb:276:in `with_connection'
This makes me think it's not finding the right table. It seems to be OK with the id column, perhaps.
I see what appears to be DataMapper is being used for PHP in the CodeIgniter framework but I'm unsure if it's the same DataMapper I'm using in Ruby.
What am I overlooking to get DataMapper to find this table?

Related

Hive - rlike not returning results meeting multiple conditions

I'm looking to return encounters later than 12-17-2020 for each patient involving any of the antibiotics. I'm expecting many results, as querying by one antibiotic at a time shows me. But when I string them together in rlike, it only returns results for one patient for the first antibiotic, amikacin. Is there something wrong in the syntax?
CREATE TABLE tsri.antibiotics AS
SELECT * FROM observation_fact_meds
WHERE start_date > "2020-12-17"
AND encounter_num in (select distinct encounter_num from visit_dimension where patient_num in ('000000', '000001', '000002', '000003', '000004', '000006', '000007') and INOUT_CD in ('Inpatient'))
AND DESCRIPTION rlike ('amikacin| amoxicillin| amoxicillin-clavulanate| Amphotericin B| ampicillin| ampicillin-sulbactam | azithromycin| aztreonam| bacitracin| cefazolin | efepime | cefiderocol| cefotaxime | cefoxitin| ceftaroline| ceftazidime | ceftazidime-avibactam| ceftriaxone | cefuroxime | cephalexin| ciprofloxacin| clarithromycin| clindamycin | Cloxacillin| Cotrimoxazole | dapsone| erythromycin| gentamicin| imipenem| imipenem-cilastatin| isoniazid| lefamulin| levofloxacin| linezolid| meropenem| metronidazole| Nafcillin| Nystatin| penicillin| pentamidine| piperacillin-tazobactam| Piperacillin | rifampin | sulfamethoxazole-trimethoprim | TNF Antimicrobial Med| tobramycin| vancomycin')
Remove the space before pattern - pls put exact string you are looking for a string seperate them only by pipe.
DESCRIPTION rlike ('amikacin|amoxicillin|...

Doctrine 2 join on PK in JSONb field

In our system we have table to track events. This table named Event have a JSONb column source to hold references to entities in other tables:
| uuid | name | source |
+--------------------------------------+-------------------------------------------+----------------------------------------------------------+
| 7916c5c9-3af2-41ce-81e4-776847029b08 | App\LoginRequest\LoginRequestExpiredEvent | {"loginRequest": "4dda7873-534d-4c0c-853b-65b4b1056dae"} |
Simplified login_request table looks like this:
| uuid | expireAt |
+--------------------------------------+---------------------+
| 4dda7873-534d-4c0c-853b-65b4b1056dae | 2019-02-14 08:00:00 |
| 13c85e8c-e2dc-4b3f-aaf5-25920e2c4d04 | 2019-02-14 22:00:00 |
I would like to SELECT all LoginRequest entities that are referenced in Event table. Please remember that both table doesn't have any foreing-key relation! LoginRequest is referenced only via JSONb field. RAW SQL works like expected:
SELECT *
FROM login_request AS lr
JOIN event AS ev ON ev.source->>'loginRequest' = text(lr.uuid)
returning resultset like:
| lr.uuid | lr.expireAt | ev.uuid | ev.name | ev.source |
+--------------------------------------+---------------------+--------------------------------------+-------------------------------------------+----------------------------------------------------------+
| 4dda7873-534d-4c0c-853b-65b4b1056dae | 2019-02-14 08:00:00 | 7916c5c9-3af2-41ce-81e4-776847029b08 | App\LoginRequest\LoginRequestExpiredEvent | {"loginRequest": "4dda7873-534d-4c0c-853b-65b4b1056dae"} |
I have troubles to get the same functionality as RAW SQL in Doctrine's DQL:
<?php
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\ORM\Query\Expr\Join;
class LoginRequestRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, LoginRequestEntity::class);
}
public function findExpiredWithEvent()
{
$qb = $this->createQueryBuilder('lr');
$qb
->select('lr')
->join(
EventEntity::class,
'ev',
Join::ON,
"ev.source->>'loginRequest' = text(lr.uuid)"
);
return $qb->getQuery()->getResult();
}
}
I cannot make JOIN on JSONb field prop work. For example above query-builder invocation returns exception:
In QueryException.php line 54:
[Syntax Error] line 0, col 104: Error: Expected end of string, got 'ON'
In QueryException.php line 43:
SELECT lr FROM App\LoginRequest\LoginRequestEntity lr
INNER JOIN App\Event\EventEntity ev
ON ev.source->>'loginRequest' = text(lr.uuid)
Is there posibility to do a JOIN on JSONb column property from other table in Doctrine's query-builder?
I have a similar sql :
select distinct lru.id, lru.* from tstdf_lru as lru inner join tsrec_failure as failure on failure.lru_id = lru.id inner join wrks_event as event on failure.failure_group_id = (event.data->>'failureGroupId')::BIGINT
Maybe that can help you.

Doctrine boolean type can't be set to false

I can't set false value to an entity column which type is boolean.
/**
* #ORM\Column(type="boolean")
*/
private $isActive;
Sending JSON:
{myEntity: {isActive: false}}
...will cause:
Integrity constraint violation: 1048 Column 'is_active' cannot be null
While sending:
{myEntity: {isActive: 0}}
...will work fine
There are some similar answers on stackoverflow however none of the solution worked.
RAW COLUMN
+--------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+-----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| is_active | tinyint(1) | NO | | NULL | |
+-------------+--------------+------+-----+---------+-----------------+
EDIT
Read my answer below. It's not a Doctrine bug but MariaDB 10.2 serie.
This is unfortunately not a solution to the main problem, however it will give some insight.
The problem translating false to null occurs with MariaDB 10.2 serie (did not test 10.0 serie).
The described problem does not occur on MySQL or MariaDB 5.5 serie and it transforms false to false in a correct way.
Going to make another issue specifically about MariaDB 10.2 bug.
Alternatively, you should evaluate doing:
/**
* #ORM\Column(type="boolean", nullable=true)
*/
private $isActive;
You will not always have the chance of and/or will want to initialize a value in the class.
UPDATE:
I realize that my answer wasn't the right one. Maybe you could initialize the property in the constructor, or call the setter automatically. In any case, you have to "deceive" Doctrine's Unit of Work that your entity has changed.

Insert collect_set values into Elasticsearch with PIG

I have a HIVE table which contains 3 columns- "id"(String), "booklist"(Array of String), and "date"(string) with the following data:
----------------------------------------------------
id | booklist | date
----------------------------------------------------
1 | ["Book1" , "Book2"] | 2017-11-27T01:00:00.000Z
2 | ["Book3" , "Book4"] | 2017-11-27T01:00:00.000Z
When trying to insert into Elasticsearch with this PIG script
-------------------------Script begins------------------------------------------------
SET hive.metastore.uris 'thrift://node:9000';
REGISTER hdfs://node:9001/library/elasticsearch-hadoop-5.0.0.jar;
DEFINE HCatLoader org.apache.hive.hcatalog.pig.HCatLoader();
DEFINE EsStore org.elasticsearch.hadoop.pig.EsStorage(
'es.nodes = elasticsearch.service.consul',
'es.port = 9200',
'es.write.operation = upsert',
'es.mapping.id = id',
'es.mapping.pig.tuple.use.field.names=true'
);
hivetable = LOAD 'default.reading' USING HCatLoader();
hivetable_flat = FOREACH hivetable
GENERATE
id AS id,
booklist as bookList,
date AS date;
STORE hivetable_flat INTO 'readings/reading' USING EsStore();
-------------------------Script Ends------------------------------------------------
When running above, i got an error saying:
ERROR 2999:Unexpected internal error. Found unrecoverable error [ip:port] returned Bad Request(400) - failed to parse [bookList]; Bailing out..
Can anyone shed any light on how to parse ARRAY of STRING into ES and get above to work?
Thank you!

Preconfigured Magento Widgets

Is there a UI or programatic system for taking advantage of the "Preconfigured Widget" functionality that's part of Magento's CMS page rendering?
When adding a widget to a CMS page, the code that renders that widget is located in the template directive processing class. This code
File: app/code/core/Mage/Widget/Model/Template/Filter.php
class Mage_Adminhtml_Cms_PageController extends Mage_Adminhtml_Controller_Action
{
...
}
When loading a Widget's paramaters, there the following bit of code
// validate required parameter type or id
if (!empty($params['type'])) {
$type = $params['type'];
} elseif (!empty($params['id'])) {
$preconfigured = Mage::getResourceSingleton('widget/widget')
->loadPreconfiguredWidget($params['id']);
$type = $preconfigured['type'];
$params = $preconfigured['parameters'];
} else {
return '';
}
This code appears to parse a widget directive tag for an id value
{{widget name="foobazbar" id=""}}
and then load the configuration from a widget model
public function loadPreconfiguredWidget($widgetId)
{
$read = $this->_getReadAdapter();
$select = $read->select();
$select->from($this->getMainTable())
->where($this->getIdFieldName() . ' = ?', $widgetId);
var_dump((string)$select);
$widget = $read->fetchRow($select);
if (is_array($widget)) {
if ($widget['parameters']) {
$widget['parameters'] = unserialize($widget['parameters']);
}
return $widget;
}
return false;
}
When I first encountered this code, I assumed it was loading up a Widget Instance model. However, it's not. Instead it's loading data from a widget/widget class, which corresponds to the widget table.
mysql> describe widget;
+------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------+----------------+
| widget_id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| code | varchar(255) | NO | MUL | NULL | |
| type | varchar(255) | NO | | NULL | |
| parameters | text | YES | | NULL | |
+------------+------------------+------+-----+---------+----------------+
Is there a UI or system for adding data to this table? Does anyone (who works fro Magento Inc. or not) know if this is a supported feature, or if it's the start of something that's been abandoned, but left in for backward compatibility reasons?
This answer is somewhat off-topic, but I'm not sure if it might satisfy your need anyway. I've discovered that you can create widget instances in the admin CMS>Widgets section, and then render them via the following code:
$oWidget = Mage::getModel('widget/widget_instance')->load('HomepageTwitter','title');
$oWidgetBlock = Mage::app()->getLayout()->createBlock($oWidget->getType(), $oWidget->getTitle(), $oWidget->getWidgetParameters());
echo $oWidgetBlock->toHtml();
Note that the block is loaded by title (rather than arbitrary ID), and that the widget parameters are passed for the Block to render.
Per several comments, and private emails, it appears this is a privatish feature for the Magento core team, and has nothing to do with Instance Widgets.

Resources