Use another column as default value in migration - sql

I am trying to add a new column to existing tableName table which has a column anotherColumn.
exports.up = function (knex, Promise) {
return knex.schema.table('tableName', table => {
table.string('newColumn').defaultTo(table.anotherColumn);
});
};
How should I get the value of the existing column into this new column?

The short answer is, you can't: the defaultTo value can't come from another column. However, if you're just trying to have the default take place at the time of migration you could do this:
exports.up = knex =>
knex.schema.table('tableName', t => {
t.string('newColumn')
})
.then(() => knex('tableName').update('newColumn', knex.ref('anotherColumn'));
It should hopefully be obvious that this will not update new rows being inserted following the migration: for that you'd need a trigger, or to ensure that you covered it in your insert code.

Related

Latest modified row for each item

I have a sql table containing multiple rows with a memberid and lastmodified date. I need to get latest modified row for each member id. This is what I have tried in EFCore 3.1.1:
var a = context.Members
.Include(m => m.Histories.OrderByDescending(h => h.LastModifiedDate)
.FirstOrDefault());
and it gives error: Lambda expression used inside Include is not valid
What am I missing?
UPDATE:
I tried this as well that didn't work either:
var a = context.Histories
.GroupBy(h => h.MemberId)
.Select(g => g.OrderByDescending(p => p.LastModifiedDate).FirstOrDefault()).ToList();
The LINQ expression '(GroupByShaperExpression:
KeySelector: (o.MemberId),
ElementSelector:(EntityShaperExpression:
EntityType: History
ValueBufferExpression:
(ProjectionBindingExpression: EmptyProjectionMember)
IsNullable: False
)
)
.OrderByDescending(p => p.LastModifiedDate)' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
While newer versions of EF Core do support some filtering via adding Where clauses, it's best to think of entities as a "complete" or "complete-able" representation of your data state. You either use the complete state of data, or you project to get the details you want.
For example, if you just want the last modified date for each member:
var lastModifiedDetails = context.Members
.Select(m => new
{
m.MemberId,
LastModifiedDate = m.Histories.OrderByDescending(h => h.LastModifiedDate)
.Select(h => h.LastModifiedDate)
.FirstOrDefault()
}).ToList();
This would give you a collection containing the MemberId and it's LastModifiedDate.
Alternatively, if you want the complete member and a quick reference to the last modified date:
var memberDetails = context.Members
.Select(m => new
{
Member = m,
LastModifiedHistory = m.Histories.OrderByDescending(h => h.LastModifiedDate)
.FirstOrDefault()
}).ToList();
Here instead of trying to get the last modified date or the latest history through a Member entity, you get a set of anonymous types that project down that detail. You iterate through that collection and can get the applicable history and/or modified date for the associated Member.

Laravel Row duplication inserted, with updateOrCreate method with Race-Condition

i have function in my controller that create a forecast :
public function updateOrCreate(Request $request, $subdomain, $uuid)
{
$fixture = Fixture::where('uuid',$uuid)->firstOrFail();
request()->validate([
'local_team_score' => 'integer|min:0',
'visitor_team_score' => 'integer|min:0',
'winner_team_id' => 'integer|nullable'
]);
if ($fixture->status !== "PENDING"){
return response()->json([
'message' => "You can not add or modify a forecast if the fixture is not pending"
], 403);
}
$winner_team = null;
// local team win
if ($request->local_team_score > $request->visitor_team_score) {
$winner_team = $fixture->localTeam;
}elseif ($request->local_team_score < $request->visitor_team_score){ //visitor win
$winner_team = $fixture->visitorTeam;
}else{ // draw
$winner_team = FixtureTeam::where('team_id',$request->winner_team_id)->first();
}
$user = auth('api')->user();
$platform = Platform::first();
$forecast = Forecast::updateOrCreate([
'user_id' => $user->id,
'fixture_id' => $fixture->id,
'platform_id' => $platform->id
],[
'local_team_score' => $request->local_team_score,
'visitor_team_score' => $request->visitor_team_score,
'winner_team_id' => is_null($winner_team) ? null : $winner_team->team_id
]);
$forecast->load('winnerTeam');
return new ForecastResource($forecast);
}
As you can see i use updateOrCreate methods to add or update a forecast.
The problem is when 2 requests from the same user run at the same time (and no forecast is already created) 2 row are inserted.
Do you have a solution ?
I See that the problem is not new but i could not find a solution https://github.com/laravel/framework/issues/19372
updateOrCreate does 2 steps:
tries to fetch the record
depending on the outcome does an update or a create.
This operation is not atomic, meaning that between step 1 and 2 another process could create the record and you would end up with duplicates (your situation).
To solve your problem you need following:
determine what columns would give the uniqueness of the record and add an unique index (probably compound between user_id, fixture_id, platform_id)
you need to let database handle the upsert (ON DUPLICATE KEY UPDATE in MySQL, ON CONFLICT (...) DO UPDATE SET in Postgres, etc). This can be achieved in Laravel by using the upsert(array $values, $uniqueBy, $update = null) instead of updateOrCreate.

Google Sheet API - How to get only updated rows

I could not able to find in the documentation on how to get only updated records/rows from Google Sheets API.
Is there a way, that I can get a timestamp of each record when it was last modified?
any guidance or any links that would solve this issue.
Thanks!
You cannot do this directly with Sheets API. You can keep track of the changes in a file using Drive API, though, but I don't think this is what you want to do.
I'd propose using an onEdit trigger using Apps Script. Every time the spreadsheet is modified, you could retrieve the data of the edited range and store it somewhere, as well as the current date.
It could be something on the following lines:
function onEdit(e) {
var timestamp = new Date();
var range = e.range;
var editedRow = range.getRow();
// Store timestamp and editedRow index somewhere you can retrieve it later (it could be in the spreadsheet itself)
}
Update:
You can create the trigger remotely using Apps Script API. First you should create a project bound to your spreadsheet and then add the corresponding code by calling projects.updateContent (you should add two files, the script itself, which contains the onEdit trigger, and the manifest file). Just beware that you can only use simple triggers with this API, not installable ones. But in your situation, that's more than enough.
I hope this is of any help.
WRITE OPERATION:
For $response = $service->spreadsheets_values->update() process (Writting file after creating it), response will be as follow:
Google_Service_Sheets_UpdateValuesResponse Object
(
[spreadsheetId] => XXX
[updatedCells] => 7
[updatedColumns] => 7
[updatedDataType:protected] => Google_Service_Sheets_ValueRange
[updatedDataDataType:protected] =>
[updatedRange] => Sheet1!A1:G1
[updatedRows] => 1
[internal_gapi_mappings:protected] => Array
(
)
[modelData:protected] => Array
(
)
[processed:protected] => Array
(
)
)
To Get Rows = $response->getUpdatedRows();
To Get Cells = $response->getUpdatedCells();
To Get Columns= $response->getUpdatedColumns();
and so on...
APPEND OPERATION:
For $response = $service->spreadsheets_values->append() process, response will be as follow:
Google_Service_Sheets_AppendValuesResponse Object
(
[spreadsheetId] => XXXX
[tableRange] => Sheet1!A1:G1
[updatesType:protected] => Google_Service_Sheets_UpdateValuesResponse
[updatesDataType:protected] =>
[internal_gapi_mappings:protected] => Array
(
)
[modelData:protected] => Array
(
[updates] => Array
(
[spreadsheetId] => XXXX
[updatedRange] => Sheet1!A2:G7
[updatedRows] => 6
[updatedColumns] => 7
[updatedCells] => 42
)
)
[processed:protected] => Array
(
)
)
To Get Rows = $response->getUpdates()->getUpdatedRows();
To Get Cells = $response->getUpdates()->getUpdatedCells();
To Get Columns= $response->getUpdates()->getUpdatedColumns();
and so on...

KnexJS raw query in migration

I have a problem with the following migration in KnexJS, working with PostgreSQL:
exports.up = (knex) => {
knex.raw('CREATE EXTENSION IF NOT EXISTS "uuid-ossp"');
return knex.schema.createTable('car_brands', (table) => {
table.uuid('brandId').unique().notNullable().primary().defaultTo(knex.raw('uuid_generate_v4()'));
table.string('name').notNullable().unique();
table.timestamp('created_at').notNullable().defaultTo(knex.raw('now()'));
table.timestamp('updated_at').notNullable().defaultTo(knex.raw('now()'));
});
};
exports.down = (knex) => {
knex.raw('drop extension if exists "uuid-ossp"');
return knex.schema.dropTable('car_brands');
};
I am using the UUID type for my default values, by using the
defaultTo(knex.raw('uuid_generate_v4()')).
However, when running the above migration, by:
knex migrate:latest --env development --knexfile knexfile.js --debug true
I get an error that:
function uuid_generate_v4() does not exist
Do you know why the knex.raw() query method is not working?
The problem is you are running
knex.raw('CREATE EXTENSION IF NOT EXISTS "uuid-ossp"');
and
knex.schema.createTable('car_brands');
asynchronously, so the first query is not executed before the second one.
Rewrite it using async/await:
exports.up = async (knex) => {
await knex.raw('CREATE EXTENSION IF NOT EXISTS "uuid-ossp"');
return knex.schema.createTable('car_brands', (table) => {
table.uuid('brandId').unique().notNullable().primary().defaultTo(knex.raw('uuid_generate_v4()'));
table.string('name').notNullable().unique();
table.timestamp('created_at').notNullable().defaultTo(knex.raw('now()'));
table.timestamp('updated_at').notNullable().defaultTo(knex.raw('now()'));
});
};
or using Promises:
exports.up = (knex) => {
knex.raw('CREATE EXTENSION IF NOT EXISTS "uuid-ossp"')
.then(() => {
return knex.schema.createTable('car_brands', (table) => {
table.uuid('brandId').unique().notNullable().primary().defaultTo(knex.raw('uuid_generate_v4()'));
table.string('name').notNullable().unique();
table.timestamp('created_at').notNullable().defaultTo(knex.raw('now()'));
table.timestamp('updated_at').notNullable().defaultTo(knex.raw('now()'));
});
})
};
These are the steps I took to resolve this issue in my app with PostgreSQL, Objection and KNEX.
Go to your database to verify your extension is available.
postgres=# SELECT * FROM pg_extension;
Verify "uuid-ossp" is installed in the database_name you need.
database_name=# CREATE EXTENSION "uuid-ossp"
Back to your app, go to the KNEX migration file where you are altering your table.
t.uuid('user_id').defaultTo(knex.raw('uuid_generate_v4()'));
Use the KNEX command to get the Batch running:
knex migrate:latest
Insert a new raw in your table and verify your UUID has been auto-generated.
I hope these steps can be helpful.

Active Record- How to select a column with relationship?

Projects can have unlimited number of columns (to form a table or something), relationship MANY to MANY. To implement this tbl_project_rel_column is created. It stores project_id, column_id AND pos position of column in Project table.
I am using AC database approach. I have 2 models Project and Column.
Project model's relations method:
public function relations(){
return array(
...
'columns'=>array(self::MANY_MANY,'Column','tbl_project_rel_column('p_id','c_id')
);
}
Now can get all project's columns using something like this:
$model = Project::model()->findbyPk($p_id);
$columns = $model->columns;
But column doesn't store 'pos'(position) value of it's certain project.
How to get 'pos' value of tpl_project_rel_column table of certain project and certain column?
You can use the through feature instead of MANY_MANY. It may also be useful to index the results by the position column. Try something like this:
public function relations()
{
return array(
'projectColumns' => array(self::HAS_MANY, 'ProjectRelColumn', 'p_id', 'index'=>'position'),
'columns' => array(self::HAS_MANY, 'Column', 'c_id', 'through'=>'projectColumns'),
}
Now you can query for projects with columns like this:
$projects = Project::model()->with('columns')->findAll();
foreach($projects as $project) {
// projectColumns are indexed by position. You can sort by this now:
ksort($project->projectColumns)
foreach($project->projectColumns as $pos => $projectColumn)
echo "Pos: $pos Column: {$projectColumn->column->name}";
}